Skip to content

Commit

Permalink
Merge pull request #279 from boostcampwm2023/BE-sendRecieveChat-#276
Browse files Browse the repository at this point in the history
[BE/#276] 전송 성공 확인 메시지 응답 구현
  • Loading branch information
koomin1227 authored Dec 4, 2023
2 parents 7db16ba + d203751 commit ef60a50
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion BE/src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ChatController {
@Post('room')
@UseGuards(AuthGuard)
async roomCreate(@Body() body: CreateRoomDto, @UserHash() userId: string) {
await this.chatService.createRoom(body.post_id, userId, body.writer);
return await this.chatService.createRoom(body.post_id, userId, body.writer);
}

@Get()
Expand Down
37 changes: 20 additions & 17 deletions BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class ChatService {
@InjectRepository(UserEntity)
private userRepository: Repository<UserEntity>,
private fcmHandler: FcmHandler,

) {}

async saveMessage(message: ChatDto) {
Expand Down Expand Up @@ -66,23 +65,27 @@ export class ChatService {
.leftJoin('post', 'post', 'post.id = chat_room.post_id')
.getRawMany();

return rooms.reduce((acc, cur) => {
acc.push({
room_id: cur.chat_room_id,
post_id: cur.chat_room_post_id,
post_title: cur.post_title,
post_thumbnail: cur.post_thumbnail,
user: cur['user.w_user_hash'],
user_profile_img: cur['user.w_profile_img'],
user_nickname: cur['user.w_nickname'],
writer: cur['user.u_user_hash'],
writer_profile_img: cur['user.u_profile_img'],
writer_nickname: cur['user.u_nickname'],
last_chat: cur.chat_message,
last_chat_date: cur.chat_create_date,
return rooms
.reduce((acc, cur) => {
acc.push({
room_id: cur.chat_room_id,
post_id: cur.chat_room_post_id,
post_title: cur.post_title,
post_thumbnail: cur.post_thumbnail,
user: cur['user.w_user_hash'],
user_profile_img: cur['user.w_profile_img'],
user_nickname: cur['user.w_nickname'],
writer: cur['user.u_user_hash'],
writer_profile_img: cur['user.u_profile_img'],
writer_nickname: cur['user.u_nickname'],
last_chat: cur.chat_message,
last_chat_date: cur.chat_create_date,
});
return acc;
}, [])
.sort((a, b) => {
return b.last_chat_date - a.last_chat_date;
});
return acc;
}, []);
}

async findRoomById(roomId: number, userId: string) {
Expand Down
5 changes: 3 additions & 2 deletions BE/src/chat/chats.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect {
this.rooms.delete(roomId);
}
}
console.log(this.rooms);
}

@SubscribeMessage('send-message')
async sendMessage(
@MessageBody() message: ChatDto,
@ConnectedSocket() client: Websocket,
) {
console.log(message);
const room = this.rooms.get(message['room_id']);
const sender = message['sender'];
await this.chatService.saveMessage(message);
Expand All @@ -53,6 +53,7 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect {
people.send(JSON.stringify({ sender, message: message['message'] }));
});
}
client.send(JSON.stringify({ sent: true }));
}

@SubscribeMessage('join-room')
Expand All @@ -71,7 +72,7 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect {
@MessageBody() message: object,
@ConnectedSocket() client: Websocket,
) {
const roomId = message['room'];
const roomId = message['room_id'];
const room = this.rooms.get(roomId);
room.delete(client);
if (room.size === 0) {
Expand Down

0 comments on commit ef60a50

Please sign in to comment.