chore: optimize and reorganize code for post and page service (#5498)

#### What type of PR is this?
/kind cleanup
/area core
/milestone 2.14.x

#### What this PR does / why we need it:
优化并重新整理文章和自定义页面 Service 的部分代码

#### Does this PR introduce a user-facing change?
```release-note
None
```
pull/5516/head
guqing 2024-03-15 23:42:08 +08:00 committed by GitHub
parent 26dc24cc00
commit 0435e42123
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 87 deletions

View File

@ -5,6 +5,7 @@ import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import lombok.Data;
import lombok.experimental.Accessors;
import run.halo.app.core.extension.content.Category;
import run.halo.app.core.extension.content.Post;
import run.halo.app.core.extension.content.Tag;
@ -17,6 +18,7 @@ import run.halo.app.core.extension.content.Tag;
* @since 2.0.0
*/
@Data
@Accessors(chain = true)
public class ListedPost {
@Schema(requiredMode = REQUIRED)

View File

@ -5,6 +5,7 @@ import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import lombok.Data;
import lombok.experimental.Accessors;
import run.halo.app.core.extension.content.SinglePage;
@ -15,6 +16,7 @@ import run.halo.app.core.extension.content.SinglePage;
* @since 2.0.0
*/
@Data
@Accessors(chain = true)
public class ListedSinglePage {
@Schema(requiredMode = REQUIRED)

View File

@ -94,48 +94,24 @@ public class PostServiceImpl extends AbstractContentService implements PostServi
private Mono<ListedPost> getListedPost(Post post) {
Assert.notNull(post, "The post must not be null.");
return Mono.just(post)
.map(p -> {
ListedPost listedPost = new ListedPost();
listedPost.setPost(p);
return listedPost;
})
.flatMap(lp -> fetchStats(post)
.doOnNext(lp::setStats)
.thenReturn(lp)
)
.flatMap(lp -> setTags(post.getSpec().getTags(), lp))
.flatMap(lp -> setCategories(post.getSpec().getCategories(), lp))
.flatMap(lp -> setContributors(post.getStatusOrDefault().getContributors(), lp))
.flatMap(lp -> setOwner(post.getSpec().getOwner(), lp));
}
var listedPost = new ListedPost().setPost(post);
private Mono<ListedPost> setTags(List<String> tagNames, ListedPost post) {
return listTags(tagNames)
var statsMono = fetchStats(post)
.doOnNext(listedPost::setStats);
var tagsMono = listTags(post.getSpec().getTags())
.collectList()
.doOnNext(post::setTags)
.map(tags -> post)
.switchIfEmpty(Mono.defer(() -> Mono.just(post)));
}
.doOnNext(listedPost::setTags);
private Mono<ListedPost> setCategories(List<String> categoryNames, ListedPost post) {
return listCategories(categoryNames)
var categoriesMono = listCategories(post.getSpec().getCategories())
.collectList()
.doOnNext(post::setCategories)
.map(categories -> post)
.switchIfEmpty(Mono.defer(() -> Mono.just(post)));
}
.doOnNext(listedPost::setCategories);
private Mono<ListedPost> setContributors(List<String> contributorNames, ListedPost post) {
return listContributors(contributorNames)
var contributorsMono = listContributors(post.getStatusOrDefault().getContributors())
.collectList()
.doOnNext(post::setContributors)
.map(contributors -> post)
.switchIfEmpty(Mono.defer(() -> Mono.just(post)));
}
.doOnNext(listedPost::setContributors);
private Mono<ListedPost> setOwner(String ownerName, ListedPost post) {
return userService.getUserOrGhost(ownerName)
var ownerMono = userService.getUserOrGhost(post.getSpec().getOwner())
.map(user -> {
Contributor contributor = new Contributor();
contributor.setName(user.getMetadata().getName());
@ -143,8 +119,9 @@ public class PostServiceImpl extends AbstractContentService implements PostServi
contributor.setAvatar(user.getSpec().getAvatar());
return contributor;
})
.doOnNext(post::setOwner)
.thenReturn(post);
.doOnNext(listedPost::setOwner);
return Mono.when(statsMono, tagsMono, categoriesMono, contributorsMono, ownerMono)
.thenReturn(listedPost);
}
private Flux<Tag> listTags(List<String> tagNames) {
@ -184,15 +161,11 @@ public class PostServiceImpl extends AbstractContentService implements PostServi
public Mono<Post> draftPost(PostRequest postRequest) {
return Mono.defer(
() -> {
Post post = postRequest.post();
var post = postRequest.post();
return getContextUsername()
.map(username -> {
post.getSpec().setOwner(username);
return post;
})
.defaultIfEmpty(post);
}
)
.doOnNext(username -> post.getSpec().setOwner(username))
.thenReturn(post);
})
.flatMap(client::create)
.flatMap(post -> {
if (postRequest.content() == null) {
@ -292,22 +265,19 @@ public class PostServiceImpl extends AbstractContentService implements PostServi
@Override
public Mono<Post> publish(Post post) {
return Mono.just(post)
.doOnNext(p -> {
var spec = post.getSpec();
spec.setPublish(true);
if (spec.getHeadSnapshot() == null) {
spec.setHeadSnapshot(spec.getBaseSnapshot());
}
spec.setReleaseSnapshot(spec.getHeadSnapshot());
}).flatMap(client::update);
var spec = post.getSpec();
spec.setPublish(true);
if (spec.getHeadSnapshot() == null) {
spec.setHeadSnapshot(spec.getBaseSnapshot());
}
spec.setReleaseSnapshot(spec.getHeadSnapshot());
return client.update(post);
}
@Override
public Mono<Post> unpublish(Post post) {
return Mono.just(post)
.doOnNext(p -> p.getSpec().setPublish(false))
.flatMap(client::update);
post.getSpec().setPublish(false);
return client.update(post);
}
@Override

View File

@ -95,11 +95,8 @@ public class SinglePageServiceImpl extends AbstractContentService implements Sin
() -> {
SinglePage page = pageRequest.page();
return getContextUsername()
.map(username -> {
page.getSpec().setOwner(username);
return page;
})
.defaultIfEmpty(page);
.doOnNext(username -> page.getSpec().setOwner(username))
.thenReturn(page);
}
)
.flatMap(client::create)
@ -170,32 +167,17 @@ public class SinglePageServiceImpl extends AbstractContentService implements Sin
private Mono<ListedSinglePage> getListedSinglePage(SinglePage singlePage) {
Assert.notNull(singlePage, "The singlePage must not be null.");
return Mono.just(singlePage)
.map(sp -> {
ListedSinglePage listedSinglePage = new ListedSinglePage();
listedSinglePage.setPage(singlePage);
return listedSinglePage;
})
.flatMap(sp -> fetchStats(singlePage)
.doOnNext(sp::setStats)
.thenReturn(sp)
)
.flatMap(lsp ->
setContributors(singlePage.getStatusOrDefault().getContributors(), lsp))
.flatMap(lsp -> setOwner(singlePage.getSpec().getOwner(), lsp));
}
var listedSinglePage = new ListedSinglePage()
.setPage(singlePage);
private Mono<ListedSinglePage> setContributors(List<String> usernames,
ListedSinglePage singlePage) {
return listContributors(usernames)
var statsMono = fetchStats(singlePage)
.doOnNext(listedSinglePage::setStats);
var contributorsMono = listContributors(singlePage.getStatusOrDefault().getContributors())
.collectList()
.doOnNext(singlePage::setContributors)
.map(contributors -> singlePage)
.defaultIfEmpty(singlePage);
}
.doOnNext(listedSinglePage::setContributors);
private Mono<ListedSinglePage> setOwner(String ownerName, ListedSinglePage page) {
return userService.getUserOrGhost(ownerName)
var ownerMono = userService.getUserOrGhost(singlePage.getSpec().getOwner())
.map(user -> {
Contributor contributor = new Contributor();
contributor.setName(user.getMetadata().getName());
@ -203,8 +185,9 @@ public class SinglePageServiceImpl extends AbstractContentService implements Sin
contributor.setAvatar(user.getSpec().getAvatar());
return contributor;
})
.doOnNext(page::setOwner)
.thenReturn(page);
.doOnNext(listedSinglePage::setOwner);
return Mono.when(statsMono, contributorsMono, ownerMono)
.thenReturn(listedSinglePage);
}
Mono<Stats> fetchStats(SinglePage singlePage) {