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

@@ -8,6 +8,7 @@ import com.openisle.service.PostService;
import com.openisle.service.ReactionService;
import com.openisle.service.CaptchaService;
import com.openisle.service.DraftService;
import com.openisle.service.SubscriptionService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
@@ -26,6 +27,7 @@ public class PostController {
private final PostService postService;
private final CommentService commentService;
private final ReactionService reactionService;
private final SubscriptionService subscriptionService;
private final CaptchaService captchaService;
private final DraftService draftService;
@@ -50,7 +52,7 @@ public class PostController {
public ResponseEntity<PostDto> getPost(@PathVariable Long id, Authentication auth) {
String viewer = auth != null ? auth.getName() : null;
Post post = postService.viewPost(id, viewer);
return ResponseEntity.ok(toDto(post));
return ResponseEntity.ok(toDto(post, viewer));
}
@GetMapping
@@ -134,6 +136,16 @@ public class PostController {
return dto;
}
private PostDto toDto(Post post, String viewer) {
PostDto dto = toDto(post);
if (viewer != null) {
dto.setSubscribed(subscriptionService.isPostSubscribed(viewer, post.getId()));
} else {
dto.setSubscribed(false);
}
return dto;
}
private CommentDto toCommentDtoWithReplies(Comment comment) {
CommentDto dto = toCommentDto(comment);
List<CommentDto> replies = commentService.getReplies(comment.getId()).stream()
@@ -223,6 +235,7 @@ public class PostController {
private List<CommentDto> comments;
private List<ReactionDto> reactions;
private java.util.List<AuthorDto> participants;
private boolean subscribed;
}
@Data

View File

@@ -14,6 +14,10 @@ public enum NotificationType {
POST_REVIEWED,
/** A subscribed post received a new comment */
POST_UPDATED,
/** Someone subscribed to your post */
POST_SUBSCRIBED,
/** Someone unsubscribed from your post */
POST_UNSUBSCRIBED,
/** Someone you follow published a new post */
FOLLOWED_POST,
/** Someone started following you */

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));