From 15c53b17b38ee949153e116032463eb11313a4d1 Mon Sep 17 00:00:00 2001 From: simplonco Date: Mon, 16 Jan 2023 17:27:35 +0100 Subject: [PATCH] blocked messages are not in hitory --- .../api_back/src/chat/chat.controller.ts | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) 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 a3971ef7..4af3025e 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts @@ -225,6 +225,12 @@ export class ChatController { let response = ""; if (room.type === 'user') { + const blocked = await this.chatService.getListBlockUser(req.user.username); + if (blocked.includes(room.name)) + { + console.log("throw error: error: true, code: 'BLOCKED_USER', message: 'you cannot enter this room, you blocked this user'"); + throw new HttpException({ error: true, code: 'BLOCKED_USER', message: `you cannot enter this room, you blocked this user` }, HttpStatus.UNAUTHORIZED); + } room.type = 'direct'; room.users = [room.name, req.user.username]; room.name += ` + ${req.user.username}`; @@ -268,7 +274,7 @@ export class ChatController { { printCaller("- in "); - let fields = ["protection", "allowed_users"]; + let fields = ["protection", "allowed_users", "type", "users"]; const room_db = await this.chatService.getRoomByName(room.name, fields); if (room_db.protection === true) @@ -280,6 +286,19 @@ export class ChatController { } } + const blocked = await this.chatService.getListBlockUser(req.user.username); + if (room_db.type === 'direct') + { + let roomname = room_db.users[0]; + if (roomname === req.user.username) + roomname = room.users[1]; + if (blocked.includes(roomname)) + { + console.log("throw error: error: true, code: 'BLOCKED_USER', message: 'you cannot enter this room, you blocked this user'"); + throw new HttpException({ error: true, code: 'BLOCKED_USER', message: `you cannot enter this room, you blocked this user` }, HttpStatus.UNAUTHORIZED); + } + } + await this.chatService.setCurrentRoom(req.user.username, room.name); let socket: socketDto = this.chatGateway.sockets.get(req.user.username); await this.chatService.socketChangeRoom(socket, room.name); @@ -386,6 +405,14 @@ export class ChatController { { printCaller("- in "); + + const blocked = await this.chatService.getListBlockUser(req.user.username); + if (blocked.includes(username)) + { + console.log("throw error: error: true, code: 'BLOCKED_USER', message: 'you cannot invite this user, you have blocked it'"); + throw new HttpException({ error: true, code: 'BLOCKED_USER', message: `you cannot invite this user, you have blocked it` }, HttpStatus.UNAUTHORIZED); + } + let current_room_name = await this.chatService.getCurrentRoomName(req.user.username); let room = await this.chatService.addUserToRoom(username, current_room_name); let message = `${username} joined the room`; @@ -428,7 +455,17 @@ export class ChatController { printCaller("- in "); const messages = await this.chatService.getMessagesFromCurrentRoom(req.user.username); - res.status(HttpStatus.OK).json({ messages: messages }); + + console.log("messages:", messages); + const blocked = await this.chatService.getListBlockUser(req.user.username); + let filtered_messages = messages.filter(message => + { + if (blocked.includes(message.name)) + return false; + return true; + }); + + res.status(HttpStatus.OK).json({ messages: filtered_messages }); printCaller("- out "); } @@ -481,7 +518,15 @@ export class ChatController { const room_name = await this.chatService.getCurrentRoomName(req.user.username); const users = await this.chatService.getAllUsersNotInRoom(room_name); - res.status(HttpStatus.OK).json({ users: users }); + const blocked = await this.chatService.getListBlockUser(req.user.username); + let filtered_users = users.filter(user => + { + if (blocked.includes(user.username)) + return false; + return true; + }); + + res.status(HttpStatus.OK).json({ users: filtered_users }); printCaller("- out "); }