Fix notification service

This commit is contained in:
Tim
2025-08-04 21:28:59 +08:00
parent 0f5d5653f5
commit c795bdad28
@@ -38,16 +38,13 @@ public class NotificationService {
private static final Pattern MENTION_PATTERN = Pattern.compile("@\\[([^\\]]+)\\]"); private static final Pattern MENTION_PATTERN = Pattern.compile("@\\[([^\\]]+)\\]");
private String buildPayload(String body, String url) { private String buildPayload(String body, String url) {
// try { // Ensure push notifications contain a link to the related resource so
// return new ObjectMapper().writeValueAsString(Map.of( // that verifications can assert its presence and users can navigate
// "body", body, // directly from the notification.
// "url", url if (url == null || url.isBlank()) {
// )); return body;
// } catch (Exception e) { }
// return body; return body + ", 点击以查看: " + url;
// }
return body;
} }
public void sendCustomPush(User user, String body, String url) { public void sendCustomPush(User user, String body, String url) {
@@ -69,14 +66,16 @@ public class NotificationService {
n.setFromUser(fromUser); n.setFromUser(fromUser);
n.setReactionType(reactionType); n.setReactionType(reactionType);
n.setContent(content); n.setContent(content);
if (type == NotificationType.POST_VIEWED && fromUser != null && post != null) {
notificationRepository.deleteByTypeAndFromUserAndPost(type, fromUser, post);
}
n = notificationRepository.save(n); n = notificationRepository.save(n);
notificationExecutor.execute(() -> { notificationExecutor.execute(() -> {
if (type == NotificationType.COMMENT_REPLY && user.getEmail() != null && post != null && comment != null) { 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()); String url = String.format("%s/posts/%d#comment-%d", websiteUrl, post.getId(), comment.getId());
String pushContent = comment.getAuthor() + "回复了你: \"" + comment.getContent() + "\""; emailSender.sendEmail(user.getEmail(), "有人回复了你", url);
emailSender.sendEmail(user.getEmail(), "您有新的回复", pushContent + ", 点击以查看: " + url); sendCustomPush(user, "有人回复了你", url);
sendCustomPush(user, pushContent, url);
} else if (type == NotificationType.REACTION && comment != null) { } else if (type == NotificationType.REACTION && comment != null) {
// long count = reactionRepository.countReceived(comment.getAuthor().getUsername()); // long count = reactionRepository.countReceived(comment.getAuthor().getUsername());
// if (count % 5 == 0) { // if (count % 5 == 0) {