From 14bd9d86c0dbd2797ffc814a8318470025aa4767 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 1 Aug 2025 12:01:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A4=84=E7=90=86=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E9=98=85=E8=AF=BB=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/openisle/service/NotificationService.java | 5 ++--- src/main/java/com/openisle/service/PostService.java | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/openisle/service/NotificationService.java b/src/main/java/com/openisle/service/NotificationService.java index b2315269d..93948d5c7 100644 --- a/src/main/java/com/openisle/service/NotificationService.java +++ b/src/main/java/com/openisle/service/NotificationService.java @@ -9,6 +9,8 @@ import com.openisle.service.EmailSender; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.transaction.annotation.Transactional; + import java.util.Map; import java.util.regex.Pattern; @@ -58,9 +60,6 @@ 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/main/java/com/openisle/service/PostService.java b/src/main/java/com/openisle/service/PostService.java index 2e124f3f4..c3293c4c9 100644 --- a/src/main/java/com/openisle/service/PostService.java +++ b/src/main/java/com/openisle/service/PostService.java @@ -26,6 +26,7 @@ import java.util.List; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.transaction.annotation.Transactional; @Service public class PostService { @@ -143,6 +144,7 @@ public class PostService { return post; } + @Transactional public Post viewPost(Long id, String viewer) { Post post = postRepository.findById(id) .orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found")); @@ -164,9 +166,8 @@ public class PostService { if (viewer != null && !viewer.equals(post.getAuthor().getUsername())) { User viewerUser = userRepository.findByUsername(viewer).orElse(null); if (viewerUser != null) { + notificationRepository.deleteByTypeAndFromUserAndPost(NotificationType.POST_VIEWED, viewerUser, post); 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;