미소를뿌리는감자의 코딩

[Aper] 채팅 목록 반환 w. read status feat 본문

프로젝트

[Aper] 채팅 목록 반환 w. read status feat

미뿌감 2024. 11. 8. 00:29
728x90

1. 개요

채팅방의 읽음 유무를 포함하는 참여하는 채팅방 목록을 반환해 주어야 했다.

해당 유저가 채팅방에 접속한 시간을 저장하고, 생성되는 채팅의 시간을 저장해서 

만약 유저가 접속한 시간보다 생성되는 채팅이 더 뒤 시간대에 있다면, 해당 유저는 그 채팅방의 최신 메시지를 읽지 않은 것이기 때문에,

read = False를 반환해 주었다.

 

2. 본문

이런 식으로 구성을 해주어야 했다.

 

우선 controller 코드

@GetMapping("/status/chatRoom")
public ResponseDto<List<ChatParticipatingResponseDto>> checkReadStatus(@AuthenticationPrincipal UserDetailsImpl userDetails) {
    return mainChatService.checkReadStatus(userDetails.user().getUserId());
    //return mainChatService.checkReadStatus(1L);
}

 

service 코드

public ResponseDto<List<ChatParticipatingResponseDto>> checkReadStatus(Long userId) {
    List<ChatParticipant> participatingChats = chatParticipantRepository.findByUserUserId(userId);

    if (participatingChats.isEmpty()) {
        return ResponseDto.fail(ChatMessageEnum.NO_PARTICIPATING_CHAT.getMessage());
    }

    List<ChatParticipatingResponseDto> participatingResponseDtos = new ArrayList<>();
    for (ChatParticipant chatParticipant : participatingChats) {
        Long roomId = chatParticipant.getChatRoom().getId();

        ChatMessage latestMessage = chatService.getLatestMessage(roomId).block();
        
        if (latestMessage == null){
            continue;
        }
        ChatParticipatingResponseDto participatingResponseDto = getChatParticipatingResponseDto(chatParticipant, latestMessage, roomId);
        participatingResponseDtos.add(participatingResponseDto);
    }

    return ResponseDto.success(ChatMessageEnum.FIND_CHAT_SUCCESS.getMessage(), participatingResponseDtos);
}

private static ChatParticipatingResponseDto getChatParticipatingResponseDto(ChatParticipant chatParticipant, ChatMessage latestMessage, Long roomId) {
    String messageContent = latestMessage.getContent();
    LocalDateTime messageTimeStamp = latestMessage.getTimestamp();

    ChatParticipatingResponseDto participatingResponseDto;
    if (chatParticipant.getLastVisited().isBefore(messageTimeStamp)) {
        participatingResponseDto = new ChatParticipatingResponseDto(
                roomId,
                chatParticipant.getIsTutor(),
                Boolean.FALSE,
                messageContent,
                messageTimeStamp
        );
    }
    else {
        participatingResponseDto = new ChatParticipatingResponseDto(
                roomId,
                chatParticipant.getIsTutor(),
                Boolean.TRUE,
                messageContent,
                messageTimeStamp
        );
    }
    return participatingResponseDto;

 

는 이와 같다.

 

유저가 참여 중인 채팅방 목록 get -> 해당 채팅 방의 최근 메시지 localDateTime Get -> 해당 유저의 해당 채팅방 마지막 접속 localDateTime Get -> 시간 비교 -> 반환

 

이런식으로 구성을 해주었다.

 

front-end skeleton 코드를 구성해서 잘 출력되는지 확인해 주었다.

 

채팅방마다 최근 메시지가 반환되는 것을 확인할 수 있다.

3. Todo

앞으로, controllerDocs의 예외처리, dto record 처리, CORS 처리, main server와의 db[mysql] 연결

을 해주어야 한다.

메인 서버와 중복되는 코드 같은 경우엔, Mono repository로 빼던가, 패키지 처리를 해야 할 듯 하다.. -> 고민 필요

내일은 코드 리펙토링

 

큰 기능들은 모두 끝내었다...! ㅜㅜㅜㅠㅠㅠㅠㅠ

728x90