Fix empty listing of single page (#2474)

#### What type of PR is this?

/kind bug
/area core
/milestone 2.0

#### What this PR does / why we need it:

Fix empty listing of single page while using PostgreSQL.

#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/2481/head
John Niang 2022-09-27 17:46:15 +08:00 committed by GitHub
parent 1fc37673f7
commit 11d91712f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 23 deletions

View File

@ -140,32 +140,38 @@ public class SinglePageServiceImpl implements SinglePageService {
private Mono<ListedSinglePage> getListedSinglePage(SinglePage singlePage) { private Mono<ListedSinglePage> getListedSinglePage(SinglePage singlePage) {
Assert.notNull(singlePage, "The singlePage must not be null."); Assert.notNull(singlePage, "The singlePage must not be null.");
ListedSinglePage listedSinglePage = new ListedSinglePage(); return Mono.just(singlePage)
listedSinglePage.setPage(singlePage); .map(sp -> {
return Mono.just(listedSinglePage) ListedSinglePage listedSinglePage = new ListedSinglePage();
.flatMap(page -> listContributors(singlePage.getStatusOrDefault().getContributors()) listedSinglePage.setPage(singlePage);
.map(contributors -> { return listedSinglePage;
page.setContributors(contributors); })
return page; .flatMap(lsp ->
})); setContributors(singlePage.getStatusOrDefault().getContributors(), lsp));
} }
private Mono<List<Contributor>> listContributors(List<String> usernames) { private Mono<ListedSinglePage> setContributors(List<String> usernames,
ListedSinglePage singlePage) {
return listContributors(usernames)
.collectList()
.doOnNext(singlePage::setContributors)
.map(contributors -> singlePage)
.defaultIfEmpty(singlePage);
}
private Flux<Contributor> listContributors(List<String> usernames) {
if (usernames == null) { if (usernames == null) {
return Mono.empty(); return Flux.empty();
} }
return Flux.fromIterable(usernames) return Flux.fromIterable(usernames)
.map(username -> client.fetch(User.class, username) .flatMap(username -> client.fetch(User.class, username))
.map(user -> { .map(user -> {
Contributor contributor = new Contributor(); Contributor contributor = new Contributor();
contributor.setName(username); contributor.setName(user.getMetadata().getName());
contributor.setDisplayName(user.getSpec().getDisplayName()); contributor.setDisplayName(user.getSpec().getDisplayName());
contributor.setAvatar(user.getSpec().getAvatar()); contributor.setAvatar(user.getSpec().getAvatar());
return contributor; return contributor;
}) });
)
.flatMap(Function.identity())
.collectList();
} }
boolean contains(Collection<String> left, List<String> right) { boolean contains(Collection<String> left, List<String> right) {

View File

@ -172,7 +172,8 @@ public class SinglePageReconciler implements Reconciler<Reconciler.Request> {
if (excerpt.getAutoGenerate()) { if (excerpt.getAutoGenerate()) {
contentService.getContent(spec.getHeadSnapshot()) contentService.getContent(spec.getHeadSnapshot())
.subscribe(content -> { .blockOptional()
.ifPresent(content -> {
String contentRevised = content.content(); String contentRevised = content.content();
status.setExcerpt(getExcerpt(contentRevised)); status.setExcerpt(getExcerpt(contentRevised));
}); });
@ -184,7 +185,7 @@ public class SinglePageReconciler implements Reconciler<Reconciler.Request> {
String headSnapshot = singlePage.getSpec().getHeadSnapshot(); String headSnapshot = singlePage.getSpec().getHeadSnapshot();
contentService.listSnapshots(Snapshot.SubjectRef.of(SinglePage.KIND, name)) contentService.listSnapshots(Snapshot.SubjectRef.of(SinglePage.KIND, name))
.collectList() .collectList()
.subscribe(snapshots -> { .blockOptional().ifPresent(snapshots -> {
List<String> contributors = snapshots.stream() List<String> contributors = snapshots.stream()
.map(snapshot -> { .map(snapshot -> {
Set<String> usernames = snapshot.getSpec().getContributors(); Set<String> usernames = snapshot.getSpec().getContributors();