protected room working at least

This commit is contained in:
simplonco
2023-01-14 19:17:46 +01:00
parent efc836eac0
commit 81b67a54e6
9 changed files with 34 additions and 8 deletions

View File

@@ -30,6 +30,8 @@ export class ChatController {
new_room.owner = room.owner; new_room.owner = room.owner;
if (room.users) if (room.users)
new_room.users = room.users; new_room.users = room.users;
if (room.allowed)
new_room.allowed = room.allowed;
return new_room; return new_room;
} }
@@ -48,9 +50,19 @@ export class ChatController {
{ {
printCaller("- in "); printCaller("- in ");
let fields = ["name", "type", "users", "protection"]; let fields = ["name", "type", "users", "protection", "allowed_users"];
const rooms = await this.chatService.getMyRooms(req.user.username, fields); const rooms = await this.chatService.getMyRooms(req.user.username, fields);
rooms.forEach(room => {
if (room.protection)
{
if (room.allowed_users.includes(req.user.username))
room.allowed = true;
else
room.allowed = false;
}
});
const ret_rooms = rooms.map(room => this.format_room(room)); const ret_rooms = rooms.map(room => this.format_room(room));
res.status(HttpStatus.OK).json({ rooms: ret_rooms }); res.status(HttpStatus.OK).json({ rooms: ret_rooms });
printCaller("- out "); printCaller("- out ");
@@ -107,7 +119,7 @@ export class ChatController {
if (test_regex.test(room.name) === false) if (test_regex.test(room.name) === false)
{ {
let forbidden_chars = room.name.replace(new RegExp(regex_base, "g"), ""); let forbidden_chars = room.name.replace(new RegExp(regex_base, "g"), "");
console.log(`throw error: error: true, code: 'FORBIDDEN_CHARACTERS', message: 'Your room name can not contains these characters : ${forbidden_chars}'`); printCaller(`throw error: error: true, code: 'FORBIDDEN_CHARACTERS', message: 'Your room name can not contains these characters : ${forbidden_chars}'`);
throw new HttpException({ error: true, code: 'FORBIDDEN_CHARACTERS', message: `Your room name can not contains these characters : ${forbidden_chars}` }, HttpStatus.OK); throw new HttpException({ error: true, code: 'FORBIDDEN_CHARACTERS', message: `Your room name can not contains these characters : ${forbidden_chars}` }, HttpStatus.OK);
} }
@@ -117,7 +129,7 @@ export class ChatController {
{ {
if (!room.password || room.password.length === 0) if (!room.password || room.password.length === 0)
{ {
console.log(`throw error: error: true, code: 'PASSWORD_TOO_SHORT', message: 'your password is too short'`); printCaller(`throw error: error: true, code: 'PASSWORD_TOO_SHORT', message: 'your password is too short'`);
throw new HttpException({ error: true, code: 'PASSWORD_TOO_SHORT', message: `your password is too short` }, HttpStatus.OK); throw new HttpException({ error: true, code: 'PASSWORD_TOO_SHORT', message: `your password is too short` }, HttpStatus.OK);
} }
} }
@@ -221,6 +233,9 @@ export class ChatController {
if (!room_db.allowed_users.includes(req.user.username)) if (!room_db.allowed_users.includes(req.user.username))
await this.chatService.setPasswordValidation(req.user.username, room); await this.chatService.setPasswordValidation(req.user.username, room);
} }
const ret_room = this.format_room(room);
res.status(HttpStatus.OK).json({ room: ret_room });
printCaller("- out "); printCaller("- out ");
} }

View File

@@ -197,7 +197,7 @@ export class ChatService {
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)
{ {
console.log(`throw error: error: true, code: 'BAD_PASSWORD', message: 'bad password'`); printCaller(`throw error: error: true, code: 'BAD_PASSWORD', message: 'bad password'`);
throw new HttpException({ error: true, code: 'BAD_PASSWORD', message: `bad password` }, HttpStatus.BAD_REQUEST); throw new HttpException({ error: true, code: 'BAD_PASSWORD', message: `bad password` }, HttpStatus.BAD_REQUEST);
} }

View File

@@ -8,6 +8,6 @@ export function printCaller(prefix: string = "") {
Error.captureStackTrace(e); Error.captureStackTrace(e);
let stack = e.stack.split('\n'); let stack = e.stack.split('\n');
let caller = stack[2].trim(); let caller = stack[2].trim();
console.log(prefix + caller); console.log(prefix + ' ' + caller);
} }
} }

View File

@@ -19,6 +19,10 @@ export class roomDto
@IsBoolean() @IsBoolean()
protection: boolean; protection: boolean;
@IsBoolean()
@IsOptional()
allowed?: boolean;
@IsString() @IsString()
@IsOptional() @IsOptional()
password?: string; password?: string;

View File

@@ -25,6 +25,11 @@ export class Chatroom
@IsBoolean() @IsBoolean()
protection: boolean = false; protection: boolean = false;
@Column({ nullable: true })
@IsBoolean()
@IsOptional()
allowed?: boolean;
@Column({ nullable: true }) @Column({ nullable: true })
@IsString() @IsString()
@IsOptional() @IsOptional()

View File

@@ -44,7 +44,7 @@
response = await create_room(room); response = await create_room(room);
// go to room // go to room
if (response.error) if (response.status >= 300)
show_error = response.error; show_error = response.error;
else else
await change_room(response.room); await change_room(response.room);

View File

@@ -12,7 +12,8 @@
{ {
console.log("inside go_to_room"); console.log("inside go_to_room");
if (room.protection) console.log("room:", room);
if (room.protection && !room.allowed)
{ {
await current_room.set(room); await current_room.set(room);
layout.set("protected"); layout.set("protected");

View File

@@ -32,7 +32,7 @@
response = await send_password(room); response = await send_password(room);
// go to room // go to room
if (response.error) if (response.status >= 300)
show_error = response.error; show_error = response.error;
else else
await change_room(response.room); await change_room(response.room);

View File

@@ -5,6 +5,7 @@ export interface Room
users?: string[]; users?: string[];
client_name?: string; client_name?: string;
protection: boolean; protection: boolean;
allowed?: boolean;
} }
export interface FetchResponse export interface FetchResponse