fix: post page size configuration not work (#3413)

#### What type of PR is this?
/kind bug
/area core
/milestone 2.3.x
#### What this PR does / why we need it:
修复文章列表分页配置取值错误的问题
此问题为 #3300 PR 引入,由于改动较大遗漏了这点没测试到。

how to test it?
修改文章设置中的大小,查看主题端分页是否符合预期:
包括首页,归档页,标签文章页,分类文章页。
<img width="575" alt="image" src="https://user-images.githubusercontent.com/38999863/221574440-eeec9c27-57ab-46d1-823f-7cc076fb2b54.png">

#### Which issue(s) this PR fixes:
Fixes #3411

#### Special notes for your reviewer:
/cc @halo-dev/sig-halo 
#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/3390/head^2
guqing 2023-02-28 17:30:16 +08:00 committed by GitHub
parent 79e705123e
commit 039d3f508d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 7 deletions

View File

@ -19,6 +19,7 @@ import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.utils.JsonUtils;
import run.halo.app.infra.utils.PathUtils;
import run.halo.app.theme.DefaultTemplateEnum;
@ -78,7 +79,7 @@ public class ArchiveRouteFactory implements RouteFactory {
ArchivePathVariables variables = ArchivePathVariables.from(request);
int pageNum = pageNumInPathVariable(request);
String requestPath = request.path();
return configuredPageSize(environmentFetcher)
return configuredPageSize(environmentFetcher, SystemSetting.Post::getArchivePageSize)
.flatMap(pageSize -> postFinder.archives(pageNum, pageSize, variables.getYear(),
variables.getMonth()))
.map(list -> new UrlContextListResult.Builder<PostArchiveVo>()

View File

@ -17,6 +17,7 @@ import reactor.core.publisher.Mono;
import run.halo.app.core.extension.User;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.theme.DefaultTemplateEnum;
import run.halo.app.theme.finders.PostFinder;
import run.halo.app.theme.finders.vo.ListedPostVo;
@ -62,7 +63,7 @@ public class AuthorPostsRouteFactory implements RouteFactory {
private Mono<UrlContextListResult<ListedPostVo>> postList(ServerRequest request, String name) {
String path = request.path();
int pageNum = pageNumInPathVariable(request);
return configuredPageSize(environmentFetcher)
return configuredPageSize(environmentFetcher, SystemSetting.Post::getPostPageSize)
.flatMap(pageSize -> postFinder.listByOwner(pageNum, pageSize, name))
.map(list -> new UrlContextListResult.Builder<ListedPostVo>()
.listResult(list)

View File

@ -18,6 +18,7 @@ import reactor.core.publisher.Mono;
import run.halo.app.core.extension.content.Category;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.exception.NotFoundException;
import run.halo.app.infra.utils.PathUtils;
import run.halo.app.theme.DefaultTemplateEnum;
@ -83,7 +84,7 @@ public class CategoryPostRouteFactory implements RouteFactory {
ServerRequest request) {
String path = request.path();
int pageNum = pageNumInPathVariable(request);
return configuredPageSize(environmentFetcher)
return configuredPageSize(environmentFetcher, SystemSetting.Post::getCategoryPageSize)
.flatMap(pageSize -> postFinder.listByCategory(pageNum, pageSize, name))
.map(list -> new UrlContextListResult.Builder<ListedPostVo>()
.listResult(list)

View File

@ -15,6 +15,7 @@ import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.theme.DefaultTemplateEnum;
import run.halo.app.theme.finders.PostFinder;
import run.halo.app.theme.finders.vo.ListedPostVo;
@ -52,7 +53,7 @@ public class IndexRouteFactory implements RouteFactory {
private Mono<UrlContextListResult<ListedPostVo>> postList(ServerRequest request) {
String path = request.path();
return configuredPageSize(environmentFetcher)
return configuredPageSize(environmentFetcher, SystemSetting.Post::getPostPageSize)
.flatMap(pageSize -> postFinder.list(pageNumInPathVariable(request), pageSize))
.map(list -> new UrlContextListResult.Builder<ListedPostVo>()
.listResult(list)

View File

@ -2,12 +2,14 @@ package run.halo.app.theme.router.factories;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import java.util.function.Function;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
/**
* @author guqing
@ -17,9 +19,10 @@ public interface RouteFactory {
RouterFunction<ServerResponse> create(String pattern);
default Mono<Integer> configuredPageSize(
SystemConfigurableEnvironmentFetcher environmentFetcher) {
SystemConfigurableEnvironmentFetcher environmentFetcher,
Function<SystemSetting.Post, Integer> mapper) {
return environmentFetcher.fetchPost()
.map(p -> defaultIfNull(p.getTagPageSize(), ModelConst.DEFAULT_PAGE_SIZE));
.map(p -> defaultIfNull(mapper.apply(p), ModelConst.DEFAULT_PAGE_SIZE));
}
default int pageNumInPathVariable(ServerRequest request) {

View File

@ -16,6 +16,7 @@ import reactor.core.publisher.Mono;
import run.halo.app.core.extension.content.Tag;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.exception.NotFoundException;
import run.halo.app.infra.utils.PathUtils;
import run.halo.app.theme.DefaultTemplateEnum;
@ -67,7 +68,7 @@ public class TagPostRouteFactory implements RouteFactory {
private Mono<UrlContextListResult<ListedPostVo>> postList(String name, Integer page,
String requestPath) {
return configuredPageSize(environmentFetcher)
return configuredPageSize(environmentFetcher, SystemSetting.Post::getTagPageSize)
.flatMap(pageSize -> postFinder.listByTag(page, pageSize, name))
.map(list -> new UrlContextListResult.Builder<ListedPostVo>()
.listResult(list)

View File

@ -0,0 +1,59 @@
package run.halo.app.theme.router.factories;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import run.halo.app.infra.SystemSetting;
/**
* Tests for {@link RouteFactory}.
*
* @author guqing
* @since 2.3.0
*/
@ExtendWith(MockitoExtension.class)
class RouteFactoryTest extends RouteFactoryTestSuite {
@Test
void configuredPageSize() {
SystemSetting.Post post = new SystemSetting.Post();
post.setPostPageSize(1);
post.setArchivePageSize(2);
post.setCategoryPageSize(3);
post.setTagPageSize(null);
when(environmentFetcher.fetchPost()).thenReturn(Mono.just(post));
TestRouteFactory routeFactory = new TestRouteFactory();
assertThat(
routeFactory.configuredPageSize(environmentFetcher, SystemSetting.Post::getTagPageSize)
.block()).isEqualTo(ModelConst.DEFAULT_PAGE_SIZE);
assertThat(
routeFactory.configuredPageSize(environmentFetcher, SystemSetting.Post::getPostPageSize)
.block()).isEqualTo(post.getPostPageSize());
assertThat(
routeFactory.configuredPageSize(environmentFetcher,
SystemSetting.Post::getCategoryPageSize).block())
.isEqualTo(post.getCategoryPageSize());
assertThat(
routeFactory.configuredPageSize(environmentFetcher,
SystemSetting.Post::getArchivePageSize).block())
.isEqualTo(post.getArchivePageSize());
}
static class TestRouteFactory implements RouteFactory {
@Override
public RouterFunction<ServerResponse> create(String pattern) {
return null;
}
}
}