mirror of https://github.com/halo-dev/halo
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
parent
2fe1b54f9d
commit
d2a03dc849
|
@ -42,6 +42,7 @@ public class CategoryReconciler implements Reconciler<Reconciler.Request> {
|
||||||
.ifPresent(category -> {
|
.ifPresent(category -> {
|
||||||
if (ExtensionUtil.isDeleted(category)) {
|
if (ExtensionUtil.isDeleted(category)) {
|
||||||
if (removeFinalizers(category.getMetadata(), Set.of(FINALIZER_NAME))) {
|
if (removeFinalizers(category.getMetadata(), Set.of(FINALIZER_NAME))) {
|
||||||
|
refreshHiddenState(category, false);
|
||||||
client.update(category);
|
client.update(category);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -50,20 +51,24 @@ public class CategoryReconciler implements Reconciler<Reconciler.Request> {
|
||||||
|
|
||||||
populatePermalinkPattern(category);
|
populatePermalinkPattern(category);
|
||||||
populatePermalink(category);
|
populatePermalink(category);
|
||||||
checkHideFromListState(category);
|
checkHiddenState(category);
|
||||||
|
|
||||||
client.update(category);
|
client.update(category);
|
||||||
});
|
});
|
||||||
return Result.doNotRetry();
|
return Result.doNotRetry();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void checkHiddenState(Category category) {
|
||||||
* TODO move this logic to before-create/update hook in the future see {@code gh-4343}.
|
|
||||||
*/
|
|
||||||
private void checkHideFromListState(Category category) {
|
|
||||||
final boolean hidden = categoryService.isCategoryHidden(category.getMetadata().getName())
|
final boolean hidden = categoryService.isCategoryHidden(category.getMetadata().getName())
|
||||||
.blockOptional()
|
.blockOptional()
|
||||||
.orElse(false);
|
.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);
|
category.getSpec().setHideFromList(hidden);
|
||||||
if (isHiddenStateChanged(category)) {
|
if (isHiddenStateChanged(category)) {
|
||||||
publishHiddenStateChangeEvent(category);
|
publishHiddenStateChangeEvent(category);
|
||||||
|
|
|
@ -28,6 +28,7 @@ import org.springframework.context.ApplicationEvent;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import run.halo.app.content.CategoryService;
|
||||||
import run.halo.app.content.ContentWrapper;
|
import run.halo.app.content.ContentWrapper;
|
||||||
import run.halo.app.content.NotificationReasonConst;
|
import run.halo.app.content.NotificationReasonConst;
|
||||||
import run.halo.app.content.PostService;
|
import run.halo.app.content.PostService;
|
||||||
|
@ -83,6 +84,7 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
|
||||||
private final PostPermalinkPolicy postPermalinkPolicy;
|
private final PostPermalinkPolicy postPermalinkPolicy;
|
||||||
private final CounterService counterService;
|
private final CounterService counterService;
|
||||||
private final CommentService commentService;
|
private final CommentService commentService;
|
||||||
|
private final CategoryService categoryService;
|
||||||
|
|
||||||
private final ApplicationEventPublisher eventPublisher;
|
private final ApplicationEventPublisher eventPublisher;
|
||||||
private final NotificationCenter notificationCenter;
|
private final NotificationCenter notificationCenter;
|
||||||
|
@ -185,6 +187,8 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
|
||||||
status.setInProgress(
|
status.setInProgress(
|
||||||
!StringUtils.equals(headSnapshot, post.getSpec().getReleaseSnapshot()));
|
!StringUtils.equals(headSnapshot, post.getSpec().getReleaseSnapshot()));
|
||||||
|
|
||||||
|
computeHiddenState(post);
|
||||||
|
|
||||||
// version + 1 is required to truly equal version
|
// version + 1 is required to truly equal version
|
||||||
// as a version will be incremented after the update
|
// as a version will be incremented after the update
|
||||||
status.setObservedVersion(post.getMetadata().getVersion() + 1);
|
status.setObservedVersion(post.getMetadata().getVersion() + 1);
|
||||||
|
@ -196,6 +200,19 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
|
||||||
return Result.doNotRetry();
|
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) {
|
private void populateLabels(Post post, Set<ApplicationEvent> events) {
|
||||||
var labels = nullSafeLabels(post);
|
var labels = nullSafeLabels(post);
|
||||||
labels.put(Post.DELETED_LABEL, String.valueOf(isTrue(post.getSpec().getDeleted())));
|
labels.put(Post.DELETED_LABEL, String.valueOf(isTrue(post.getSpec().getDeleted())));
|
||||||
|
|
Loading…
Reference in New Issue