Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/2292
- Loading branch information
Showing
38 changed files
with
849 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
application/src/main/java/run/halo/app/theme/finders/SinglePageConversionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package run.halo.app.theme.finders; | ||
|
||
import reactor.core.publisher.Mono; | ||
import run.halo.app.core.extension.content.SinglePage; | ||
import run.halo.app.theme.finders.vo.ListedSinglePageVo; | ||
import run.halo.app.theme.finders.vo.SinglePageVo; | ||
|
||
/** | ||
* A service that converts {@link SinglePage} to {@link SinglePageVo}. | ||
* | ||
* @author guqing | ||
* @since 2.6.0 | ||
*/ | ||
public interface SinglePageConversionService { | ||
|
||
Mono<SinglePageVo> convertToVo(SinglePage singlePage, String snapshotName); | ||
|
||
Mono<SinglePageVo> convertToVo(SinglePage singlePage); | ||
|
||
Mono<ListedSinglePageVo> convertToListedVo(SinglePage singlePage); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...cation/src/main/java/run/halo/app/theme/finders/impl/SinglePageConversionServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package run.halo.app.theme.finders.impl; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.CollectionUtils; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.content.SinglePageService; | ||
import run.halo.app.core.extension.content.SinglePage; | ||
import run.halo.app.metrics.CounterService; | ||
import run.halo.app.metrics.MeterUtils; | ||
import run.halo.app.theme.finders.ContributorFinder; | ||
import run.halo.app.theme.finders.SinglePageConversionService; | ||
import run.halo.app.theme.finders.vo.ContentVo; | ||
import run.halo.app.theme.finders.vo.ListedSinglePageVo; | ||
import run.halo.app.theme.finders.vo.SinglePageVo; | ||
import run.halo.app.theme.finders.vo.StatsVo; | ||
|
||
/** | ||
* Default implementation of {@link SinglePageConversionService}. | ||
* | ||
* @author guqing | ||
* @since 2.6.0 | ||
*/ | ||
@Component | ||
@RequiredArgsConstructor | ||
public class SinglePageConversionServiceImpl implements SinglePageConversionService { | ||
|
||
private final SinglePageService singlePageService; | ||
|
||
private final ContributorFinder contributorFinder; | ||
|
||
private final CounterService counterService; | ||
|
||
@Override | ||
public Mono<SinglePageVo> convertToVo(SinglePage singlePage, String snapshotName) { | ||
return convert(singlePage, snapshotName); | ||
} | ||
|
||
@Override | ||
public Mono<SinglePageVo> convertToVo(SinglePage singlePage) { | ||
return convert(singlePage, singlePage.getSpec().getReleaseSnapshot()); | ||
} | ||
|
||
@Override | ||
public Mono<ListedSinglePageVo> convertToListedVo(SinglePage singlePage) { | ||
return Mono.fromSupplier( | ||
() -> { | ||
ListedSinglePageVo pageVo = ListedSinglePageVo.from(singlePage); | ||
pageVo.setContributors(List.of()); | ||
return pageVo; | ||
}) | ||
.flatMap(this::populateStats) | ||
.flatMap(this::populateContributors); | ||
} | ||
|
||
Mono<SinglePageVo> convert(SinglePage singlePage, String snapshotName) { | ||
Assert.notNull(singlePage, "Single page must not be null"); | ||
Assert.hasText(snapshotName, "Snapshot name must not be empty"); | ||
return Mono.just(singlePage) | ||
.map(page -> { | ||
SinglePageVo pageVo = SinglePageVo.from(page); | ||
pageVo.setContributors(List.of()); | ||
pageVo.setContent(ContentVo.empty()); | ||
return pageVo; | ||
}) | ||
.flatMap(this::populateStats) | ||
.flatMap(this::populateContributors) | ||
.flatMap(page -> populateContent(page, snapshotName)) | ||
.flatMap(page -> contributorFinder.getContributor(page.getSpec().getOwner()) | ||
.doOnNext(page::setOwner) | ||
.thenReturn(page) | ||
); | ||
} | ||
|
||
Mono<SinglePageVo> populateContent(SinglePageVo singlePageVo, String snapshotName) { | ||
Assert.notNull(singlePageVo, "Single page vo must not be null"); | ||
Assert.hasText(snapshotName, "Snapshot name must not be empty"); | ||
return singlePageService.getContent(snapshotName, singlePageVo.getSpec().getBaseSnapshot()) | ||
.map(contentWrapper -> ContentVo.builder() | ||
.content(contentWrapper.getContent()) | ||
.raw(contentWrapper.getRaw()) | ||
.build() | ||
) | ||
.doOnNext(singlePageVo::setContent) | ||
.thenReturn(singlePageVo); | ||
} | ||
|
||
<T extends ListedSinglePageVo> Mono<T> populateStats(T pageVo) { | ||
String name = pageVo.getMetadata().getName(); | ||
return counterService.getByName(MeterUtils.nameOf(SinglePage.class, name)) | ||
.map(counter -> StatsVo.builder() | ||
.visit(counter.getVisit()) | ||
.upvote(counter.getUpvote()) | ||
.comment(counter.getApprovedComment()) | ||
.build() | ||
) | ||
.doOnNext(pageVo::setStats) | ||
.thenReturn(pageVo); | ||
} | ||
|
||
<T extends ListedSinglePageVo> Mono<T> populateContributors(T pageVo) { | ||
List<String> names = pageVo.getStatus().getContributors(); | ||
if (CollectionUtils.isEmpty(names)) { | ||
return Mono.just(pageVo); | ||
} | ||
return contributorFinder.getContributors(names) | ||
.collectList() | ||
.doOnNext(pageVo::setContributors) | ||
.thenReturn(pageVo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...pp/theme/router/factories/ModelConst.java → ...run/halo/app/theme/router/ModelConst.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
application/src/main/java/run/halo/app/theme/router/ModelMapUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package run.halo.app.theme.router; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import run.halo.app.core.extension.content.Post; | ||
import run.halo.app.core.extension.content.SinglePage; | ||
import run.halo.app.extension.Scheme; | ||
import run.halo.app.theme.DefaultTemplateEnum; | ||
import run.halo.app.theme.finders.vo.PostVo; | ||
import run.halo.app.theme.finders.vo.SinglePageVo; | ||
|
||
/** | ||
* A util class for building model map. | ||
* | ||
* @author guqing | ||
* @since 2.6.0 | ||
*/ | ||
public abstract class ModelMapUtils { | ||
private static final Scheme POST_SCHEME = Scheme.buildFromType(Post.class); | ||
private static final Scheme SINGLE_PAGE_SCHEME = Scheme.buildFromType(SinglePage.class); | ||
|
||
/** | ||
* Build post view model. | ||
* | ||
* @param postVo post vo | ||
* @return model map | ||
*/ | ||
public static Map<String, Object> postModel(PostVo postVo) { | ||
Map<String, Object> model = new HashMap<>(); | ||
model.put("name", postVo.getMetadata().getName()); | ||
model.put(ModelConst.TEMPLATE_ID, DefaultTemplateEnum.POST.getValue()); | ||
model.put("groupVersionKind", POST_SCHEME.groupVersionKind()); | ||
model.put("plural", POST_SCHEME.plural()); | ||
model.put("post", postVo); | ||
return model; | ||
} | ||
|
||
/** | ||
* Build single page view model. | ||
* | ||
* @param pageVo page vo | ||
* @return model map | ||
*/ | ||
public static Map<String, Object> singlePageModel(SinglePageVo pageVo) { | ||
Map<String, Object> model = new HashMap<>(); | ||
model.put("name", pageVo.getMetadata().getName()); | ||
model.put("groupVersionKind", SINGLE_PAGE_SCHEME.groupVersionKind()); | ||
model.put("plural", SINGLE_PAGE_SCHEME.plural()); | ||
model.put(ModelConst.TEMPLATE_ID, DefaultTemplateEnum.SINGLE_PAGE.getValue()); | ||
model.put("singlePage", pageVo); | ||
return model; | ||
} | ||
} |
Oops, something went wrong.