fixed messages when leaving and changing name
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user