mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-04-21 11:27:27 +08:00
Deduplicate post view notifications
This commit is contained in:
@@ -18,4 +18,6 @@ public interface NotificationRepository extends JpaRepository<Notification, Long
|
|||||||
List<Notification> findByComment(Comment comment);
|
List<Notification> findByComment(Comment comment);
|
||||||
|
|
||||||
void deleteByTypeAndFromUser(NotificationType type, User fromUser);
|
void deleteByTypeAndFromUser(NotificationType type, User fromUser);
|
||||||
|
|
||||||
|
void deleteByTypeAndFromUserAndPost(NotificationType type, User fromUser, Post post);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ public class NotificationService {
|
|||||||
|
|
||||||
public Notification createNotification(User user, NotificationType type, Post post, Comment comment, Boolean approved,
|
public Notification createNotification(User user, NotificationType type, Post post, Comment comment, Boolean approved,
|
||||||
User fromUser, ReactionType reactionType, String content) {
|
User fromUser, ReactionType reactionType, String content) {
|
||||||
|
if (type == NotificationType.POST_VIEWED && post != null && fromUser != null) {
|
||||||
|
notificationRepository.deleteByTypeAndFromUserAndPost(type, fromUser, post);
|
||||||
|
}
|
||||||
Notification n = new Notification();
|
Notification n = new Notification();
|
||||||
n.setUser(user);
|
n.setUser(user);
|
||||||
n.setType(type);
|
n.setType(type);
|
||||||
|
|||||||
@@ -168,4 +168,27 @@ class NotificationServiceTest {
|
|||||||
verify(email).sendEmail("a@a.com", "有人回复了你", "https://ex.com/posts/1#comment-2");
|
verify(email).sendEmail("a@a.com", "有人回复了你", "https://ex.com/posts/1#comment-2");
|
||||||
verify(push).sendNotification(eq(user), contains("/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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user