From d1a3a6e12028646b0a0be5b16d0647c18fdec6ee Mon Sep 17 00:00:00 2001 From: simplonco Date: Tue, 17 Jan 2023 19:39:55 +0100 Subject: [PATCH] wip still not resolved pbm with map socket --- .../nestjs/api_back/src/chat/chat.service.ts | 138 +++++++++++++++--- .../nestjs/api_back/src/chat/dev/dev_utils.ts | 16 ++ 2 files changed, 135 insertions(+), 19 deletions(-) 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 e761dfd8..3c47f66d 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts @@ -12,7 +12,7 @@ import { messagesDto } from './dto/messages.dto'; import { socketDto } from './dto/socket.dto'; import { muteDto } from './dto/mute.dto'; import * as bcrypt from 'bcrypt'; -import { printCaller } from './dev/dev_utils'; +import { printCaller, printCallerError } from './dev/dev_utils'; @Injectable() @@ -34,23 +34,21 @@ export class ChatService { addSocket(key: string, socket: socketDto) { - printCaller(`-- in (key: ${key}) `); + printCaller(`----------------- in (key: ${key}) `); this.sockets.set(key, socket); console.log("this.sockets:", this.sockets); } - getSocket(key: string) { - printCaller("-- in "); + printCaller("----------------- in "); let socket = this.sockets.get(key); console.log("this.sockets:", this.sockets); console.log("socket:", socket); return socket; } - removeSocket(key: string) { - printCaller("-- in "); + printCaller("----------------- in "); this.sockets.delete(key); } @@ -86,7 +84,10 @@ export class ChatService { if (rooms) return rooms; else + { + printCallerError(`ERROR in chat: rooms not found for ${username}`); return []; + } } async getMyDirects(username: string): Promise @@ -103,7 +104,10 @@ export class ChatService { if (directs) return directs; else + { + printCallerError(`ERROR in chat: directs not found for ${username}`); return []; + } } async getAllRooms(): Promise @@ -118,7 +122,10 @@ export class ChatService { if (rooms) return rooms; else + { + printCallerError(`ERROR in chat: all rooms not found`); return []; + } } async getAllNotMyRooms(username: string): Promise @@ -140,7 +147,10 @@ export class ChatService { if (rooms) return rooms; else + { + printCallerError(`ERROR in chat: rooms not found for ${username}`); return []; + } } async getAllOtherRoomsAndUsers(username: string): Promise @@ -149,8 +159,11 @@ export class ChatService { const all_rooms = await this.getAllNotMyRooms(username); const all_users = await this.getAllUsersNotMyRooms(username); - if (!all_users || !all_users) + if (!all_rooms || !all_users) + { + printCallerError(`ERROR in chat: rooms or users not found for ${username}`); return []; + } let row_rooms = all_rooms.map(room => { return { @@ -172,7 +185,10 @@ export class ChatService { if (rooms) return rooms; else + { + printCallerError(`ERROR in chat: rooms not found for ${username}`); return []; + } } async getMessagesFromCurrentRoom(username: string): Promise @@ -181,10 +197,16 @@ export class ChatService { const user_db = await this.getUserByName(username); if (!user_db) + { + printCallerError(`ERROR in chat: user not found for ${username}`); return []; + } const currentRoom = await this.getRoomByName(user_db.currentRoom); if (!currentRoom) + { + printCallerError(`ERROR in chat: current room not found for ${username}`); return []; + } let messages = null; if (currentRoom) messages = currentRoom.messages; @@ -193,7 +215,10 @@ export class ChatService { if (messages) return messages; else + { + printCallerError(`ERROR in chat: messages not found for ${username}`); return []; + } } async getCurrentRoomName(username: string): Promise @@ -203,15 +228,17 @@ export class ChatService { const user_db = await this.getUserByName(username); if (!user_db) { - printCaller(`error: 'the user ${username} was not found'`); + printCallerError(`ERROR in chat: 'the user ${username} was not found'`); return; } + if (!user_db.currentRoom) + { + printCallerError(`ERROR in chat: currentRoom not found in ${user_db}`); + return ""; + } printCaller("-- out "); - if (user_db && user_db.currentRoom) - return user_db.currentRoom; - else - return ""; + return user_db.currentRoom; } async getRoomByName(room_name: string, fieldsToReturn: string[] = null): Promise @@ -234,7 +261,10 @@ export class ChatService { if (room) return room; else + { + printCallerError(`ERROR in chat: room ${room_name} not found`); return null; + } } async getRoomById(id: number): Promise @@ -250,7 +280,10 @@ export class ChatService { if (room) return room; else + { + printCallerError(`ERROR in chat: room not found ${id}`); return null; + } } @@ -263,7 +296,10 @@ export class ChatService { const user_db = await this.getUserByName(username); if (!user_db) + { + printCallerError(`ERROR in chat: user ${user_db} not found`); return ""; + } user_db.currentRoom = room_name; await this.userRepository.save(user_db); @@ -277,7 +313,10 @@ export class ChatService { const room_db = await this.getRoomByName(room.name); if (!room_db) + { + printCallerError(`ERROR in chat: room ${room_db} not found`); return; + } const is_match = await bcrypt.compare(room.password, room_db.hash); if (!is_match) { @@ -303,7 +342,10 @@ export class ChatService { const room_db = await this.getRoomByName(room.name); if (!room_db) + { + printCallerError(`ERROR in chat: room not found for ${username}`); return; + } if (!room_db.admins.includes(username)) { @@ -365,7 +407,10 @@ export class ChatService { const room_db = await this.getRoomByName(room_name); if (!room_db) + { + printCallerError(`ERROR in chat: room ${room_name} not found`); return; + } if (room_db.type === "direct") { printCaller(`throw error: error: true, code: 'NO_DIRECT_ADMIN', message: 'there are no admins in direct messages'`); @@ -424,7 +469,10 @@ export class ChatService { let user = await this.getUserByName(username); if (!user) + { + printCallerError(`ERROR in chat: room not found for ${username}`); return; + } let relation = await this.findOneRelationshipByUsername(to_block_username, username); if (relation) { @@ -518,6 +566,11 @@ export class ChatService { printCaller("-- in "); const room: Chatroom = await this.getRoomByName(room_name); + if(!room) + { + printCallerError(`ERROR in chat: room not found for ${username}`); + return null; + } // update room with new user room.users.push(username); @@ -540,6 +593,11 @@ export class ChatService { printCaller("-- in "); const my_room = await this.getRoomByName(room_name); + if(!my_room) + { + printCallerError(`ERROR in chat: room not found for ${username}`); + return; + } let chat_message: messagesDto = { name: username, message: message, @@ -555,6 +613,11 @@ export class ChatService { printCaller("-- in "); const room_db = await this.getRoomByName(room_name); + if(!room_db) + { + printCallerError(`ERROR in chat: room not found for ${username}`); + return; + } if (!room_db.admins.includes(username)) { @@ -594,6 +657,11 @@ export class ChatService { let messages = [`${username} left the room`]; const room = await this.getRoomByName(room_name); + if(!room) + { + printCallerError(`ERROR in chat: room not found for ${username}`); + return []; + } if (!room.users.includes(username)) { console.log("throw error: error: true, code: 'USER_NOT_FOUND', message: 'your are not in this room'"); @@ -635,6 +703,11 @@ export class ChatService { printCaller("-- in "); const room_db = await this.getRoomByName(room_name); + if(!room_db) + { + printCallerError(`ERROR in chat: room not found for ${username}`); + return; + } let index = room_db.mutes.findIndex(mute => mute.name === username); if (index > -1) @@ -659,7 +732,13 @@ export class ChatService { .getOne(); printCaller("-- out "); - return user; + if (user) + return user; + else + { + printCallerError(`ERROR in chat: user not found for ${username}`); + return null; + } } async getAllUsersNotMyRooms(username: string): Promise @@ -667,6 +746,11 @@ export class ChatService { printCaller("-- in "); const directs = await this.getMyDirects(username); + if(!directs) + { + printCallerError(`ERROR in chat: direct room not found for ${username}`); + return null; + } // get all users from directs let usernames = directs.map(room => { @@ -683,7 +767,13 @@ export class ChatService { .getMany(); printCaller("-- out "); - return users; + if (users) + return users; + else + { + printCallerError(`ERROR in chat: users not found for ${username}`); + return []; + } } async getAllUsersNotInRoom(current_room_name: string): Promise @@ -700,7 +790,13 @@ export class ChatService { .getMany(); printCaller("-- out "); - return users; + if (users) + return users; + else + { + printCallerError(`ERROR in chat: users not found for ${current_room_name}`); + return []; + } } @@ -711,7 +807,6 @@ export class ChatService { { printCaller("-- in "); - let room_name = await this.getCurrentRoomName(socket.username); if (!room_name) { @@ -793,11 +888,16 @@ export class ChatService { { printCaller("-- in "); - console.log("old_name:", old_name); - console.log("new_name:", new_name); - console.log("sockets:", this.sockets); + console.log("-- old_name:", old_name); + console.log("-- new_name:", new_name); + console.log("-- sockets:", this.sockets); let rooms: Chatroom[] = await this.getAllRooms(); + if(!rooms) + { + printCallerError(`ERROR in chat: all rooms not found`); + return null; + } await rooms.forEach((room) => { let room_has_change: boolean = false; diff --git a/srcs/requirements/nestjs/api_back/src/chat/dev/dev_utils.ts b/srcs/requirements/nestjs/api_back/src/chat/dev/dev_utils.ts index e3dc8ae0..29ce3f45 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/dev/dev_utils.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/dev/dev_utils.ts @@ -12,3 +12,19 @@ export function printCaller(...prefix) console.log(...prefix, caller); } } + +export function printCallerError(...prefix) +{ + try + { + throw new Error(); + } + catch (e) + { + Error.captureStackTrace(e); + let stack = e.stack.split('\n'); + let caller = stack[2].trim(); + console.log("\x1b[33m%s\x1b[0m", ...prefix, caller); + } +} +