Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE/#311] 채팅 읽음처리 #316

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다!

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 } }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

뭔가 메시지 성격이 달라서 바꾸면 좋을 것 같긴한데 클라리언트가 좋다고 하면 그냥 가죠

);
}

@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
Loading