From 55cb21ccaa27331f738f8bdf28d6e5c2ca2965b9 Mon Sep 17 00:00:00 2001
From: guqing <38999863+guqing@users.noreply.github.com>
Date: Wed, 28 Jun 2023 23:48:11 +0800
Subject: [PATCH] feat: support for obtaining the newest comments on theme-side
(#4104)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
#### What type of PR is this?
/kind feature
/milestone 2.7.x
/area core
/area theme
#### What this PR does / why we need it:
主题端支持获取最新评论
可能存在的问题:
主题端如果想展示评论所属的具体的主体比如 Moment 可能不好展示
how to test it?
通过 list 方法获取评论看排序和数据是否正确
```html
->
->
```
#### Which issue(s) this PR fixes:
Fixes #4088
#### Does this PR introduce a user-facing change?
```release-note
主题端支持获取最新评论
```
---
.editorconfig | 2 +-
.../app/core/extension/content/Comment.java | 1 +
.../halo/app/theme/finders/CommentFinder.java | 2 +-
.../impl/CommentPublicQueryServiceImpl.java | 28 +++++++++++--------
.../halo/app/theme/finders/vo/CommentVo.java | 10 ++++---
5 files changed, 26 insertions(+), 17 deletions(-)
diff --git a/.editorconfig b/.editorconfig
index 3f5767e239..d21cc9b75e 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -9,7 +9,7 @@ tab_width = 4
ij_continuation_indent_size = 8
ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
-ij_formatter_tags_enabled = false
+ij_formatter_tags_enabled = true
ij_smart_tabs = false
ij_wrap_on_typing = false
diff --git a/api/src/main/java/run/halo/app/core/extension/content/Comment.java b/api/src/main/java/run/halo/app/core/extension/content/Comment.java
index 068ca55fa8..5684826c73 100644
--- a/api/src/main/java/run/halo/app/core/extension/content/Comment.java
+++ b/api/src/main/java/run/halo/app/core/extension/content/Comment.java
@@ -45,6 +45,7 @@ public CommentStatus getStatusOrDefault() {
}
@Data
+ @ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public static class CommentSpec extends BaseCommentSpec {
diff --git a/application/src/main/java/run/halo/app/theme/finders/CommentFinder.java b/application/src/main/java/run/halo/app/theme/finders/CommentFinder.java
index 2a14fe6fe3..a8cb0a2107 100644
--- a/application/src/main/java/run/halo/app/theme/finders/CommentFinder.java
+++ b/application/src/main/java/run/halo/app/theme/finders/CommentFinder.java
@@ -18,7 +18,7 @@ public interface CommentFinder {
Mono getByName(String name);
- Mono> list(Ref ref, @Nullable Integer page,
+ Mono> list(@Nullable Ref ref, @Nullable Integer page,
@Nullable Integer size);
Mono> listReply(String commentName, @Nullable Integer page,
diff --git a/application/src/main/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceImpl.java b/application/src/main/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceImpl.java
index 634c5dcd57..21c56609a2 100644
--- a/application/src/main/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceImpl.java
+++ b/application/src/main/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceImpl.java
@@ -11,6 +11,7 @@
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
+import org.springframework.lang.Nullable;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Component;
@@ -44,6 +45,7 @@
@Component
@RequiredArgsConstructor
public class CommentPublicQueryServiceImpl implements CommentPublicQueryService {
+ private static final int DEFAULT_SIZE = 10;
private final ReactiveExtensionClient client;
private final UserService userService;
@@ -117,7 +119,7 @@ Mono toCommentVo(Comment comment) {
.doOnNext(commentVo::setOwner)
.thenReturn(commentVo)
)
- .flatMap(commentVo -> filterCommentSensitiveData(commentVo));
+ .flatMap(this::filterCommentSensitiveData);
}
private Mono extends CommentVo> filterCommentSensitiveData(CommentVo commentVo) {
@@ -138,8 +140,9 @@ private Mono extends CommentVo> filterCommentSensitiveData(CommentVo commentVo
return Mono.just(commentVo);
}
- private Mono
- populateStats(Class clazz, T vo) {
+ // @formatter:off
+ private
+ Mono populateStats(Class clazz, T vo) {
return counterService.getByName(MeterUtils.nameOf(clazz, vo.getMetadata()
.getName()))
.map(counter -> CommentStatsVo.builder()
@@ -148,6 +151,7 @@ private Mono extends CommentVo> filterCommentSensitiveData(CommentVo commentVo
)
.defaultIfEmpty(CommentStatsVo.empty());
}
+ // @formatter:on
Mono toReplyVo(Reply reply) {
return Mono.just(ReplyVo.from(reply))
@@ -158,7 +162,7 @@ Mono toReplyVo(Reply reply) {
.doOnNext(replyVo::setOwner)
.thenReturn(replyVo)
)
- .flatMap(replyVo -> filterReplySensitiveData(replyVo));
+ .flatMap(this::filterReplySensitiveData);
}
private Mono extends ReplyVo> filterReplySensitiveData(ReplyVo replyVo) {
@@ -187,11 +191,13 @@ private Mono getOwnerInfo(Comment.CommentOwner owner) {
.map(OwnerInfo::from);
}
- private Mono> fixedCommentPredicate(Ref ref) {
- Assert.notNull(ref, "Comment subject reference must not be null");
- // Ref must be equal to the comment subject
- Predicate refPredicate = comment -> comment.getSpec().getSubjectRef().equals(ref)
- && comment.getMetadata().getDeletionTimestamp() == null;
+ private Mono> fixedCommentPredicate(@Nullable Ref ref) {
+ Predicate basePredicate =
+ comment -> comment.getMetadata().getDeletionTimestamp() == null;
+ if (ref != null) {
+ basePredicate = basePredicate
+ .and(comment -> comment.getSpec().getSubjectRef().equals(ref));
+ }
// is approved and not hidden
Predicate approvedPredicate =
@@ -206,7 +212,7 @@ private Mono> fixedCommentPredicate(Ref ref) {
return approvedPredicate.or(isOwner);
})
.defaultIfEmpty(approvedPredicate)
- .map(refPredicate::and);
+ .map(basePredicate::and);
}
private Mono> fixedReplyPredicate(String commentName) {
@@ -277,6 +283,6 @@ int pageNullSafe(Integer page) {
}
int sizeNullSafe(Integer size) {
- return ObjectUtils.defaultIfNull(size, 10);
+ return ObjectUtils.defaultIfNull(size, DEFAULT_SIZE);
}
}
diff --git a/application/src/main/java/run/halo/app/theme/finders/vo/CommentVo.java b/application/src/main/java/run/halo/app/theme/finders/vo/CommentVo.java
index 1f25159557..a6725b3c07 100644
--- a/application/src/main/java/run/halo/app/theme/finders/vo/CommentVo.java
+++ b/application/src/main/java/run/halo/app/theme/finders/vo/CommentVo.java
@@ -1,5 +1,7 @@
package run.halo.app.theme.finders.vo;
+import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
+
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;
@@ -19,18 +21,18 @@
@EqualsAndHashCode
public class CommentVo implements ExtensionVoOperator {
- @Schema(required = true)
+ @Schema(requiredMode = REQUIRED)
private MetadataOperator metadata;
- @Schema(required = true)
+ @Schema(requiredMode = REQUIRED)
private Comment.CommentSpec spec;
private Comment.CommentStatus status;
- @Schema(required = true)
+ @Schema(requiredMode = REQUIRED)
private OwnerInfo owner;
- @Schema(required = true)
+ @Schema(requiredMode = REQUIRED)
private CommentStatsVo stats;
/**