mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-08-14 07:11:00 +00:00
Merge pull request #301 from nagisa77/tl0f6z-codex
Fix duplicate 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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import com.openisle.service.EmailSender;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import java.util.List;
|
|||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class PostService {
|
public class PostService {
|
||||||
@@ -143,6 +144,7 @@ public class PostService {
|
|||||||
return post;
|
return post;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public Post viewPost(Long id, String viewer) {
|
public Post viewPost(Long id, String viewer) {
|
||||||
Post post = postRepository.findById(id)
|
Post post = postRepository.findById(id)
|
||||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
|
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
|
||||||
@@ -164,9 +166,8 @@ public class PostService {
|
|||||||
if (viewer != null && !viewer.equals(post.getAuthor().getUsername())) {
|
if (viewer != null && !viewer.equals(post.getAuthor().getUsername())) {
|
||||||
User viewerUser = userRepository.findByUsername(viewer).orElse(null);
|
User viewerUser = userRepository.findByUsername(viewer).orElse(null);
|
||||||
if (viewerUser != null) {
|
if (viewerUser != null) {
|
||||||
|
notificationRepository.deleteByTypeAndFromUserAndPost(NotificationType.POST_VIEWED, viewerUser, post);
|
||||||
notificationService.createNotification(post.getAuthor(), NotificationType.POST_VIEWED, post, null, null, viewerUser, null, null);
|
notificationService.createNotification(post.getAuthor(), NotificationType.POST_VIEWED, post, null, null, viewerUser, null, null);
|
||||||
} else {
|
|
||||||
notificationService.createNotification(post.getAuthor(), NotificationType.POST_VIEWED, post, null, null, null, null, null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return post;
|
return post;
|
||||||
|
|||||||
@@ -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