fixed double socket initialisation and room settings not updating password

This commit is contained in:
simplonco
2023-01-18 02:22:09 +01:00
parent 441ff5ef14
commit 45a571b414
8 changed files with 111 additions and 9 deletions

View File

@@ -94,6 +94,26 @@ export class ChatController {
printCaller("- out "); 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(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@Get('allrooms') @Get('allrooms')
@@ -410,7 +430,7 @@ export class ChatController {
let message = `${req.user.username} removed a new password`; let message = `${req.user.username} removed a new password`;
room.allowed_users = []; room.allowed_users = [];
room.protection = false; 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 // inform other connected users
let socket: socketDto = this.chatService.getSocket(req.user.username); let socket: socketDto = this.chatService.getSocket(req.user.username);

View File

@@ -31,10 +31,23 @@ implements OnGatewayConnection, OnGatewayDisconnect
if (!socket.username) if (!socket.username)
return; return;
printCaller("--- socket.username:", socket.username);
// save socket and server
this.chatService.addSocket(socket.username, socket); this.chatService.addSocket(socket.username, socket);
socket_server.server = this.server; 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`; let not_emit: string = `${socket.username}_not_emit`;
socket.join(not_emit); socket.join(not_emit);
@@ -46,9 +59,23 @@ implements OnGatewayConnection, OnGatewayDisconnect
}); });
let current_room = await this.chatService.getCurrentRoomName(socket.username); let current_room = await this.chatService.getCurrentRoomName(socket.username);
socket.join(current_room); if (current_room)
socket.join(current_room);
} }
async handleDisconnect(socket: socketDto) { 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); this.chatService.removeSocket(socket.username);
} }

View File

@@ -329,6 +329,45 @@ export class ChatService {
printCaller("-- out "); 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> async setPassword(username: string, message: string, room: roomDto, old_password?: string): Promise<void>
{ {
printCaller("-- in "); 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'"); 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); 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_db.protection)
{ {
if (room.protection && !old_password) 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`; 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 this.addMessageToRoom(room_name, socket.username, message);
await socket.to(room_name).except(socket_name).emit('message', socket.username, message);
printCaller("-- out "); printCaller("-- out ");
} }

View File

@@ -3,8 +3,6 @@
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import GenerateUserDisplay from '../../pieces/GenerateUserDisplay.svelte'; import GenerateUserDisplay from '../../pieces/GenerateUserDisplay.svelte';
import { push } from 'svelte-spa-router'; import { push } from 'svelte-spa-router';
import Chat from '../../pieces/chat/Chat.svelte';
import { fetchUser } from "../../pieces/utils"; import { fetchUser } from "../../pieces/utils";
let user; let user;

View File

@@ -75,8 +75,6 @@
style = style_light; style = style_light;
} }
init_socket();
$: change_style($location); $: change_style($location);
$: { $: {
$location; $location;

View File

@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { layout, current_room, settings_user } from './Store_chat'; import { layout, current_room, settings_user } from './Store_chat';
import { get_room_users, leave_room, get_is_admin } from './Request_rooms'; import { get_room_users, leave_room, get_is_admin, get_current_room } from './Request_rooms';
import { User } from './Types_chat'; import { User } from './Types_chat';
import { to_print } from './Utils_chat'; import { to_print } from './Utils_chat';
import Button from './Element_button.svelte'; import Button from './Element_button.svelte';
@@ -13,6 +13,9 @@
let is_admin = false; let is_admin = false;
get_is_admin().then(response => is_admin = response); get_is_admin().then(response => is_admin = response);
get_current_room().then(response => current_room.set(response.room));
$: console.log("current_room infos:", $current_room);
to_print("current_room:", $current_room); to_print("current_room:", $current_room);
function user_profile(room_user: string) function user_profile(room_user: string)

View File

@@ -66,6 +66,16 @@ export async function change_room(room: Room): Promise<FetchResponse>
return response; return response;
} }
export async function get_current_room()
{
to_print("in validate_password");
let response: FetchResponse = await fetch_chat_request('currentroom', FetchMethod.GET);
to_print("response from get_current_room:", response);
return response;
}
export async function validate_password(room: Room) export async function validate_password(room: Room)
{ {
to_print("in validate_password"); to_print("in validate_password");

View File

@@ -8,6 +8,7 @@ const address = `http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}`
export async function init_socket() export async function init_socket()
{ {
to_print("in init_socket"); to_print("in init_socket");
//console.error("in init_socket");
const user = await fetchUser(); const user = await fetchUser();
if (!user) if (!user)
return; return;