[Fix] DIRECT 채팅방 재입장 및 메시지 전송 시 ChatPart 복구#171
Conversation
📝 Walkthrough📋 대략적 요약DIRECT 채팅에서 상대 사용자가 나간(ChatPart 삭제) 상태였을 때, 채팅방에 재진입하거나 메시지를 보낼 때 자동으로 상대의 ChatPart를 복구하는 기능을 구현합니다. 저장소 메서드 변경으로 삭제된 상대도 조회 가능하게 하고, 진입/메시지 송신 시점에 복구를 수행합니다. 🔄 변경 사항DIRECT 채팅 참여자 복구 흐름
📌 검토 포인트
🎯 리뷰 난도🎯 2 (Simple) | ⏱️ ~10 분 소규모 변경이지만 저장소 계약 변경이 포함되어 있으므로, 다른 코드 경로에서 의도하지 않은 영향을 주지 않는지 신중히 검토하세요. 🔗 관련 PR
🎭 격려의 말
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/main/java/com/capstone/pickIt/api/chat/service/ChatMessageCommandServiceImpl.java (1)
108-162:⚠️ Potential issue | 🔴 Critical | 🏗️ Heavy liftDIRECT에서 상대 ChatPart 복구가 “루프 밖”에서 실행되도록 위치/순서 수정 필요
findActiveParticipantsWithUserByChatRoomId는cp.deletedAt IS NULL(및 현재 사용자 제외 조건)로 조회합니다. 그래서 DIRECT에서 상대가 이미 나가(deletedAt != null) 있으면participants가 비어 for 루프가 돌지 않고, 현재 루프 내부의findOpponentIncludingDeleted(...).ifPresent(ChatPart::restore)가 호출되지 않습니다(=복구/재노출 기능이 데드 패스).
restore는 루프 밖으로 빼서participants(및unreadCountMap) 조회 이전에 실행되게 변경하세요.- JPA에서는 변경 후 같은 트랜잭션 내 조회 반영이 중요하므로(dirty checking/flush 타이밍), 순서만 바꾸어도 해결되고, 필요 시
flush/저장 명시로 조회 일관성을 맞추는 방향을 권장합니다(Spring@Transactional, JPA flush/dirty checking 개념 참고).권장 diff (복구 블록을 participants 조회 이전으로 이동)
eventPublisher.publishEvent( new ChatRoomBroadcastEvent(chatRoom.getId(), response) ); + // DIRECT 채팅 상대방이 나간 상태라면 채팅방 재노출을 위해 복구 + // (참여자 조회 이전에 수행하여 복구된 상대도 알림 대상에 포함되도록 함) + if (chatRoom.getChatType() == ChatType.DIRECT) { + chatPartRepository.findOpponentIncludingDeleted(chatRoom.getId(), currentUserId) + .ifPresent(ChatPart::restore); + } + /* * 채팅방 참여자별 채팅 목록 갱신 알림 이벤트 발행 */ List<ChatPart> participants = chatPartRepository.findActiveParticipantsWithUserByChatRoomId(chatRoom.getId());- // DIRECT 채팅 상대방이 나간 상태라면 채팅방 재노출을 위해 복구 - if (chatRoom.getChatType() == ChatType.DIRECT) { - chatPartRepository.findOpponentIncludingDeleted(chatRoom.getId(), currentUserId) - .ifPresent(ChatPart::restore); - } - // 채팅 목록(lastMessage, unreadCount) 갱신용 개인 알림 이벤트 발행 eventPublisher.publishEvent( new ChatUserNotificationEvent(receiverId, notification) );🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/main/java/com/capstone/pickIt/api/chat/service/ChatMessageCommandServiceImpl.java` around lines 108 - 162, The DIRECT-chat opponent restore logic must run before you fetch participants/unreadCountMap because findActiveParticipantsWithUserByChatRoomId excludes deleted ChatPart rows, causing the for-loop to skip restoration; move the call to chatPartRepository.findOpponentIncludingDeleted(chatRoom.getId(), currentUserId).ifPresent(ChatPart::restore) to execute prior to calling findActiveParticipantsWithUserByChatRoomId(...) and countUnreadMessagesByUsersInChatRoom(...), and ensure the transaction will flush the change (e.g., rely on `@Transactional` or call save/flush if necessary) so subsequent queries see the restored ChatPart.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In
`@src/main/java/com/capstone/pickIt/api/chat/service/ChatMessageCommandServiceImpl.java`:
- Around line 108-162: The DIRECT-chat opponent restore logic must run before
you fetch participants/unreadCountMap because
findActiveParticipantsWithUserByChatRoomId excludes deleted ChatPart rows,
causing the for-loop to skip restoration; move the call to
chatPartRepository.findOpponentIncludingDeleted(chatRoom.getId(),
currentUserId).ifPresent(ChatPart::restore) to execute prior to calling
findActiveParticipantsWithUserByChatRoomId(...) and
countUnreadMessagesByUsersInChatRoom(...), and ensure the transaction will flush
the change (e.g., rely on `@Transactional` or call save/flush if necessary) so
subsequent queries see the restored ChatPart.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 85f349d2-ae29-4ab3-b443-3b7cab421a5c
📒 Files selected for processing (4)
src/main/java/com/capstone/pickIt/api/chat/service/ChatMessageCommandServiceImpl.javasrc/main/java/com/capstone/pickIt/api/chat/service/ChatRoomCommandServiceImpl.javasrc/main/java/com/capstone/pickIt/api/chat/service/ChatRoomQueryServiceImpl.javasrc/main/java/com/capstone/pickIt/domain/chat/repository/ChatPartRepository.java
🔗 관련 이슈
closes #170
📌 작업 내용
🧪 테스트 결과
📸 스크린샷 (선택)
📎 참고 사항 (선택)
Summary by CodeRabbit
릴리스 노트
버그 수정
개선사항