diff --git a/README.md b/README.md index c1669546..d4dee72e 100644 --- a/README.md +++ b/README.md @@ -258,5 +258,8 @@ - on rooms with password - on direct rooms - after password is change / set / removed -- [ ] +- [ ] join empty room +- user join empty public room +- user join empty protected room +- is admin ? diff --git a/srcs/requirements/nestjs/api_back/src/app.module.ts b/srcs/requirements/nestjs/api_back/src/app.module.ts index 9fa1ea3f..8c50cf28 100644 --- a/srcs/requirements/nestjs/api_back/src/app.module.ts +++ b/srcs/requirements/nestjs/api_back/src/app.module.ts @@ -8,7 +8,6 @@ import { FriendshipsModule } from './friendship/friendships.module'; import { AuthenticationModule } from './auth/42/authentication.module'; import { PassportModule } from '@nestjs/passport'; import { GameModule } from './game/game.module'; -import { ChatGateway } from './chat/chat.gateway'; import { ChatModule } from './chat/chat.module'; @Module({ @@ -32,7 +31,6 @@ import { ChatModule } from './chat/chat.module'; synchronize: true, }), ChatModule, - // GameModule, ], controllers: [AppController], providers: [AppService], diff --git a/srcs/requirements/nestjs/api_back/src/auth/42/authentication.module.ts b/srcs/requirements/nestjs/api_back/src/auth/42/authentication.module.ts index d7982f81..ec8d6df7 100644 --- a/srcs/requirements/nestjs/api_back/src/auth/42/authentication.module.ts +++ b/srcs/requirements/nestjs/api_back/src/auth/42/authentication.module.ts @@ -9,11 +9,22 @@ import { AuthenticationController } from './authentication.controller'; import { AuthenticationService } from './authentication.service'; import { FortyTwoStrategy } from './strategy/42strategy'; import { SessionSerializer } from './utils/serializer'; +import { ChatModule } from 'src/chat/chat.module'; +import { ChatService } from 'src/chat/chat.service'; +import { Chatroom } from 'src/chat/entities/chatroom.entity'; @Module({ - imports: [TypeOrmModule.forFeature([User, Friendship]), UsersModule, + imports: [TypeOrmModule.forFeature([User, Friendship, Chatroom]), + UsersModule, + ChatModule, ], - providers: [AuthenticationService, FortyTwoStrategy, UsersService, SessionSerializer, FriendshipService + providers: [ + AuthenticationService, + FortyTwoStrategy, + UsersService, + SessionSerializer, + FriendshipService, + ChatService, ], exports: [AuthenticationService], controllers: [AuthenticationController], diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts index d377fbe5..19e4cd14 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts @@ -52,6 +52,7 @@ export class ChatController { let fields = ["name", "type", "users", "protection", "allowed_users"]; const rooms = await this.chatService.getMyRooms(req.user.username, fields); + console.log("rooms:", rooms); const blocked = await this.chatService.getListBlockUser(req.user.username); let filtered_rooms = rooms.filter(room => diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.module.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.module.ts index a35d3f0e..576b716b 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.module.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.module.ts @@ -1,10 +1,10 @@ -import { Module } from '@nestjs/common'; +import { Module, forwardRef } from '@nestjs/common'; +import { FriendshipsModule } from 'src/friendship/friendships.module'; +import { TypeOrmModule } from '@nestjs/typeorm'; import { ChatController } from './chat.controller'; import { ChatService } from './chat.service'; import { ChatGateway } from './chat.gateway'; import { UsersModule } from 'src/users/users.module'; -import { FriendshipsModule } from 'src/friendship/friendships.module'; -import { TypeOrmModule } from '@nestjs/typeorm'; import { Chatroom } from './entities/chatroom.entity'; import { User } from 'src/users/entities/user.entity'; import { Friendship } from 'src/friendship/entities/friendship.entity'; @@ -12,14 +12,13 @@ import { Friendship } from 'src/friendship/entities/friendship.entity'; @Module({ imports: [ TypeOrmModule.forFeature([Chatroom, User, Friendship]), - UsersModule, FriendshipsModule, + forwardRef(() => UsersModule), ], controllers: [ ChatController, ], - exports: [ - ], + exports: [], providers: [ ChatService, ChatGateway, diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts index a0ba234d..1d49462f 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts @@ -1,4 +1,4 @@ -import { HttpException, HttpStatus, Injectable, Res } from '@nestjs/common'; +import { HttpException, HttpStatus, Injectable, Res, NotFoundException, forwardRef, Inject } from '@nestjs/common'; import { User } from 'src/users/entities/user.entity'; import { Friendship, FriendshipStatus } from 'src/friendship/entities/friendship.entity'; import { Chatroom } from './entities/chatroom.entity'; @@ -19,6 +19,7 @@ import { printCaller } from './dev/dev_utils'; export class ChatService { constructor( + @Inject(forwardRef(() => UsersService)) private usersService: UsersService, private friendshipService: FriendshipService, @InjectRepository(User) @@ -144,6 +145,11 @@ export class ChatService { printCaller("-- in "); const user_db = await this.getUserByName(username); + if (!user_db) + { + printCaller(`throw error: error: true, code: 'USER_NOT_FOUND', message: 'the user was not found'`); + return; + } printCaller("-- out "); return user_db.currentRoom; @@ -703,10 +709,61 @@ export class ChatService { /* LAST MINUTE BAD SOLUTION ******************************* */ - async changeAllUsernames(socket: socketDto, room_name: string): Promise + async changeAllUsernames(old_name: string, new_name: string): Promise { printCaller("-- in "); + console.log("old_name:", old_name); + console.log("new_name:", new_name); + + let rooms: Chatroom[] = await this.getAllRooms(); + await rooms.forEach((room) => + { + let room_has_change: boolean = false; + + // users: string[] + room.users = room.users.map(item => + { + if (item === old_name) + { + room_has_change = true; + return new_name + } + return item; + }); + + // admins: string[] + room.admins = room.admins.map(item => + { + if (item === old_name) + { + room_has_change = true; + return new_name + } + return item; + }); + + // users: string[] + room.allowed_users = room.allowed_users.map(item => + { + if (item === old_name) + { + room_has_change = true; + return new_name + } + return item; + }); + + // owner: string + if(room.owner === old_name) + room.owner = new_name; + + if (room_has_change) + this.chatroomRepository.save(room); + this.addMessageToRoom(room.name, "SERVER", `${old_name} changes it's name for ${new_name}`); + }); + + rooms = await this.getAllRooms(); printCaller("-- out "); } diff --git a/srcs/requirements/nestjs/api_back/src/game/game.module.ts b/srcs/requirements/nestjs/api_back/src/game/game.module.ts index e834599f..8a83d108 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.module.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { Module, forwardRef } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Friendship } from 'src/friendship/entities/friendship.entity'; import { FriendshipService } from 'src/friendship/friendship.service'; @@ -8,10 +8,21 @@ import { Game } from './entity/game.entity'; import { TokenGame } from './entity/tokenGame.entity'; import { GameController } from './game.controller'; import { GameService } from './game.service'; +import { ChatModule } from 'src/chat/chat.module'; +import { ChatService } from 'src/chat/chat.service'; +import { Chatroom } from 'src/chat/entities/chatroom.entity'; @Module({ - imports: [TypeOrmModule.forFeature([TokenGame, User, Game, Friendship])], + imports: [ + TypeOrmModule.forFeature([TokenGame, User, Game, Friendship, Chatroom]), + ChatModule, + ], controllers: [GameController], - providers: [GameService, UsersService, FriendshipService] + providers: [ + GameService, + UsersService, + FriendshipService, + ChatService, + ] }) export class GameModule {} diff --git a/srcs/requirements/nestjs/api_back/src/users/users.module.ts b/srcs/requirements/nestjs/api_back/src/users/users.module.ts index 6b11cdf1..76d1d6c4 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.module.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { Module, forwardRef } from '@nestjs/common'; import { UsersService } from './users.service'; import { UsersController } from './users.controller'; import { User } from './entities/user.entity'; @@ -6,10 +6,16 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { Friendship } from '../friendship/entities/friendship.entity'; import { UserStats } from './entities/userStat.entities'; import { FriendshipService } from 'src/friendship/friendship.service'; +import { ChatModule } from 'src/chat/chat.module'; +import { ChatService } from 'src/chat/chat.service'; +import { Chatroom } from 'src/chat/entities/chatroom.entity'; @Module({ - imports: [TypeOrmModule.forFeature([User, Friendship, UserStats])], - providers: [UsersService, FriendshipService], + imports: [ + TypeOrmModule.forFeature([User, Friendship, UserStats, Chatroom]), + forwardRef(() => ChatModule), + ], + providers: [UsersService, FriendshipService, ChatService], exports: [UsersService], controllers: [UsersController], }) diff --git a/srcs/requirements/nestjs/api_back/src/users/users.service.ts b/srcs/requirements/nestjs/api_back/src/users/users.service.ts index ac145c6d..29bcd6fa 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.service.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.service.ts @@ -1,4 +1,4 @@ -import { HttpException, HttpStatus, Injectable, NotFoundException, } from '@nestjs/common'; +import { HttpException, HttpStatus, Injectable, NotFoundException, forwardRef, Inject } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { User } from './entities/user.entity'; import { Repository, Not } from 'typeorm'; @@ -7,12 +7,15 @@ import { UpdateUsersDto } from './dto/update-users.dto'; import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto'; import { UserStats } from './entities/userStat.entities'; import { FriendshipService } from 'src/friendship/friendship.service'; +import { ChatService } from 'src/chat/chat.service'; // On va devoir sûrement trouver un moyen plus simple pour passer l'id, sûrement via des pipes // ou des interceptors, mais pour l'instant on va faire comme ça. @Injectable() export class UsersService { constructor( + @Inject(forwardRef(() => ChatService)) + private chatService: ChatService, private readonly friendshipService: FriendshipService, @InjectRepository(User) private readonly userRepository: Repository, @@ -110,7 +113,8 @@ export class UsersService { if (!user) throw new HttpException(`The user could not be updated.`,HttpStatus.NOT_FOUND); this.userRepository.save(user); - + // here goes the horrible bandage from hugo + this.chatService.changeAllUsernames(username, updateUserDto.username); return user; }