mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-03 02:20:49 +08:00
Add subscription feature
This commit is contained in:
@@ -8,6 +8,7 @@ import com.openisle.repository.CommentRepository;
|
||||
import com.openisle.repository.PostRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.service.NotificationService;
|
||||
import com.openisle.service.SubscriptionService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -22,6 +23,7 @@ public class CommentService {
|
||||
private final PostRepository postRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final NotificationService notificationService;
|
||||
private final SubscriptionService subscriptionService;
|
||||
|
||||
public Comment addComment(String username, Long postId, String content) {
|
||||
User author = userRepository.findByUsername(username)
|
||||
@@ -36,6 +38,16 @@ public class CommentService {
|
||||
if (!author.getId().equals(post.getAuthor().getId())) {
|
||||
notificationService.createNotification(post.getAuthor(), NotificationType.COMMENT_REPLY, post, comment, null);
|
||||
}
|
||||
for (User u : subscriptionService.getPostSubscribers(postId)) {
|
||||
if (!u.getId().equals(author.getId())) {
|
||||
notificationService.createNotification(u, NotificationType.POST_UPDATED, post, comment, null);
|
||||
}
|
||||
}
|
||||
for (User u : subscriptionService.getSubscribers(author.getUsername())) {
|
||||
if (!u.getId().equals(author.getId())) {
|
||||
notificationService.createNotification(u, NotificationType.USER_ACTIVITY, post, comment, null);
|
||||
}
|
||||
}
|
||||
return comment;
|
||||
}
|
||||
|
||||
@@ -53,6 +65,21 @@ public class CommentService {
|
||||
if (!author.getId().equals(parent.getAuthor().getId())) {
|
||||
notificationService.createNotification(parent.getAuthor(), NotificationType.COMMENT_REPLY, parent.getPost(), comment, null);
|
||||
}
|
||||
for (User u : subscriptionService.getCommentSubscribers(parentId)) {
|
||||
if (!u.getId().equals(author.getId())) {
|
||||
notificationService.createNotification(u, NotificationType.COMMENT_REPLY, parent.getPost(), comment, null);
|
||||
}
|
||||
}
|
||||
for (User u : subscriptionService.getPostSubscribers(parent.getPost().getId())) {
|
||||
if (!u.getId().equals(author.getId())) {
|
||||
notificationService.createNotification(u, NotificationType.POST_UPDATED, parent.getPost(), comment, null);
|
||||
}
|
||||
}
|
||||
for (User u : subscriptionService.getSubscribers(author.getUsername())) {
|
||||
if (!u.getId().equals(author.getId())) {
|
||||
notificationService.createNotification(u, NotificationType.USER_ACTIVITY, parent.getPost(), comment, null);
|
||||
}
|
||||
}
|
||||
return comment;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.openisle.repository.PostRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.repository.CategoryRepository;
|
||||
import com.openisle.repository.TagRepository;
|
||||
import com.openisle.service.SubscriptionService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -25,6 +26,7 @@ public class PostService {
|
||||
private final TagRepository tagRepository;
|
||||
private final PublishMode publishMode;
|
||||
private final NotificationService notificationService;
|
||||
private final SubscriptionService subscriptionService;
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
public PostService(PostRepository postRepository,
|
||||
@@ -32,12 +34,14 @@ public class PostService {
|
||||
CategoryRepository categoryRepository,
|
||||
TagRepository tagRepository,
|
||||
NotificationService notificationService,
|
||||
SubscriptionService subscriptionService,
|
||||
@Value("${app.post.publish-mode:DIRECT}") PublishMode publishMode) {
|
||||
this.postRepository = postRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.categoryRepository = categoryRepository;
|
||||
this.tagRepository = tagRepository;
|
||||
this.notificationService = notificationService;
|
||||
this.subscriptionService = subscriptionService;
|
||||
this.publishMode = publishMode;
|
||||
}
|
||||
|
||||
@@ -64,7 +68,14 @@ public class PostService {
|
||||
post.setCategory(category);
|
||||
post.setTags(new java.util.HashSet<>(tags));
|
||||
post.setStatus(publishMode == PublishMode.REVIEW ? PostStatus.PENDING : PostStatus.PUBLISHED);
|
||||
return postRepository.save(post);
|
||||
post = postRepository.save(post);
|
||||
// notify followers of author
|
||||
for (User u : subscriptionService.getSubscribers(author.getUsername())) {
|
||||
if (!u.getId().equals(author.getId())) {
|
||||
notificationService.createNotification(u, NotificationType.USER_ACTIVITY, post, null, null);
|
||||
}
|
||||
}
|
||||
return post;
|
||||
}
|
||||
|
||||
public Post viewPost(Long id, String viewer) {
|
||||
|
||||
102
src/main/java/com/openisle/service/SubscriptionService.java
Normal file
102
src/main/java/com/openisle/service/SubscriptionService.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.model.*;
|
||||
import com.openisle.repository.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SubscriptionService {
|
||||
private final PostSubscriptionRepository postSubRepo;
|
||||
private final CommentSubscriptionRepository commentSubRepo;
|
||||
private final UserSubscriptionRepository userSubRepo;
|
||||
private final UserRepository userRepo;
|
||||
private final PostRepository postRepo;
|
||||
private final CommentRepository commentRepo;
|
||||
|
||||
public void subscribePost(String username, Long postId) {
|
||||
User user = userRepo.findByUsername(username).orElseThrow();
|
||||
Post post = postRepo.findById(postId).orElseThrow();
|
||||
postSubRepo.findByUserAndPost(user, post).orElseGet(() -> {
|
||||
PostSubscription ps = new PostSubscription();
|
||||
ps.setUser(user);
|
||||
ps.setPost(post);
|
||||
return postSubRepo.save(ps);
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void subscribeComment(String username, Long commentId) {
|
||||
User user = userRepo.findByUsername(username).orElseThrow();
|
||||
Comment comment = commentRepo.findById(commentId).orElseThrow();
|
||||
commentSubRepo.findByUserAndComment(user, comment).orElseGet(() -> {
|
||||
CommentSubscription cs = new CommentSubscription();
|
||||
cs.setUser(user);
|
||||
cs.setComment(comment);
|
||||
return commentSubRepo.save(cs);
|
||||
});
|
||||
}
|
||||
|
||||
public void unsubscribeComment(String username, Long commentId) {
|
||||
User user = userRepo.findByUsername(username).orElseThrow();
|
||||
Comment comment = commentRepo.findById(commentId).orElseThrow();
|
||||
commentSubRepo.findByUserAndComment(user, comment).ifPresent(commentSubRepo::delete);
|
||||
}
|
||||
|
||||
public void subscribeUser(String username, String targetName) {
|
||||
if (username.equals(targetName)) return;
|
||||
User subscriber = userRepo.findByUsername(username).orElseThrow();
|
||||
User target = userRepo.findByUsername(targetName).orElseThrow();
|
||||
userSubRepo.findBySubscriberAndTarget(subscriber, target).orElseGet(() -> {
|
||||
UserSubscription us = new UserSubscription();
|
||||
us.setSubscriber(subscriber);
|
||||
us.setTarget(target);
|
||||
return userSubRepo.save(us);
|
||||
});
|
||||
}
|
||||
|
||||
public void unsubscribeUser(String username, String targetName) {
|
||||
User subscriber = userRepo.findByUsername(username).orElseThrow();
|
||||
User target = userRepo.findByUsername(targetName).orElseThrow();
|
||||
userSubRepo.findBySubscriberAndTarget(subscriber, target).ifPresent(userSubRepo::delete);
|
||||
}
|
||||
|
||||
public List<User> getSubscribedUsers(String username) {
|
||||
User user = userRepo.findByUsername(username).orElseThrow();
|
||||
return userSubRepo.findBySubscriber(user).stream().map(UserSubscription::getTarget).toList();
|
||||
}
|
||||
|
||||
public List<User> getSubscribers(String username) {
|
||||
User user = userRepo.findByUsername(username).orElseThrow();
|
||||
return userSubRepo.findByTarget(user).stream().map(UserSubscription::getSubscriber).toList();
|
||||
}
|
||||
|
||||
public List<User> getPostSubscribers(Long postId) {
|
||||
Post post = postRepo.findById(postId).orElseThrow();
|
||||
return postSubRepo.findByPost(post).stream().map(PostSubscription::getUser).toList();
|
||||
}
|
||||
|
||||
public List<User> getCommentSubscribers(Long commentId) {
|
||||
Comment c = commentRepo.findById(commentId).orElseThrow();
|
||||
return commentSubRepo.findByComment(c).stream().map(CommentSubscription::getUser).toList();
|
||||
}
|
||||
|
||||
|
||||
public long countSubscribers(String username) {
|
||||
User user = userRepo.findByUsername(username).orElseThrow();
|
||||
return userSubRepo.countByTarget(user);
|
||||
}
|
||||
|
||||
public long countSubscribed(String username) {
|
||||
User user = userRepo.findByUsername(username).orElseThrow();
|
||||
return userSubRepo.countBySubscriber(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user