-
Notifications
You must be signed in to change notification settings - Fork 1
refactor : 채팅 메시지 전송 리팩토링 #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
94b4cd7
1284857
fc5ff71
2d51667
b963c9f
77bdf45
96d87fc
031791a
ae81b27
14350bc
5efc1b1
3c71556
c0c0345
972f5af
761d298
bf43b7e
a310cb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,8 +24,11 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.time.LocalDateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Collections; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Set; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.stream.Collectors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Slf4j | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -71,7 +74,7 @@ public NotificationDto createNotification(Long receiverId, Long senderId, Notifi | |||||||||||||||||||||||||||||||||||||||||||||||||||
| publishNotificationToRedis(savedNotification, senderNickname, message); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.info("알림 생성 및 발송: receiverId={}, type={}, refId={}", receiverId, type, refId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NotificationDto.from(savedNotification); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NotificationDto.from(savedNotification, message); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -87,7 +90,7 @@ public NotificationDto createSystemNotification(Long receiverId, NotificationTyp | |||||||||||||||||||||||||||||||||||||||||||||||||||
| publishNotificationToRedis(savedNotification, "시스템", message); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.info("시스템 알림 생성 및 발송: receiverId={}, type={}, refId={}", receiverId, type, refId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NotificationDto.from(savedNotification); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NotificationDto.from(savedNotification, message); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ===== 미읽음 개수 조회 ===== | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -143,8 +146,23 @@ public NotificationListResponseDto getNotifications(Long userId) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| combinedNotifications.addAll(unreadNotifications); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| combinedNotifications.addAll(readNotifications); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // sender nickname 배치 조회 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set<Long> senderIds = combinedNotifications.stream() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map(Notification::getSenderId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter(Objects::nonNull) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .collect(Collectors.toSet()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<Long, String> nicknameMap = senderIds.isEmpty() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? Collections.emptyMap() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| : userRepository.findAllById(senderIds).stream() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .collect(Collectors.toMap(User::getId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| u -> u.getNickname() != null ? u.getNickname() : "시스템")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<NotificationDto> notificationDtos = combinedNotifications.stream() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map(NotificationDto::from) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map(n -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| String senderNickname = nicknameMap.getOrDefault(n.getSenderId(), "시스템"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| String message = generateNotificationMessage(n.getNotificationType(), senderNickname); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NotificationDto.from(n, message); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+154
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 로직은 시스템 알림(senderId가 null인 경우)과 닉네임이 없는 사용자를 모두 "시스템"이라는 닉네임으로 처리하고 있습니다. 시스템 알림과 일반 사용자를 구분하여 처리하는 것이 좋습니다. 다만, 탈퇴한 사용자와 같은 예외 케이스 처리는 현재 단계에서 나중에 진행해도 무방합니다. 또한, userRepository.findAllById(senderIds)는 User 엔티티 전체를 조회하므로, 알림 목록이 길어질 경우 성능 최적화를 위해 닉네임만 조회하는 프로젝션 사용을 고려해볼 수 있습니다.
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| .collect(Collectors.toList()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NotificationListResponseDto.of(notificationDtos, unreadNotifications.size()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
senderNickname을 결정하는 로직을 더 간결하게 개선할 수 있습니다.nicknameMap.getOrDefault는 키가null인 경우에도 안전하게 동작하므로 명시적인null체크를 생략할 수 있습니다.