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"];
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);
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 =>
{
@@ -284,6 +294,12 @@ export class ChatController {
let fields = ["protection", "allowed_users", "type", "users"];
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.allowed_users.includes(req.user.username))
@@ -445,10 +461,10 @@ export class ChatController {
// leaving message
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);
await socket.to(socket.room).emit('message', "SERVER", message);
this.chatService.addMessageToRoom(room_name, "SERVER", message);
socket.to(socket.room).emit('message', "SERVER", message);
});
await socket.leave(socket.room);

View File

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

View File

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

View File

@@ -45,13 +45,16 @@ export async function join_room(room: 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("room sent to change:", 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();
@@ -59,6 +62,8 @@ export async function change_room(room: Room)
current_room.set(room);
layout.set("room");
return response;
}
export async function validate_password(room: Room)