feat: implement post subscription notifications

This commit is contained in:
Tim
2025-07-11 13:17:04 +08:00
parent 2740394c42
commit 452a486063
5 changed files with 135 additions and 7 deletions

View File

@@ -26,6 +26,10 @@ public class SubscriptionService {
PostSubscription ps = new PostSubscription();
ps.setUser(user);
ps.setPost(post);
if (!user.getId().equals(post.getAuthor().getId())) {
notificationService.createNotification(post.getAuthor(),
NotificationType.POST_SUBSCRIBED, post, null, null, user, null);
}
return postSubRepo.save(ps);
});
}
@@ -33,7 +37,13 @@ public class SubscriptionService {
public void unsubscribePost(String username, Long postId) {
User user = userRepo.findByUsername(username).orElseThrow();
Post post = postRepo.findById(postId).orElseThrow();
postSubRepo.findByUserAndPost(user, post).ifPresent(postSubRepo::delete);
postSubRepo.findByUserAndPost(user, post).ifPresent(ps -> {
postSubRepo.delete(ps);
if (!user.getId().equals(post.getAuthor().getId())) {
notificationService.createNotification(post.getAuthor(),
NotificationType.POST_UNSUBSCRIBED, post, null, null, user, null);
}
});
}
public void subscribeComment(String username, Long commentId) {
@@ -117,6 +127,15 @@ public class SubscriptionService {
return userSubRepo.findBySubscriberAndTarget(subscriber, target).isPresent();
}
public boolean isPostSubscribed(String username, Long postId) {
if (username == null || postId == null) {
return false;
}
User user = userRepo.findByUsername(username).orElseThrow();
Post post = postRepo.findById(postId).orElseThrow();
return postSubRepo.findByUserAndPost(user, post).isPresent();
}
private Optional<User> findUser(String identifier) {
if (identifier.matches("\\d+")) {
return userRepo.findById(Long.parseLong(identifier));