chore: provide post content retrieval bean for plugins (#5981)

#### What type of PR is this?
/kind improvement
/area core
/area plugin
/milestone 2.16.x

#### What this PR does / why we need it:
为插件提供文章内容获取的 bean 以简化文章内容获取

#### Which issue(s) this PR fixes:
Fixes #

#### Does this PR introduce a user-facing change?
```release-note
为插件提供文章内容获取的 Bean
```
pull/5900/head
guqing 2024-05-24 14:46:51 +08:00 committed by GitHub
parent 9ec608be3b
commit cb2138580c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package run.halo.app.content;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public interface PostContentService {
Mono<ContentWrapper> getHeadContent(String postName);
Mono<ContentWrapper> getReleaseContent(String postName);
Mono<ContentWrapper> getSpecifiedContent(String postName, String snapshotName);
Flux<String> listSnapshots(String postName);
}

View File

@ -0,0 +1,58 @@
package run.halo.app.content;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.content.Post;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.Ref;
/**
* Provides ability to get post content for the specified post.
*
* @author guqing
* @since 2.16.0
*/
@Component
public class PostContentServiceImpl extends AbstractContentService implements PostContentService {
private final ReactiveExtensionClient client;
public PostContentServiceImpl(ReactiveExtensionClient client) {
super(client);
this.client = client;
}
@Override
public Mono<ContentWrapper> getHeadContent(String postName) {
return client.get(Post.class, postName)
.flatMap(post -> {
var headSnapshot = post.getSpec().getHeadSnapshot();
return super.getContent(headSnapshot, post.getSpec().getBaseSnapshot());
});
}
@Override
public Mono<ContentWrapper> getReleaseContent(String postName) {
return client.get(Post.class, postName)
.flatMap(post -> {
var releaseSnapshot = post.getSpec().getReleaseSnapshot();
return super.getContent(releaseSnapshot, post.getSpec().getBaseSnapshot());
});
}
@Override
public Mono<ContentWrapper> getSpecifiedContent(String postName, String snapshotName) {
return client.get(Post.class, postName)
.flatMap(post -> {
var baseSnapshot = post.getSpec().getBaseSnapshot();
return super.getContent(snapshotName, baseSnapshot);
});
}
@Override
public Flux<String> listSnapshots(String postName) {
return client.get(Post.class, postName)
.flatMapMany(page -> listSnapshotsBy(Ref.of(page)))
.map(snapshot -> snapshot.getMetadata().getName());
}
}

View File

@ -3,6 +3,7 @@ package run.halo.app.plugin;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.security.web.server.context.ServerSecurityContextRepository;
import run.halo.app.content.PostContentService;
import run.halo.app.core.extension.service.AttachmentService;
import run.halo.app.extension.DefaultSchemeManager;
import run.halo.app.extension.ExtensionClient;
@ -53,6 +54,8 @@ public enum SharedApplicationContextFactory {
rootContext.getBean(NotificationCenter.class));
beanFactory.registerSingleton("externalLinkProcessor",
rootContext.getBean(ExternalLinkProcessor.class));
beanFactory.registerSingleton("postContentService",
rootContext.getBean(PostContentService.class));
// TODO add more shared instance here
sharedContext.refresh();