-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
CommentPublicQueryServiceImpl.java
296 lines (264 loc) · 11.5 KB
/
CommentPublicQueryServiceImpl.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
291
292
293
294
295
296
package run.halo.app.theme.finders.impl;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import java.security.Principal;
import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.comparator.Comparators;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.content.comment.OwnerInfo;
import run.halo.app.content.comment.ReplyService;
import run.halo.app.core.extension.content.Comment;
import run.halo.app.core.extension.content.Reply;
import run.halo.app.core.extension.service.UserService;
import run.halo.app.extension.AbstractExtension;
import run.halo.app.extension.ListResult;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.Ref;
import run.halo.app.infra.AnonymousUserConst;
import run.halo.app.metrics.CounterService;
import run.halo.app.metrics.MeterUtils;
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.ExtensionVoOperator;
import run.halo.app.theme.finders.vo.ReplyVo;
/**
* comment public query service implementation.
*
* @author LIlGG
*/
@Component
@RequiredArgsConstructor
public class CommentPublicQueryServiceImpl implements CommentPublicQueryService {
private static final int DEFAULT_SIZE = 10;
private final ReactiveExtensionClient client;
private final UserService userService;
private final CounterService counterService;
@Override
public Mono<CommentVo> getByName(String name) {
return client.fetch(Comment.class, name)
.flatMap(this::toCommentVo);
}
@Override
public Mono<ListResult<CommentVo>> list(Ref ref, Integer page, Integer size) {
return list(ref, page, size, defaultComparator());
}
@Override
public Mono<ListResult<CommentVo>> list(Ref ref, Integer page, Integer size,
Comparator<Comment> comparator) {
final Comparator<Comment> commentComparator =
Objects.isNull(comparator) ? defaultComparator()
: comparator.thenComparing(defaultComparator());
return fixedCommentPredicate(ref)
.flatMap(fixedPredicate ->
client.list(Comment.class, fixedPredicate,
commentComparator,
pageNullSafe(page), sizeNullSafe(size))
.flatMap(list -> Flux.fromStream(list.get().map(this::toCommentVo))
.concatMap(Function.identity())
.collectList()
.map(commentVos -> new ListResult<>(list.getPage(), list.getSize(),
list.getTotal(),
commentVos)
)
)
.defaultIfEmpty(new ListResult<>(page, size, 0L, List.of()))
);
}
@Override
public Mono<ListResult<ReplyVo>> listReply(String commentName, Integer page, Integer size) {
return listReply(commentName, page, size, ReplyService.creationTimeAscComparator());
}
@Override
public Mono<ListResult<ReplyVo>> listReply(String commentName, Integer page, Integer size,
Comparator<Reply> comparator) {
return fixedReplyPredicate(commentName)
.flatMap(fixedPredicate ->
client.list(Reply.class, fixedPredicate,
comparator,
pageNullSafe(page), sizeNullSafe(size))
.flatMap(list -> Flux.fromStream(list.get().map(this::toReplyVo))
.concatMap(Function.identity())
.collectList()
.map(replyVos -> new ListResult<>(list.getPage(), list.getSize(),
list.getTotal(),
replyVos))
)
.defaultIfEmpty(new ListResult<>(page, size, 0L, List.of()))
);
}
Mono<CommentVo> toCommentVo(Comment comment) {
Comment.CommentOwner owner = comment.getSpec().getOwner();
return Mono.just(CommentVo.from(comment))
.flatMap(commentVo -> populateStats(Comment.class, commentVo)
.doOnNext(commentVo::setStats)
.thenReturn(commentVo))
.flatMap(commentVo -> getOwnerInfo(owner)
.doOnNext(commentVo::setOwner)
.thenReturn(commentVo)
)
.flatMap(this::filterCommentSensitiveData);
}
private Mono<? extends CommentVo> filterCommentSensitiveData(CommentVo commentVo) {
var owner = commentVo.getOwner();
commentVo.setOwner(OwnerInfo
.builder()
.displayName(owner.getDisplayName())
.avatar(owner.getAvatar())
.kind(owner.getKind())
.build());
commentVo.getSpec().setIpAddress("");
var specOwner = commentVo.getSpec().getOwner();
specOwner.setName("");
if (specOwner.getAnnotations() != null) {
specOwner.getAnnotations().remove("Email");
}
return Mono.just(commentVo);
}
// @formatter:off
private <E extends AbstractExtension, T extends ExtensionVoOperator>
Mono<CommentStatsVo> populateStats(Class<E> clazz, T vo) {
return counterService.getByName(MeterUtils.nameOf(clazz, vo.getMetadata()
.getName()))
.map(counter -> CommentStatsVo.builder()
.upvote(counter.getUpvote())
.build()
)
.defaultIfEmpty(CommentStatsVo.empty());
}
// @formatter:on
Mono<ReplyVo> toReplyVo(Reply reply) {
return Mono.just(ReplyVo.from(reply))
.flatMap(replyVo -> populateStats(Reply.class, replyVo)
.doOnNext(replyVo::setStats)
.thenReturn(replyVo))
.flatMap(replyVo -> getOwnerInfo(reply.getSpec().getOwner())
.doOnNext(replyVo::setOwner)
.thenReturn(replyVo)
)
.flatMap(this::filterReplySensitiveData);
}
private Mono<? extends ReplyVo> filterReplySensitiveData(ReplyVo replyVo) {
var owner = replyVo.getOwner();
replyVo.setOwner(OwnerInfo
.builder()
.displayName(owner.getDisplayName())
.avatar(owner.getAvatar())
.kind(owner.getKind())
.build());
replyVo.getSpec().setIpAddress("");
var specOwner = replyVo.getSpec().getOwner();
specOwner.setName("");
if (specOwner.getAnnotations() != null) {
specOwner.getAnnotations().remove("Email");
}
return Mono.just(replyVo);
}
private Mono<OwnerInfo> getOwnerInfo(Comment.CommentOwner owner) {
if (Comment.CommentOwner.KIND_EMAIL.equals(owner.getKind())) {
return Mono.just(OwnerInfo.from(owner));
}
return userService.getUserOrGhost(owner.getName())
.map(OwnerInfo::from);
}
private Mono<Predicate<Comment>> fixedCommentPredicate(@Nullable Ref ref) {
Predicate<Comment> basePredicate =
comment -> comment.getMetadata().getDeletionTimestamp() == null;
if (ref != null) {
basePredicate = basePredicate
.and(comment -> comment.getSpec().getSubjectRef().equals(ref));
}
// is approved and not hidden
Predicate<Comment> approvedPredicate =
comment -> BooleanUtils.isFalse(comment.getSpec().getHidden())
&& BooleanUtils.isTrue(comment.getSpec().getApproved());
return getCurrentUserWithoutAnonymous()
.map(username -> {
Predicate<Comment> isOwner = comment -> {
Comment.CommentOwner owner = comment.getSpec().getOwner();
return owner != null && StringUtils.equals(username, owner.getName());
};
return approvedPredicate.or(isOwner);
})
.defaultIfEmpty(approvedPredicate)
.map(basePredicate::and);
}
private Mono<Predicate<Reply>> fixedReplyPredicate(String commentName) {
Assert.notNull(commentName, "The commentName must not be null");
// The comment name must be equal to the comment name of the reply
Predicate<Reply> commentNamePredicate =
reply -> reply.getSpec().getCommentName().equals(commentName)
&& reply.getMetadata().getDeletionTimestamp() == null;
// is approved and not hidden
Predicate<Reply> approvedPredicate =
reply -> BooleanUtils.isFalse(reply.getSpec().getHidden())
&& BooleanUtils.isTrue(reply.getSpec().getApproved());
return getCurrentUserWithoutAnonymous()
.map(username -> {
Predicate<Reply> isOwner = reply -> {
Comment.CommentOwner owner = reply.getSpec().getOwner();
return owner != null && StringUtils.equals(username, owner.getName());
};
return approvedPredicate.or(isOwner);
})
.defaultIfEmpty(approvedPredicate)
.map(commentNamePredicate::and);
}
Mono<String> getCurrentUserWithoutAnonymous() {
return ReactiveSecurityContextHolder.getContext()
.mapNotNull(SecurityContext::getAuthentication)
.map(Principal::getName)
.filter(username -> !AnonymousUserConst.PRINCIPAL.equals(username));
}
static Comparator<Comment> defaultComparator() {
return new CommentComparator();
}
static class CommentComparator implements Comparator<Comment> {
@Override
public int compare(Comment c1, Comment c2) {
boolean c1Top = BooleanUtils.isTrue(c1.getSpec().getTop());
boolean c2Top = BooleanUtils.isTrue(c2.getSpec().getTop());
// c1 top = true && c2 top = false
if (c1Top && !c2Top) {
return -1;
}
// c1 top = false && c2 top = true
if (!c1Top && c2Top) {
return 1;
}
// c1 top = c2 top = true || c1 top = c2 top = false
var priorityComparator = Comparator.<Comment, Integer>comparing(
comment -> defaultIfNull(comment.getSpec().getPriority(), 0));
var creationTimeComparator = Comparator.<Comment, Instant>comparing(
comment -> comment.getSpec().getCreationTime(),
Comparators.nullsHigh(Comparator.<Instant>reverseOrder()));
var nameComparator = Comparator.<Comment, String>comparing(
comment -> comment.getMetadata().getName());
if (c1Top) {
return priorityComparator.thenComparing(creationTimeComparator)
.thenComparing(nameComparator)
.compare(c1, c2);
}
return creationTimeComparator.thenComparing(nameComparator).compare(c1, c2);
}
}
int pageNullSafe(Integer page) {
return defaultIfNull(page, 1);
}
int sizeNullSafe(Integer size) {
return defaultIfNull(size, DEFAULT_SIZE);
}
}