Skip to content
Merged

Test #718

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -102,13 +103,23 @@ public void acknowledgeRead(ReadAckRequest ack, Member reader, String chatRoomId
String unReadRedisKey = UN_READ_MESSAGE_COUNTER_KEY.formatted(chatRoomId, reader.getMemberId());
redisTemplate.delete(unReadRedisKey);

// 채팅방 리스트에 즉시 갱신하기 위한 코드
ChatMessage lastReadMessage = chatMessageRepository.findById(ack.getLastReadMessageId())
.orElseThrow(() -> new CustomException(ErrorCode.MESSAGE_NOT_FOUND));

String preview = toPreview(lastReadMessage);
String formattedSendDate = formattingSendDate(TimeUtil.toLocalDateTime(lastReadMessage.getSendDate()));

rabbitTemplate.convertAndSend(
"",
chatRabbitMqProperties.unreadRoutingKey() + reader.getMemberId(),
Map.of("chatRoomId", chatRoomId,
"unReadMessageCount", 0,
"lastMessageId", ack.getLastReadMessageId())
buildUnreadTopicPayload(
chatRoomId,
0L,
lastReadMessage.getChatMessageId(),
lastReadMessage.getChatMessageType(),
preview,
formattedSendDate
)
);

ChatRoom chatRoom = chatRoomService.findChatRoomById(chatRoomId);
Expand Down Expand Up @@ -275,14 +286,15 @@ private ChatMessage handleNewChatMessage(Member sender, ChatMessageRequest reque
rabbitTemplate.convertAndSend(
"",
chatRabbitMqProperties.unreadRoutingKey() + chatRoomMemberId,
Map.of(
"chatRoomId", chatRoom.getChatRoomId(),
"unReadMessageCount", count,
"lastMessage", request.toPreview(),
"lastMessageType", request.getType(),
"sendDate", formattedSendDate,
"lastMessageId", message.getChatMessageId()
));
buildUnreadTopicPayload(
chatRoom.getChatRoomId(),
count,
message.getChatMessageId(),
request.getType(),
request.toPreview(),
formattedSendDate
)
);
}
return message;
}
Expand Down Expand Up @@ -429,4 +441,29 @@ private void updateLastMessageInfo(ChatRoom chatRoom, ChatMessage chatMessage, S

chatRoom.updateLastMessageType(chatMessage.getChatMessageType());
}

private Map<String, Object> buildUnreadTopicPayload(String chatRoomId, long unReadMessageCount, String lastMessageId,
ChatMessageType lastMessageType, String lastMessage, String sendDate
) {
Map<String, Object> payload = new LinkedHashMap<>();
payload.put("lastMessageId", lastMessageId);
payload.put("lastMessageType", lastMessageType);
payload.put("unReadMessageCount", unReadMessageCount);
payload.put("chatRoomId", chatRoomId);
payload.put("lastMessage", lastMessage == null ? "" : lastMessage);
payload.put("sendDate", sendDate == null ? "" : sendDate);
return payload;
}


private String toPreview(ChatMessage chatMessage) {
return switch (chatMessage.getChatMessageType()) {
case TEXT -> nvl(chatMessage.getMessage(), "");
case PICTURE -> ChatMessageType.PICTURE.getDescription();
case FULFILLMENT_FORM -> ChatMessageType.FULFILLMENT_FORM.getDescription();
case ACCEPTED_FULFILLMENT_FORM -> ChatMessageType.ACCEPTED_FULFILLMENT_FORM.getDescription();
case REJECTED_FULFILLMENT_FORM -> ChatMessageType.REJECTED_FULFILLMENT_FORM.getDescription();
case UPDATE_FULFILLMENT_FORM -> ChatMessageType.UPDATE_FULFILLMENT_FORM.getDescription();
};
}
}