From 748f7f9709f019c4235f6a6427b2458e370d6f60 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 30 Jul 2025 12:06:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=82=AE=E4=BB=B6=E3=80=81=E8=AE=AF?= =?UTF-8?q?=E6=81=AF=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../openisle/service/NotificationService.java | 30 ++++++++++++++----- .../com/openisle/service/ReactionService.java | 16 ---------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/openisle/service/NotificationService.java b/src/main/java/com/openisle/service/NotificationService.java index 371879430..27233f5a7 100644 --- a/src/main/java/com/openisle/service/NotificationService.java +++ b/src/main/java/com/openisle/service/NotificationService.java @@ -2,6 +2,7 @@ package com.openisle.service; import com.openisle.model.*; import com.openisle.repository.NotificationRepository; +import com.openisle.repository.ReactionRepository; import com.openisle.repository.UserRepository; import lombok.RequiredArgsConstructor; import com.openisle.service.EmailSender; @@ -20,6 +21,7 @@ public class NotificationService { private final UserRepository userRepository; private final EmailSender emailSender; private final PushNotificationService pushNotificationService; + private final ReactionRepository reactionRepository; @Value("${app.website-url}") private String websiteUrl; @@ -56,16 +58,28 @@ public class NotificationService { n.setContent(content); n = notificationRepository.save(n); - String body = "You have a new notification"; - String url = websiteUrl + "/messages"; - if (type == NotificationType.COMMENT_REPLY && post != null && comment != null) { - body = "有人回复了你"; - url = String.format("%s/posts/%d#comment-%d", websiteUrl, post.getId(), comment.getId()); - } - sendCustomPush(user, body, url); - if (type == NotificationType.COMMENT_REPLY && user.getEmail() != null && post != null && comment != null) { + String url = String.format("%s/posts/%d#comment-%d", websiteUrl, post.getId(), comment.getId()); emailSender.sendEmail(user.getEmail(), "【OpenIsle】有人回复了你", url); + sendCustomPush(user, "有人回复了你", url); + } else if (type == NotificationType.REACTION && comment != null) { + long count = reactionRepository.countReceived(comment.getAuthor().getUsername()); + if (count % 5 == 0) { + String url = websiteUrl + "/messages"; + sendCustomPush(comment.getAuthor(), "你有新的互动", url); + if (comment.getAuthor().getEmail() != null) { + emailSender.sendEmail(comment.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url); + } + } + } else if (type == NotificationType.REACTION && post != null) { + long count = reactionRepository.countReceived(post.getAuthor().getUsername()); + if (count % 5 == 0) { + String url = websiteUrl + "/messages"; + sendCustomPush(post.getAuthor(), "你有新的互动", url); + if (post.getAuthor().getEmail() != null) { + emailSender.sendEmail(post.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url); + } + } } return n; diff --git a/src/main/java/com/openisle/service/ReactionService.java b/src/main/java/com/openisle/service/ReactionService.java index deb7cc1b6..393de62b3 100644 --- a/src/main/java/com/openisle/service/ReactionService.java +++ b/src/main/java/com/openisle/service/ReactionService.java @@ -47,14 +47,6 @@ public class ReactionService { reaction = reactionRepository.save(reaction); if (!user.getId().equals(post.getAuthor().getId())) { notificationService.createNotification(post.getAuthor(), NotificationType.REACTION, post, null, null, user, type, null); - long count = reactionRepository.countReceived(post.getAuthor().getUsername()); - if (count % 5 == 0) { - String url = websiteUrl + "/messages"; - notificationService.sendCustomPush(post.getAuthor(), "你有新的互动", url); - if (post.getAuthor().getEmail() != null) { - emailSender.sendEmail(post.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url); - } - } } return reaction; } @@ -78,14 +70,6 @@ public class ReactionService { reaction = reactionRepository.save(reaction); if (!user.getId().equals(comment.getAuthor().getId())) { notificationService.createNotification(comment.getAuthor(), NotificationType.REACTION, comment.getPost(), comment, null, user, type, null); - long count = reactionRepository.countReceived(comment.getAuthor().getUsername()); - if (count % 5 == 0) { - String url = websiteUrl + "/messages"; - notificationService.sendCustomPush(comment.getAuthor(), "你有新的互动", url); - if (comment.getAuthor().getEmail() != null) { - emailSender.sendEmail(comment.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url); - } - } } return reaction; }