optimize(backend): optimize /api/posts/latest-reply

resolves #554
This commit is contained in:
netcat
2025-08-14 17:53:01 +08:00
parent 53c603f33a
commit 1e87e9252d
5 changed files with 64 additions and 2 deletions

View File

@@ -58,6 +58,11 @@ public class CommentService {
comment.setContent(content);
comment = commentRepository.save(comment);
log.debug("Comment {} saved for post {}", comment.getId(), postId);
post.setCommentCount(post.getCommentCount() + 1);
post.setLastReplyAt(LocalDateTime.now());
postRepository.save(post);
imageUploader.addReferences(imageUploader.extractUrls(content));
if (!author.getId().equals(post.getAuthor().getId())) {
notificationService.createNotification(post.getAuthor(), NotificationType.COMMENT_REPLY, post, comment, null, null, null, null);
@@ -101,6 +106,13 @@ public class CommentService {
comment.setContent(content);
comment = commentRepository.save(comment);
log.debug("Reply {} saved for parent {}", comment.getId(), parentId);
Post post = postRepository.findById(parent.getPost().getId())
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
post.setCommentCount(post.getCommentCount() + 1);
post.setLastReplyAt(LocalDateTime.now());
postRepository.save(post);
imageUploader.addReferences(imageUploader.extractUrls(content));
if (!author.getId().equals(parent.getAuthor().getId())) {
notificationService.createNotification(parent.getAuthor(), NotificationType.COMMENT_REPLY, parent.getPost(), comment, null, null, null, null);

View File

@@ -612,7 +612,7 @@ public class PostService {
.sorted(java.util.Comparator
.comparing(Post::getPinnedAt, java.util.Comparator.nullsLast(java.util.Comparator.reverseOrder()))
.thenComparing(p -> {
java.time.LocalDateTime t = commentRepository.findLastCommentTime(p);
java.time.LocalDateTime t = p.getLastReplyAt();
return t != null ? t : p.getCreatedAt();
}, java.util.Comparator.nullsLast(java.util.Comparator.reverseOrder())))
.toList();