fixed double socket initialisation and room settings not updating password
This commit is contained in:
@@ -94,6 +94,26 @@ export class ChatController {
|
||||
printCaller("- out ");
|
||||
}
|
||||
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
@Get('currentroom')
|
||||
async getCurrentRooms(@Req() req, @Res() res): Promise<void>
|
||||
{
|
||||
printCaller("- in ");
|
||||
|
||||
const current_room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
||||
if (!current_room_name)
|
||||
{
|
||||
printCallerError(`controllerror: true, code: 'ROOM_NOT_FOUND', message: 'current room not found for ${req.user.username}'`);
|
||||
throw new HttpException({ error: true, code: 'ROOM_NOT_FOUND', message: `current room not found for ${req.user.username}` }, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
const current_room = await this.chatService.getRoomByName(current_room_name);
|
||||
|
||||
const ret_room = this.format_room(current_room);
|
||||
res.status(HttpStatus.OK).json({ room: ret_room });
|
||||
printCaller("- out ");
|
||||
}
|
||||
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
@Get('allrooms')
|
||||
@@ -410,7 +430,7 @@ export class ChatController {
|
||||
let message = `${req.user.username} removed a new password`;
|
||||
room.allowed_users = [];
|
||||
room.protection = false;
|
||||
await this.chatService.setPassword(req.user.username, message, room);
|
||||
await this.chatService.removePassword(req.user.username, message, room);
|
||||
|
||||
// inform other connected users
|
||||
let socket: socketDto = this.chatService.getSocket(req.user.username);
|
||||
|
||||
@@ -31,10 +31,23 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
||||
|
||||
if (!socket.username)
|
||||
return;
|
||||
printCaller("--- socket.username:", socket.username);
|
||||
|
||||
// save socket and server
|
||||
this.chatService.addSocket(socket.username, socket);
|
||||
socket_server.server = this.server;
|
||||
printCaller("--- socket.username:", socket.username);
|
||||
|
||||
// check if socket already exist and delete if it does
|
||||
console.log("---- socket.username", socket.username);
|
||||
let serveur_console: any = this.server;
|
||||
await serveur_console.sockets.sockets.forEach((sock: any) => console.log(sock.id, sock.username));
|
||||
await serveur_console.sockets.adapter.rooms.forEach((value, key) =>
|
||||
{
|
||||
console.log("");
|
||||
console.log("room name:", key);
|
||||
console.log("room users id:");
|
||||
console.log(value);
|
||||
});
|
||||
|
||||
let not_emit: string = `${socket.username}_not_emit`;
|
||||
socket.join(not_emit);
|
||||
@@ -46,9 +59,23 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
||||
});
|
||||
|
||||
let current_room = await this.chatService.getCurrentRoomName(socket.username);
|
||||
socket.join(current_room);
|
||||
if (current_room)
|
||||
socket.join(current_room);
|
||||
}
|
||||
|
||||
async handleDisconnect(socket: socketDto) {
|
||||
printCallerError("handleDisconnect");
|
||||
console.log("---- socket.username", socket.username);
|
||||
let serveur_console: any = this.server;
|
||||
await serveur_console.sockets.sockets.forEach((sock: any) => console.log(sock.id, sock.username));
|
||||
await serveur_console.sockets.adapter.rooms.forEach((value, key) =>
|
||||
{
|
||||
console.log("");
|
||||
console.log("room name:", key);
|
||||
console.log("room users id:");
|
||||
console.log(value);
|
||||
});
|
||||
|
||||
this.chatService.removeSocket(socket.username);
|
||||
}
|
||||
|
||||
|
||||
@@ -329,6 +329,45 @@ export class ChatService {
|
||||
printCaller("-- out ");
|
||||
}
|
||||
|
||||
async removePassword(username: string, message: string, room: roomDto): Promise<void>
|
||||
{
|
||||
printCaller("-- in ");
|
||||
|
||||
const room_db = await this.getRoomByName(room.name);
|
||||
if (!room_db)
|
||||
{
|
||||
printCallerError(`ERROR in chat: room not found for ${username}`);
|
||||
return;
|
||||
}
|
||||
console.log("---- room_db:", room_db);
|
||||
|
||||
if (!room_db.admins.includes(username))
|
||||
{
|
||||
console.log("throw error: error: true, code: 'NO_ADMIN', message: 'only admins are allowed to remove password'");
|
||||
throw new HttpException({ error: true, code: 'NO_ADMIN', message: `only admins are allowed to remove password` }, HttpStatus.FORBIDDEN);
|
||||
}
|
||||
if (!room.password)
|
||||
{
|
||||
console.log("throw error: error: true, code: 'NO_PASSWORD', message: 'you must provide a password'");
|
||||
throw new HttpException({ error: true, code: 'NO_PASSWORD', message: `you must provide a password` }, HttpStatus.FORBIDDEN);
|
||||
}
|
||||
const is_match = await bcrypt.compare(room.password, room_db.hash);
|
||||
if (!is_match)
|
||||
{
|
||||
printCaller(`throw error: error: true, code: 'BAD_PASSWORD', message: 'you provided a bad password'`);
|
||||
throw new HttpException({ error: true, code: 'BAD_PASSWORD', message: `you provided a bad password` }, HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
// add password to chatroom
|
||||
room_db.allowed_users = room.allowed_users;
|
||||
room_db.protection = false;
|
||||
room_db.hash = "";
|
||||
room_db.messages.push({ name: "SERVER", message: message });
|
||||
await this.chatroomRepository.save(room_db);
|
||||
|
||||
printCaller("-- out ");
|
||||
}
|
||||
|
||||
async setPassword(username: string, message: string, room: roomDto, old_password?: string): Promise<void>
|
||||
{
|
||||
printCaller("-- in ");
|
||||
@@ -357,6 +396,7 @@ export class ChatService {
|
||||
console.log("throw error: error: true, code: 'NO_PASSWORD', message: 'this room has no password protection'");
|
||||
throw new HttpException({ error: true, code: 'NO_PASSWORD', message: `this room has no password protection` }, HttpStatus.FORBIDDEN);
|
||||
}
|
||||
console.log("---- room_db:", room_db);
|
||||
if (room_db.protection)
|
||||
{
|
||||
if (room.protection && !old_password)
|
||||
@@ -848,9 +888,14 @@ export class ChatService {
|
||||
}
|
||||
}
|
||||
|
||||
console.log("---- socket.username", socket.username);
|
||||
console.log("---- room_name", room_name);
|
||||
console.log("---- socket.room", socket.room);
|
||||
console.log("---- socket.username", socket.username);
|
||||
|
||||
let socket_name = `${socket.username}_not_emit`;
|
||||
await socket.to(socket.room).except(socket_name).emit('message', socket.username, message);
|
||||
await this.addMessageToRoom(room_name, socket.username, message);
|
||||
await socket.to(room_name).except(socket_name).emit('message', socket.username, message);
|
||||
|
||||
printCaller("-- out ");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user