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;
if (room.users)
new_room.users = room.users;
if (room.allowed)
new_room.allowed = room.allowed;
return new_room;
}
@@ -48,9 +50,19 @@ export class ChatController {
{
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);
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));
res.status(HttpStatus.OK).json({ rooms: ret_rooms });
printCaller("- out ");
@@ -107,7 +119,7 @@ export class ChatController {
if (test_regex.test(room.name) === false)
{
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);
}
@@ -117,7 +129,7 @@ export class ChatController {
{
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);
}
}
@@ -221,6 +233,9 @@ export class ChatController {
if (!room_db.allowed_users.includes(req.user.username))
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 ");
}

View File

@@ -197,7 +197,7 @@ export class ChatService {
const is_match = await bcrypt.compare(room.password, room_db.hash);
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);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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