forked from halo-dev/halo
/
PostReconciler.java
354 lines (313 loc) · 14.8 KB
/
PostReconciler.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package run.halo.app.core.extension.reconciler;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static run.halo.app.extension.ExtensionUtil.addFinalizers;
import static run.halo.app.extension.ExtensionUtil.removeFinalizers;
import static run.halo.app.extension.index.query.QueryFactory.equal;
import com.google.common.hash.Hashing;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import run.halo.app.content.ContentWrapper;
import run.halo.app.content.NotificationReasonConst;
import run.halo.app.content.PostService;
import run.halo.app.content.permalinks.PostPermalinkPolicy;
import run.halo.app.core.extension.content.Comment;
import run.halo.app.core.extension.content.Constant;
import run.halo.app.core.extension.content.Post;
import run.halo.app.core.extension.content.Post.PostPhase;
import run.halo.app.core.extension.content.Post.VisibleEnum;
import run.halo.app.core.extension.content.Snapshot;
import run.halo.app.core.extension.notification.Subscription;
import run.halo.app.event.post.PostDeletedEvent;
import run.halo.app.event.post.PostPublishedEvent;
import run.halo.app.event.post.PostUnpublishedEvent;
import run.halo.app.event.post.PostUpdatedEvent;
import run.halo.app.event.post.PostVisibleChangedEvent;
import run.halo.app.extension.DefaultExtensionMatcher;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.ExtensionOperator;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.Ref;
import run.halo.app.extension.controller.Controller;
import run.halo.app.extension.controller.ControllerBuilder;
import run.halo.app.extension.controller.Reconciler;
import run.halo.app.extension.index.query.QueryFactory;
import run.halo.app.extension.router.selector.FieldSelector;
import run.halo.app.infra.Condition;
import run.halo.app.infra.ConditionStatus;
import run.halo.app.infra.utils.HaloUtils;
import run.halo.app.metrics.CounterService;
import run.halo.app.metrics.MeterUtils;
import run.halo.app.notification.NotificationCenter;
/**
* <p>Reconciler for {@link Post}.</p>
*
* <p>things to do:</p>
* <ul>
* 1. generate permalink
* 2. generate excerpt if auto generate is enabled
* </ul>
*
* @author guqing
* @since 2.0.0
*/
@AllArgsConstructor
@Component
public class PostReconciler implements Reconciler<Reconciler.Request> {
private static final String FINALIZER_NAME = "post-protection";
private final ExtensionClient client;
private final PostService postService;
private final PostPermalinkPolicy postPermalinkPolicy;
private final CounterService counterService;
private final ApplicationEventPublisher eventPublisher;
private final NotificationCenter notificationCenter;
@Override
public Result reconcile(Request request) {
var events = new HashSet<ApplicationEvent>();
client.fetch(Post.class, request.name())
.ifPresent(post -> {
if (ExtensionOperator.isDeleted(post)) {
removeFinalizers(post.getMetadata(), Set.of(FINALIZER_NAME));
unPublishPost(post, events);
events.add(new PostDeletedEvent(this, post));
cleanUpResources(post);
// update post to be able to be collected by gc collector.
client.update(post);
// fire event after updating post
events.forEach(eventPublisher::publishEvent);
return;
}
addFinalizers(post.getMetadata(), Set.of(FINALIZER_NAME));
subscribeNewCommentNotification(post);
var labels = post.getMetadata().getLabels();
if (labels == null) {
labels = new HashMap<>();
post.getMetadata().setLabels(labels);
}
var annotations = post.getMetadata().getAnnotations();
if (annotations == null) {
annotations = new HashMap<>();
post.getMetadata().setAnnotations(annotations);
}
if (!annotations.containsKey(Post.PUBLISHED_LABEL)) {
labels.put(Post.PUBLISHED_LABEL, BooleanUtils.FALSE);
}
var status = post.getStatus();
if (status == null) {
status = new Post.PostStatus();
post.setStatus(status);
}
if (post.isPublished() && post.getSpec().getPublishTime() == null) {
post.getSpec().setPublishTime(Instant.now());
}
// calculate the sha256sum
var configSha256sum = Hashing.sha256().hashString(post.getSpec().toString(), UTF_8)
.toString();
var oldConfigChecksum = annotations.get(Constant.CHECKSUM_CONFIG_ANNO);
if (!Objects.equals(oldConfigChecksum, configSha256sum)) {
// if the checksum doesn't match
events.add(new PostUpdatedEvent(this, post.getMetadata().getName()));
annotations.put(Constant.CHECKSUM_CONFIG_ANNO, configSha256sum);
}
var expectDelete = defaultIfNull(post.getSpec().getDeleted(), false);
var expectPublish = defaultIfNull(post.getSpec().getPublish(), false);
if (expectDelete || !expectPublish) {
unPublishPost(post, events);
} else {
publishPost(post, events);
}
labels.put(Post.DELETED_LABEL, expectDelete.toString());
var expectVisible = defaultIfNull(post.getSpec().getVisible(), VisibleEnum.PUBLIC);
var oldVisible = VisibleEnum.from(labels.get(Post.VISIBLE_LABEL));
if (!Objects.equals(oldVisible, expectVisible)) {
eventPublisher.publishEvent(
new PostVisibleChangedEvent(request.name(), oldVisible, expectVisible));
}
labels.put(Post.VISIBLE_LABEL, expectVisible.toString());
var ownerName = post.getSpec().getOwner();
if (StringUtils.isNotBlank(ownerName)) {
labels.put(Post.OWNER_LABEL, ownerName);
}
var publishTime = post.getSpec().getPublishTime();
if (publishTime != null) {
labels.put(Post.ARCHIVE_YEAR_LABEL, HaloUtils.getYearText(publishTime));
labels.put(Post.ARCHIVE_MONTH_LABEL, HaloUtils.getMonthText(publishTime));
labels.put(Post.ARCHIVE_DAY_LABEL, HaloUtils.getDayText(publishTime));
}
var permalinkPattern = postPermalinkPolicy.pattern();
annotations.put(Constant.PERMALINK_PATTERN_ANNO, permalinkPattern);
status.setPermalink(postPermalinkPolicy.permalink(post));
if (status.getPhase() == null) {
status.setPhase(PostPhase.DRAFT.toString());
}
var excerpt = post.getSpec().getExcerpt();
if (excerpt == null) {
excerpt = new Post.Excerpt();
}
var isAutoGenerate = defaultIfNull(excerpt.getAutoGenerate(), true);
if (isAutoGenerate) {
Optional<ContentWrapper> contentWrapper =
postService.getContent(post.getSpec().getReleaseSnapshot(),
post.getSpec().getBaseSnapshot())
.blockOptional();
if (contentWrapper.isPresent()) {
String contentRevised = contentWrapper.get().getContent();
status.setExcerpt(getExcerpt(contentRevised));
}
} else {
status.setExcerpt(excerpt.getRaw());
}
var ref = Ref.of(post);
// handle contributors
var headSnapshot = post.getSpec().getHeadSnapshot();
var contributors = listSnapshots(ref)
.stream()
.map(snapshot -> {
Set<String> usernames = snapshot.getSpec().getContributors();
return Objects.requireNonNullElseGet(usernames,
() -> new HashSet<String>());
})
.flatMap(Set::stream)
.distinct()
.sorted()
.toList();
status.setContributors(contributors);
// update in progress status
status.setInProgress(
!StringUtils.equals(headSnapshot, post.getSpec().getReleaseSnapshot()));
// version + 1 is required to truly equal version
// as a version will be incremented after the update
status.setObservedVersion(post.getMetadata().getVersion() + 1);
client.update(post);
// fire event after updating post
events.forEach(eventPublisher::publishEvent);
});
return Result.doNotRetry();
}
@Override
public Controller setupWith(ControllerBuilder builder) {
return builder
.extension(new Post())
.onAddMatcher(DefaultExtensionMatcher.builder(client, Post.GVK)
.fieldSelector(FieldSelector.of(
equal(Post.REQUIRE_SYNC_ON_STARTUP_INDEX_NAME, BooleanUtils.TRUE))
)
.build()
)
.build();
}
void subscribeNewCommentNotification(Post post) {
var subscriber = new Subscription.Subscriber();
subscriber.setName(post.getSpec().getOwner());
var interestReason = new Subscription.InterestReason();
interestReason.setReasonType(NotificationReasonConst.NEW_COMMENT_ON_POST);
interestReason.setSubject(Subscription.ReasonSubject.builder()
.apiVersion(post.getApiVersion())
.kind(post.getKind())
.name(post.getMetadata().getName())
.build());
notificationCenter.subscribe(subscriber, interestReason).block();
}
private void publishPost(Post post, Set<ApplicationEvent> events) {
var expectReleaseSnapshot = post.getSpec().getReleaseSnapshot();
if (StringUtils.isBlank(expectReleaseSnapshot)) {
// Do nothing if release snapshot is not set
return;
}
var annotations = post.getMetadata().getAnnotations();
var lastReleaseSnapshot = annotations.get(Post.LAST_RELEASED_SNAPSHOT_ANNO);
if (post.isPublished()
&& Objects.equals(expectReleaseSnapshot, lastReleaseSnapshot)) {
// If the release snapshot is not change
return;
}
var status = post.getStatus();
// validate the release snapshot
var snapshot = client.fetch(Snapshot.class, expectReleaseSnapshot);
if (snapshot.isEmpty()) {
Condition condition = Condition.builder()
.type(PostPhase.FAILED.name())
.reason("SnapshotNotFound")
.message(
String.format("Snapshot [%s] not found for publish", expectReleaseSnapshot))
.status(ConditionStatus.FALSE)
.lastTransitionTime(Instant.now())
.build();
status.getConditionsOrDefault().addAndEvictFIFO(condition);
status.setPhase(PostPhase.FAILED.name());
return;
}
annotations.put(Post.LAST_RELEASED_SNAPSHOT_ANNO, expectReleaseSnapshot);
status.setPhase(PostPhase.PUBLISHED.toString());
var condition = Condition.builder()
.type(PostPhase.PUBLISHED.name())
.reason("Published")
.message("Post published successfully.")
.lastTransitionTime(Instant.now())
.status(ConditionStatus.TRUE)
.build();
status.getConditionsOrDefault().addAndEvictFIFO(condition);
var labels = post.getMetadata().getLabels();
labels.put(Post.PUBLISHED_LABEL, Boolean.TRUE.toString());
if (post.getSpec().getPublishTime() == null) {
// TODO Set the field in creation hook in the future.
post.getSpec().setPublishTime(Instant.now());
}
status.setLastModifyTime(snapshot.get().getSpec().getLastModifyTime());
events.add(new PostPublishedEvent(this, post.getMetadata().getName()));
}
void unPublishPost(Post post, Set<ApplicationEvent> events) {
if (!post.isPublished()) {
return;
}
var labels = post.getMetadata().getLabels();
labels.put(Post.PUBLISHED_LABEL, Boolean.FALSE.toString());
final var status = post.getStatus();
var condition = new Condition();
condition.setType("CancelledPublish");
condition.setStatus(ConditionStatus.TRUE);
condition.setReason(condition.getType());
condition.setMessage("CancelledPublish");
condition.setLastTransitionTime(Instant.now());
status.getConditionsOrDefault().addAndEvictFIFO(condition);
status.setPhase(PostPhase.DRAFT.toString());
events.add(new PostUnpublishedEvent(this, post.getMetadata().getName()));
}
private void cleanUpResources(Post post) {
// clean up snapshots
final Ref ref = Ref.of(post);
listSnapshots(ref).forEach(client::delete);
// clean up comments
client.list(Comment.class, comment -> ref.equals(comment.getSpec().getSubjectRef()),
null)
.forEach(client::delete);
// delete counter
counterService.deleteByName(MeterUtils.nameOf(Post.class, post.getMetadata().getName()))
.block();
}
private String getExcerpt(String htmlContent) {
String shortHtmlContent = StringUtils.substring(htmlContent, 0, 500);
String text = Jsoup.parse(shortHtmlContent).text();
// TODO The default capture 150 words as excerpt
return StringUtils.substring(text, 0, 150);
}
List<Snapshot> listSnapshots(Ref ref) {
var snapshotListOptions = new ListOptions();
snapshotListOptions.setFieldSelector(FieldSelector.of(
QueryFactory.equal("spec.subjectRef", Snapshot.toSubjectRefKey(ref))));
return client.listAll(Snapshot.class, snapshotListOptions, Sort.unsorted());
}
}