-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
SinglePageFinderImpl.java
108 lines (95 loc) · 4.07 KB
/
SinglePageFinderImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package run.halo.app.theme.finders.impl;
import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.lang.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.content.SinglePageService;
import run.halo.app.core.extension.content.Post;
import run.halo.app.core.extension.content.SinglePage;
import run.halo.app.extension.ListResult;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.theme.finders.Finder;
import run.halo.app.theme.finders.SinglePageConversionService;
import run.halo.app.theme.finders.SinglePageFinder;
import run.halo.app.theme.finders.vo.ContentVo;
import run.halo.app.theme.finders.vo.ListedSinglePageVo;
import run.halo.app.theme.finders.vo.SinglePageVo;
/**
* A default implementation of {@link SinglePage}.
*
* @author guqing
* @since 2.0.0
*/
@Finder("singlePageFinder")
@AllArgsConstructor
public class SinglePageFinderImpl implements SinglePageFinder {
public static final Predicate<SinglePage> FIXED_PREDICATE = page -> page.isPublished()
&& Objects.equals(false, page.getSpec().getDeleted())
&& Post.VisibleEnum.PUBLIC.equals(page.getSpec().getVisible());
private final ReactiveExtensionClient client;
private final SinglePageConversionService singlePagePublicQueryService;
private final SinglePageService singlePageService;
@Override
public Mono<SinglePageVo> getByName(String pageName) {
return client.get(SinglePage.class, pageName)
.filter(FIXED_PREDICATE)
.flatMap(singlePagePublicQueryService::convertToVo);
}
@Override
public Mono<ContentVo> content(String pageName) {
return singlePageService.getReleaseContent(pageName)
.map(wrapper -> ContentVo.builder().content(wrapper.getContent())
.raw(wrapper.getRaw()).build());
}
@Override
public Mono<ListResult<ListedSinglePageVo>> list(Integer page, Integer size) {
return list(page, size, null, null);
}
@Override
public Mono<ListResult<ListedSinglePageVo>> list(@Nullable Integer page, @Nullable Integer size,
@Nullable Predicate<SinglePage> predicate, @Nullable Comparator<SinglePage> comparator) {
var predicateToUse = Optional.ofNullable(predicate)
.map(p -> p.and(FIXED_PREDICATE))
.orElse(FIXED_PREDICATE);
var comparatorToUse = Optional.ofNullable(comparator)
.orElse(defaultComparator());
return client.list(SinglePage.class, predicateToUse,
comparatorToUse, pageNullSafe(page), sizeNullSafe(size))
.flatMap(list -> Flux.fromStream(list.get())
.concatMap(singlePagePublicQueryService::convertToListedVo)
.collectList()
.map(pageVos -> new ListResult<>(list.getPage(), list.getSize(), list.getTotal(),
pageVos)
)
)
.defaultIfEmpty(new ListResult<>(0, 0, 0, List.of()));
}
static Comparator<SinglePage> defaultComparator() {
Function<SinglePage, Boolean> pinned =
page -> Objects.requireNonNullElse(page.getSpec().getPinned(), false);
Function<SinglePage, Integer> priority =
page -> Objects.requireNonNullElse(page.getSpec().getPriority(), 0);
Function<SinglePage, Instant> creationTimestamp =
page -> page.getMetadata().getCreationTimestamp();
Function<SinglePage, String> name = page -> page.getMetadata().getName();
return Comparator.comparing(pinned)
.thenComparing(priority)
.thenComparing(creationTimestamp)
.thenComparing(name)
.reversed();
}
int pageNullSafe(Integer page) {
return ObjectUtils.defaultIfNull(page, 1);
}
int sizeNullSafe(Integer size) {
return ObjectUtils.defaultIfNull(size, 10);
}
}