fix: post excerptare generated using only published content that is in use (#2457)

#### What type of PR is this?
/kind improvement
/area core
/milestone 2.0

#### What this PR does / why we need it:
文章自动摘要只从正在使用的已发布版本的内容中取得
#### Which issue(s) this PR fixes:

Fixes #2452

#### Special notes for your reviewer:
/cc @halo-dev/sig-halo 
#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/2471/head
guqing 2022-09-26 16:28:19 +08:00 committed by GitHub
parent ed8dddbafa
commit 721cddff5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 6 deletions

View File

@ -109,7 +109,7 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
spec.setExcerpt(excerpt); spec.setExcerpt(excerpt);
} }
if (excerpt.getAutoGenerate()) { if (excerpt.getAutoGenerate()) {
contentService.getContent(spec.getHeadSnapshot()) contentService.getContent(spec.getReleaseSnapshot())
.subscribe(content -> { .subscribe(content -> {
String contentRevised = content.content(); String contentRevised = content.content();
status.setExcerpt(getExcerpt(contentRevised)); status.setExcerpt(getExcerpt(contentRevised));

View File

@ -7,6 +7,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.time.Instant;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -54,10 +55,8 @@ class PostReconcilerTest {
post.getSpec().setHeadSnapshot("post-A-head-snapshot"); post.getSpec().setHeadSnapshot("post-A-head-snapshot");
when(client.fetch(eq(Post.class), eq(name))) when(client.fetch(eq(Post.class), eq(name)))
.thenReturn(Optional.of(post)); .thenReturn(Optional.of(post));
when(contentService.getContent(eq(post.getSpec().getHeadSnapshot()))) when(contentService.getContent(eq(post.getSpec().getReleaseSnapshot())))
.thenReturn(Mono.just( .thenReturn(Mono.empty());
new ContentWrapper(post.getSpec().getHeadSnapshot(), "hello world",
"<p>hello world</p>", "markdown")));
Snapshot snapshotV1 = TestPost.snapshotV1(); Snapshot snapshotV1 = TestPost.snapshotV1();
Snapshot snapshotV2 = TestPost.snapshotV2(); Snapshot snapshotV2 = TestPost.snapshotV2();
@ -77,8 +76,39 @@ class PostReconcilerTest {
verify(postPermalinkPolicy, times(0)).onPermalinkUpdate(any()); verify(postPermalinkPolicy, times(0)).onPermalinkUpdate(any());
Post value = captor.getValue(); Post value = captor.getValue();
assertThat(value.getStatus().getExcerpt()).isEqualTo("hello world"); assertThat(value.getStatus().getExcerpt()).isNull();
assertThat(value.getStatus().getContributors()).isEqualTo(List.of("guqing", "zhangsan")); assertThat(value.getStatus().getContributors()).isEqualTo(List.of("guqing", "zhangsan"));
} }
@Test
void reconcileExcerpt() {
// https://github.com/halo-dev/halo/issues/2452
String name = "post-A";
Post post = TestPost.postV1();
post.getSpec().setPublished(true);
post.getSpec().setHeadSnapshot("post-A-head-snapshot");
post.getSpec().setReleaseSnapshot("post-fake-released-snapshot");
when(client.fetch(eq(Post.class), eq(name)))
.thenReturn(Optional.of(post));
when(contentService.getContent(eq(post.getSpec().getReleaseSnapshot())))
.thenReturn(Mono.just(
new ContentWrapper(post.getSpec().getHeadSnapshot(), "hello world",
"<p>hello world</p>", "markdown")));
Snapshot snapshotV1 = TestPost.snapshotV1();
Snapshot snapshotV2 = TestPost.snapshotV2();
snapshotV2.getSpec().setPublishTime(Instant.now());
snapshotV1.getSpec().setContributors(Set.of("guqing"));
snapshotV2.getSpec().setContributors(Set.of("guqing", "zhangsan"));
when(contentService.listSnapshots(any()))
.thenReturn(Flux.just(snapshotV1, snapshotV2));
ArgumentCaptor<Post> captor = ArgumentCaptor.forClass(Post.class);
postReconciler.reconcile(new Reconciler.Request(name));
verify(client, times(3)).update(captor.capture());
Post value = captor.getValue();
assertThat(value.getStatus().getExcerpt()).isEqualTo("hello world");
}
} }