fixed a mutitudes of errors in chat

This commit is contained in:
simplonco
2023-01-18 03:54:39 +01:00
parent 532576cb08
commit 035eb2a46a
7 changed files with 137 additions and 48 deletions

View File

@@ -97,7 +97,7 @@ export class ChatController {
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@Get('currentroom') @Get('currentroom')
async getCurrentRooms(@Req() req, @Res() res): Promise<void> async getCurrentRoom(@Req() req, @Res() res): Promise<void>
{ {
printCaller("- in "); printCaller("- in ");
@@ -572,6 +572,42 @@ export class ChatController {
printCaller("- out "); printCaller("- out ");
} }
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('userinfos')
async getUserInfos(@Body('username') username: string, @Req() req, @Res() res): Promise<void>
{
printCaller("- in ");
const room_name = await this.chatService.getCurrentRoomName(username);
if (!room_name)
{
console.log("throw error: error: true, code: 'CURRENT_ROOM_NAME_NOT_FOUND', message: 'current_room_name_not_found'");
throw new HttpException({ error: true, code: 'CURRENT_ROOM_NAME_NOT_FOUND', message: `current_room_name_not_found` }, HttpStatus.UNAUTHORIZED);
}
const current_room = await this.chatService.getRoomByName(room_name);
if (!current_room)
{
console.log("throw error: error: true, code: 'CURRENT_ROOM_NOT_FOUND', message: 'current_room_not_found'");
throw new HttpException({ error: true, code: 'CURRENT_ROOM_NOT_FOUND', message: `current_room_not_found` }, HttpStatus.UNAUTHORIZED);
}
const blocked = await this.chatService.getListBlockUser(req.user.username);
const mute = await this.chatService.getUserMute(req.user.username, username);
let user: any = { name: username, ismute: false, mute_date: null };
user.isadmin = current_room.admins.includes(username);
if (blocked)
user.isblocked = blocked.includes(username);
if (mute && mute.name === username)
{
user.ismute = true;
user.mute_date = mute.date;
}
res.status(HttpStatus.OK).json({ user: user });
printCaller("- out ");
}
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@Post('setmute') @Post('setmute')
@@ -583,6 +619,7 @@ export class ChatController {
await this.chatService.addMute(req.user.username, room_name, mute); await this.chatService.addMute(req.user.username, room_name, mute);
// inform other connected users // inform other connected users
console.log("mute object:", mute);
let message = `${req.user.username} has muted ${mute.name} untill ${time}`; let message = `${req.user.username} has muted ${mute.name} untill ${time}`;
let socket: socketDto = this.chatService.getSocket(req.user.username); let socket: socketDto = this.chatService.getSocket(req.user.username);
let server = this.chatGateway.server; let server = this.chatGateway.server;
@@ -598,7 +635,7 @@ export class ChatController {
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@Post('ismute') @Post('ismute')
async isuserMute(@Body('username') username: string, @Req() req, @Res() res): Promise<void> async isUserMute(@Body('username') username: string, @Req() req, @Res() res): Promise<void>
{ {
printCaller("- in "); printCaller("- in ");
@@ -618,6 +655,11 @@ export class ChatController {
printCaller("- in "); printCaller("- in ");
const room_name = await this.chatService.getCurrentRoomName(req.user.username); const room_name = await this.chatService.getCurrentRoomName(req.user.username);
if (!room_name)
{
console.log("throw error: error: true, code: 'CURRENT_ROOM_NOT_FOUND', message: 'current_room_not_found'");
throw new HttpException({ error: true, code: 'CURRENT_ROOM_NOT_FOUND', message: `current_room_not_found` }, HttpStatus.UNAUTHORIZED);
}
const fields = ["admins"]; const fields = ["admins"];
const room_db = await this.chatService.getRoomByName(room_name, fields); const room_db = await this.chatService.getRoomByName(room_name, fields);

View File

@@ -285,6 +285,18 @@ export class ChatService {
} }
} }
async getUserMute(current_username: string, username: string, ): Promise<muteDto>
{
printCaller("- in ");
const room_name = await this.getCurrentRoomName(current_username);
const current_room = await this.getRoomByName(room_name);
let mute = current_room.mutes.find(mute => mute.name === username);
printCaller("- out ");
return mute;
}
/* SETTERS ************************************************ /* SETTERS ************************************************
*/ */
@@ -447,8 +459,8 @@ export class ChatService {
const room_db = await this.getRoomByName(room_name); const room_db = await this.getRoomByName(room_name);
if (!room_db) if (!room_db)
{ {
printCallerError(`ERROR in chat: room ${room_name} not found`); printCaller(`throw error: error: true, code: 'ROOM_NOT_FOUND', message: 'room ${room_name} not found'`);
return; throw new HttpException({ error: true, code: 'ROOM_NOT_FOUND', message: `room ${room_name} not found` }, HttpStatus.UNAUTHORIZED);
} }
if (room_db.type === "direct") if (room_db.type === "direct")
{ {
@@ -460,6 +472,11 @@ export class ChatService {
printCaller(`throw error: error: true, code: 'NOT_ADMIN', message: 'you cannot set someone else as admin, since you are not admin yourself'`); printCaller(`throw error: error: true, code: 'NOT_ADMIN', message: 'you cannot set someone else as admin, since you are not admin yourself'`);
throw new HttpException({ error: true, code: 'NOT_ADMIN', message: `you cannot set someone else as admin, since you are not admin yourself` }, HttpStatus.UNAUTHORIZED); throw new HttpException({ error: true, code: 'NOT_ADMIN', message: `you cannot set someone else as admin, since you are not admin yourself` }, HttpStatus.UNAUTHORIZED);
} }
if (room_db.admins.includes(user_username))
{
printCaller(`throw error: error: true, code: 'ALREADY_ADMIN', message: 'this user is already an admin'`);
throw new HttpException({ error: true, code: 'ALREADY_ADMIN', message: `this user is already an admin` }, HttpStatus.UNAUTHORIZED);
}
room_db.admins.push(user_username); room_db.admins.push(user_username);
await this.chatroomRepository.save(room_db); await this.chatroomRepository.save(room_db);
@@ -653,8 +670,8 @@ export class ChatService {
const room_db = await this.getRoomByName(room_name); const room_db = await this.getRoomByName(room_name);
if(!room_db) if(!room_db)
{ {
printCallerError(`ERROR in chat: room not found for ${username}`); console.log("throw error: error: true, code: 'ROOM_NOT_FOUND', message: 'room not found for ${username}'");
return; throw new HttpException({ error: true, code: 'ROOM_NOT_FOUND', message: `room not found for ${username}` }, HttpStatus.FORBIDDEN);
} }
if (!room_db.admins.includes(username)) if (!room_db.admins.includes(username))
@@ -678,6 +695,11 @@ export class ChatService {
}); });
if (!already_here) if (!already_here)
room_db.mutes.push(mute); room_db.mutes.push(mute);
else
{
console.log("throw error: error: true, code: 'ALREADY_MUTE', message: 'this user is already mute for this room'");
throw new HttpException({ error: true, code: 'ALREADY_MUTE', message: `this user is already mute for this room` }, HttpStatus.FORBIDDEN);
}
} }
await this.chatroomRepository.save(room_db); await this.chatroomRepository.save(room_db);
@@ -904,9 +926,17 @@ export class ChatService {
{ {
printCaller("-- in "); printCaller("-- in ");
if (socket)
{
socket.leave(socket.room); socket.leave(socket.room);
socket.join(room_name); socket.join(room_name);
socket.room = room_name; socket.room = room_name;
}
else
{
printCaller(`throw error: error: true, code: 'SOCKET_UNDEFINED', message: 'socket_undefined'`);
throw new HttpException({ error: true, code: 'SOCKET_UNDEFINED', message: `socket_undefined` }, HttpStatus.BAD_REQUEST);
}
printCaller("-- out "); printCaller("-- out ");
} }

View File

@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { layout, settings_user } from './Store_chat'; import { layout, settings_user } from './Store_chat';
import { set_mute, get_is_mute, get_unmute } from './Request_rooms'; import { set_mute, get_unmute } from './Request_rooms';
import Button from './Element_button.svelte'; import Button from './Element_button.svelte';
import Warning from './Element_warning.svelte'; import Warning from './Element_warning.svelte';
@@ -9,20 +9,12 @@
let response: FetchResponse; let response: FetchResponse;
let show_error = false; let show_error = false;
let is_mute = false;
let mute_date: date;
let date_string: string; let date_string: string;
get_is_mute($settings_user).then(response => if ($settings_user.mute_date)
{ date_string = stringify_date(new Date($settings_user.mute_date));
if (response && response.name)
is_mute = true;
if (response && response.date)
mute_date = response.date;
if (mute_date)
date_string = stringify_date(new Date(mute_date));
else else
date_string = "eternity"; date_string = "eternity";
});
let is_forever; let is_forever;
let minutes: number = 0; let minutes: number = 0;
@@ -41,11 +33,6 @@
if (!formIsValid) if (!formIsValid)
return; return;
console.log("is_forever:", is_forever);
console.log("minutes:", minutes);
console.log("hours:", hours);
console.log("days:", days);
let date_limit: Date; let date_limit: Date;
let time: string; let time: string;
@@ -54,24 +41,23 @@
else else
{ {
let duration = minutes * (1000 * 60) + hours * (1000 * 60 * 60) + days * (1000 * 60 * 60 * 24); let duration = minutes * (1000 * 60) + hours * (1000 * 60 * 60) + days * (1000 * 60 * 60 * 24);
console.log("duration:", duration);
let date_start = new Date(); let date_start = new Date();
date_limit = new Date(date_start.getTime() + duration); date_limit = new Date(date_start.getTime() + duration);
time = stringify_date(date_limit); time = stringify_date(date_limit);
console.log("time:", time);
} }
response = await set_mute(date_limit, $settings_user, time); response = await set_mute(date_limit, $settings_user.name, time);
// print error // print error
if (response.status >= 300 || response.error) if (response.status >= 300 || response.error)
show_error = response.error; show_error = response.error;
else
layout.set("room"); layout.set("room");
} }
async function unmute() async function unmute()
{ {
get_unmute($settings_user); get_unmute($settings_user.name);
layout.set("room"); layout.set("room");
} }
@@ -86,7 +72,7 @@
<!-- user --> <!-- user -->
<Button my_class="user deactivate"> <Button my_class="user deactivate">
{$settings_user} {$settings_user.name}
</Button> </Button>
<!-- close --> <!-- close -->
@@ -99,7 +85,7 @@
{#if show_error} {#if show_error}
<Warning content={response.message}/> <Warning content={response.message}/>
{/if} {/if}
{#if is_mute === true } {#if $settings_user.ismute }
<p class="__center">this user is mute untill {date_string}</p> <p class="__center">this user is mute untill {date_string}</p>
<Button on:click={unmute}> <Button on:click={unmute}>
un-mute un-mute

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, get_current_room } from './Request_rooms'; import { get_room_users, leave_room, get_is_admin, set_current_room, set_settings_user } 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,7 +13,7 @@
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)); set_current_room();
$: console.log("current_room infos:", $current_room); $: console.log("current_room infos:", $current_room);
to_print("current_room:", $current_room); to_print("current_room:", $current_room);
@@ -22,6 +22,7 @@
{ {
to_print("in user_profile"); to_print("in user_profile");
settings_user.set(room_user); settings_user.set(room_user);
set_settings_user(room_user.name);
layout.set("user"); layout.set("user");
} }

