fix: incorrect post display when linking or unlinking hidden categories (#6204)

#### What type of PR is this?
/kind bug
/area core
/milestone 2.17.x

#### What this PR does / why we need it:
修复文章关联或取消关联隐藏分类后显示不正确的问题

#### Which issue(s) this PR fixes:
Fixes #6194
Fixes #6195

#### Does this PR introduce a user-facing change?
```release-note
修复文章关联或取消关联隐藏分类后显示不正确的问题
```
pull/6216/head
guqing 2024-06-28 21:51:00 +08:00 committed by GitHub
parent 2fe1b54f9d
commit d2a03dc849
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 5 deletions

View File

@ -42,6 +42,7 @@ public class CategoryReconciler implements Reconciler<Reconciler.Request> {
.ifPresent(category -> {
if (ExtensionUtil.isDeleted(category)) {
if (removeFinalizers(category.getMetadata(), Set.of(FINALIZER_NAME))) {
refreshHiddenState(category, false);
client.update(category);
}
return;
@ -50,20 +51,24 @@ public class CategoryReconciler implements Reconciler<Reconciler.Request> {
populatePermalinkPattern(category);
populatePermalink(category);
checkHideFromListState(category);
checkHiddenState(category);
client.update(category);
});
return Result.doNotRetry();
}
/**
* TODO move this logic to before-create/update hook in the future see {@code gh-4343}.
*/
private void checkHideFromListState(Category category) {
private void checkHiddenState(Category category) {
final boolean hidden = categoryService.isCategoryHidden(category.getMetadata().getName())
.blockOptional()
.orElse(false);
refreshHiddenState(category, hidden);
}
/**
* TODO move this logic to before-create/update hook in the future see {@code gh-4343}.
*/
private void refreshHiddenState(Category category, boolean hidden) {
category.getSpec().setHideFromList(hidden);
if (isHiddenStateChanged(category)) {
publishHiddenStateChangeEvent(category);

View File

@ -28,6 +28,7 @@ import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import run.halo.app.content.CategoryService;
import run.halo.app.content.ContentWrapper;
import run.halo.app.content.NotificationReasonConst;
import run.halo.app.content.PostService;
@ -83,6 +84,7 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
private final PostPermalinkPolicy postPermalinkPolicy;
private final CounterService counterService;
private final CommentService commentService;
private final CategoryService categoryService;
private final ApplicationEventPublisher eventPublisher;
private final NotificationCenter notificationCenter;
@ -185,6 +187,8 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
status.setInProgress(
!StringUtils.equals(headSnapshot, post.getSpec().getReleaseSnapshot()));
computeHiddenState(post);
// version + 1 is required to truly equal version
// as a version will be incremented after the update
status.setObservedVersion(post.getMetadata().getVersion() + 1);
@ -196,6 +200,19 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
return Result.doNotRetry();
}
private void computeHiddenState(Post post) {
var categories = post.getSpec().getCategories();
if (categories == null) {
post.getStatusOrDefault().setHideFromList(false);
return;
}
var hidden = categories.stream()
.anyMatch(categoryName -> categoryService.isCategoryHidden(categoryName)
.blockOptional().orElse(false)
);
post.getStatusOrDefault().setHideFromList(hidden);
}
private void populateLabels(Post post, Set<ApplicationEvent> events) {
var labels = nullSafeLabels(post);
labels.put(Post.DELETED_LABEL, String.valueOf(isTrue(post.getSpec().getDeleted())));