change socket username

This commit is contained in:
simplonco
2023-01-17 17:50:10 +01:00
parent d0dfb4a533
commit c6492bcec5
3 changed files with 96 additions and 36 deletions

View File

@@ -122,7 +122,7 @@ export class ChatController {
let message = `${username} is now admin`; let message = `${username} is now admin`;
await this.chatService.addMessageToRoom(current_room_name, "SERVER", message); await this.chatService.addMessageToRoom(current_room_name, "SERVER", message);
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatService.sockets.get(req.user.username);
let server = this.chatGateway.server; let server = this.chatGateway.server;
await server.in(socket.room).emit('message', "SERVER", message); await server.in(socket.room).emit('message', "SERVER", message);
@@ -207,7 +207,7 @@ export class ChatController {
let message = `${req.user.username} changed the password`; let message = `${req.user.username} changed the password`;
room.allowed_users = [req.user.username]; room.allowed_users = [req.user.username];
await this.chatService.setPassword(req.user.username, message, room); await this.chatService.setPassword(req.user.username, message, room);
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatService.sockets.get(req.user.username);
await socket.to(socket.room).emit('message', "SERVER", message); await socket.to(socket.room).emit('message', "SERVER", message);
} }
@@ -267,7 +267,7 @@ export class ChatController {
room = 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); let socket: socketDto = this.chatService.sockets.get(req.user.username);
await this.chatService.socketJoinRoom(socket, room.name); await this.chatService.socketJoinRoom(socket, room.name);
const ret_room = this.format_room(room); const ret_room = this.format_room(room);
@@ -308,7 +308,7 @@ export class ChatController {
} }
await this.chatService.setCurrentRoom(req.user.username, room.name); await this.chatService.setCurrentRoom(req.user.username, room.name);
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatService.sockets.get(req.user.username);
await this.chatService.socketChangeRoom(socket, room.name); await this.chatService.socketChangeRoom(socket, room.name);
const ret_room = this.format_room(room); const ret_room = this.format_room(room);
@@ -354,7 +354,7 @@ export class ChatController {
await this.chatService.setPassword(req.user.username, message, room, old_password); await this.chatService.setPassword(req.user.username, message, room, old_password);
// inform other connected users // inform other connected users
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatService.sockets.get(req.user.username);
await socket.to(socket.room).emit('message', "SERVER", message); await socket.to(socket.room).emit('message', "SERVER", message);
await socket.to(socket.room).emit('new_password'); await socket.to(socket.room).emit('new_password');
@@ -376,7 +376,7 @@ export class ChatController {
await this.chatService.setPassword(req.user.username, message, room); await this.chatService.setPassword(req.user.username, message, room);
// inform other connected users // inform other connected users
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatService.sockets.get(req.user.username);
await socket.to(socket.room).emit('message', "SERVER", message); await socket.to(socket.room).emit('message', "SERVER", message);
await socket.to(socket.room).emit('new_password'); await socket.to(socket.room).emit('new_password');
@@ -398,7 +398,7 @@ export class ChatController {
await this.chatService.setPassword(req.user.username, message, room); await this.chatService.setPassword(req.user.username, message, room);
// inform other connected users // inform other connected users
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatService.sockets.get(req.user.username);
await socket.to(socket.room).emit('message', "SERVER", message); await socket.to(socket.room).emit('message', "SERVER", message);
const ret_room = this.format_room(room); const ret_room = this.format_room(room);
@@ -445,7 +445,7 @@ export class ChatController {
await this.chatService.setCurrentRoom(req.user.username, ""); await this.chatService.setCurrentRoom(req.user.username, "");
// leaving message // leaving message
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatService.sockets.get(req.user.username);
await messages.forEach(async (message) => await messages.forEach(async (message) =>
{ {
await this.chatService.addMessageToRoom(room_name, "SERVER", message); await this.chatService.addMessageToRoom(room_name, "SERVER", message);
@@ -552,7 +552,7 @@ export class ChatController {
// inform other connected users // inform other connected users
let message = `${req.user.username} has muted ${mute.name} untill ${time}`; let message = `${req.user.username} has muted ${mute.name} untill ${time}`;
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatService.sockets.get(req.user.username);
let server = this.chatGateway.server; let server = this.chatGateway.server;
await this.chatService.addMessageToRoom(room_name, "SERVER", message); await this.chatService.addMessageToRoom(room_name, "SERVER", message);
await server.in(socket.room).emit('message', "SERVER", message); await server.in(socket.room).emit('message', "SERVER", message);
@@ -601,7 +601,7 @@ export class ChatController {
await this.chatService.removeMute(username, room_name); await this.chatService.removeMute(username, room_name);
let message = `${req.user.username} has un-muted ${username}`; let message = `${req.user.username} has un-muted ${username}`;
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatService.sockets.get(req.user.username);
let server = this.chatGateway.server; let server = this.chatGateway.server;
await this.chatService.addMessageToRoom(room_name, "SERVER", message); await this.chatService.addMessageToRoom(room_name, "SERVER", message);
await server.in(socket.room).emit('message', "SERVER", message); await server.in(socket.room).emit('message', "SERVER", message);
@@ -619,7 +619,7 @@ export class ChatController {
await this.chatService.addBlockUser(req.user.username, username); await this.chatService.addBlockUser(req.user.username, username);
let user_socket: socketDto = this.chatGateway.sockets.get(req.user.username); let user_socket: socketDto = this.chatService.sockets.get(req.user.username);
user_socket.join(`${username}_not_emit`); user_socket.join(`${username}_not_emit`);
res.status(HttpStatus.OK).json({ message: "successfull block" }); res.status(HttpStatus.OK).json({ message: "successfull block" });
@@ -634,7 +634,7 @@ export class ChatController {
printCaller("- in "); printCaller("- in ");
await this.chatService.removeBlockUser(req.user.username, username); await this.chatService.removeBlockUser(req.user.username, username);
let user_socket: socketDto = this.chatGateway.sockets.get(req.user.username); let user_socket: socketDto = this.chatService.sockets.get(req.user.username);
user_socket.leave(`${username}_not_emit`); user_socket.leave(`${username}_not_emit`);
res.status(HttpStatus.OK).json({ message: "successfull unblock" }); res.status(HttpStatus.OK).json({ message: "successfull unblock" });

View File

@@ -19,8 +19,6 @@ implements OnGatewayConnection, OnGatewayDisconnect
private chatService: ChatService, private chatService: ChatService,
) {} ) {}
sockets = new Map<string, socketDto>();
@WebSocketServer() @WebSocketServer()
server; server;
@@ -33,7 +31,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
if (!socket.username) if (!socket.username)
return; return;
this.sockets.set(socket.username, socket); this.chatService.sockets.set(socket.username, socket);
printCaller("--- socket.username:", socket.username); printCaller("--- socket.username:", socket.username);
let not_emit: string = `${socket.username}_not_emit`; let not_emit: string = `${socket.username}_not_emit`;
@@ -49,7 +47,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
socket.join(current_room); socket.join(current_room);
} }
async handleDisconnect(socket: socketDto) { async handleDisconnect(socket: socketDto) {
this.sockets.delete(socket.username); this.chatService.sockets.delete(socket.username);
} }
@SubscribeMessage('join') @SubscribeMessage('join')
@@ -74,6 +72,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
async handleMessage(@ConnectedSocket() socket: socketDto, @MessageBody() message: string): Promise<void> async handleMessage(@ConnectedSocket() socket: socketDto, @MessageBody() message: string): Promise<void>
{ {
console.log('- in handleMessage gateway'); console.log('- in handleMessage gateway');
await this.chatService.socketIncommingMessage(socket, message); await this.chatService.socketIncommingMessage(socket, message);
console.log('- out handleMessage gateway'); console.log('- out handleMessage gateway');
} }

View File

@@ -30,6 +30,8 @@ export class ChatService {
private readonly friendshipRepository: Repository<Friendship>, private readonly friendshipRepository: Repository<Friendship>,
) {} ) {}
sockets = new Map<string, socketDto>();
// temp for test // temp for test
sleep(ms) { sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
@@ -44,6 +46,7 @@ export class ChatService {
printCaller("-- in "); printCaller("-- in ");
//await this.sleep(1000); //await this.sleep(1000);
let rooms: Chatroom[];
const queryBuilder = this.chatroomRepository const queryBuilder = this.chatroomRepository
.createQueryBuilder('chatroom') .createQueryBuilder('chatroom')
@@ -55,48 +58,67 @@ export class ChatService {
queryBuilder.select(fields); queryBuilder.select(fields);
} }
const rooms = await queryBuilder.getMany(); rooms = await queryBuilder.getMany();
printCaller("-- out "); printCaller("-- out ");
return rooms; if (rooms)
return rooms;
else
return [];
} }
async getMyDirects(username: string): Promise<Chatroom[]> async getMyDirects(username: string): Promise<Chatroom[]>
{ {
printCaller("-- in "); printCaller("-- in ");
let directs: Chatroom[];
const my_rooms = await this.getMyRooms(username); const my_rooms = await this.getMyRooms(username);
const directs = my_rooms.filter(room => room.type === 'direct'); if (my_rooms)
directs = my_rooms.filter(room => room.type === 'direct');
printCaller("-- out "); printCaller("-- out ");
return directs; if (directs)
return directs;
else
return [];
} }
async getAllRooms(): Promise<Chatroom[]> async getAllRooms(): Promise<Chatroom[]>
{ {
printCaller("-- in "); printCaller("-- in ");
const rooms = await this.chatroomRepository let rooms: Chatroom[] = await this.chatroomRepository
.createQueryBuilder('chatroom') .createQueryBuilder('chatroom')
.getMany(); .getMany();
printCaller("-- out "); printCaller("-- out ");
return rooms; if (rooms)
return rooms;
else
return [];
} }
async getAllNotMyRooms(username: string): Promise<Chatroom[]> async getAllNotMyRooms(username: string): Promise<Chatroom[]>
{ {
printCaller("-- in "); printCaller("-- in ");
let rooms: Chatroom[];
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
const rooms = await this.chatroomRepository if (user_db)
.createQueryBuilder('chatroom') {
.where('chatroom.type NOT IN (:...type)', { type: ['private', 'direct'] }) rooms = await this.chatroomRepository
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` }) .createQueryBuilder('chatroom')
.getMany(); .where('chatroom.type NOT IN (:...type)', { type: ['private', 'direct'] })
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` })
.getMany();
}
printCaller("-- out "); printCaller("-- out ");
return rooms; if (rooms)
return rooms;
else
return [];
} }
async getAllOtherRoomsAndUsers(username: string): Promise<roomDto[]> async getAllOtherRoomsAndUsers(username: string): Promise<roomDto[]>
@@ -105,6 +127,8 @@ 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)
return [];
let row_rooms = all_rooms.map(room => { let row_rooms = all_rooms.map(room => {
return { return {
@@ -123,7 +147,10 @@ export class ChatService {
let rooms: roomDto[] = row_rooms.concat(users); let rooms: roomDto[] = row_rooms.concat(users);
printCaller("-- out "); printCaller("-- out ");
return rooms; if (rooms)
return rooms;
else
return [];
} }
async getMessagesFromCurrentRoom(username: string): Promise<messagesDto[]> async getMessagesFromCurrentRoom(username: string): Promise<messagesDto[]>
@@ -131,13 +158,20 @@ export class ChatService {
printCaller("-- in "); printCaller("-- in ");
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
if (!user_db)
return [];
const currentRoom = await this.getRoomByName(user_db.currentRoom); const currentRoom = await this.getRoomByName(user_db.currentRoom);
if (!currentRoom)
return [];
let messages = null; let messages = null;
if (currentRoom) if (currentRoom)
messages = currentRoom.messages; messages = currentRoom.messages;
printCaller("-- out "); printCaller("-- out ");
return messages; if (messages)
return messages;
else
return [];
} }
async getCurrentRoomName(username: string): Promise<string> async getCurrentRoomName(username: string): Promise<string>
@@ -147,12 +181,15 @@ export class ChatService {
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
if (!user_db) if (!user_db)
{ {
printCaller(`throw error: error: true, code: 'USER_NOT_FOUND', message: 'the user was not found'`); printCaller(`error: 'the user was not found'`);
return; return;
} }
printCaller("-- out "); printCaller("-- out ");
return user_db.currentRoom; if (user_db && 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>
@@ -172,7 +209,10 @@ export class ChatService {
const room = await queryBuilder.getOne(); const room = await queryBuilder.getOne();
printCaller("-- out "); printCaller("-- out ");
return room; if (room)
return room;
else
return null;
} }
async getRoomById(id: number): Promise<Chatroom> async getRoomById(id: number): Promise<Chatroom>
@@ -185,7 +225,10 @@ export class ChatService {
.getOne(); .getOne();
printCaller("-- out "); printCaller("-- out ");
return room; if (room)
return room;
else
return null;
} }
@@ -197,6 +240,8 @@ export class ChatService {
printCaller("-- in "); printCaller("-- in ");
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
if (!user_db)
return "";
user_db.currentRoom = room_name; user_db.currentRoom = room_name;
await this.userRepository.save(user_db); await this.userRepository.save(user_db);
@@ -209,6 +254,8 @@ 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)
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)
{ {
@@ -233,6 +280,8 @@ export class ChatService {
} }
const room_db = await this.getRoomByName(room.name); const room_db = await this.getRoomByName(room.name);
if (!room_db)
return;
if (!room_db.admins.includes(username)) if (!room_db.admins.includes(username))
{ {
@@ -293,6 +342,8 @@ 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)
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'`);
@@ -310,7 +361,7 @@ export class ChatService {
printCaller("-- out "); printCaller("-- out ");
} }
async findOneRelationshipByUsername(friendUsername : string, username : string) async findOneRelationshipByUsername(friendUsername : string, username : string): Promise<SendableFriendship>
{ {
printCaller("-- in "); printCaller("-- in ");
@@ -350,6 +401,8 @@ export class ChatService {
printCaller("-- in "); printCaller("-- in ");
let user = await this.getUserByName(username); let user = await this.getUserByName(username);
if (!user)
return;
let relation = await this.findOneRelationshipByUsername(to_block_username, username); let relation = await this.findOneRelationshipByUsername(to_block_username, username);
if (relation) if (relation)
{ {
@@ -760,10 +813,18 @@ export class ChatService {
if (room_has_change) if (room_has_change)
this.chatroomRepository.save(room); this.chatroomRepository.save(room);
this.addMessageToRoom(room.name, "SERVER", `${old_name} changes it's name for ${new_name}`); this.addMessageToRoom(room.name, "SERVER", `${old_name} changes it's name for ${new_name}`);
let socket: socketDto = this.sockets.get(old_name);
if (socket)
{
this.sockets.delete(old_name);
socket.username = new_name;
this.sockets.set(new_name, socket);
}
}); });
rooms = await this.getAllRooms();
printCaller("-- out "); printCaller("-- out ");
} }