Skip to content

Commit

Permalink
feat: 피드 루트 댓글 작성시 피드의 작성자에게 새로운 댓글 알림 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjerry committed Oct 6, 2023
1 parent e11d1da commit 62aad15
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ public CommentResponse create(

final Comment savedComment = commentRepository.save(comment);

eventPublisher.publish(savedComment, member);
publishEvent(savedComment, feed);

return CommentResponse.from(savedComment);
}

private void publishEvent(final Comment comment, final Feed feed) {
if (comment.getParent().isPresent()) {
eventPublisher.publish(comment);
return;
}
eventPublisher.publish(comment, feed.getWriter());
}

private Comment findSavedComment(final Long commentId) {
return commentRepository.findById(commentId)
.orElseThrow(() -> new CommentException(NOT_FOUND_COMMENT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,20 @@ public static CommentNotificationEvent of(final Comment comment, final Comment t
trigger.getParentIdOrSelfId()
);
}

public static CommentNotificationEvent of(final Comment comment, final Member receiver) {
final Member sender = comment.getMember();

return new CommentNotificationEvent(
receiver.getId(),
comment.getId(),
LocalDateTime.now(),
UPDATE_NOTIFICATION_COMMENT_TYPE,
comment.getContent(),
sender.getName(),
sender.getImageUrl(),
comment.getFeed().getId(),
comment.getParentIdOrSelfId()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@ public class EventPublisher {
private final MemberRepository memberRepository;
private final CommentRepository commentRepository;

public void publish(final Comment trigger, final Member loginMember) {
public void publish(final Comment trigger) {
final Member sender = trigger.getMember();

final Set<Comment> notificationCommentCandidates = trigger.getParent()
.map(parent -> findRelatedCommentsExcludingLoginMember(loginMember, parent))
.map(parent -> findRelatedCommentsExcludingLoginMember(sender, parent))
.orElse(Collections.emptySet());

notificationCommentCandidates.stream()
.map(it -> CommentNotificationEvent.of(it, trigger))
.forEach(applicationEventPublisher::publishEvent);
}

public void publish(final Comment comment, final Member receiver) {
if (comment.isNotOwner(receiver.getId())) {
applicationEventPublisher.publishEvent(CommentNotificationEvent.of(comment, receiver));
}
}

private Set<Comment> findRelatedCommentsExcludingLoginMember(
final Member loginMember,
final Comment parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,42 @@ void test_publish_comment_error_firebase() throws Exception {
() -> assertEquals(1, notificationRepository.findAll().size())
);
}

@Test
@DisplayName("publish(Comment, Member) : 피드에 최상위 부모 댓글이 달리면 피드의 작성자에게 알림이 발송된다.")
void test_publish_comment_to_feed_writer() {
//given
final CommentAddRequest 부모_댓글 = new CommentAddRequest("내용1", feed.getId(), null);

doNothing().when(firebaseCloudMessageClient).sendMessageTo(any(Notification.class), anyLong());

//when
commentCommandService.create(부모_댓글, 댓글_작성자1);

//then
assertAll(
() -> verify(firebaseCloudMessageClient, times(1))
.sendMessageTo(any(Notification.class), anyLong()),
() -> assertEquals(1, notificationRepository.findAll().size())
);
}

@Test
@DisplayName("publish(Comment, Member) : 피드에 최상위 부모 댓글 작성자와 피드 작성자가 동일할 경우 알림을 발행하지 않는다.")
void test_do_not_publish_if_comment_writer_equals_feed_writer() {
//given
final CommentAddRequest 부모_댓글 = new CommentAddRequest("내용1", feed.getId(), null);

doNothing().when(firebaseCloudMessageClient).sendMessageTo(any(Notification.class), anyLong());

//when
commentCommandService.create(부모_댓글, feed.getWriter());

//then
assertAll(
() -> verify(firebaseCloudMessageClient, times(0))
.sendMessageTo(any(Notification.class), anyLong()),
() -> assertEquals(0, notificationRepository.findAll().size())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void test_publish_comment_parent_children() throws Exception {
Comment.createChild(feed, 부모_댓글, 로그인_사용자, "내용4"));

//when
eventPublisher.publish(알림_트리거_댓글, 로그인_사용자);
eventPublisher.publish(알림_트리거_댓글);

//then
verify(applicationEventPublisher, times(2))
Expand All @@ -212,7 +212,7 @@ void test_publish_comment_parent_children_not_notification() throws Exception {
);

//when
eventPublisher.publish(알림_트리거_댓글, 댓글_작성자1);
eventPublisher.publish(알림_트리거_댓글);

//then
verify(applicationEventPublisher, times(0))
Expand All @@ -231,7 +231,7 @@ void test_publish_root_comment_no_notification() throws Exception {
);

//when
eventPublisher.publish(알림_트리거_댓글, 댓글_작성자1);
eventPublisher.publish(알림_트리거_댓글);

//then
verify(applicationEventPublisher, times(0))
Expand Down Expand Up @@ -266,7 +266,7 @@ void test_publish_comment_not_notification_deletedComment() throws Exception {
);

//when
eventPublisher.publish(알림_트리거_댓글, 로그인_사용자);
eventPublisher.publish(알림_트리거_댓글);

//then
verify(applicationEventPublisher, times(1))
Expand Down Expand Up @@ -301,7 +301,7 @@ void test_publish_comment_not_notification_deletedComment2() throws Exception {
);

//when
eventPublisher.publish(알림_트리거_댓글, 로그인_사용자);
eventPublisher.publish(알림_트리거_댓글);

//then
verify(applicationEventPublisher, times(2))
Expand Down Expand Up @@ -337,4 +337,41 @@ void publishMessageEvent() {
.usingRecursiveComparison()
.isEqualTo(expectedEvent);
}

@Test
@DisplayName("publish(Comment, Member) : 피드에 ROOT 댓글이 작성되면 피드의 작성자에게 알림이 발송된다.")
void test_publish_to_feed_writer_when_root_comment_created() throws Exception {
//given
final Member 댓글_작성자1 = memberRepository.findById(1L).get();
eventRepository.save(event);
feedRepository.save(feed);
final Comment 알림_트리거_댓글 = commentRepository.save(
Comment.createRoot(feed, 댓글_작성자1, "내용1")
);

//when
eventPublisher.publish(알림_트리거_댓글, feed.getWriter());

//then
verify(applicationEventPublisher, times(1))
.publishEvent(any(NotificationEvent.class));
}

@Test
@DisplayName("publish(Comment, Member) : 피드 작성자와 피드의 ROOT 댓글 작성자가 동일할 경우 알림을 발송하지 않는다.")
void test_do_not_publish_notification_if_comment_writer_equals_feed_writer() throws Exception {
//given
eventRepository.save(event);
feedRepository.save(feed);
final Comment 알림_트리거_댓글 = commentRepository.save(
Comment.createRoot(feed, feed.getWriter(), "내용1")
);

//when
eventPublisher.publish(알림_트리거_댓글, feed.getWriter());

//then
verify(applicationEventPublisher, times(0))
.publishEvent(any(EventNotificationEvent.class));
}
}

0 comments on commit 62aad15

Please sign in to comment.