From db31b5d6c151d31205753def45768fe646538c03 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Fri, 1 Aug 2025 11:55:17 +0800 Subject: [PATCH] Deduplicate post view notifications --- .../repository/NotificationRepository.java | 2 ++ .../openisle/service/NotificationService.java | 3 +++ .../service/NotificationServiceTest.java | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/main/java/com/openisle/repository/NotificationRepository.java b/src/main/java/com/openisle/repository/NotificationRepository.java index f0d538c49..0cbec0d60 100644 --- a/src/main/java/com/openisle/repository/NotificationRepository.java +++ b/src/main/java/com/openisle/repository/NotificationRepository.java @@ -18,4 +18,6 @@ public interface NotificationRepository extends JpaRepository findByComment(Comment comment); void deleteByTypeAndFromUser(NotificationType type, User fromUser); + + void deleteByTypeAndFromUserAndPost(NotificationType type, User fromUser, Post post); } diff --git a/src/main/java/com/openisle/service/NotificationService.java b/src/main/java/com/openisle/service/NotificationService.java index 561b59b28..b2315269d 100644 --- a/src/main/java/com/openisle/service/NotificationService.java +++ b/src/main/java/com/openisle/service/NotificationService.java @@ -58,6 +58,9 @@ public class NotificationService { public Notification createNotification(User user, NotificationType type, Post post, Comment comment, Boolean approved, User fromUser, ReactionType reactionType, String content) { + if (type == NotificationType.POST_VIEWED && post != null && fromUser != null) { + notificationRepository.deleteByTypeAndFromUserAndPost(type, fromUser, post); + } Notification n = new Notification(); n.setUser(user); n.setType(type); diff --git a/src/test/java/com/openisle/service/NotificationServiceTest.java b/src/test/java/com/openisle/service/NotificationServiceTest.java index 59d43d5e4..c4ca007a1 100644 --- a/src/test/java/com/openisle/service/NotificationServiceTest.java +++ b/src/test/java/com/openisle/service/NotificationServiceTest.java @@ -168,4 +168,27 @@ class NotificationServiceTest { verify(email).sendEmail("a@a.com", "有人回复了你", "https://ex.com/posts/1#comment-2"); verify(push).sendNotification(eq(user), contains("/posts/1#comment-2")); } + + @Test + void postViewedNotificationDeletesOldOnes() { + NotificationRepository nRepo = mock(NotificationRepository.class); + UserRepository uRepo = mock(UserRepository.class); + ReactionRepository rRepo = mock(ReactionRepository.class); + EmailSender email = mock(EmailSender.class); + PushNotificationService push = mock(PushNotificationService.class); + Executor executor = Runnable::run; + NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor); + org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com"); + + User owner = new User(); + User viewer = new User(); + Post post = new Post(); + + when(nRepo.save(any(Notification.class))).thenAnswer(i -> i.getArgument(0)); + + service.createNotification(owner, NotificationType.POST_VIEWED, post, null, null, viewer, null, null); + + verify(nRepo).deleteByTypeAndFromUserAndPost(NotificationType.POST_VIEWED, viewer, post); + verify(nRepo).save(any(Notification.class)); + } }