From 039d3f508d80037f9e1187c104f2c56b178d93b8 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Tue, 28 Feb 2023 17:30:16 +0800 Subject: [PATCH] fix: post page size configuration not work (#3413) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### 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? 修改文章设置中的大小,查看主题端分页是否符合预期: 包括首页,归档页,标签文章页,分类文章页。 image #### 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 ``` --- .../router/factories/ArchiveRouteFactory.java | 3 +- .../factories/AuthorPostsRouteFactory.java | 3 +- .../factories/CategoryPostRouteFactory.java | 3 +- .../router/factories/IndexRouteFactory.java | 3 +- .../theme/router/factories/RouteFactory.java | 7 ++- .../router/factories/TagPostRouteFactory.java | 3 +- .../router/factories/RouteFactoryTest.java | 59 +++++++++++++++++++ 7 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 src/test/java/run/halo/app/theme/router/factories/RouteFactoryTest.java diff --git a/src/main/java/run/halo/app/theme/router/factories/ArchiveRouteFactory.java b/src/main/java/run/halo/app/theme/router/factories/ArchiveRouteFactory.java index 62f4bf0d3..9b2583803 100644 --- a/src/main/java/run/halo/app/theme/router/factories/ArchiveRouteFactory.java +++ b/src/main/java/run/halo/app/theme/router/factories/ArchiveRouteFactory.java @@ -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() diff --git a/src/main/java/run/halo/app/theme/router/factories/AuthorPostsRouteFactory.java b/src/main/java/run/halo/app/theme/router/factories/AuthorPostsRouteFactory.java index 553c6b54d..555ae7718 100644 --- a/src/main/java/run/halo/app/theme/router/factories/AuthorPostsRouteFactory.java +++ b/src/main/java/run/halo/app/theme/router/factories/AuthorPostsRouteFactory.java @@ -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> 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() .listResult(list) diff --git a/src/main/java/run/halo/app/theme/router/factories/CategoryPostRouteFactory.java b/src/main/java/run/halo/app/theme/router/factories/CategoryPostRouteFactory.java index 871ab9879..f04d1332f 100644 --- a/src/main/java/run/halo/app/theme/router/factories/CategoryPostRouteFactory.java +++ b/src/main/java/run/halo/app/theme/router/factories/CategoryPostRouteFactory.java @@ -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() .listResult(list) diff --git a/src/main/java/run/halo/app/theme/router/factories/IndexRouteFactory.java b/src/main/java/run/halo/app/theme/router/factories/IndexRouteFactory.java index 125d878b2..94b653f49 100644 --- a/src/main/java/run/halo/app/theme/router/factories/IndexRouteFactory.java +++ b/src/main/java/run/halo/app/theme/router/factories/IndexRouteFactory.java @@ -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> 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() .listResult(list) diff --git a/src/main/java/run/halo/app/theme/router/factories/RouteFactory.java b/src/main/java/run/halo/app/theme/router/factories/RouteFactory.java index 94c03a9cb..bd48ec811 100644 --- a/src/main/java/run/halo/app/theme/router/factories/RouteFactory.java +++ b/src/main/java/run/halo/app/theme/router/factories/RouteFactory.java @@ -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 create(String pattern); default Mono configuredPageSize( - SystemConfigurableEnvironmentFetcher environmentFetcher) { + SystemConfigurableEnvironmentFetcher environmentFetcher, + Function 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) { diff --git a/src/main/java/run/halo/app/theme/router/factories/TagPostRouteFactory.java b/src/main/java/run/halo/app/theme/router/factories/TagPostRouteFactory.java index 15d5eb8d7..097ecc413 100644 --- a/src/main/java/run/halo/app/theme/router/factories/TagPostRouteFactory.java +++ b/src/main/java/run/halo/app/theme/router/factories/TagPostRouteFactory.java @@ -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> 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() .listResult(list) diff --git a/src/test/java/run/halo/app/theme/router/factories/RouteFactoryTest.java b/src/test/java/run/halo/app/theme/router/factories/RouteFactoryTest.java new file mode 100644 index 000000000..b97af8598 --- /dev/null +++ b/src/test/java/run/halo/app/theme/router/factories/RouteFactoryTest.java @@ -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 create(String pattern) { + return null; + } + } +} \ No newline at end of file