-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
PostFinderImpl.java
290 lines (259 loc) · 10.7 KB
/
PostFinderImpl.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package run.halo.app.theme.finders.impl;
import static run.halo.app.theme.finders.PostPublicQueryService.FIXED_PREDICATE;
import java.time.Instant;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.util.comparator.Comparators;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.content.Post;
import run.halo.app.extension.ListResult;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.exception.ExtensionNotFoundException;
import run.halo.app.infra.utils.HaloUtils;
import run.halo.app.theme.finders.Finder;
import run.halo.app.theme.finders.PostFinder;
import run.halo.app.theme.finders.PostPublicQueryService;
import run.halo.app.theme.finders.vo.ContentVo;
import run.halo.app.theme.finders.vo.ListedPostVo;
import run.halo.app.theme.finders.vo.NavigationPostVo;
import run.halo.app.theme.finders.vo.PostArchiveVo;
import run.halo.app.theme.finders.vo.PostArchiveYearMonthVo;
import run.halo.app.theme.finders.vo.PostVo;
/**
* A finder for {@link Post}.
*
* @author guqing
* @since 2.0.0
*/
@Finder("postFinder")
@AllArgsConstructor
public class PostFinderImpl implements PostFinder {
private final ReactiveExtensionClient client;
private final PostPublicQueryService postPublicQueryService;
@Override
public Mono<PostVo> getByName(String postName) {
return client.get(Post.class, postName)
.filter(FIXED_PREDICATE)
.flatMap(post -> postPublicQueryService.convertToVo(post,
post.getSpec().getReleaseSnapshot())
);
}
@Override
public Mono<ContentVo> content(String postName) {
return postPublicQueryService.getContent(postName);
}
@Override
public Mono<NavigationPostVo> cursor(String currentName) {
// TODO Optimize the post names query here
return client.list(Post.class, FIXED_PREDICATE, defaultComparator())
.map(post -> post.getMetadata().getName())
.collectList()
.flatMap(postNames -> Mono.just(NavigationPostVo.builder())
.flatMap(builder -> getByName(currentName)
.doOnNext(builder::current)
.thenReturn(builder)
)
.flatMap(builder -> {
Pair<String, String> previousNextPair =
postPreviousNextPair(postNames, currentName);
String previousPostName = previousNextPair.getLeft();
String nextPostName = previousNextPair.getRight();
return fetchByName(previousPostName)
.doOnNext(builder::previous)
.then(fetchByName(nextPostName))
.doOnNext(builder::next)
.thenReturn(builder);
})
.map(NavigationPostVo.NavigationPostVoBuilder::build))
.defaultIfEmpty(NavigationPostVo.empty());
}
private Mono<PostVo> fetchByName(String name) {
if (StringUtils.isBlank(name)) {
return Mono.empty();
}
return getByName(name)
.onErrorResume(ExtensionNotFoundException.class::isInstance, (error) -> Mono.empty());
}
@Override
public Flux<ListedPostVo> listAll() {
return client.list(Post.class, FIXED_PREDICATE, defaultComparator())
.concatMap(postPublicQueryService::convertToListedVo);
}
static Pair<String, String> postPreviousNextPair(List<String> postNames,
String currentName) {
FixedSizeSlidingWindow<String> window = new FixedSizeSlidingWindow<>(3);
for (String postName : postNames) {
window.add(postName);
if (!window.isFull()) {
continue;
}
int index = window.indexOf(currentName);
if (index == -1) {
continue;
}
// got expected window
if (index < 2) {
break;
}
}
List<String> elements = window.elements();
// current post index
int index = elements.indexOf(currentName);
String previousPostName = null;
if (index > 0) {
previousPostName = elements.get(index - 1);
}
String nextPostName = null;
if (elements.size() - 1 > index) {
nextPostName = elements.get(index + 1);
}
return Pair.of(previousPostName, nextPostName);
}
static class FixedSizeSlidingWindow<T> {
Deque<T> queue;
int size;
public FixedSizeSlidingWindow(int size) {
this.size = size;
// FIFO
queue = new ArrayDeque<>(size);
}
/**
* Add element to the window.
* The element added first will be deleted when the element in the collection exceeds
* {@code size}.
*/
public void add(T t) {
if (queue.size() == size) {
// remove first
queue.poll();
}
// add to last
queue.add(t);
}
public int indexOf(T o) {
List<T> elements = elements();
return elements.indexOf(o);
}
public List<T> elements() {
return new ArrayList<>(queue);
}
public boolean isFull() {
return queue.size() == size;
}
}
@Override
public Mono<ListResult<ListedPostVo>> list(Integer page, Integer size) {
return postPublicQueryService.list(page, size, null, defaultComparator());
}
@Override
public Mono<ListResult<ListedPostVo>> listByCategory(Integer page, Integer size,
String categoryName) {
return postPublicQueryService.list(page, size,
post -> contains(post.getSpec().getCategories(), categoryName), defaultComparator());
}
@Override
public Mono<ListResult<ListedPostVo>> listByTag(Integer page, Integer size, String tag) {
return postPublicQueryService.list(page, size,
post -> contains(post.getSpec().getTags(), tag), defaultComparator());
}
@Override
public Mono<ListResult<ListedPostVo>> listByOwner(Integer page, Integer size, String owner) {
return postPublicQueryService.list(page, size,
post -> post.getSpec().getOwner().equals(owner), defaultComparator());
}
@Override
public Mono<ListResult<PostArchiveVo>> archives(Integer page, Integer size) {
return archives(page, size, null, null);
}
@Override
public Mono<ListResult<PostArchiveVo>> archives(Integer page, Integer size, String year) {
return archives(page, size, year, null);
}
@Override
public Mono<ListResult<PostArchiveVo>> archives(Integer page, Integer size, String year,
String month) {
return postPublicQueryService.list(page, size, post -> {
Map<String, String> labels = post.getMetadata().getLabels();
if (labels == null) {
return false;
}
boolean yearMatch = StringUtils.isBlank(year)
|| year.equals(labels.get(Post.ARCHIVE_YEAR_LABEL));
boolean monthMatch = StringUtils.isBlank(month)
|| month.equals(labels.get(Post.ARCHIVE_MONTH_LABEL));
return yearMatch && monthMatch;
}, archiveComparator())
.map(list -> {
Map<String, List<ListedPostVo>> yearPosts = list.get()
.collect(Collectors.groupingBy(
post -> HaloUtils.getYearText(post.getSpec().getPublishTime())));
List<PostArchiveVo> postArchives =
yearPosts.entrySet().stream().map(entry -> {
String key = entry.getKey();
// archives by month
Map<String, List<ListedPostVo>> monthPosts = entry.getValue().stream()
.collect(Collectors.groupingBy(
post -> HaloUtils.getMonthText(post.getSpec().getPublishTime())));
// convert to archive year month value objects
List<PostArchiveYearMonthVo> monthArchives = monthPosts.entrySet()
.stream()
.map(monthEntry -> PostArchiveYearMonthVo.builder()
.posts(monthEntry.getValue())
.month(monthEntry.getKey())
.build()
)
.sorted(
Comparator.comparing(PostArchiveYearMonthVo::getMonth).reversed())
.toList();
return PostArchiveVo.builder()
.year(String.valueOf(key))
.months(monthArchives)
.build();
})
.sorted(Comparator.comparing(PostArchiveVo::getYear).reversed())
.toList();
return new ListResult<>(list.getPage(), list.getSize(), list.getTotal(),
postArchives);
})
.defaultIfEmpty(new ListResult<>(page, size, 0, List.of()));
}
private boolean contains(List<String> c, String key) {
if (StringUtils.isBlank(key) || c == null) {
return false;
}
return c.contains(key);
}
static Comparator<Post> defaultComparator() {
Function<Post, Boolean> pinned =
post -> Objects.requireNonNullElse(post.getSpec().getPinned(), false);
Function<Post, Integer> priority =
post -> Objects.requireNonNullElse(post.getSpec().getPriority(), 0);
Function<Post, Instant> publishTime =
post -> post.getSpec().getPublishTime();
Function<Post, String> name = post -> post.getMetadata().getName();
return Comparator.comparing(pinned)
.thenComparing(priority)
.thenComparing(publishTime, Comparators.nullsLow())
.thenComparing(name)
.reversed();
}
static Comparator<Post> archiveComparator() {
Function<Post, Instant> publishTime =
post -> post.getSpec().getPublishTime();
Function<Post, String> name = post -> post.getMetadata().getName();
return Comparator.comparing(publishTime, Comparators.nullsLow())
.thenComparing(name)
.reversed();
}
}