wip protected, already solved error http handling for createroom
This commit is contained in:
@@ -111,8 +111,8 @@ 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: Your room name can not contains these characters : ${forbidden_chars}`);
|
console.log(`throw error: display: true, code: 'FORBIDDEN_CHARACTERS', message: 'Your room name can not contains these characters : ${forbidden_chars}'`);
|
||||||
throw new HttpException( `Your room name can not contains these characters : ${forbidden_chars}`, HttpStatus.UNPROCESSABLE_ENTITY);
|
throw new HttpException({ display: true, code: 'FORBIDDEN_CHARACTERS', message: `Your room name can not contains these characters : ${forbidden_chars}` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof room.protection === 'undefined')
|
if (typeof room.protection === 'undefined')
|
||||||
@@ -121,8 +121,8 @@ export class ChatController {
|
|||||||
{
|
{
|
||||||
if (!room.password || room.password.length === 0)
|
if (!room.password || room.password.length === 0)
|
||||||
{
|
{
|
||||||
console.log(`throw error: code: 'PASSWORD_BAD_FORMAT', message: 'your password is too short'`);
|
console.log(`throw error: display: true, code: 'PASSWORD_TOO_SHORT', message: 'your password is too short'`);
|
||||||
throw new HttpException({ code: 'PASSWORD_BAD_FORMAT', message: `your password is too short` }, HttpStatus.UNPROCESSABLE_ENTITY);
|
throw new HttpException({ display: true, code: 'PASSWORD_TOO_SHORT', message: `your password is too short` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
room.users = [req.user.username];
|
room.users = [req.user.username];
|
||||||
@@ -156,18 +156,18 @@ export class ChatController {
|
|||||||
const room_db = await this.chatService.getRoomByName(room.name, fields);
|
const room_db = await this.chatService.getRoomByName(room.name, fields);
|
||||||
if (room_db.type === 'direct')
|
if (room_db.type === 'direct')
|
||||||
{
|
{
|
||||||
console.log("throw error: cannot join a direct messages room");
|
console.log("throw error: display: true, code: 'JOIN_DIRECT_FORBIDDEN', message: 'cannot join a direct messages room'");
|
||||||
throw new HttpException( `cannot join a direct messages room`, HttpStatus.CONFLICT);
|
throw new HttpException({ display: true, code: 'JOIN_DIRECT_FORBIDDEN', message: `cannot join a direct messages room` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
if (room_db.type === 'private')
|
if (room_db.type === 'private')
|
||||||
{
|
{
|
||||||
console.log("throw error: cannot join a private room");
|
console.log("throw error: display: true, code: 'JOIN_PRIVATE_FORBIDDEN', message: 'cannot join a private room'");
|
||||||
throw new HttpException( `cannot join a private room`, HttpStatus.CONFLICT);
|
throw new HttpException({ display: true, code: 'JOIN_PRIVATE_FORBIDDEN', message: `cannot join a private room` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
if (room_db.users.includes(req.user.username))
|
if (room_db.users.includes(req.user.username))
|
||||||
{
|
{
|
||||||
console.log("throw error: your have already joined this room");
|
console.log("throw error: display: true, code: 'ALREADY_JOIN', message: 'your have already joined this room'");
|
||||||
throw new HttpException( `your have already joined this room`, HttpStatus.CONFLICT);
|
throw new HttpException({ display: true, code: 'ALREADY_JOIN', message: `your have already joined this room` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
room = await this.chatService.addUserToRoom(req.user.username, room.name);
|
room = await this.chatService.addUserToRoom(req.user.username, room.name);
|
||||||
}
|
}
|
||||||
@@ -188,14 +188,42 @@ export class ChatController {
|
|||||||
{
|
{
|
||||||
console.log("- in changeRoom controller");
|
console.log("- in changeRoom controller");
|
||||||
|
|
||||||
|
let fields = ["protection", "allowed_users"];
|
||||||
|
const room_db = await this.chatService.getRoomByName(room.name, fields);
|
||||||
|
if (room_db.protection === true)
|
||||||
|
{
|
||||||
|
if (!room_db.allowed_users.includes(req.user.username))
|
||||||
|
{
|
||||||
|
console.log("throw error: display: true, code: 'NEED_AUTHENTICATE', message: 'You didn't provide the password for this room'");
|
||||||
|
throw new HttpException({ display: true, code: 'NEED_AUTHENTICATE', message: `You didn't provide the password for this room` }, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.chatService.setCurrentRoom(req.user.username, room.name);
|
||||||
|
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||||
|
await this.chatService.socketChangeRoom(socket, room.name);
|
||||||
|
|
||||||
|
const ret_room = this.format_room(room);
|
||||||
|
res.status(HttpStatus.OK).json({ room: ret_room });
|
||||||
|
|
||||||
|
console.log("- out changeRoom controller");
|
||||||
|
}
|
||||||
|
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
@Post('password')
|
||||||
|
async setPassword(@Body() room: roomDto, @Req() req, @Res() res): Promise<void>
|
||||||
|
{
|
||||||
|
console.log("- in setPassword controller");
|
||||||
|
|
||||||
let fields = ["protection", "allowed_users"];
|
let fields = ["protection", "allowed_users"];
|
||||||
const room_db = await this.chatService.getRoomByName(room.name, fields);
|
const room_db = await this.chatService.getRoomByName(room.name, fields);
|
||||||
if (room_db.protection === true)
|
if (room_db.protection === true)
|
||||||
{
|
{
|
||||||
if (!room.password)
|
if (!room.password)
|
||||||
{
|
{
|
||||||
console.log("throw error: code: 'PASSWORD_MISSING', message: 'this room is protected, you need to provide a password'");
|
console.log("throw error: display: true, code: 'PASSWORD_MISSING', message: 'this room is protected, you need to provide a password'");
|
||||||
throw new HttpException({ code: 'PASSWORD_MISSING', message: `this room is protected, you need to provide a password` }, HttpStatus.BAD_REQUEST);
|
throw new HttpException({ display: true, code: 'PASSWORD_MISSING', message: `this room is protected, you need to provide a password` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
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);
|
||||||
@@ -208,7 +236,7 @@ export class ChatController {
|
|||||||
const ret_room = this.format_room(room);
|
const ret_room = this.format_room(room);
|
||||||
res.status(HttpStatus.OK).json({ room: ret_room });
|
res.status(HttpStatus.OK).json({ room: ret_room });
|
||||||
|
|
||||||
console.log("- out changeRoom controller");
|
console.log("- out setPassword controller");
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
|
|||||||
@@ -194,8 +194,8 @@ 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: code: 'BAD_PASSWORD', message: 'bad password'`);
|
console.log(`throw error: display: true, code: 'BAD_PASSWORD', message: 'bad password'`);
|
||||||
throw new HttpException({ code: 'BAD_PASSWORD', message: `bad password` }, HttpStatus.UNAUTHORIZED);
|
throw new HttpException({ display: true, code: 'BAD_PASSWORD', message: `bad password` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
room_db.allowed_users.push(username);
|
room_db.allowed_users.push(username);
|
||||||
@@ -215,8 +215,8 @@ export class ChatService {
|
|||||||
const find_room = await this.getRoomByName(room.name);
|
const find_room = await this.getRoomByName(room.name);
|
||||||
if (find_room)
|
if (find_room)
|
||||||
{
|
{
|
||||||
console.log("throw error: This room name already exist");
|
console.log("throw error: display: true, code: 'ROOM_CONFLICT', message: 'This room name already exist'");
|
||||||
throw new HttpException( `This room name already exist`, HttpStatus.CONFLICT);
|
throw new HttpException({ display: true, code: 'ROOM_CONFLICT', message: `This room name already exist` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
let hash;
|
let hash;
|
||||||
@@ -225,8 +225,8 @@ export class ChatService {
|
|||||||
console.log("in room protection hash");
|
console.log("in room protection hash");
|
||||||
if (room.type === 'direct')
|
if (room.type === 'direct')
|
||||||
{
|
{
|
||||||
console.log("throw error: code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'");
|
console.log("throw error: display: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'");
|
||||||
throw new HttpException({ code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room`}, HttpStatus.CONFLICT);
|
throw new HttpException({ display: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room`}, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
const saltOrRounds = 10;
|
const saltOrRounds = 10;
|
||||||
const password = room.password;
|
const password = room.password;
|
||||||
@@ -295,13 +295,13 @@ export class ChatService {
|
|||||||
const room = await this.getRoomByName(room_name);
|
const room = await this.getRoomByName(room_name);
|
||||||
if (!room.users.includes(username))
|
if (!room.users.includes(username))
|
||||||
{
|
{
|
||||||
console.log("throw error: your are not in this room");
|
console.log("throw error: display: true, code: 'USER_NOT_FOUND', message: 'your are not in this room'");
|
||||||
throw new HttpException(`your are not in this room`, HttpStatus.CONFLICT);
|
throw new HttpException({ display: true, code: 'USER_NOT_FOUND', message: `your are not in this room` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
if (room.type === "direct")
|
if (room.type === "direct")
|
||||||
{
|
{
|
||||||
console.log("throw error: code: 'LEAVING_DIRECT_FORBIDDEN', message: 'you cannot leave a direct messages conversation'");
|
console.log("throw error: display: true, code: 'LEAVE_DIRECY_FORBIDDEN', message: 'you cannot leave a direct messages conversation'");
|
||||||
throw new HttpException({ code: `LEAVING_DIRECT_FORBIDDEN`, message: `you cannot leave a direct messages conversation`, status: HttpStatus.CONFLICT }, HttpStatus.CONFLICT);
|
throw new HttpException({ display: true, code: 'LEAVE_DIRECY_FORBIDDEN', message: `you cannot leave a direct messages conversation` }, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete user from room
|
// delete user from room
|
||||||
|
|||||||
@@ -3,4 +3,15 @@ export interface Room
|
|||||||
name: string;
|
name: string;
|
||||||
type: "public" | "protected" | "private" | "direct" | "user";
|
type: "public" | "protected" | "private" | "direct" | "user";
|
||||||
users?: string[];
|
users?: string[];
|
||||||
|
client_name?: string;
|
||||||
|
protection?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FetchResponse
|
||||||
|
{
|
||||||
|
status: number;
|
||||||
|
code?: string;
|
||||||
|
display?: boolean;
|
||||||
|
message?: string;
|
||||||
|
room?: any;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import { msgs, layout, allowed_chars } from './Store_chat';
|
import { msgs, layout, allowed_chars } from './Store_chat';
|
||||||
import { change_room, create_room } from './Request_rooms';
|
import { change_room, create_room } from './Request_rooms';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
import type { FetchResponse } from './Interface_chat';
|
||||||
import Button from './Element_button.svelte';
|
import Button from './Element_button.svelte';
|
||||||
import Warning from './Element_warning.svelte';
|
import Warning from './Element_warning.svelte';
|
||||||
|
|
||||||
@@ -22,10 +23,8 @@
|
|||||||
let room_type: string;
|
let room_type: string;
|
||||||
let is_protected = false;
|
let is_protected = false;
|
||||||
let room_password: string;
|
let room_password: string;
|
||||||
let response = {
|
let response: FetchResponse;
|
||||||
status: 0,
|
let show_error = false;
|
||||||
message: "",
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleSubmit(evt)
|
async function handleSubmit(evt)
|
||||||
{
|
{
|
||||||
@@ -37,12 +36,14 @@
|
|||||||
let room = {
|
let room = {
|
||||||
name: room_name,
|
name: room_name,
|
||||||
type: room_type,
|
type: room_type,
|
||||||
|
protection: is_protected,
|
||||||
};
|
};
|
||||||
if (is_protected === true)
|
if (is_protected === true)
|
||||||
room.password = room_password;
|
room.password = room_password;
|
||||||
// send the new room
|
// send the new room
|
||||||
response = await create_room(room);
|
response = await create_room(room);
|
||||||
|
|
||||||
|
show_error = response.display;
|
||||||
// go to room
|
// go to room
|
||||||
if (response.status === 200)
|
if (response.status === 200)
|
||||||
await change_room(response.room);
|
await change_room(response.room);
|
||||||
@@ -70,7 +71,7 @@
|
|||||||
<!-- panel_create -->
|
<!-- panel_create -->
|
||||||
<div class="panel panel_create __border_top">
|
<div class="panel panel_create __border_top">
|
||||||
<form on:submit|preventDefault={handleSubmit}>
|
<form on:submit|preventDefault={handleSubmit}>
|
||||||
{#if response.status >= 300}
|
{#if show_error}
|
||||||
<Warning content={response.message}/>
|
<Warning content={response.message}/>
|
||||||
{/if}
|
{/if}
|
||||||
<!-- name: -->
|
<!-- name: -->
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
|
||||||
import { layout, current_room_name, current_room_type } from './Store_chat';
|
import { layout, current_room_name, current_room_type } from './Store_chat';
|
||||||
import { change_room } from './Request_rooms';
|
import { change_room, send_password } from './Request_rooms';
|
||||||
|
import type { FetchResponse } from './Interface_chat';
|
||||||
import Button from './Element_button.svelte';
|
import Button from './Element_button.svelte';
|
||||||
import Warning from './Element_warning.svelte';
|
import Warning from './Element_warning.svelte';
|
||||||
|
|
||||||
export let back = "";
|
export let back = "";
|
||||||
|
|
||||||
let room_password: string;
|
let room_password: string;
|
||||||
let response = {
|
let response: FetchResponse;
|
||||||
status: 0,
|
let show_error = false;
|
||||||
message: "",
|
|
||||||
};
|
|
||||||
|
|
||||||
async function handleSubmit(evt)
|
async function handleSubmit(evt)
|
||||||
{
|
{
|
||||||
@@ -26,8 +25,13 @@
|
|||||||
password: room_password,
|
password: room_password,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// send password
|
||||||
|
response = await send_password(room);
|
||||||
|
|
||||||
|
show_error = response.display;
|
||||||
// go to room
|
// go to room
|
||||||
response = await change_room(room);
|
if (response.status === 200)
|
||||||
|
await change_room(response.room);
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
@@ -59,7 +63,7 @@
|
|||||||
</form>
|
</form>
|
||||||
|
|
||||||
<form on:submit|preventDefault={handleSubmit}>
|
<form on:submit|preventDefault={handleSubmit}>
|
||||||
{#if response.status >= 300}
|
{#if show_error}
|
||||||
<Warning content={response.message}/>
|
<Warning content={response.message}/>
|
||||||
{/if}
|
{/if}
|
||||||
<label for="chat_pswd"><p>enter password :</p></label>
|
<label for="chat_pswd"><p>enter password :</p></label>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { msgs, user, layout, socket, current_room_name, current_room_type } from './Store_chat';
|
import { msgs, user, layout, socket, current_room_name, current_room_type } from './Store_chat';
|
||||||
import type { Room } from './Interface_chat';
|
import type { Room, FetchResponse } from './Interface_chat';
|
||||||
|
import { set_client_name_on_room, fill_fetch_response } from './Request_utils';
|
||||||
|
|
||||||
export async function get_room_messages()
|
export async function get_room_messages()
|
||||||
{
|
{
|
||||||
@@ -24,23 +25,31 @@ export async function create_room(room: Room)
|
|||||||
{
|
{
|
||||||
console.log("in create_room");
|
console.log("in create_room");
|
||||||
|
|
||||||
|
let response: FetchResponse = { status: 0 };
|
||||||
|
|
||||||
// send the new room
|
// send the new room
|
||||||
const response = await fetch('/api/v2/chat/create', {
|
try {
|
||||||
|
const resp = await fetch('/api/v2/chat/create', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(room),
|
body: JSON.stringify(room),
|
||||||
});
|
});
|
||||||
|
console.log("resp.status:");
|
||||||
|
console.log(resp.status);
|
||||||
|
response.status = resp.status;
|
||||||
|
if (!resp.ok)
|
||||||
|
throw new Error(resp.statusText);
|
||||||
|
|
||||||
// get response status and message
|
// get response message
|
||||||
let response_status = response.status;
|
let data = await resp.json();
|
||||||
let data = await response.json();
|
fill_fetch_response(response, data);
|
||||||
let response_message = "";
|
}
|
||||||
|
catch (error)
|
||||||
|
{
|
||||||
|
console.error('Error', error);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return response;
|
||||||
status: response_status,
|
|
||||||
message: data.message,
|
|
||||||
room: data.room,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function join_room(room: Room)
|
export async function join_room(room: Room)
|
||||||
@@ -57,21 +66,6 @@ export async function join_room(room: Room)
|
|||||||
return data.room;
|
return data.room;
|
||||||
}
|
}
|
||||||
|
|
||||||
function set_client_name_on_room(room: Room)
|
|
||||||
{
|
|
||||||
console.log("in set_client_name_on_room, for room:", room);
|
|
||||||
if (room.type === 'direct')
|
|
||||||
{
|
|
||||||
console.log("in direct room");
|
|
||||||
room.client_name = room.users[0];
|
|
||||||
if (room.client_name === user.username)
|
|
||||||
room.client_name = room.users[1];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
room.client_name = room.name;
|
|
||||||
return room;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function change_room(room: Room)
|
export async function change_room(room: Room)
|
||||||
{
|
{
|
||||||
console.log("in change_room");
|
console.log("in change_room");
|
||||||
@@ -92,6 +86,37 @@ export async function change_room(room: Room)
|
|||||||
layout.set("room");
|
layout.set("room");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function send_password(room: Room)
|
||||||
|
{
|
||||||
|
console.log("in create_room");
|
||||||
|
|
||||||
|
let response: FetchResponse = { status: 0 };
|
||||||
|
|
||||||
|
// send the new room
|
||||||
|
try {
|
||||||
|
const resp = await fetch('/api/v2/chat/create', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(room),
|
||||||
|
});
|
||||||
|
console.log("resp.status:");
|
||||||
|
console.log(resp.status);
|
||||||
|
response.status = resp.status;
|
||||||
|
if (!resp.ok)
|
||||||
|
throw new Error(resp.statusText);
|
||||||
|
|
||||||
|
// get response message
|
||||||
|
let data = await resp.json();
|
||||||
|
fill_fetch_response(response, data);
|
||||||
|
}
|
||||||
|
catch (error)
|
||||||
|
{
|
||||||
|
console.error('Error', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
export async function invite_user(user_name: string)
|
export async function invite_user(user_name: string)
|
||||||
{
|
{
|
||||||
console.log("in invite_user");
|
console.log("in invite_user");
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { user } from './Store_chat';
|
||||||
|
import type { Room, FetchResponse } from './Interface_chat';
|
||||||
|
|
||||||
|
export function set_client_name_on_room(room: Room)
|
||||||
|
{
|
||||||
|
console.log("in set_client_name_on_room, for room:", room);
|
||||||
|
if (room.type === 'direct')
|
||||||
|
{
|
||||||
|
console.log("in direct room");
|
||||||
|
room.client_name = room.users[0];
|
||||||
|
if (room.client_name === user.username)
|
||||||
|
room.client_name = room.users[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
room.client_name = room.name;
|
||||||
|
return room;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fill_fetch_response(response: FetchResponse, data: any)
|
||||||
|
{
|
||||||
|
if (data.display)
|
||||||
|
response.display = data.display;
|
||||||
|
if (data.code)
|
||||||
|
response.code = data.code;
|
||||||
|
if (data.message)
|
||||||
|
response.message = data.message;
|
||||||
|
if (data.room)
|
||||||
|
response.room = data.room;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user