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 4308c4cc..53551842 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts @@ -115,10 +115,16 @@ export class ChatController { throw new HttpException( `Your room name can not contains these characters : ${forbidden_chars}`, HttpStatus.UNPROCESSABLE_ENTITY); } - if (!room.password || room.password.length === 0) + if (typeof room.protection === 'undefined') room.protection = false; - else - room.protection = true; + else if (room.protection === true) + { + if (!room.password || room.password.length === 0) + { + console.log(`throw error: code: 'PASSWORD_BAD_FORMAT', message: 'your password is too short'`); + throw new HttpException({ code: 'PASSWORD_BAD_FORMAT', message: `your password is too short` }, HttpStatus.UNPROCESSABLE_ENTITY); + } + } room.users = [req.user.username]; await this.chatService.addUserToNewRoom(req.user.username, room); @@ -191,7 +197,7 @@ export class ChatController { console.log("throw error: code: 'PASSWORD_MISSING', message: 'this room is protected, you need to provide a password'"); throw new HttpException({ code: 'PASSWORD_MISSING', message: `this room is protected, you need to provide a password` }, HttpStatus.BAD_REQUEST); } - if (!room_db.allowed_users.contains(req.user.username)) + if (!room_db.allowed_users.includes(req.user.username)) await this.chatService.setPasswordValidation(req.user.username, room); } 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 18763305..8b3e30c3 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts @@ -45,7 +45,6 @@ export class ChatService { } const rooms = await queryBuilder.getMany(); - console.log("--- rooms:", rooms); console.log("-- out getMyRooms service"); return rooms; @@ -69,7 +68,6 @@ export class ChatService { const rooms = await this.chatroomRepository .createQueryBuilder('chatroom') .getMany(); - console.log("--- rooms:", rooms); console.log("-- out getAllRooms service"); return rooms; @@ -85,7 +83,6 @@ export class ChatService { .where('chatroom.type NOT IN (:...type)', { type: ['private', 'direct'] }) .andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` }) .getMany(); - console.log("--- rooms:", rooms); console.log("-- out getAllNotMyRooms service"); return rooms; @@ -95,11 +92,6 @@ export class ChatService { { console.log("-- in getAllOtherRoomsAndUsers service"); - const temp = await this.chatroomRepository - .createQueryBuilder('chatroom') - .getMany(); - console.log("all rooms:", temp); - const all_rooms = await this.getAllNotMyRooms(username); const all_users = await this.getAllUsersNotMyRooms(username); @@ -116,7 +108,6 @@ export class ChatService { }; }); let rooms: roomDto[] = row_rooms.concat(users); - console.log("--- rooms:", rooms); console.log("-- in getAllOtherRoomsAndUsers service"); return rooms; @@ -140,9 +131,7 @@ export class ChatService { { console.log("-- in getCurrentRoomName service"); - console.log("username:", username); const user_db = await this.getUserByName(username); - console.log("user_db:", user_db); console.log("-- out getCurrentRoomName service"); return user_db.currentRoom; @@ -152,7 +141,6 @@ export class ChatService { { console.log("-- in getRoomByName service"); - console.log("room_name:", room_name); const queryBuilder = this.chatroomRepository .createQueryBuilder('chatroom') .where('chatroom.name = :name', { name: room_name }); @@ -164,7 +152,6 @@ export class ChatService { } const room = await queryBuilder.getOne(); - console.log("room:", room); console.log("-- out getRoomByName service"); return room; @@ -235,6 +222,7 @@ export class ChatService { let hash; if (room.protection) { + console.log("in room protection hash"); if (room.type === 'direct') { console.log("throw error: code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'"); @@ -361,7 +349,6 @@ export class ChatService { .createQueryBuilder('user') .where('user.username NOT IN (:...usernames)', { usernames: usernames }) .getMany(); - console.log("--- users:", users); console.log("-- out getAllUsersNotMyRooms service"); return users; @@ -379,7 +366,6 @@ export class ChatService { .createQueryBuilder('user') .where('user.username NOT IN (:...usernames)', { usernames: usernames }) .getMany(); - console.log("--- users:", users); console.log("-- out getAllUsersNotInRoom service"); return users; @@ -413,7 +399,7 @@ export class ChatService { async socketJoinRoom(socket: socketDto, room_name: string): Promise { - console.log('- in socketJoinRoom service'); + console.log('-- in socketJoinRoom service'); socket.leave(socket.room); socket.join(room_name); @@ -422,7 +408,7 @@ export class ChatService { await socket.to(socket.room).emit('message', "SERVER", message); await this.addMessageToRoom(room_name, "SERVER", message); - console.log('- out socketJoinRoom service'); + console.log('-- out socketJoinRoom service'); } } 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 fb32e7fe..c4dfba54 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 @@ -40,7 +40,7 @@ export class roomDto @IsArray() @IsString({ each: true }) @IsOptional() - allowed_users: string[]; // usernames + allowed_users?: string[]; // usernames @IsArray() //@IsInstance(messagesDto, { each: true }) diff --git a/srcs/requirements/nestjs/api_back/src/chat/entities/chatroom.entity.ts b/srcs/requirements/nestjs/api_back/src/chat/entities/chatroom.entity.ts index ff59646f..ece8702e 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/entities/chatroom.entity.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/entities/chatroom.entity.ts @@ -25,7 +25,7 @@ export class Chatroom @IsBoolean() protection: boolean = false; - @Column() + @Column({ nullable: true }) @IsString() @IsOptional() hash?: string;