Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: provide post content retrieval bean for plugins #5981

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions api/src/main/java/run/halo/app/content/PostContentService.java
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -53,6 +54,8 @@ public static ApplicationContext create(ApplicationContext rootContext) {
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();
Expand Down