fixed messages when leaving and changing name

This commit is contained in:
simplonco
2023-01-17 22:22:18 +01:00
parent 4a075c452b
commit 441ff5ef14
4 changed files with 48 additions and 9 deletions

View File

@@ -52,7 +52,17 @@ export class ChatController {
let fields = ["name", "type", "users", "protection", "allowed_users"]; 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);
if (!rooms)
{
printCallerError(`controllerror: true, code: 'ROOMS_NOT_FOUND', message: 'rooms not found for ${req.user.username}'`);
throw new HttpException({ error: true, code: 'ROOMS_NOT_FOUND', message: `rooms not found for ${req.user.username}` }, HttpStatus.UNPROCESSABLE_ENTITY);
}
const blocked = await this.chatService.getListBlockUser(req.user.username); const blocked = await this.chatService.getListBlockUser(req.user.username);
if (!blocked)
{
printCallerError(`controllerror: true, code: 'USERS_NOT_FOUND', message: 'blocked users not found for ${req.user.username}'`);
throw new HttpException({ error: true, code: 'USERS_NOT_FOUND', message: `blocked users not found for ${req.user.username}` }, HttpStatus.UNPROCESSABLE_ENTITY);
}
let filtered_rooms = rooms.filter(room => let filtered_rooms = rooms.filter(room =>
{ {
@@ -284,6 +294,12 @@ export class ChatController {
let fields = ["protection", "allowed_users", "type", "users"]; let fields = ["protection", "allowed_users", "type", "users"];
const room_db = await this.chatService.getRoomByName(room.name, fields); const room_db = await this.chatService.getRoomByName(room.name, fields);
if (!room_db.users.includes(req.user.username))
{
console.log("throw error: error: true, code: 'NEED_JOIN', message: 'you didn't join this room'");
throw new HttpException({ error: true, code: 'NEED_JOIN', message: `you didn't join this room` }, HttpStatus.UNAUTHORIZED);
}
if (room_db.protection === true) if (room_db.protection === true)
{ {
if (!room_db.allowed_users.includes(req.user.username)) if (!room_db.allowed_users.includes(req.user.username))
@@ -445,10 +461,10 @@ export class ChatController {
// leaving message // leaving message
let socket: socketDto = this.chatService.getSocket(req.user.username); let socket: socketDto = this.chatService.getSocket(req.user.username);
await messages.forEach(async (message) => await messages.forEach((message) =>
{ {
await this.chatService.addMessageToRoom(room_name, "SERVER", message); this.chatService.addMessageToRoom(room_name, "SERVER", message);
await socket.to(socket.room).emit('message', "SERVER", message); socket.to(socket.room).emit('message', "SERVER", message);
}); });
await socket.leave(socket.room); await socket.leave(socket.room);

View File

@@ -65,6 +65,9 @@ export class ChatService {
async getMyRooms(username: string, fieldsToReturn: string[] = null): Promise<Chatroom[]> async getMyRooms(username: string, fieldsToReturn: string[] = null): Promise<Chatroom[]>
{ {
printCaller("-- in "); printCaller("-- in ");
console.log("---- username:", username)
console.log("---- all rooms:", await this.getAllRooms());
//await this.sleep(1000); //await this.sleep(1000);
let rooms: Chatroom[]; let rooms: Chatroom[];
@@ -648,6 +651,8 @@ export class ChatService {
async removeUserFromRoom(username: string, room_name: string): Promise<string[]> async removeUserFromRoom(username: string, room_name: string): Promise<string[]>
{ {
printCaller("-- in "); printCaller("-- in ");
console.log("---- username:", username);
console.log("---- room_name:", room_name);
let messages = [`${username} left the room`]; let messages = [`${username} left the room`];
@@ -687,7 +692,10 @@ export class ChatService {
else else
room.owner = ""; room.owner = "";
} }
console.log("---- room:", room);
await this.chatroomRepository.save(room); await this.chatroomRepository.save(room);
console.log("---- all rooms:", await this.getAllRooms());
printCaller("-- out "); printCaller("-- out ");
return messages; return messages;
@@ -940,7 +948,6 @@ export class ChatService {
}); });
let socket = this.getSocket(old_name); let socket = this.getSocket(old_name);
printCaller("----------- socket", socket);
if (socket) if (socket)
{ {
this.removeSocket(old_name); this.removeSocket(old_name);

View File

@@ -1,13 +1,18 @@
<script> <script lang="ts">
import { layout, msgs, user, current_room } from './Store_chat'; import { layout, msgs, user, current_room } from './Store_chat';
import { change_room, get_room_messages, get_my_rooms } from './Request_rooms'; import { change_room, get_room_messages, get_my_rooms } from './Request_rooms';
import { to_print } from './Utils_chat'; import { to_print } from './Utils_chat';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import Button from './Element_button.svelte'; import Button from './Element_button.svelte';
import type { FetchResponse } from './Types_chat';
import Warning from './Element_warning.svelte';
let rooms = get_my_rooms(); let rooms = get_my_rooms();
let response: FetchResponse;
let show_error = false;
// go to clicked room // go to clicked room
async function go_to_room(room) async function go_to_room(room)
{ {
@@ -21,8 +26,11 @@
} }
else else
{ {
await change_room(room); response = await change_room(room);
await get_room_messages();
// print error message
if (response.status >= 300 || response.error)
show_error = response.error;
} }
} }
@@ -47,6 +55,9 @@
<!-- panel home --> <!-- panel home -->
<div class="panel panel_home __border_top"> <div class="panel panel_home __border_top">
{#if show_error}
<Warning content={response.message}/>
{/if}
<p class="title">list of your rooms :</p> <p class="title">list of your rooms :</p>
<div class="room_list"> <div class="room_list">
<div class="__show_if_only_child"> <div class="__show_if_only_child">

View File

@@ -45,13 +45,16 @@ export async function join_room(room: Room)
return response.room; return response.room;
} }
export async function change_room(room: Room) export async function change_room(room: Room): Promise<FetchResponse>
{ {
to_print("in change_room"); to_print("in change_room");
to_print("room sent to change:", room); to_print("room sent to change:", room);
let response: FetchResponse = await fetch_chat_request('change', FetchMethod.POST, room); let response: FetchResponse = await fetch_chat_request('change', FetchMethod.POST, room);
to_print("room returned from change:", response.room); to_print("room returned from change:", response);
if (response.status >= 300 || response.error)
return response;
await get_room_messages(); await get_room_messages();
@@ -59,6 +62,8 @@ export async function change_room(room: Room)
current_room.set(room); current_room.set(room);
layout.set("room"); layout.set("room");
return response;
} }
export async function validate_password(room: Room) export async function validate_password(room: Room)