Skip to content

Commit

Permalink
Fix : 메세지 리스트 최신순 정렬 로직 fix
Browse files Browse the repository at this point in the history
- Comparator 활용
- null 처리(임시)
  • Loading branch information
HyemIin committed Jan 20, 2024
1 parent 5a877ff commit 003b543
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/main/java/com/catchroom/chat/global/pubsub/RedisSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,17 @@ public void sendRoomList(String publishMessage) {
ChatMessageDto chatMessage = objectMapper.readValue(publishMessage, MessageSubDto.class).getChatMessageDto();
List<ChatRoomListGetResponse> chatRoomListGetResponseList = objectMapper.readValue(publishMessage, MessageSubDto.class)
.getList();

Collections.sort(chatRoomListGetResponseList, new ChatRoomListGetResponseComparator());
Comparator<ChatRoomListGetResponse> comparator = new Comparator<ChatRoomListGetResponse>() {
@Override
public int compare(ChatRoomListGetResponse o1, ChatRoomListGetResponse o2) {
if (o1.getChatMessageDto() != null && o2.getChatMessageDto() != null) {
return LocalDateTime.parse(o1.getChatMessageDto().getTime()).withNano(0).compareTo(LocalDateTime.parse(o2.getChatMessageDto().getTime()).withNano(0));
} else {
return 0;
}
}
};
Collections.sort(chatRoomListGetResponseList,comparator);
// 로그인 유저 채팅방 리스트 최신화
messagingTemplate.convertAndSend("/sub/chat/roomlist/" + chatMessage.getUserId(), chatRoomListGetResponseList);
} catch (Exception e) {
Expand All @@ -59,12 +68,3 @@ public void sendRoomList(String publishMessage) {

}

class ChatRoomListGetResponseComparator implements Comparator<ChatRoomListGetResponse> {
@Override
public int compare(ChatRoomListGetResponse response1, ChatRoomListGetResponse response2) {
LocalDateTime time1 = LocalDateTime.parse(response1.getChatMessageDto().getTime());
LocalDateTime time2 = LocalDateTime.parse(response2.getChatMessageDto().getTime());
return time1.compareTo(time2);
}
}

0 comments on commit 003b543

Please sign in to comment.