mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-06 12:00:44 +08:00
Record user visits on posts listing
This commit is contained in:
44
src/main/java/com/openisle/service/PostReadService.java
Normal file
44
src/main/java/com/openisle/service/PostReadService.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.model.Post;
|
||||
import com.openisle.model.PostRead;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.repository.PostReadRepository;
|
||||
import com.openisle.repository.PostRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PostReadService {
|
||||
private final PostReadRepository postReadRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final PostRepository postRepository;
|
||||
|
||||
public void recordRead(String username, Long postId) {
|
||||
if (username == null) return;
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
Post post = postRepository.findById(postId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Post not found"));
|
||||
postReadRepository.findByUserAndPost(user, post).ifPresentOrElse(pr -> {
|
||||
pr.setLastReadAt(LocalDateTime.now());
|
||||
postReadRepository.save(pr);
|
||||
}, () -> {
|
||||
PostRead pr = new PostRead();
|
||||
pr.setUser(user);
|
||||
pr.setPost(post);
|
||||
pr.setLastReadAt(LocalDateTime.now());
|
||||
postReadRepository.save(pr);
|
||||
});
|
||||
}
|
||||
|
||||
public long countReads(String username) {
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
return postReadRepository.countByUser(user);
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,7 @@ public class PostService {
|
||||
private final ReactionRepository reactionRepository;
|
||||
private final PostSubscriptionRepository postSubscriptionRepository;
|
||||
private final NotificationRepository notificationRepository;
|
||||
private final PostReadService postReadService;
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
public PostService(PostRepository postRepository,
|
||||
@@ -52,6 +53,7 @@ public class PostService {
|
||||
ReactionRepository reactionRepository,
|
||||
PostSubscriptionRepository postSubscriptionRepository,
|
||||
NotificationRepository notificationRepository,
|
||||
PostReadService postReadService,
|
||||
@Value("${app.post.publish-mode:DIRECT}") PublishMode publishMode) {
|
||||
this.postRepository = postRepository;
|
||||
this.userRepository = userRepository;
|
||||
@@ -64,6 +66,7 @@ public class PostService {
|
||||
this.reactionRepository = reactionRepository;
|
||||
this.postSubscriptionRepository = postSubscriptionRepository;
|
||||
this.notificationRepository = notificationRepository;
|
||||
this.postReadService = postReadService;
|
||||
this.publishMode = publishMode;
|
||||
}
|
||||
|
||||
@@ -142,6 +145,9 @@ public class PostService {
|
||||
}
|
||||
post.setViews(post.getViews() + 1);
|
||||
post = postRepository.save(post);
|
||||
if (viewer != null) {
|
||||
postReadService.recordRead(viewer, id);
|
||||
}
|
||||
if (viewer != null && !viewer.equals(post.getAuthor().getUsername())) {
|
||||
User viewerUser = userRepository.findByUsername(viewer).orElse(null);
|
||||
if (viewerUser != null) {
|
||||
|
||||
@@ -87,4 +87,12 @@ public class ReactionService {
|
||||
public java.util.List<Long> topCommentIds(String username, int limit) {
|
||||
return reactionRepository.findTopCommentIds(username, org.springframework.data.domain.PageRequest.of(0, limit));
|
||||
}
|
||||
|
||||
public long countLikesSent(String username) {
|
||||
return reactionRepository.countLikesSent(username);
|
||||
}
|
||||
|
||||
public long countLikesReceived(String username) {
|
||||
return reactionRepository.countLikesReceived(username);
|
||||
}
|
||||
}
|
||||
|
||||
35
src/main/java/com/openisle/service/UserVisitService.java
Normal file
35
src/main/java/com/openisle/service/UserVisitService.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.model.UserVisit;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.repository.UserVisitRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserVisitService {
|
||||
private final UserVisitRepository userVisitRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public void recordVisit(String username) {
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
LocalDate today = LocalDate.now();
|
||||
userVisitRepository.findByUserAndVisitDate(user, today).orElseGet(() -> {
|
||||
UserVisit visit = new UserVisit();
|
||||
visit.setUser(user);
|
||||
visit.setVisitDate(today);
|
||||
return userVisitRepository.save(visit);
|
||||
});
|
||||
}
|
||||
|
||||
public long countVisits(String username) {
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
return userVisitRepository.countByUser(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user