feat: email notifications for replies and reactions

This commit is contained in:
Tim
2025-07-30 11:37:40 +08:00
parent 69f5745fe8
commit 3464137511
5 changed files with 112 additions and 6 deletions

View File

@@ -11,7 +11,9 @@ import com.openisle.repository.PostRepository;
import com.openisle.repository.ReactionRepository;
import com.openisle.repository.UserRepository;
import com.openisle.service.NotificationService;
import com.openisle.service.EmailSender;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
@@ -22,6 +24,10 @@ public class ReactionService {
private final PostRepository postRepository;
private final CommentRepository commentRepository;
private final NotificationService notificationService;
private final EmailSender emailSender;
@Value("${app.website-url}")
private String websiteUrl;
public Reaction reactToPost(String username, Long postId, ReactionType type) {
User user = userRepository.findByUsername(username)
@@ -41,6 +47,11 @@ 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 && post.getAuthor().getEmail() != null) {
String url = websiteUrl + "/messages";
emailSender.sendEmail(post.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url);
}
}
return reaction;
}
@@ -64,6 +75,11 @@ 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 && comment.getAuthor().getEmail() != null) {
String url = websiteUrl + "/messages";
emailSender.sendEmail(comment.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url);
}
}
return reaction;
}