Skip to content

Commit

Permalink
Merge pull request #236 from 100-hours-a-week/hotfix-chatResponse
Browse files Browse the repository at this point in the history
fix: 채팅방 메시지 조회 시 인증된 사용자 확인 로직 추가
  • Loading branch information
Namgyu11 authored Sep 30, 2024
2 parents 6d4ba15 + 4520c3e commit c1afe54
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface ChatMessageService {

List<ChatMessageResponse> getMessages(Long chatRoomId);

List<ChatMessageResponse> getMessagesAfterId(Long chatRoomId, String lastMessageId, int size);
List<ChatMessageResponse> getMessagesAfterId(Long memberId, Long chatRoomId, String lastMessageId, int size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ private void sendMessageToNotification(Long chatRoomId, ChatMessageResponse mess
List<Long> activeMemberIds = activeSessionIds.stream()
.map(sessionId -> Long.valueOf(sessionId.toString()))
.toList();

// 채팅방 회원 리스트 중 채팅방에 입장하지 않은 사람들에게 알림 발송
memberIds.stream()
.filter(memberId -> !activeMemberIds.contains(memberId))
Expand All @@ -113,7 +112,14 @@ private void sendMessageToNotification(Long chatRoomId, ChatMessageResponse mess
}

@Override
public List<ChatMessageResponse> getMessagesAfterId(Long chatRoomId, String lastMessageId, int size) {
public List<ChatMessageResponse> 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));

Expand All @@ -131,6 +137,7 @@ public List<ChatMessageResponse> getMessagesAfterId(Long chatRoomId, String last
.sorted(Comparator.comparing(ChatMessage::getCreatedAt))
.map(ChatMessageResponse::fromEntity)
.toList();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,10 +37,11 @@ public void sendMessage(@Payload ChatMessageRequest chatMessage,

@GetMapping("/api/v1/chatRoom/{chatRoomId}/messages")
public ResponseEntity<List<ChatMessageResponse>> 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));
}


Expand Down

0 comments on commit c1afe54

Please sign in to comment.