From 4520c3e8453f4ff3ba9250e4741623db1e2f247c Mon Sep 17 00:00:00 2001 From: Namgyu11 <103015031+Namgyu11@users.noreply.github.com> Date: Mon, 30 Sep 2024 14:42:58 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=EC=9D=B8?= =?UTF-8?q?=EC=A6=9D=EB=90=9C=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 채팅방 메시지 조회 시 현재 인증된 사용자를 확인하는 로직을 추가했습니다. - 잘못된 사용자가 채팅방 메시지를 요청할 경우 예외를 발생시키도록 수정했습니다. - memberId 파라미터를 추가하여 사용자 인증 여부를 확인할 수 있도록 수정했습니다. Signed-off-by: Namgyu11 <103015031+Namgyu11@users.noreply.github.com> --- .../chat/service/ChatMessageService.java | 2 +- .../chat/service/impl/ChatMessageServiceImpl.java | 11 +++++++++-- .../connectrip_be/chat/web/ChatMessageController.java | 4 +++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/connectripbe/connectrip_be/chat/service/ChatMessageService.java b/src/main/java/connectripbe/connectrip_be/chat/service/ChatMessageService.java index a873ff41..95e0e4d9 100644 --- a/src/main/java/connectripbe/connectrip_be/chat/service/ChatMessageService.java +++ b/src/main/java/connectripbe/connectrip_be/chat/service/ChatMessageService.java @@ -10,5 +10,5 @@ public interface ChatMessageService { List getMessages(Long chatRoomId); - List getMessagesAfterId(Long chatRoomId, String lastMessageId, int size); + List getMessagesAfterId(Long memberId, Long chatRoomId, String lastMessageId, int size); } diff --git a/src/main/java/connectripbe/connectrip_be/chat/service/impl/ChatMessageServiceImpl.java b/src/main/java/connectripbe/connectrip_be/chat/service/impl/ChatMessageServiceImpl.java index 06cd7aae..9e3afc35 100644 --- a/src/main/java/connectripbe/connectrip_be/chat/service/impl/ChatMessageServiceImpl.java +++ b/src/main/java/connectripbe/connectrip_be/chat/service/impl/ChatMessageServiceImpl.java @@ -101,7 +101,6 @@ private void sendMessageToNotification(Long chatRoomId, ChatMessageResponse mess List activeMemberIds = activeSessionIds.stream() .map(sessionId -> Long.valueOf(sessionId.toString())) .toList(); - // 채팅방 회원 리스트 중 채팅방에 입장하지 않은 사람들에게 알림 발송 memberIds.stream() .filter(memberId -> !activeMemberIds.contains(memberId)) @@ -113,7 +112,14 @@ private void sendMessageToNotification(Long chatRoomId, ChatMessageResponse mess } @Override - public List getMessagesAfterId(Long chatRoomId, String lastMessageId, int size) { + public List getMessagesAfterId(Long memberId, Long chatRoomId, String lastMessageId, + int size) { + + boolean exists = chatRoomMemberRepository.existsByChatRoom_IdAndMember_Id(chatRoomId, memberId); + if (!exists) { + throw new GlobalException(ErrorCode.CHAT_ROOM_MEMBER_NOT_FOUND); + } + Query query = new Query(); query.addCriteria(Criteria.where("chatRoomId").is(chatRoomId)); @@ -131,6 +137,7 @@ public List getMessagesAfterId(Long chatRoomId, String last .sorted(Comparator.comparing(ChatMessage::getCreatedAt)) .map(ChatMessageResponse::fromEntity) .toList(); + } } diff --git a/src/main/java/connectripbe/connectrip_be/chat/web/ChatMessageController.java b/src/main/java/connectripbe/connectrip_be/chat/web/ChatMessageController.java index d8c1f159..c34481c1 100644 --- a/src/main/java/connectripbe/connectrip_be/chat/web/ChatMessageController.java +++ b/src/main/java/connectripbe/connectrip_be/chat/web/ChatMessageController.java @@ -12,6 +12,7 @@ import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.Payload; import org.springframework.messaging.simp.SimpMessagingTemplate; +import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; @@ -36,10 +37,11 @@ public void sendMessage(@Payload ChatMessageRequest chatMessage, @GetMapping("/api/v1/chatRoom/{chatRoomId}/messages") public ResponseEntity> getChatRoomMessages( + @AuthenticationPrincipal Long memberId, @PathVariable Long chatRoomId, @RequestParam String lastMessageId, @RequestParam(defaultValue = "50") int size) { - return ResponseEntity.ok(chatMessageService.getMessagesAfterId(chatRoomId, lastMessageId, size)); + return ResponseEntity.ok(chatMessageService.getMessagesAfterId(memberId, chatRoomId, lastMessageId, size)); }