Skip to content

Commit

Permalink
Merge pull request #243 from 100-hours-a-week/feat-newMessageResponse
Browse files Browse the repository at this point in the history
feat: 읽지 않은 메시지 확인 기능 추가
  • Loading branch information
Namgyu11 authored Oct 1, 2024
2 parents 789867d + 525646b commit 49018f7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package connectripbe.connectrip_be.chat.dto;

public record ChatUnreadMessagesResponse(
boolean hasUnreadMessages
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import connectripbe.connectrip_be.chat.dto.ChatRoomEnterDto;
import connectripbe.connectrip_be.chat.dto.ChatRoomListResponse;
import connectripbe.connectrip_be.chat.dto.ChatRoomMemberResponse;
import connectripbe.connectrip_be.chat.dto.ChatUnreadMessagesResponse;
import java.util.List;

public interface ChatRoomService {
Expand All @@ -17,4 +18,5 @@ public interface ChatRoomService {

ChatRoomEnterDto enterChatRoom(Long chatRoomId, Long memberId);

ChatUnreadMessagesResponse hasUnreadMessagesAcrossRooms(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import connectripbe.connectrip_be.chat.dto.ChatRoomEnterDto;
import connectripbe.connectrip_be.chat.dto.ChatRoomListResponse;
import connectripbe.connectrip_be.chat.dto.ChatRoomMemberResponse;
import connectripbe.connectrip_be.chat.dto.ChatUnreadMessagesResponse;
import connectripbe.connectrip_be.chat.entity.ChatMessage;
import connectripbe.connectrip_be.chat.entity.ChatRoomEntity;
import connectripbe.connectrip_be.chat.entity.ChatRoomMemberEntity;
Expand Down Expand Up @@ -278,6 +279,30 @@ private boolean hasUnreadMessage(Long chatRoomId, Long memberId) {
return lastReadMessageId == null || latestMessage.getId().compareTo(lastReadMessageId) > 0;
}

@Override
public ChatUnreadMessagesResponse hasUnreadMessagesAcrossRooms(Long memberId) {
List<ChatRoomMemberEntity> chatRooms = chatRoomMemberRepository.myChatRoomList(memberId);

for (ChatRoomMemberEntity chatRoomMember : chatRooms) {
ChatRoomEntity chatRoom = chatRoomMember.getChatRoom();
String lastReadMessageId = chatRoomMember.getLastReadMessageId();

// 채팅방의 마지막 메시지를 가져옴
ChatMessage latestMessage = chatMessageRepository.findFirstByChatRoomIdOrderByCreatedAtDesc(
chatRoom.getId())
.orElse(null);

if (latestMessage != null &&
(lastReadMessageId == null || latestMessage.getId().compareTo(lastReadMessageId) > 0)) {
// 읽지 않은 메시지가 하나라도 있으면 true 반환
return new ChatUnreadMessagesResponse(true);
}
}

// 모든 채팅방을 확인했을 때 읽지 않은 메시지가 없으면 false 반환
return new ChatUnreadMessagesResponse(false);
}

private ChatRoomEntity getChatRoom(Long chatRoomId) {
return chatRoomRepository.findById(chatRoomId)
.orElseThrow(() -> new GlobalException(ErrorCode.CHAT_ROOM_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import connectripbe.connectrip_be.chat.dto.ChatRoomEnterDto;
import connectripbe.connectrip_be.chat.dto.ChatRoomListResponse;
import connectripbe.connectrip_be.chat.dto.ChatUnreadMessagesResponse;
import connectripbe.connectrip_be.chat.service.ChatRoomService;
import connectripbe.connectrip_be.global.dto.GlobalResponse;
import java.util.List;
Expand Down Expand Up @@ -61,4 +62,16 @@ public ResponseEntity<GlobalResponse<ChatRoomEnterDto>> enterChatRoom(


}


// 채팅방 중 새로운 메시지 온다면 표시
@GetMapping("/new")
public ResponseEntity<GlobalResponse<ChatUnreadMessagesResponse>> hasUnreadMessagesAcrossRooms(
@AuthenticationPrincipal Long memberId
) {
return
ResponseEntity
.ok()
.body(new GlobalResponse<>("SUCCESS", chatRoomService.hasUnreadMessagesAcrossRooms(memberId)));
}
}

0 comments on commit 49018f7

Please sign in to comment.