Skip to content

Commit

Permalink
Merge pull request #316 from boostcampwm2023/BE-readChat-#311
Browse files Browse the repository at this point in the history
[BE/#311] 채팅 읽음처리
  • Loading branch information
koomin1227 authored Dec 5, 2023
2 parents 7e4338d + 5ee3d8b commit 105d80f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
12 changes: 11 additions & 1 deletion BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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,
Expand Down
35 changes: 30 additions & 5 deletions BE/src/chat/chats.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion BE/src/chat/dto/chat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsString } from 'class-validator';
import { IsBoolean, IsString } from 'class-validator';

export class ChatDto {
@IsString()
Expand Down

0 comments on commit 105d80f

Please sign in to comment.