From d79dc8877dff4d00baf21f6a6bb54c077595cf21 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Wed, 27 Aug 2025 20:23:28 +0800 Subject: [PATCH 1/2] fix: handle reaction notification and point deduction --- .../controller/ReactionController.java | 2 ++ .../com/openisle/model/PointHistoryType.java | 2 ++ .../repository/NotificationRepository.java | 5 +++++ .../openisle/service/NotificationService.java | 8 +++++++ .../com/openisle/service/PointService.java | 21 +++++++++++++++++++ .../com/openisle/service/ReactionService.java | 2 ++ 6 files changed, 40 insertions(+) diff --git a/backend/src/main/java/com/openisle/controller/ReactionController.java b/backend/src/main/java/com/openisle/controller/ReactionController.java index ce0966160..47450834a 100644 --- a/backend/src/main/java/com/openisle/controller/ReactionController.java +++ b/backend/src/main/java/com/openisle/controller/ReactionController.java @@ -36,6 +36,7 @@ public class ReactionController { Authentication auth) { Reaction reaction = reactionService.reactToPost(auth.getName(), postId, req.getType()); if (reaction == null) { + pointService.deductForReactionOfPost(auth.getName(), postId); return ResponseEntity.noContent().build(); } ReactionDto dto = reactionMapper.toDto(reaction); @@ -50,6 +51,7 @@ public class ReactionController { Authentication auth) { Reaction reaction = reactionService.reactToComment(auth.getName(), commentId, req.getType()); if (reaction == null) { + pointService.deductForReactionOfComment(auth.getName(), commentId); return ResponseEntity.noContent().build(); } ReactionDto dto = reactionMapper.toDto(reaction); diff --git a/backend/src/main/java/com/openisle/model/PointHistoryType.java b/backend/src/main/java/com/openisle/model/PointHistoryType.java index 5bff37197..7b2bd59b6 100644 --- a/backend/src/main/java/com/openisle/model/PointHistoryType.java +++ b/backend/src/main/java/com/openisle/model/PointHistoryType.java @@ -5,6 +5,8 @@ public enum PointHistoryType { COMMENT, POST_LIKED, COMMENT_LIKED, + POST_LIKE_CANCELLED, + COMMENT_LIKE_CANCELLED, INVITE, FEATURE, SYSTEM_ONLINE, diff --git a/backend/src/main/java/com/openisle/repository/NotificationRepository.java b/backend/src/main/java/com/openisle/repository/NotificationRepository.java index d0a68ca68..644a07686 100644 --- a/backend/src/main/java/com/openisle/repository/NotificationRepository.java +++ b/backend/src/main/java/com/openisle/repository/NotificationRepository.java @@ -5,6 +5,7 @@ import com.openisle.model.User; import com.openisle.model.Post; import com.openisle.model.Comment; import com.openisle.model.NotificationType; +import com.openisle.model.ReactionType; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -29,4 +30,8 @@ public interface NotificationRepository extends JpaRepository findByTypeAndFromUser(NotificationType type, User fromUser); void deleteByTypeAndFromUserAndPost(NotificationType type, User fromUser, Post post); + + void deleteByTypeAndFromUserAndPostAndReactionType(NotificationType type, User fromUser, Post post, ReactionType reactionType); + + void deleteByTypeAndFromUserAndCommentAndReactionType(NotificationType type, User fromUser, Comment comment, ReactionType reactionType); } diff --git a/backend/src/main/java/com/openisle/service/NotificationService.java b/backend/src/main/java/com/openisle/service/NotificationService.java index 91b9847d3..d6db3f029 100644 --- a/backend/src/main/java/com/openisle/service/NotificationService.java +++ b/backend/src/main/java/com/openisle/service/NotificationService.java @@ -114,6 +114,14 @@ public class NotificationService { return n; } + public void deleteReactionNotification(User fromUser, Post post, Comment comment, ReactionType reactionType) { + if (post != null) { + notificationRepository.deleteByTypeAndFromUserAndPostAndReactionType(NotificationType.REACTION, fromUser, post, reactionType); + } else if (comment != null) { + notificationRepository.deleteByTypeAndFromUserAndCommentAndReactionType(NotificationType.REACTION, fromUser, comment, reactionType); + } + } + /** * Create notifications for all admins when a user submits a register request. * Old register request notifications from the same applicant are removed first. diff --git a/backend/src/main/java/com/openisle/service/PointService.java b/backend/src/main/java/com/openisle/service/PointService.java index 086f2c7a9..4bfe410af 100644 --- a/backend/src/main/java/com/openisle/service/PointService.java +++ b/backend/src/main/java/com/openisle/service/PointService.java @@ -150,6 +150,16 @@ public class PointService { return addPoint(poster, 10, PointHistoryType.POST_LIKED, post, null, reactioner); } + public int deductForReactionOfPost(String reactionerName, Long postId) { + User poster = postRepository.findById(postId).orElseThrow().getAuthor(); + User reactioner = userRepository.findByUsername(reactionerName).orElseThrow(); + if (poster.getId().equals(reactioner.getId())) { + return 0; + } + Post post = postRepository.findById(postId).orElseThrow(); + return addPoint(poster, -10, PointHistoryType.POST_LIKE_CANCELLED, post, null, reactioner); + } + // 考虑点赞者和评论者是同一个的情况 public int awardForReactionOfComment(String reactionerName, Long commentId) { // 根据帖子id找到评论者 @@ -169,6 +179,17 @@ public class PointService { return addPoint(commenter, 10, PointHistoryType.COMMENT_LIKED, post, comment, reactioner); } + public int deductForReactionOfComment(String reactionerName, Long commentId) { + User commenter = commentRepository.findById(commentId).orElseThrow().getAuthor(); + User reactioner = userRepository.findByUsername(reactionerName).orElseThrow(); + if (commenter.getId().equals(reactioner.getId())) { + return 0; + } + Comment comment = commentRepository.findById(commentId).orElseThrow(); + Post post = comment.getPost(); + return addPoint(commenter, -10, PointHistoryType.COMMENT_LIKE_CANCELLED, post, comment, reactioner); + } + public java.util.List listHistory(String userName) { User user = userRepository.findByUsername(userName).orElseThrow(); if (pointHistoryRepository.countByUser(user) == 0) { diff --git a/backend/src/main/java/com/openisle/service/ReactionService.java b/backend/src/main/java/com/openisle/service/ReactionService.java index 8df020eaf..f76ea2890 100644 --- a/backend/src/main/java/com/openisle/service/ReactionService.java +++ b/backend/src/main/java/com/openisle/service/ReactionService.java @@ -42,6 +42,7 @@ public class ReactionService { java.util.Optional existing = reactionRepository.findByUserAndPostAndType(user, post, type); if (existing.isPresent()) { + notificationService.deleteReactionNotification(user, post, null, type); reactionRepository.delete(existing.get()); return null; } @@ -65,6 +66,7 @@ public class ReactionService { java.util.Optional existing = reactionRepository.findByUserAndCommentAndType(user, comment, type); if (existing.isPresent()) { + notificationService.deleteReactionNotification(user, null, comment, type); reactionRepository.delete(existing.get()); return null; } From ec0fd63e300f838ed15016b01b385af6b4ec3c0b Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 27 Aug 2025 20:27:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E5=B8=96=E5=AD=90=E5=8F=AF=E8=A2=AB?= =?UTF-8?q?=E5=88=B7=E7=A7=AF=E5=88=86=EF=BC=8C=E5=BA=94=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E8=B5=9E=E6=B6=88=E9=99=A4=E7=A7=AF=E5=88=86?= =?UTF-8?q?=F0=9F=98=82=20#685?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend_nuxt/pages/points.vue | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/frontend_nuxt/pages/points.vue b/frontend_nuxt/pages/points.vue index 68cb5f22d..325777cb7 100644 --- a/frontend_nuxt/pages/points.vue +++ b/frontend_nuxt/pages/points.vue @@ -104,6 +104,31 @@ ,获得{{ item.amount }}积分 + +