diff --git a/containers/api/src/app.controller.ts b/containers/api/src/app.controller.ts index e8523ca8..492ed809 100644 --- a/containers/api/src/app.controller.ts +++ b/containers/api/src/app.controller.ts @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -import { Controller, Request, Req, Get, Post, UseGuards, Redirect, Res, Body, UploadedFile, UseInterceptors} from '@nestjs/common'; +import { Controller, Request, Req, Get, Post, UseGuards, Redirect, Res, Body, UploadedFile, UseInterceptors } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { JwtAuthGuard } from './auth/jwt-auth.guard'; import { AuthService } from './auth/auth.service'; @@ -41,258 +41,254 @@ import { formatWithOptions } from 'util'; @Controller('/api') export class AppController { - constructor(private authService: AuthService, - private loginClass: loginClass, - private chatService: ChatService, - private userService: UsersService, ) {} + constructor(private authService: AuthService, + private loginClass: loginClass, + private chatService: ChatService, + private userService: UsersService,) { } kFactor = 36; scaleFactor = 400; -//======================================================================================================== -//======================================================================================================== -// User -//======================================================================================================== -//======================================================================================================== + //======================================================================================================== + //======================================================================================================== + // User + //======================================================================================================== + //======================================================================================================== - @UseGuards(JwtAuthGuard) - @Get('/profile') - async getProfile(@Request() req) { - // const myJSON = JSON.stringify(req.user); - // console.log(`req user api= ${req.user}`) - // console.log(`json user api= ${myJSON}`) - // return req.user; - return await this.userService.findOne(req.user.username); - } - - @UseGuards(JwtAuthGuard) - @Post('/user') - async getUser( @Body() data: any) { - console.log(`usernamewwww= ${data.username}`) - return await this.userService.findOne(data.username); - } - - @UseGuards(JwtAuthGuard) - @Get('/users') - async getUsers( @Body() data: any) { - console.log(`usernamewwww= ${data.username}`) - return await this.userService.findAll(); - } - - @UseGuards(JwtAuthGuard) - @Get('/friends') - async getFriends(@Request() req) { - // return await this.userService.getFriends(req.user.username); - return await this.userService.getFriends(req.user.username); - } - - @UseGuards(JwtAuthGuard) - @Post('/friend')//need to do it 2 time when user accept one for each - async newFriend(@Request() req, @Body() data: any) { - // return await this.userService.getFriends(req.user.username); - console.log(`user= ${req.user.username}`) - const user = await this.userService.findOne(req.user.username) - if (!user) - return (0); - if (user.friends.find(item => item === data.username)) - { - user.friendRequest = user.friendRequest.filter((item) => item !== data.username); - this.userService.save(user); - return (1); + @UseGuards(JwtAuthGuard) + @Get('/profile') + async getProfile(@Request() req) { + // const myJSON = JSON.stringify(req.user); + // console.log(`req user api= ${req.user}`) + // console.log(`json user api= ${myJSON}`) + // return req.user; + return await this.userService.findOne(req.user.username); } - //create personnal conv for user - //await this.userService.addFriend(user, data.username); + + @UseGuards(JwtAuthGuard) + @Post('/user') + async getUser(@Body() data: any) { + console.log(`usernamewwww= ${data.username}`) + return await this.userService.findOne(data.username); + } + + @UseGuards(JwtAuthGuard) + @Get('/users') + async getUsers(@Body() data: any) { + console.log(`usernamewwww= ${data.username}`) + return await this.userService.findAll(); + } + + @UseGuards(JwtAuthGuard) + @Get('/friends') + async getFriends(@Request() req) { + // return await this.userService.getFriends(req.user.username); + return await this.userService.getFriends(req.user.username); + } + + @UseGuards(JwtAuthGuard) + @Post('/friend')//need to do it 2 time when user accept one for each + async newFriend(@Request() req, @Body() data: any) { + // return await this.userService.getFriends(req.user.username); + console.log(`user= ${req.user.username}`) + const user = await this.userService.findOne(req.user.username) + if (!user) + return (0); + if (user.friends.find(item => item === data.username)) { + user.friendRequest = user.friendRequest.filter((item) => item !== data.username); + this.userService.save(user); + return (1); + } + //create personnal conv for user + //await this.userService.addFriend(user, data.username); - // const amIhere = data.members.includes(req.user.username); - // if (!amIhere) - const conv = { - id: null, - name: req.user.username + ", " + data.username, - banned: [], - admin: [], - muted: [], - members: [], - owner: req.user.username, - password: null, - messages: null, - group: false, - private: false, - }; - conv.members.push(req.user.username); - conv.members.push(data.username); - await this.chatService.createConv(conv); + // const amIhere = data.members.includes(req.user.username); + // if (!amIhere) + const conv = { + id: null, + name: req.user.username + ", " + data.username, + banned: [], + admin: [], + muted: [], + members: [], + owner: req.user.username, + password: null, + messages: null, + group: false, + private: false, + }; + conv.members.push(req.user.username); + conv.members.push(data.username); + await this.chatService.createConv(conv); - return await this.userService.addFriend(user, data.username); + return await this.userService.addFriend(user, data.username); - } + } - @UseGuards(JwtAuthGuard) - @Post('/block') - async newBlocked(@Request() req, @Body() data: any) { - // return await this.userService.getFriends(req.user.username); - console.log(`user= ${req.user.username}`) - if (data.username === req.user.username) - return (0); - const user = await this.userService.findOne(req.user.username) - return await this.userService.addBlocked(user, data.username); - } + @UseGuards(JwtAuthGuard) + @Post('/block') + async newBlocked(@Request() req, @Body() data: any) { + // return await this.userService.getFriends(req.user.username); + console.log(`user= ${req.user.username}`) + if (data.username === req.user.username) + return (0); + const user = await this.userService.findOne(req.user.username) + return await this.userService.addBlocked(user, data.username); + } - @UseGuards(JwtAuthGuard) - @Post('/invite') - async newInvite(@Request() req, @Body() data: any) { - console.log(`user= ${req.user.username}`) - if (data.username === req.user.username) - return (0); - const user = await this.userService.findOne(data.username) - if (!user) - return (0); - console.log(`CALL IT!!!!!`) - return await this.userService.newInvite(user, req.user.username); - } + @UseGuards(JwtAuthGuard) + @Post('/invite') + async newInvite(@Request() req, @Body() data: any) { + console.log(`user= ${req.user.username}`) + if (data.username === req.user.username) + return (0); + const user = await this.userService.findOne(data.username) + if (!user) + return (0); + console.log(`CALL IT!!!!!`) + return await this.userService.newInvite(user, req.user.username); + } - @UseGuards(JwtAuthGuard) - @Get('/inviteRequest') - async getInvite(@Request() req) { - // return await this.userService.getFriends(req.user.username); - console.log(`useawdawd\n\n\nr= ${req.user.username}`) - // const user = await this.userService.findOne(req.user.username) - return await this.userService.getInvite(req.user.username); - } + @UseGuards(JwtAuthGuard) + @Get('/inviteRequest') + async getInvite(@Request() req) { + // return await this.userService.getFriends(req.user.username); + console.log(`useawdawd\n\n\nr= ${req.user.username}`) + // const user = await this.userService.findOne(req.user.username) + return await this.userService.getInvite(req.user.username); + } - @UseGuards(JwtAuthGuard) - @Post('/refuseInvite') - async refuseInvite(@Request() req, @Body() data: any) { - // return await this.userService.getFriends(req.user.username); - // console.log(`useawdawd\n\n\nr= ${req.user.username}`) - const user = await this.userService.findOne(req.user.username) - return await this.userService.refuseInvite(user, data.username); - } + @UseGuards(JwtAuthGuard) + @Post('/refuseInvite') + async refuseInvite(@Request() req, @Body() data: any) { + // return await this.userService.getFriends(req.user.username); + // console.log(`useawdawd\n\n\nr= ${req.user.username}`) + const user = await this.userService.findOne(req.user.username) + return await this.userService.refuseInvite(user, data.username); + } - @UseGuards(JwtAuthGuard) - @Post('/status') - async setStatus(@Request() req, @Body() data: any) { - const user = await this.userService.findOne(req.user.username); + @UseGuards(JwtAuthGuard) + @Post('/status') + async setStatus(@Request() req, @Body() data: any) { + const user = await this.userService.findOne(req.user.username); - user.status = data.status; - await this.userService.save(user); - } + user.status = data.status; + await this.userService.save(user); + } - @UseGuards(JwtAuthGuard) - @Post('/nickname') - async setNickname(@Request() req, @Body() data: any) { - // let user = req.user - // user.nickname = data.nickname - console.log(`user= ${req.user.username}`) - const taken = await this.userService.findNickname(data.nickname) - if (taken) - return (0); - let user = await this.userService.findOne(req.user.username) - user.nickname = data.nickname; - // return await this.userService.getFriends(req.user.username); - return await this.userService.save(user); - } + @UseGuards(JwtAuthGuard) + @Post('/nickname') + async setNickname(@Request() req, @Body() data: any) { + // let user = req.user + // user.nickname = data.nickname + console.log(`user= ${req.user.username}`) + const taken = await this.userService.findNickname(data.nickname) + if (taken) + return (0); + let user = await this.userService.findOne(req.user.username) + user.nickname = data.nickname; + // return await this.userService.getFriends(req.user.username); + return await this.userService.save(user); + } - @UseGuards(JwtAuthGuard) - @Post('/picture') - @UseInterceptors(FileInterceptor('photo')) - async setProfilPicture(@Request() req, @UploadedFile() file: Express.Multer.File) { - let user = await this.userService.findOne(req.user.username) - if (!file) - user.photo = null; - else - user.photo = file.buffer; - return await this.userService.save(user); - } + @UseGuards(JwtAuthGuard) + @Post('/picture') + @UseInterceptors(FileInterceptor('photo')) + async setProfilPicture(@Request() req, @UploadedFile() file: Express.Multer.File) { + let user = await this.userService.findOne(req.user.username) + if (!file) + user.photo = null; + else + user.photo = file.buffer; + return await this.userService.save(user); + } - @UseGuards(JwtAuthGuard) - @Post('/getPicture') - async getProfilPicture(@Body() data: any) { - // console.log(`dataaaaa= ${data.username}`) - return await this.userService.getPic(data.username) + @UseGuards(JwtAuthGuard) + @Post('/getPicture') + async getProfilPicture(@Body() data: any) { + // console.log(`dataaaaa= ${data.username}`) + return await this.userService.getPic(data.username) - // return user.photo - // const photoData = user.photo; - // Buffer.from(user.photo, 'binary').buffer; - // const arrayBuffer = ArrayBuffer.from(photoData, 'binary'); - // return await this.userService.save(user); - } + // return user.photo + // const photoData = user.photo; + // Buffer.from(user.photo, 'binary').buffer; + // const arrayBuffer = ArrayBuffer.from(photoData, 'binary'); + // return await this.userService.save(user); + } -//======================================================================================================== -//======================================================================================================== -// Pong -//======================================================================================================== -//======================================================================================================== + //======================================================================================================== + //======================================================================================================== + // Pong + //======================================================================================================== + //======================================================================================================== - @UseGuards(JwtAuthGuard) - @Post('/win') - async addWin(@Request() req, @Body() data: any) { - console.log("WIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIN: ", req.user.username) - const user = await this.userService.findOne(req.user.username); - console.log("User", user) - // const user2 = await this.userService.findOne(data.opName); - user.win++; - const Esp = 1 / (1 + Math.pow(10, (data.opRank - user.rank) / this.scaleFactor)) - const newRank = user.rank + this.kFactor * (1 - Esp); + @UseGuards(JwtAuthGuard) + @Post('/win') + async addWin(@Request() req, @Body() data: any) { + console.log("WIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIN: ", req.user.username) + const user = await this.userService.findOne(req.user.username); + console.log("User", user) + // const user2 = await this.userService.findOne(data.opName); + user.win++; + const Esp = 1 / (1 + Math.pow(10, (data.opRank - user.rank) / this.scaleFactor)) + const newRank = user.rank + this.kFactor * (1 - Esp); - user.rank = newRank; - console.log(`win new rank= ${newRank}`); - console.log(`data win = ${data}`) + user.rank = newRank; + console.log(`win new rank= ${newRank}`); + console.log(`data win = ${data}`) - const newMatch = new MatchLog; - newMatch.myScore = data.myScore; - newMatch.opScore = data.opScore; - newMatch.opponent = data.opName; - newMatch.parent = user; - console.log(`newMatch WIIIN = ${newMatch}`); - await this.userService.saveChild(user, newMatch); - } + const newMatch = new MatchLog; + newMatch.myScore = data.myScore; + newMatch.opScore = data.opScore; + newMatch.opponent = data.opName; + newMatch.parent = user; + console.log(`newMatch WIIIN = ${newMatch}`); + await this.userService.saveChild(user, newMatch); + } - @UseGuards(JwtAuthGuard) - @Post('/loss') - async addLoss(@Request() req, @Body() data: any) { - console.log("LOOOOOOOOOOOOOOOSE: ", req.user.username) - const user = await this.userService.findOne(req.user.username); - console.log("User", user) - user.loss++; + @UseGuards(JwtAuthGuard) + @Post('/loss') + async addLoss(@Request() req, @Body() data: any) { + console.log("LOOOOOOOOOOOOOOOSE: ", req.user.username) + const user = await this.userService.findOne(req.user.username); + console.log("User", user) + user.loss++; - const Esp = 1 / (1 + Math.pow(10, (data.opRank - user.rank) / this.scaleFactor)) - const newRank = user.rank + this.kFactor * (0 - Esp); + const Esp = 1 / (1 + Math.pow(10, (data.opRank - user.rank) / this.scaleFactor)) + const newRank = user.rank + this.kFactor * (0 - Esp); - user.rank = newRank; - console.log(`loss new rank= ${newRank}`); - console.log(`data loss = ${data}`) + user.rank = newRank; + console.log(`loss new rank= ${newRank}`); + console.log(`data loss = ${data}`) - const newMatch = new MatchLog; - newMatch.myScore = data.myScore; - newMatch.opScore = data.opScore; - newMatch.opponent = data.opName; - newMatch.parent = user; - console.log(`newMatch Loose= ${newMatch}`); - await this.userService.saveChild(user, newMatch); - } + const newMatch = new MatchLog; + newMatch.myScore = data.myScore; + newMatch.opScore = data.opScore; + newMatch.opponent = data.opName; + newMatch.parent = user; + console.log(`newMatch Loose= ${newMatch}`); + await this.userService.saveChild(user, newMatch); + } - @UseGuards(JwtAuthGuard) - @Get('/rank') - async getRank(@Request() req) - { - const user = await this.userService.findOne(req.user.username); - return user.rank; - } + @UseGuards(JwtAuthGuard) + @Get('/rank') + async getRank(@Request() req) { + const user = await this.userService.findOne(req.user.username); + return user.rank; + } -// @UseGuards(JwtAuthGuard) - @Get('/ranking') - async getRanking() - { - return await this.userService.getRanking(); - } + // @UseGuards(JwtAuthGuard) + @Get('/ranking') + async getRanking() { + return await this.userService.getRanking(); + } - @UseGuards(JwtAuthGuard) - @Post('/partyInvite') - async partyInvite(@Request() req, @Body() data: any) - { + @UseGuards(JwtAuthGuard) + @Post('/partyInvite') + async partyInvite(@Request() req, @Body() data: any) { //find data.username and add invite to list console.log("data post priv invite=", data); const user = await this.userService.findOne(data.username); @@ -302,12 +298,11 @@ export class AppController { await this.userService.save(user); // user.partyInvite.push(data); console.log("invite === ", user.partyInvite) - } + } - @UseGuards(JwtAuthGuard) - @Get('/partyInvite') - async getPartyInvite(@Request() req, @Body() data: any) - { + @UseGuards(JwtAuthGuard) + @Get('/partyInvite') + async getPartyInvite(@Request() req, @Body() data: any) { //find data.username and add invite to list const user = await this.userService.findOne(req.user.username); user.partyInvite = user.partyInvite || []; @@ -315,328 +310,323 @@ export class AppController { // user.partyInvite.push(data); // console.log("data invite === ", data.username) return user.partyInvite; - } + } - @UseGuards(JwtAuthGuard) - @Post('/deleteInvite') - async deleteInvite(@Request() req, @Body() data: any) - { - console.log("delete invite user= ", data.username) - const user = await this.userService.findOne(req.user.username); + @UseGuards(JwtAuthGuard) + @Post('/deleteInvite') + async deleteInvite(@Request() req, @Body() data: any) { + console.log("delete invite user= ", data.username) + const user = await this.userService.findOne(req.user.username); - // user.partyInvite = user.partyInvite.filter(item => Object.values(item)[1] !== req.user.username); - console.log("user.partyInvite before", user.partyInvite) - user.partyInvite = user.partyInvite.filter((item) => Object.values(item)[1] !== data.username); - console.log("user.partyInvite after", user.partyInvite) - this.userService.save(user); - } + // user.partyInvite = user.partyInvite.filter(item => Object.values(item)[1] !== req.user.username); + console.log("user.partyInvite before", user.partyInvite) + user.partyInvite = user.partyInvite.filter((item) => Object.values(item)[1] !== data.username); + console.log("user.partyInvite after", user.partyInvite) + this.userService.save(user); + } - @UseGuards(JwtAuthGuard) - @Post('/history') - async getHistory(@Body() data: any) - { - // const user = await this.userService.findOne(req.user.username); - // return user.rank; - return await this.userService.getHistory(data.username); + @UseGuards(JwtAuthGuard) + @Post('/history') + async getHistory(@Body() data: any) { + // const user = await this.userService.findOne(req.user.username); + // return user.rank; + return await this.userService.getHistory(data.username); - // if (user) { - // const children = user.children; - // console.log(user); - // console.log(user.children); // or perform any operations with the children - // return children; - // // You can also access specific properties of each child - // // children.forEach((child) => { - // // console.log(child.id); - // // console.log(child.opponent); - // // // Access other child properties as needed - // // }); + // if (user) { + // const children = user.children; + // console.log(user); + // console.log(user.children); // or perform any operations with the children + // return children; + // // You can also access specific properties of each child + // // children.forEach((child) => { + // // console.log(child.id); + // // console.log(child.opponent); + // // // Access other child properties as needed + // // }); + // } + } + + + //======================================================================================================== + //======================================================================================================== + // Auth + //======================================================================================================== + //======================================================================================================== + + // import { Prisma } from "@prisma/client"; + // import { Request, Response, NextFunction } from "express"; + // import { prisma } from "../server"; + + + + @Redirect('http://' + process.env.BASE_URL + '/token', 302) + @Get('auth/login') + async login2(@Req() request: Request) { + const url = request.url; + const user = await this.loginClass.Login42(url); + console.log(`user in auth/login= ${user}`); + console.log(`user in auth/login= ${user.username}`); + const data = await this.authService.login(user); + console.log(`all data in api = ${data}`); + const myJSON = JSON.stringify(data); + console.log(`all data json version= ${myJSON}`); + console.log(`data in api = ${(await data).access_token}`); + // console.log(`data i = ${(await data).access_token}`) + const token = (await data).access_token; + // console + await this.userService.save(user); + return { url: 'http://' + process.env.BASE_URL + `/token?data=${encodeURIComponent(JSON.stringify(token))}` }; + } + + @UseGuards(JwtAuthGuard) + @Get('/2fa') + async get2fa(@Request() req) { + const user = await this.userService.findOne(req.user.username); + return user.otp_enabled; + } + + + @UseGuards(JwtAuthGuard) + @Post('/otp') + async createOTP(@Request() req) { + const user = await this.userService.findOne(req.user.username); + // const user2 = await this.userService.findOne(req.user.username); + const res = await generateOTP(user); + await this.userService.save(user); + // console.log(user); + return res; + } + + @UseGuards(JwtAuthGuard) + @Post('/verifyOtp') + async verifyOTP(@Request() req, @Body() data: any) { + const user = await this.userService.findOne(req.user.username); + const res = await VerifyOTP(user, data.token) + console.log('token in verify=', data.token) + console.log('res in verify=', res) + await this.userService.save(user); + return res + } + + @UseGuards(JwtAuthGuard) + @Post('/validateOtp') + async validateOTP(@Request() req, @Body() data: any) { + const user = await this.userService.findOne(req.user.username); + const res = await ValidateOTP(user, data.token) + // await this.userService.save(user); + return res + } + + @UseGuards(JwtAuthGuard) + @Post('/deleteOtp') + async deleteOTP(@Request() req, @Body() data: any) { + const user = await this.userService.findOne(req.user.username); + user.otp_verified = false; + await this.userService.save(user); + // const res = await ValidateOTP(user, data.token) + // await this.userService.save(user); + // return res + } + + // @UseGuards(JwtAuthGuard) + // @Get('/QRcode') + // async createQrCode(@Request() req) + // { + // return (await generateQRcode(req)); // } - } + @UseGuards(JwtAuthGuard) + @Post('/quit') + async setOffline(@Request() req) { + const user = await this.userService.findOne(req.user.username); + user.sessionNumber--; + if (!user.sessionNumber) + user.status = 0; + console.log("quit sessionNUmber :", user.sessionNumber); -//======================================================================================================== -//======================================================================================================== -// Auth -//======================================================================================================== -//======================================================================================================== - -// import { Prisma } from "@prisma/client"; -// import { Request, Response, NextFunction } from "express"; -// import { prisma } from "../server"; - - - -@Redirect('http://' + process.env.BASE_URL + '/token', 302) -@Get('auth/login') - async login2(@Req() request: Request) { - const url = request.url; - const user = await this.loginClass.Login42(url); - console.log(`user in auth/login= ${user}`); - console.log(`user in auth/login= ${user.username}`); - const data = await this.authService.login(user); - console.log(`all data in api = ${data}`); - const myJSON = JSON.stringify(data); - console.log(`all data json version= ${myJSON}`); - console.log(`data in api = ${(await data).access_token}`); - // console.log(`data i = ${(await data).access_token}`) - const token = (await data).access_token; - // console - await this.userService.save(user); - return { url: 'http://' + process.env.BASE_URL + `/token?data=${encodeURIComponent(JSON.stringify(token))}` }; - } - - @UseGuards(JwtAuthGuard) - @Get('/2fa') - async get2fa(@Request() req) - { - const user = await this.userService.findOne(req.user.username); - return user.otp_enabled; - } - - - @UseGuards(JwtAuthGuard) - @Post('/otp') - async createOTP(@Request() req) - { - const user = await this.userService.findOne(req.user.username); - // const user2 = await this.userService.findOne(req.user.username); - const res = await generateOTP(user); - await this.userService.save(user); - // console.log(user); - return res; - } - - @UseGuards(JwtAuthGuard) - @Post('/verifyOtp') - async verifyOTP(@Request() req, @Body() data: any) - { - const user = await this.userService.findOne(req.user.username); - const res = await VerifyOTP(user, data.token) - console.log('token in verify=', data.token) - console.log('res in verify=', res) - await this.userService.save(user); - return res - } - - @UseGuards(JwtAuthGuard) - @Post('/validateOtp') - async validateOTP(@Request() req, @Body() data: any) - { - const user = await this.userService.findOne(req.user.username); - const res = await ValidateOTP(user, data.token) - // await this.userService.save(user); - return res - } - - @UseGuards(JwtAuthGuard) - @Post('/deleteOtp') - async deleteOTP(@Request() req, @Body() data: any) - { - const user = await this.userService.findOne(req.user.username); - user.otp_verified = false; - await this.userService.save(user); - // const res = await ValidateOTP(user, data.token) - // await this.userService.save(user); - // return res - } - -// @UseGuards(JwtAuthGuard) -// @Get('/QRcode') -// async createQrCode(@Request() req) -// { -// return (await generateQRcode(req)); -// } - - @UseGuards(JwtAuthGuard) - @Post('/quit') - async setOffline(@Request() req) { - const user = await this.userService.findOne(req.user.username); - user.sessionNumber-- ; - if (!user.sessionNumber) - user.status = 0; - console.log("quit sessionNUmber :",user.sessionNumber); - - await this.userService.save(user); - console.log("User quit"); - } - - @UseGuards(JwtAuthGuard) - @Post('/addSession') - async addSession(@Request() req) { - - const user = await this.userService.findOne(req.user.username); - user.sessionNumber++ ; - console.log("addSession sessionNUmber :",user.sessionNumber); - - await this.userService.save(user); - } - -//======================================================================================================== -//======================================================================================================== -// Chat -//======================================================================================================== -//======================================================================================================== - - @UseGuards(JwtAuthGuard) - @Post('/conv') - async createConv(@Request() req, @Body() data: any) { - ///create conv and return it ? id? - console.log(`data post /conv= ${data}`); - console.log(`data post /conv= ${data.members}`); - // console.log(`data post /conv= ${data.name}`); - - // const param = data; - const amIhere = data.members.includes(req.user.username); - if (!amIhere) - data.members.push(req.user.username) - // let test = {id: 2, members: "cc"}; - data.admin = [] - data.admin.push(req.user.username) - data.owner = req.user.username - data.group = true; - return await this.chatService.createConv(data); - // res.json(messages); - } - - @UseGuards(JwtAuthGuard) - @Get('/conv') - async getConv(@Request() req) { - return await this.chatService.getConv(req.user.username); - } - - @UseGuards(JwtAuthGuard) - @Get('/convs') - async getConvs() { - return await this.chatService.findAll(); - } - - @UseGuards(JwtAuthGuard) - @Post('/convId') - async getConvById(@Body() data: any) { - return await this.chatService.findConv(data.convId); - } - - - @UseGuards(JwtAuthGuard) - @Post('/message') - async postMessage(@Request() req, @Body() data: any) { - //if i can post post ? - let message = - { - convid: data.convId, - sender: data.sender, - text: data.text, - // createdAt: null, - id: null, - } - console.log(data); - return await this.chatService.createMessage(message, req.user.username); - } - - @UseGuards(JwtAuthGuard) - @Post('/member') - async getMember(@Body() data: any) { - console.log(data); - console.log(`get member= ${data.convId}`); - return await this.chatService.findConv(data.convId); - } - - @UseGuards(JwtAuthGuard) - @Post('/getMessage') - async getMessage(@Body() data: any) { - console.log(data); - // console.log(req.query) - console.log(`data get /conv= ${data.convId}`); - // let test = {id: 2, members: "cc"}; - - - return await this.chatService.getMessages(data.convId); - // return await this.chatService.getConv(req.user.username); - - - // res.json(messages); - } - - @UseGuards(JwtAuthGuard) - @Post('/name') - async setName(@Body() data: any) { - //find conv - // data.convId - return await this.chatService.setName(data.convId, data.name) - } - - @UseGuards(JwtAuthGuard) - @Post('/password') - async setPassword(@Body() data: any) { - return await this.chatService.setPassword(data.convId, data.password) - } - - @UseGuards(JwtAuthGuard) - @Post('/verifyPassword') - async verifyPassword(@Body() data: any) { - return await this.chatService.verifyPassword(data.convId, data.password) + await this.userService.save(user); + console.log("User quit"); } - @UseGuards(JwtAuthGuard) - @Post('/inviteConv') - async inviteUser(@Body() data: any) { - return await this.chatService.inviteUser(data.convId, data.username) + @UseGuards(JwtAuthGuard) + @Post('/addSession') + async addSession(@Request() req) { + + const user = await this.userService.findOne(req.user.username); + user.sessionNumber += 1; + if (user.status !== 2) //super + user.status = 1; + console.log("addSession sessionNUmber :", user.sessionNumber); + + await this.userService.save(user); } - @UseGuards(JwtAuthGuard) - @Post('/ban') - async banUser(@Body() data: any) { - if (!data.username) - return ; - return await this.chatService.banUser(data.convId, data.username) + //======================================================================================================== + //======================================================================================================== + // Chat + //======================================================================================================== + //======================================================================================================== + + @UseGuards(JwtAuthGuard) + @Post('/conv') + async createConv(@Request() req, @Body() data: any) { + ///create conv and return it ? id? + console.log(`data post /conv= ${data}`); + console.log(`data post /conv= ${data.members}`); + // console.log(`data post /conv= ${data.name}`); + + // const param = data; + const amIhere = data.members.includes(req.user.username); + if (!amIhere) + data.members.push(req.user.username) + // let test = {id: 2, members: "cc"}; + data.admin = [] + data.admin.push(req.user.username) + data.owner = req.user.username + data.group = true; + return await this.chatService.createConv(data); + // res.json(messages); } - @UseGuards(JwtAuthGuard) - @Post('/admin') - async setAdmin(@Body() data: any) { - if (!data.username) - return ; - return await this.chatService.setAdmin(data.convId, data.username) - } + @UseGuards(JwtAuthGuard) + @Get('/conv') + async getConv(@Request() req) { + return await this.chatService.getConv(req.user.username); + } - @UseGuards(JwtAuthGuard) - @Post('/mute') - async muteUser(@Body() data: any) { - if (!data.username) - return ; - return await this.chatService.muteUser(data.convId, data.username, data.time) - } + @UseGuards(JwtAuthGuard) + @Get('/convs') + async getConvs() { + return await this.chatService.findAll(); + } - @UseGuards(JwtAuthGuard) - @Post('/isAdmin') - async isAdmin(@Request() req, @Body() data: any) { - console.log("isdamin= ", req.user.username, " id=", data.convId) - return await this.chatService.isAdmin(data.convId, req.user.username) - } + @UseGuards(JwtAuthGuard) + @Post('/convId') + async getConvById(@Body() data: any) { + return await this.chatService.findConv(data.convId); + } - @UseGuards(JwtAuthGuard) - @Post('/private') - async setPrivate(@Body() data: any) { - return await this.chatService.setPrivate(data.convId, true) - } - @UseGuards(JwtAuthGuard) - @Post('/public') - async setPublic(@Body() data: any) { - return await this.chatService.setPrivate(data.convId, false) - } + @UseGuards(JwtAuthGuard) + @Post('/message') + async postMessage(@Request() req, @Body() data: any) { + //if i can post post ? + let message = + { + convid: data.convId, + sender: data.sender, + text: data.text, + // createdAt: null, + id: null, + } + console.log(data); + return await this.chatService.createMessage(message, req.user.username); + } - @UseGuards(JwtAuthGuard) - @Post('/allowed') - async isAllowed(@Request() req, @Body() data: any) { - return await this.chatService.isAllowed(data.convId, req.user.username) - } + @UseGuards(JwtAuthGuard) + @Post('/member') + async getMember(@Body() data: any) { + console.log(data); + console.log(`get member= ${data.convId}`); + return await this.chatService.findConv(data.convId); + } - @UseGuards(JwtAuthGuard) - @Post('/join') - async joinChannel(@Request() req, @Body() data: any) { - return await this.chatService.joinChannel(data.convId, req.user.username) - } + @UseGuards(JwtAuthGuard) + @Post('/getMessage') + async getMessage(@Body() data: any) { + console.log(data); + // console.log(req.query) + console.log(`data get /conv= ${data.convId}`); + // let test = {id: 2, members: "cc"}; + + + return await this.chatService.getMessages(data.convId); + // return await this.chatService.getConv(req.user.username); + + + // res.json(messages); + } + + @UseGuards(JwtAuthGuard) + @Post('/name') + async setName(@Body() data: any) { + //find conv + // data.convId + return await this.chatService.setName(data.convId, data.name) + } + + @UseGuards(JwtAuthGuard) + @Post('/password') + async setPassword(@Body() data: any) { + return await this.chatService.setPassword(data.convId, data.password) + } + + @UseGuards(JwtAuthGuard) + @Post('/verifyPassword') + async verifyPassword(@Body() data: any) { + return await this.chatService.verifyPassword(data.convId, data.password) + } + + @UseGuards(JwtAuthGuard) + @Post('/inviteConv') + async inviteUser(@Body() data: any) { + return await this.chatService.inviteUser(data.convId, data.username) + } + + @UseGuards(JwtAuthGuard) + @Post('/ban') + async banUser(@Body() data: any) { + if (!data.username) + return; + return await this.chatService.banUser(data.convId, data.username) + } + + @UseGuards(JwtAuthGuard) + @Post('/admin') + async setAdmin(@Body() data: any) { + if (!data.username) + return; + return await this.chatService.setAdmin(data.convId, data.username) + } + + @UseGuards(JwtAuthGuard) + @Post('/mute') + async muteUser(@Body() data: any) { + if (!data.username) + return; + return await this.chatService.muteUser(data.convId, data.username, data.time) + } + + @UseGuards(JwtAuthGuard) + @Post('/isAdmin') + async isAdmin(@Request() req, @Body() data: any) { + console.log("isdamin= ", req.user.username, " id=", data.convId) + return await this.chatService.isAdmin(data.convId, req.user.username) + } + + @UseGuards(JwtAuthGuard) + @Post('/private') + async setPrivate(@Body() data: any) { + return await this.chatService.setPrivate(data.convId, true) + } + + @UseGuards(JwtAuthGuard) + @Post('/public') + async setPublic(@Body() data: any) { + return await this.chatService.setPrivate(data.convId, false) + } + + @UseGuards(JwtAuthGuard) + @Post('/allowed') + async isAllowed(@Request() req, @Body() data: any) { + return await this.chatService.isAllowed(data.convId, req.user.username) + } + + @UseGuards(JwtAuthGuard) + @Post('/join') + async joinChannel(@Request() req, @Body() data: any) { + return await this.chatService.joinChannel(data.convId, req.user.username) + } } diff --git a/containers/api/src/auth/login42.ts b/containers/api/src/auth/login42.ts index 5c9e3368..c60cd2fa 100644 --- a/containers/api/src/auth/login42.ts +++ b/containers/api/src/auth/login42.ts @@ -74,14 +74,10 @@ export class loginClass { friends: null, blocked: null, photo: null, - sessionNumber: 1, + sessionNumber: 0, }; await this.usersService.create(user); } - // if (user.status !== 2) //super - // user.status = 1; - // user.sessionNumber++; - console.log(user.sessionNumber); const myJSON = JSON.stringify(user); console.log(`in login42 user= ${myJSON}`) diff --git a/containers/react/src/components/App.tsx b/containers/react/src/components/App.tsx index 6b0d74ad..d0ad2095 100644 --- a/containers/react/src/components/App.tsx +++ b/containers/react/src/components/App.tsx @@ -36,18 +36,7 @@ function AnimatedRoute() { } }; - - window.addEventListener('beforeunload', handleBeforeUnload); - return () => { - window.removeEventListener('beforeunload', handleBeforeUnload); - }; - }, []); - - useEffect(() => { - const handleLoad = async () => { - console.log('Page loaded'); - console.log("je suis a addSession"); if (!localStorage.getItem('token')) return; try { @@ -57,9 +46,10 @@ function AnimatedRoute() { } }; - window.addEventListener('load', handleLoad); + handleLoad(); + window.addEventListener('beforeunload', handleBeforeUnload); return () => { - window.removeEventListener('load', handleLoad); + window.removeEventListener('beforeunload', handleBeforeUnload); }; }, []);