diff --git a/README.md b/README.md index c952c5ab..5459bbea 100644 --- a/README.md +++ b/README.md @@ -74,11 +74,11 @@ - [/] chat in room - [/] join public rooms - [ ] join private rooms -- [ ] join direct rooms +- [/] join direct rooms - [/] see all joignable rooms - [/] see all my rooms - [/] leave room -- [ ] leave direct +- [ ] leave direct impossible - [ ] invite someone in room - [ ] make admin - [ ] ban 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 12c8a9b1..4e6d2131 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts @@ -49,7 +49,6 @@ export class ChatController { console.log("- in getAllRooms controller"); const rooms: roomDto[] = await this.chatService.getAllOtherRoomsAndUsers(req.user.username) - console.log("--- rooms:", rooms); res.status(HttpStatus.OK).json({ rooms: rooms }); console.log("- out getAllRooms controller"); @@ -96,6 +95,8 @@ export class ChatController { throw new HttpException(`Your room name can not contains these characters : ${forbidden_chars}`, HttpStatus.UNPROCESSABLE_ENTITY); } + room.users = [req.user.username]; + room.owner = req.user.username; await this.chatService.addUserToNewRoom(req.user.username, room); res.status(HttpStatus.OK).json({ room: room }); @@ -117,13 +118,15 @@ export class ChatController { room.type = 'direct'; room.users = [room.name, req.user.username]; room.name += ` + ${req.user.username}`; + room.owner = req.user.username; await this.chatService.addUserToNewRoom(req.user.username, room); } else - await this.chatService.addUserToRoom(req.user.username, room.name); + room = await this.chatService.addUserToRoom(req.user.username, room.name); let socket: socketDto = this.chatGateway.sockets.get(req.user.username); await this.chatService.socketJoinRoom(socket, room.name); + res.status(HttpStatus.OK).json({ room: room }); console.log("- out joinRoom controller"); @@ -146,10 +149,15 @@ export class ChatController { @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) - @Post('leave') - async leaveRoom(@Body() body) + @Delete('leave') + async leaveRoom(@Req() req, @Res() res): Promise { console.log("- in leaveRoom controller"); + + const room_name = await this.chatService.getCurrentRoomName(req.user.username); + let response = await this.chatService.removeUserFromRoom(req.user.username, room_name); + res.status(HttpStatus.OK).json({ message: response }); + console.log("- out leaveRoom controller"); } @@ -181,19 +189,5 @@ export class ChatController { console.log("- out getRoomUsers controller"); } - @UseGuards(AuthenticateGuard) - @UseGuards(TwoFactorGuard) - @Delete('removeuser') - async removeUser(@Req() req, @Res() res): Promise - { - console.log("- in removeUser controller"); - - const room_name = await this.chatService.getCurrentRoomName(req.user.username); - let response = await this.chatService.removeUserFromRoom(req.user.username, room_name); - res.status(HttpStatus.OK).json({ message: response }); - - console.log("- out removeUser controller"); - } - } 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 a59720db..6e12e9da 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts @@ -82,6 +82,7 @@ export class ChatService { const rooms = await this.chatroomRepository .createQueryBuilder('chatroom') .where('chatroom.type != :type', { type: 'private' }) + .andWhere('chatroom.type != :type', { type: 'direct' }) .andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` }) .getMany(); console.log("--- rooms:", rooms); @@ -200,17 +201,15 @@ export class ChatService { const newChatroom = new Chatroom(); newChatroom.name = room.name; newChatroom.type = room.type; - newChatroom.owner = username; - newChatroom.users = [username]; - if (room.type === 'direct') - newChatroom.users = room.users; + newChatroom.owner = room.owner; + newChatroom.users = room.users; newChatroom.messages = [{ name: "SERVER", message: `creation of room ${room.name}` }]; await this.chatroomRepository.save(newChatroom); console.log("-- out addUserToNewRoom service"); } - async addUserToRoom(username: string, room_name: string): Promise + async addUserToRoom(username: string, room_name: string): Promise { console.log("-- in addUserToRoom service"); @@ -223,6 +222,7 @@ export class ChatService { this.chatroomRepository.save(room); console.log("-- out addUserToRoom service"); + return room; } async addMessageToRoom(room_name: string, username: string, message: string): Promise @@ -251,6 +251,8 @@ export class ChatService { const room = await this.getRoomByName(room_name); if (!room.users.includes(username)) throw new HttpException(`your are not in this room`, HttpStatus.CONFLICT); + if (room.type === "direct") + throw new HttpException(`you cannot leave a direct messages conversation`, HttpStatus.CONFLICT); // delete user from room room.users.push(username); diff --git a/srcs/requirements/nestjs/api_back/src/chat/dto/room.dto.ts b/srcs/requirements/nestjs/api_back/src/chat/dto/room.dto.ts index 6dcf8099..d98f3fe5 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/dto/room.dto.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/dto/room.dto.ts @@ -11,6 +11,14 @@ export class roomDto @IsIn(["public", "protected", "private", "direct", "user"]) type: string; + @IsString() + @IsOptional() + owner?: string; + + @IsString() + @IsOptional() + client_name?: string; + @IsArray() @IsString({ each: true }) @IsOptional() diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_home.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_home.svelte index a341b6b1..679e5c5d 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_home.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_home.svelte @@ -48,7 +48,7 @@ {:then rooms} {#each rooms as room} {/each} {/await} diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_room_set.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_room_set.svelte index 8e2933bf..d1cca48f 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_room_set.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_room_set.svelte @@ -1,7 +1,7 @@