View File

@@ -17,6 +17,8 @@
let response: FetchResponse; let response: FetchResponse;
let show_error = false; let show_error = false;
$: console.log("settings_user infos:", $settings_user);
function game_invitation() function game_invitation()
{ {
to_print("in game_invitation"); to_print("in game_invitation");
@@ -53,6 +55,7 @@
//show errors //show errors
if (response.status >= 300 || response.error) if (response.status >= 300 || response.error)
show_error = response.error; show_error = response.error;
else
layout.set("room"); layout.set("room");
} }
@@ -105,13 +108,21 @@
{/if} {/if}
{#if is_admin && back === "room_set" && $current_room.type !== "direct"} {#if is_admin && back === "room_set" && $current_room.type !== "direct"}
{#if !$settings_user.isadmin}
<Button on:click={make_user_admin}> <Button on:click={make_user_admin}>
make admin make admin
</Button> </Button>
{/if}
{#if $settings_user.ismute}
<Button new_layout="mute">
unmute
</Button>
{:else}
<Button new_layout="mute"> <Button new_layout="mute">
mute mute
</Button> </Button>
{/if} {/if}
{/if}
</div> </div>

View File

@@ -1,4 +1,4 @@
import { msgs, user, layout, socket, current_room } from './Store_chat'; import { msgs, user, layout, socket, current_room, settings_user } from './Store_chat';
import type { Room, FetchResponse } from './Types_chat'; import type { Room, FetchResponse } from './Types_chat';
import { FetchMethod, Mute, User } from './Types_chat'; import { FetchMethod, Mute, User } from './Types_chat';
import { to_print } from './Utils_chat'; import { to_print } from './Utils_chat';
@@ -66,14 +66,30 @@ export async function change_room(room: Room): Promise<FetchResponse>
return response; return response;
} }
export async function get_current_room() export async function set_current_room()
{ {
to_print("in validate_password"); to_print("in set_current_room");
let response: FetchResponse = await fetch_chat_request('currentroom', FetchMethod.GET); let response: FetchResponse = await fetch_chat_request('currentroom', FetchMethod.GET);
to_print("response from get_current_room:", response); to_print("response from get_current_room:", response);
return response; if (response && response.room)
{
set_client_name_on_room(response.room);
current_room.set(response.room);
}
}
export async function set_settings_user(username: string)
{
to_print("in set_settings_user");
to_print("username for set_settings_user:", username);
let response: FetchResponse = await fetch_chat_request('userinfos', FetchMethod.POST, {username: username});
to_print("response from set_settings_user:", response);
if (response && response.user)
settings_user.set(response.user);
} }
export async function validate_password(room: Room) export async function validate_password(room: Room)

View File

@@ -25,6 +25,8 @@ export interface User
name: string; name: string;
isadmin: boolean; isadmin: boolean;
isblocked: boolean; isblocked: boolean;
ismute: boolean;
mute_date: Date;
} }
export interface FetchResponse export interface FetchResponse
@@ -34,6 +36,7 @@ export interface FetchResponse
code?: string; code?: string;
message?: string; message?: string;
messages?: Message[]; messages?: Message[];
user?: User;
users?: User[]; users?: User[];
room?: Room; room?: Room;
rooms?: Room[]; rooms?: Room[];