wip still not resolved pbm with map socket

This commit is contained in:
simplonco
2023-01-17 19:39:55 +01:00
parent 1ff9653eac
commit d1a3a6e120
2 changed files with 135 additions and 19 deletions

View File

@@ -12,7 +12,7 @@ import { messagesDto } from './dto/messages.dto';
import { socketDto } from './dto/socket.dto'; import { socketDto } from './dto/socket.dto';
import { muteDto } from './dto/mute.dto'; import { muteDto } from './dto/mute.dto';
import * as bcrypt from 'bcrypt'; import * as bcrypt from 'bcrypt';
import { printCaller } from './dev/dev_utils'; import { printCaller, printCallerError } from './dev/dev_utils';
@Injectable() @Injectable()
@@ -34,23 +34,21 @@ export class ChatService {
addSocket(key: string, socket: socketDto) addSocket(key: string, socket: socketDto)
{ {
printCaller(`-- in (key: ${key}) `); printCaller(`----------------- in (key: ${key}) `);
this.sockets.set(key, socket); this.sockets.set(key, socket);
console.log("this.sockets:", this.sockets); console.log("this.sockets:", this.sockets);
} }
getSocket(key: string) getSocket(key: string)
{ {
printCaller("-- in "); printCaller("----------------- in ");
let socket = this.sockets.get(key); let socket = this.sockets.get(key);
console.log("this.sockets:", this.sockets); console.log("this.sockets:", this.sockets);
console.log("socket:", socket); console.log("socket:", socket);
return socket; return socket;
} }
removeSocket(key: string) removeSocket(key: string)
{ {
printCaller("-- in "); printCaller("----------------- in ");
this.sockets.delete(key); this.sockets.delete(key);
} }
@@ -86,7 +84,10 @@ export class ChatService {
if (rooms) if (rooms)
return rooms; return rooms;
else else
{
printCallerError(`ERROR in chat: rooms not found for ${username}`);
return []; return [];
}
} }
async getMyDirects(username: string): Promise<Chatroom[]> async getMyDirects(username: string): Promise<Chatroom[]>
@@ -103,7 +104,10 @@ export class ChatService {
if (directs) if (directs)
return directs; return directs;
else else
{
printCallerError(`ERROR in chat: directs not found for ${username}`);
return []; return [];
}
} }
async getAllRooms(): Promise<Chatroom[]> async getAllRooms(): Promise<Chatroom[]>
@@ -118,7 +122,10 @@ export class ChatService {
if (rooms) if (rooms)
return rooms; return rooms;
else else
{
printCallerError(`ERROR in chat: all rooms not found`);
return []; return [];
}
} }
async getAllNotMyRooms(username: string): Promise<Chatroom[]> async getAllNotMyRooms(username: string): Promise<Chatroom[]>
@@ -140,7 +147,10 @@ export class ChatService {
if (rooms) if (rooms)
return rooms; return rooms;
else else
{
printCallerError(`ERROR in chat: rooms not found for ${username}`);
return []; return [];
}
} }
async getAllOtherRoomsAndUsers(username: string): Promise<roomDto[]> async getAllOtherRoomsAndUsers(username: string): Promise<roomDto[]>
@@ -149,8 +159,11 @@ export class ChatService {
const all_rooms = await this.getAllNotMyRooms(username); const all_rooms = await this.getAllNotMyRooms(username);
const all_users = await this.getAllUsersNotMyRooms(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 []; return [];
}
let row_rooms = all_rooms.map(room => { let row_rooms = all_rooms.map(room => {
return { return {
@@ -172,7 +185,10 @@ export class ChatService {
if (rooms) if (rooms)
return rooms; return rooms;
else else
{
printCallerError(`ERROR in chat: rooms not found for ${username}`);
return []; return [];
}
} }
async getMessagesFromCurrentRoom(username: string): Promise<messagesDto[]> async getMessagesFromCurrentRoom(username: string): Promise<messagesDto[]>
@@ -181,10 +197,16 @@ export class ChatService {
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
if (!user_db) if (!user_db)
{
printCallerError(`ERROR in chat: user not found for ${username}`);
return []; return [];
}
const currentRoom = await this.getRoomByName(user_db.currentRoom); const currentRoom = await this.getRoomByName(user_db.currentRoom);
if (!currentRoom) if (!currentRoom)
{
printCallerError(`ERROR in chat: current room not found for ${username}`);
return []; return [];
}
let messages = null; let messages = null;
if (currentRoom) if (currentRoom)
messages = currentRoom.messages; messages = currentRoom.messages;
@@ -193,7 +215,10 @@ export class ChatService {
if (messages) if (messages)
return messages; return messages;
else else
{
printCallerError(`ERROR in chat: messages not found for ${username}`);
return []; return [];
}
} }
async getCurrentRoomName(username: string): Promise<string> async getCurrentRoomName(username: string): Promise<string>
@@ -203,15 +228,17 @@ export class ChatService {
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
if (!user_db) if (!user_db)
{ {
printCaller(`error: 'the user ${username} was not found'`); printCallerError(`ERROR in chat: 'the user ${username} was not found'`);
return; return;
} }
if (!user_db.currentRoom)
{
printCallerError(`ERROR in chat: currentRoom not found in ${user_db}`);
return "";
}
printCaller("-- out "); printCaller("-- out ");
if (user_db && user_db.currentRoom) return user_db.currentRoom;
return user_db.currentRoom;
else
return "";
} }
async getRoomByName(room_name: string, fieldsToReturn: string[] = null): Promise<Chatroom> async getRoomByName(room_name: string, fieldsToReturn: string[] = null): Promise<Chatroom>
@@ -234,7 +261,10 @@ export class ChatService {
if (room) if (room)
return room; return room;
else else
{
printCallerError(`ERROR in chat: room ${room_name} not found`);
return null; return null;
}
} }
async getRoomById(id: number): Promise<Chatroom> async getRoomById(id: number): Promise<Chatroom>
@@ -250,7 +280,10 @@ export class ChatService {
if (room) if (room)
return room; return room;
else else
{
printCallerError(`ERROR in chat: room not found ${id}`);
return null; return null;
}
} }
@@ -263,7 +296,10 @@ export class ChatService {
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
if (!user_db) if (!user_db)
{
printCallerError(`ERROR in chat: user ${user_db} not found`);
return ""; return "";
}
user_db.currentRoom = room_name; user_db.currentRoom = room_name;
await this.userRepository.save(user_db); await this.userRepository.save(user_db);
@@ -277,7 +313,10 @@ export class ChatService {
const room_db = await this.getRoomByName(room.name); const room_db = await this.getRoomByName(room.name);
if (!room_db) if (!room_db)
{
printCallerError(`ERROR in chat: room ${room_db} not found`);
return; return;
}
const is_match = await bcrypt.compare(room.password, room_db.hash); const is_match = await bcrypt.compare(room.password, room_db.hash);
if (!is_match) if (!is_match)
{ {
@@ -303,7 +342,10 @@ export class ChatService {
const room_db = await this.getRoomByName(room.name); const room_db = await this.getRoomByName(room.name);
if (!room_db) if (!room_db)
{
printCallerError(`ERROR in chat: room not found for ${username}`);
return; return;
}
if (!room_db.admins.includes(username)) if (!room_db.admins.includes(username))
{ {
@@ -365,7 +407,10 @@ export class ChatService {
const room_db = await this.getRoomByName(room_name); const room_db = await this.getRoomByName(room_name);
if (!room_db) if (!room_db)
{
printCallerError(`ERROR in chat: room ${room_name} not found`);
return; return;
}
if (room_db.type === "direct") if (room_db.type === "direct")
{ {
printCaller(`throw error: error: true, code: 'NO_DIRECT_ADMIN', message: 'there are no admins in direct messages'`); 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); let user = await this.getUserByName(username);
if (!user) if (!user)
{
printCallerError(`ERROR in chat: room not found for ${username}`);
return; return;
}
let relation = await this.findOneRelationshipByUsername(to_block_username, username); let relation = await this.findOneRelationshipByUsername(to_block_username, username);
if (relation) if (relation)
{ {
@@ -518,6 +566,11 @@ export class ChatService {
printCaller("-- in "); printCaller("-- in ");
const room: Chatroom = await this.getRoomByName(room_name); 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 // update room with new user
room.users.push(username); room.users.push(username);
@@ -540,6 +593,11 @@ export class ChatService {
printCaller("-- in "); printCaller("-- in ");
const my_room = await this.getRoomByName(room_name); 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 = { let chat_message: messagesDto = {
name: username, name: username,
message: message, message: message,
@@ -555,6 +613,11 @@ export class ChatService {
printCaller("-- in "); printCaller("-- in ");
const room_db = await this.getRoomByName(room_name); 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)) if (!room_db.admins.includes(username))
{ {
@@ -594,6 +657,11 @@ export class ChatService {
let messages = [`${username} left the room`]; let messages = [`${username} left the room`];
const room = await this.getRoomByName(room_name); const room = await this.getRoomByName(room_name);
if(!room)
{
printCallerError(`ERROR in chat: room not found for ${username}`);
return [];
}
if (!room.users.includes(username)) if (!room.users.includes(username))
{ {
console.log("throw error: error: true, code: 'USER_NOT_FOUND', message: 'your are not in this room'"); 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 "); printCaller("-- in ");
const room_db = await this.getRoomByName(room_name); 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); let index = room_db.mutes.findIndex(mute => mute.name === username);
if (index > -1) if (index > -1)
@@ -659,7 +732,13 @@ export class ChatService {
.getOne(); .getOne();
printCaller("-- out "); 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<User[]> async getAllUsersNotMyRooms(username: string): Promise<User[]>
@@ -667,6 +746,11 @@ export class ChatService {
printCaller("-- in "); printCaller("-- in ");
const directs = await this.getMyDirects(username); 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 // get all users from directs
let usernames = directs.map(room => { let usernames = directs.map(room => {
@@ -683,7 +767,13 @@ export class ChatService {
.getMany(); .getMany();
printCaller("-- out "); 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<User[]> async getAllUsersNotInRoom(current_room_name: string): Promise<User[]>
@@ -700,7 +790,13 @@ export class ChatService {
.getMany(); .getMany();
printCaller("-- out "); 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 "); printCaller("-- in ");
let room_name = await this.getCurrentRoomName(socket.username); let room_name = await this.getCurrentRoomName(socket.username);
if (!room_name) if (!room_name)
{ {
@@ -793,11 +888,16 @@ export class ChatService {
{ {
printCaller("-- in "); printCaller("-- in ");
console.log("old_name:", old_name); console.log("-- old_name:", old_name);
console.log("new_name:", new_name); console.log("-- new_name:", new_name);
console.log("sockets:", this.sockets); console.log("-- sockets:", this.sockets);
let rooms: Chatroom[] = await this.getAllRooms(); let rooms: Chatroom[] = await this.getAllRooms();
if(!rooms)
{
printCallerError(`ERROR in chat: all rooms not found`);
return null;
}
await rooms.forEach((room) => await rooms.forEach((room) =>
{ {
let room_has_change: boolean = false; let room_has_change: boolean = false;

View File

@@ -12,3 +12,19 @@ export function printCaller(...prefix)
console.log(...prefix, caller); 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);
}
}