Skip to content

Commit

Permalink
feat: add support for fetching replies alongside comment list (#5505)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind feature
/area core
/milestone 2.14.x
/kind api-change

#### What this PR does / why we need it:
主题端评论列表支持同时获得评论数据

Resolves #5435

#### Does this PR introduce a user-facing change?
```release-note
主题端评论列表支持同时获得评论数据
```
  • Loading branch information
guqing committed Mar 16, 2024
1 parent 7c3f8b9 commit 5e073bf
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.fn.builders.schema.Builder;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -55,6 +54,7 @@
import run.halo.app.theme.finders.CommentFinder;
import run.halo.app.theme.finders.CommentPublicQueryService;
import run.halo.app.theme.finders.vo.CommentVo;
import run.halo.app.theme.finders.vo.CommentWithReplyVo;
import run.halo.app.theme.finders.vo.ReplyVo;

/**
Expand All @@ -69,7 +69,6 @@ public class CommentFinderEndpoint implements CustomEndpoint {
private final ReplyService replyService;
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
private final RateLimiterRegistry rateLimiterRegistry;
private final MessageSource messageSource;

@Override
public RouterFunction<ServerResponse> endpoint() {
Expand Down Expand Up @@ -112,7 +111,7 @@ public RouterFunction<ServerResponse> endpoint() {
.description("List comments.")
.tag(tag)
.response(responseBuilder()
.implementation(ListResult.generateGenericClass(CommentVo.class))
.implementation(ListResult.generateGenericClass(CommentWithReplyVo.class))
);
QueryParamBuildUtil.buildParametersFromType(builder, CommentQuery.class);
})
Expand Down Expand Up @@ -212,6 +211,13 @@ private boolean checkReplyOwner(Reply reply, Boolean onlySystemUser) {
Mono<ServerResponse> listComments(ServerRequest request) {
CommentQuery commentQuery = new CommentQuery(request);
return commentPublicQueryService.list(commentQuery.toRef(), commentQuery.toPageRequest())
.flatMap(result -> {
if (commentQuery.getWithReplies()) {
return commentPublicQueryService.convertToWithReplyVo(result,
commentQuery.getReplySize());
}
return Mono.just(result);
})
.flatMap(list -> ServerResponse.ok().bodyValue(list));
}

Expand Down Expand Up @@ -278,6 +284,20 @@ public String getName() {
return name;
}

@Schema(description = "Whether to include replies. Default is false.",
defaultValue = "false")
public Boolean getWithReplies() {
var withReplies = queryParams.getFirst("withReplies");
return StringUtils.isNotBlank(withReplies) && Boolean.parseBoolean(withReplies);
}

@Schema(description = "Reply size of the comment, default is 10, only works when "
+ "withReplies is true.", defaultValue = "10")
public int getReplySize() {
var replySize = queryParams.getFirst("replySize");
return StringUtils.isNotBlank(replySize) ? Integer.parseInt(replySize) : 10;
}

@ArraySchema(uniqueItems = true,
arraySchema = @Schema(name = "sort",
description = "Sort property and direction of the list result. Supported fields: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import run.halo.app.extension.PageRequest;
import run.halo.app.extension.Ref;
import run.halo.app.theme.finders.vo.CommentVo;
import run.halo.app.theme.finders.vo.CommentWithReplyVo;
import run.halo.app.theme.finders.vo.ReplyVo;

/**
Expand All @@ -21,6 +22,9 @@ Mono<ListResult<CommentVo>> list(Ref ref, @Nullable Integer page,

Mono<ListResult<CommentVo>> list(Ref ref, @Nullable PageRequest pageRequest);

Mono<ListResult<CommentWithReplyVo>> convertToWithReplyVo(ListResult<CommentVo> comments,
int replySize);

Mono<ListResult<ReplyVo>> listReply(String commentName, @Nullable Integer page,
@Nullable Integer size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import run.halo.app.theme.finders.CommentPublicQueryService;
import run.halo.app.theme.finders.vo.CommentStatsVo;
import run.halo.app.theme.finders.vo.CommentVo;
import run.halo.app.theme.finders.vo.CommentWithReplyVo;
import run.halo.app.theme.finders.vo.ExtensionVoOperator;
import run.halo.app.theme.finders.vo.ReplyVo;

Expand Down Expand Up @@ -94,6 +95,26 @@ public Mono<ListResult<CommentVo>> list(Ref ref, PageRequest pageParam) {
.defaultIfEmpty(ListResult.emptyResult());
}

@Override
public Mono<ListResult<CommentWithReplyVo>> convertToWithReplyVo(ListResult<CommentVo> comments,
int replySize) {
return Flux.fromIterable(comments.getItems())
.flatMap(commentVo -> {
var commentName = commentVo.getMetadata().getName();
return listReply(commentName, 1, replySize)
.map(replyList -> CommentWithReplyVo.from(commentVo)
.setReplies(replyList)
);
})
.collectList()
.map(result -> new ListResult<>(
comments.getPage(),
comments.getSize(),
comments.getTotal(),
result)
);
}

@Override
public Mono<ListResult<ReplyVo>> listReply(String commentName, Integer page, Integer size) {
return listReply(commentName, PageRequestImpl.of(pageNullSafe(page), sizeNullSafe(size),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
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;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import run.halo.app.content.comment.OwnerInfo;
import run.halo.app.core.extension.content.Comment;
import run.halo.app.extension.MetadataOperator;
Expand All @@ -17,7 +17,7 @@
* @since 2.0.0
*/
@Data
@Builder
@Accessors(chain = true)
@EqualsAndHashCode
public class CommentVo implements ExtensionVoOperator {

Expand All @@ -42,10 +42,9 @@ public class CommentVo implements ExtensionVoOperator {
* @return a value object for {@link Comment}
*/
public static CommentVo from(Comment comment) {
return CommentVo.builder()
.metadata(comment.getMetadata())
.spec(comment.getSpec())
.status(comment.getStatus())
.build();
return new CommentVo()
.setMetadata(comment.getMetadata())
.setSpec(comment.getSpec())
.setStatus(comment.getStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package run.halo.app.theme.finders.vo;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import run.halo.app.extension.ListResult;

/**
* <p>A value object for comment with reply.</p>
*
* @author guqing
* @since 2.14.0
*/
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class CommentWithReplyVo extends CommentVo {

private ListResult<ReplyVo> replies;

/**
* Convert {@link CommentVo} to {@link CommentWithReplyVo}.
*/
public static CommentWithReplyVo from(CommentVo commentVo) {
var commentWithReply = new CommentWithReplyVo();
BeanUtils.copyProperties(commentVo, commentWithReply);
commentWithReply.setReplies(ListResult.emptyResult());
return commentWithReply;
}
}

0 comments on commit 5e073bf

Please sign in to comment.