From 64a0f266eeca3a04e9ce227716a9d38750c84fde Mon Sep 17 00:00:00 2001 From: ybchar Date: Wed, 16 Oct 2024 00:11:01 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=A4=91=EB=B3=B5=20=EB=8C=93=EA=B8=80?= =?UTF-8?q?=20=EC=95=8C=EB=A6=BC=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/application/CommentService.java | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/depromeet/stonebed/domain/comment/application/CommentService.java b/src/main/java/com/depromeet/stonebed/domain/comment/application/CommentService.java index ea90dac..e32bf9e 100644 --- a/src/main/java/com/depromeet/stonebed/domain/comment/application/CommentService.java +++ b/src/main/java/com/depromeet/stonebed/domain/comment/application/CommentService.java @@ -17,11 +17,11 @@ import com.depromeet.stonebed.global.error.ErrorCode; import com.depromeet.stonebed.global.error.exception.CustomException; import com.depromeet.stonebed.global.util.MemberUtil; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; @@ -46,7 +46,7 @@ public CommentCreateResponse createComment(CommentCreateRequest request) { final Comment comment = createAndSaveComment(request, member, missionRecord); - sendCommentNotification(missionRecord, comment); + sendCommentNotification(missionRecord, comment, request.parentId()); return CommentCreateResponse.of(comment.getId()); } @@ -77,37 +77,46 @@ private Comment createAndSaveComment( return commentRepository.save(comment); } - private void sendCommentNotification(MissionRecord missionRecord, Comment comment) { + /** + * 댓글 알림 메서드 + * + * @param missionRecord: 작성된 미션 기록 + * @param comment: 새로 생성한 댓글 + * @param parentId: 부모 댓글의 ID + */ + private void sendCommentNotification( + MissionRecord missionRecord, Comment comment, Long parentId) { Member missionRecordOwner = missionRecord.getMember(); Member commentWriter = comment.getWriter(); - // 1. 게시물 작성자가 댓글 작성자가 아닐 때 알림 - if (!missionRecordOwner.equals(commentWriter)) { - sendNotification( + // 1. 부모 댓글이 없는 경우 게시물 작성자에게만 COMMENT 알림 + if (!missionRecordOwner.equals(commentWriter) && parentId == null) { + sendCommentNotification( missionRecordOwner, FcmNotificationConstants.COMMENT, missionRecord, comment, commentWriter); + return; } // 2. 대댓글 작성 시 부모 댓글 작성자에게 RE_COMMENT 알림 if (comment.getParent() != null) { + // 부모 댓글 작성자 Member parentCommentWriter = comment.getParent().getWriter(); // 부모 댓글 작성자가 대댓글 작성자가 아닌 경우에만 알림 전송 if (!parentCommentWriter.equals(commentWriter)) { - sendNotification( + sendCommentNotification( parentCommentWriter, FcmNotificationConstants.RE_COMMENT, missionRecord, comment, commentWriter); } - - // 게시물 작성자에게는 RECORD_RE_COMMENT 알림 if (!missionRecordOwner.equals(commentWriter)) { - sendNotification( + // 게시물 작성자에게는 RECORD_RE_COMMENT 알림 + sendCommentNotification( missionRecordOwner, FcmNotificationConstants.RECORD_RE_COMMENT, missionRecord, @@ -115,12 +124,12 @@ private void sendCommentNotification(MissionRecord missionRecord, Comment commen commentWriter); } - // Collect unique recipients for notifications - Set commentRecipients = collectNotificationRecipients(missionRecord, comment); + Set commentRecipients = collectNotificationRecipients(comment); + commentRecipients.remove(parentCommentWriter); // Send notifications to unique recipients for (Member recipient : commentRecipients) { - sendNotification( + sendCommentNotification( recipient, FcmNotificationConstants.RE_COMMENT, missionRecord, @@ -130,7 +139,7 @@ private void sendCommentNotification(MissionRecord missionRecord, Comment commen } } - private void sendNotification( + private void sendCommentNotification( Member recipient, FcmNotificationConstants notificationType, MissionRecord missionRecord, @@ -152,27 +161,24 @@ private void sendNotification( FcmNotificationType.valueOf(notificationTypeName)); } - private Set collectNotificationRecipients( - MissionRecord missionRecord, Comment comment) { - Set notificationRecipients = new HashSet<>(); - notificationRecipients.add(missionRecord.getMember()); + private Set collectNotificationRecipients(Comment comment) { + Map notificationRecipientsMap = new HashMap<>(); Comment currentComment = comment; while (currentComment.getParent() != null) { currentComment = currentComment.getParent(); currentComment.getReplyComments().stream() .map(Comment::getWriter) - .forEach(notificationRecipients::add); + .forEach(writer -> notificationRecipientsMap.put(writer.getId(), writer)); } - notificationRecipients.remove(comment.getWriter()); - return notificationRecipients; + notificationRecipientsMap.remove(comment.getWriter().getId()); + + return new HashSet<>(notificationRecipientsMap.values()); } private List retrieveFcmTokens(Set notificationRecipients) { return notificationRecipients.stream() - .map(fcmTokenRepository::findByMember) - .filter(Optional::isPresent) - .map(Optional::get) + .flatMap(member -> fcmTokenRepository.findByMember(member).stream()) .map(FcmToken::getToken) .filter(Objects::nonNull) .filter(token -> !token.isEmpty() && !token.isBlank())