-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAbstractPostRssProvider.java
More file actions
148 lines (132 loc) · 6.33 KB
/
AbstractPostRssProvider.java
File metadata and controls
148 lines (132 loc) · 6.33 KB
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
package run.halo.feed.provider;
import static run.halo.feed.RssUtils.genRelativeThumbUri;
import java.util.List;
import java.util.function.Function;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ServerWebInputException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import run.halo.app.content.ContentWrapper;
import run.halo.app.content.PostContentService;
import run.halo.app.core.attachment.ThumbnailSize;
import run.halo.app.core.extension.User;
import run.halo.app.core.extension.content.Category;
import run.halo.app.core.extension.content.Post;
import run.halo.app.extension.ExtensionUtil;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.ListResult;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.index.query.QueryFactory;
import run.halo.app.infra.ExternalLinkProcessor;
import run.halo.app.infra.ExternalUrlSupplier;
import run.halo.app.infra.SystemInfoGetter;
import run.halo.app.plugin.ReactiveSettingFetcher;
import run.halo.feed.BasicProp;
import run.halo.feed.RSS2;
import run.halo.feed.service.PostService;
@RequiredArgsConstructor
public abstract class AbstractPostRssProvider {
protected final PostService postService;
protected final ReactiveExtensionClient client;
protected final ExternalLinkProcessor externalLinkProcessor;
protected final ReactiveSettingFetcher settingFetcher;
protected final PostContentService postContentService;
protected final ExternalUrlSupplier externalUrlSupplier;
protected final SystemInfoGetter systemInfoGetter;
public Mono<RSS2> handler(ServerRequest request) {
var builder = RSS2.builder();
var rssMono = systemInfoGetter.get()
.doOnNext(systemInfo -> builder
.title(systemInfo.getTitle())
.image(externalLinkProcessor.processLink(systemInfo.getLogo()))
.description(
StringUtils.defaultIfBlank(systemInfo.getSubtitle(), systemInfo.getTitle()))
.link(externalUrlSupplier.getURL(request.exchange().getRequest()).toString())
)
.subscribeOn(Schedulers.boundedElastic());
var itemsMono = listPosts(request)
.concatMap(postWithContent -> {
var post = postWithContent.getPost();
var permalink = post.getStatusOrDefault().getPermalink();
var itemBuilder = RSS2.Item.builder()
.title(post.getSpec().getTitle())
.link(externalLinkProcessor.processLink(permalink))
.pubDate(post.getSpec().getPublishTime())
.guid(post.getStatusOrDefault().getPermalink())
.description(postWithContent.getContent());
if (StringUtils.isNotBlank(post.getSpec().getCover())) {
itemBuilder.enclosureUrl(genThumbUrl(post.getSpec().getCover()))
.enclosureType("image/jpeg");
}
var ownerName = post.getSpec().getOwner();
var userMono = fetchUser(ownerName)
.map(user -> user.getSpec().getDisplayName())
.doOnNext(itemBuilder::author);
var categoryMono = fetchCategoryDisplayName(post.getSpec().getCategories())
.collectList()
.doOnNext(itemBuilder::categories);
return Mono.when(userMono, categoryMono)
.then(Mono.fromSupplier(itemBuilder::build));
})
.collectList()
.doOnNext(builder::items)
.subscribeOn(Schedulers.boundedElastic());
return Mono.when(rssMono, itemsMono)
.then(Mono.fromSupplier(builder::build));
}
private String genThumbUrl(String url) {
return externalLinkProcessor.processLink(genRelativeThumbUri(url, ThumbnailSize.M));
}
protected Flux<PostWithContent> listPosts(ServerRequest request) {
return listPostsByFunc(basicProp -> postService.listPosts(basicProp.getOutputNum())
.flatMapIterable(ListResult::getItems));
}
protected Flux<PostWithContent> listPostsByFunc(Function<BasicProp, Flux<Post>> postListFunc) {
return BasicProp.getBasicProp(settingFetcher)
.flatMapMany(basicProp -> postListFunc.apply(basicProp)
.concatMap(post -> {
var postWithContent = new PostWithContent()
.setPost(post);
if (basicProp.isExcerptDescriptionType()) {
// Prevent parsing excerpt as html
var escapedContent =
StringEscapeUtils.escapeXml10(post.getStatusOrDefault().getExcerpt());
postWithContent.setContent(escapedContent);
return Mono.just(postWithContent);
}
return postContentService.getReleaseContent(post.getMetadata().getName())
.map(ContentWrapper::getContent)
.doOnNext(content -> postWithContent.setContent(
StringUtils.defaultString(content))
)
.thenReturn(postWithContent);
})
);
}
protected Mono<User> fetchUser(String username) {
return client.fetch(User.class, username)
.switchIfEmpty(Mono.error(new ServerWebInputException("User not found")));
}
private Flux<String> fetchCategoryDisplayName(List<String> categoryNames) {
if (CollectionUtils.isEmpty(categoryNames)) {
return Flux.empty();
}
return client.listAll(Category.class, ListOptions.builder()
.fieldQuery(QueryFactory.in("metadata.name", categoryNames))
.build(), ExtensionUtil.defaultSort())
.map(category -> category.getSpec().getDisplayName());
}
@Data
@Accessors(chain = true)
protected static class PostWithContent {
private Post post;
private String content;
}
}