diff --git a/BE/src/chat/chat.service.ts b/BE/src/chat/chat.service.ts index 84eff73..3f84081 100644 --- a/BE/src/chat/chat.service.ts +++ b/BE/src/chat/chat.service.ts @@ -30,11 +30,12 @@ export class ChatService { private configService: ConfigService, ) {} - async saveMessage(message: ChatDto) { + async saveMessage(message: ChatDto, is_read: boolean) { const chat = new ChatEntity(); chat.sender = message.sender; chat.message = message.message; chat.chat_room = message.room_id; + chat.is_read = is_read; await this.chatRepository.save(chat); } @@ -109,6 +110,15 @@ export class ChatService { } async findRoomById(roomId: number, userId: string) { + await this.chatRepository + .createQueryBuilder('chat') + .update() + .set({ is_read: true }) + .where('chat.chat_room = :roomId', { roomId: roomId }) + .andWhere('chat.is_read = :isRead', { isRead: false }) + .andWhere('chat.sender != :userId', { userId: userId }) + .execute(); + const room = await this.chatRoomRepository.findOne({ where: { id: roomId, diff --git a/BE/src/chat/chats.gateway.ts b/BE/src/chat/chats.gateway.ts index 7dff93d..eee5abe 100644 --- a/BE/src/chat/chats.gateway.ts +++ b/BE/src/chat/chats.gateway.ts @@ -61,16 +61,28 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect { ); const room = this.rooms.get(message['room_id']); const sender = message['sender']; - await this.chatService.saveMessage(message); if (room.size === 1) { + await this.chatService.saveMessage(message, false); await this.chatService.sendPush(message); } else { + await this.chatService.saveMessage(message, true); room.forEach((people) => { if (people !== client) - people.send(JSON.stringify({ sender, message: message['message'] })); + people.send( + JSON.stringify({ + event: 'send-message', + data: { + sender: sender, + message: message['message'], + is_read: true, + }, + }), + ); }); } - client.send(JSON.stringify({ sent: true })); + client.send( + JSON.stringify({ event: 'send-message', data: { sent: true } }), + ); } @SubscribeMessage('join-room') @@ -80,8 +92,21 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect { ) { const roomId = message['room_id']; client.roomId = roomId; - if (this.rooms.has(roomId)) this.rooms.get(roomId).add(client); - else this.rooms.set(roomId, new Set([client])); + if (this.rooms.has(roomId)) { + this.rooms.get(roomId).add(client); + + const room = this.rooms.get(roomId); + + room.forEach((people) => { + if (people !== client) + people.send( + JSON.stringify({ + event: 'join-room', + data: { 'opp-entered': true }, + }), + ); + }); + } else this.rooms.set(roomId, new Set([client])); this.logger.debug( `[${client.userId}] join room : ${roomId}`, 'ChatsGateway', diff --git a/BE/src/chat/dto/chat.dto.ts b/BE/src/chat/dto/chat.dto.ts index b0c282b..093ef89 100644 --- a/BE/src/chat/dto/chat.dto.ts +++ b/BE/src/chat/dto/chat.dto.ts @@ -1,4 +1,4 @@ -import { IsString } from 'class-validator'; +import { IsBoolean, IsString } from 'class-validator'; export class ChatDto { @IsString()