diff --git a/src/main/java/run/halo/app/core/extension/reconciler/TagReconciler.java b/src/main/java/run/halo/app/core/extension/reconciler/TagReconciler.java index 861897e24a..4a17cc2b95 100644 --- a/src/main/java/run/halo/app/core/extension/reconciler/TagReconciler.java +++ b/src/main/java/run/halo/app/core/extension/reconciler/TagReconciler.java @@ -1,6 +1,5 @@ package run.halo.app.core.extension.reconciler; -import java.time.Duration; import java.util.HashSet; import java.util.List; import java.util.Objects; @@ -34,20 +33,17 @@ public TagReconciler(ExtensionClient client, TagPermalinkPolicy tagPermalinkPoli @Override public Result reconcile(Request request) { - return client.fetch(Tag.class, request.name()) - .map(tag -> { + client.fetch(Tag.class, request.name()) + .ifPresent(tag -> { if (isDeleted(tag)) { cleanUpResourcesAndRemoveFinalizer(request.name()); - return new Result(false, null); + return; } addFinalizerIfNecessary(tag); - this.reconcileStatusPermalink(request.name()); - reconcileStatusPosts(request.name()); - return new Result(true, Duration.ofMinutes(1)); - }) - .orElseGet(() -> new Result(false, null)); + }); + return new Result(false, null); } @Override @@ -90,22 +86,6 @@ private void cleanUpResourcesAndRemoveFinalizer(String tagName) { }); } - private void reconcileStatusPermalink(String tagName) { - client.fetch(Tag.class, tagName) - .ifPresent(tag -> { - Tag oldTag = JsonUtils.deepCopy(tag); - tagPermalinkPolicy.onPermalinkDelete(oldTag); - - tag.getStatusOrDefault() - .setPermalink(tagPermalinkPolicy.permalink(tag)); - tagPermalinkPolicy.onPermalinkAdd(tag); - - if (!oldTag.equals(tag)) { - client.update(tag); - } - }); - } - private void reconcileStatusPosts(String tagName) { client.fetch(Tag.class, tagName).ifPresent(tag -> { Tag oldTag = JsonUtils.deepCopy(tag); diff --git a/src/main/java/run/halo/app/core/extension/reconciler/TagRouteReconciler.java b/src/main/java/run/halo/app/core/extension/reconciler/TagRouteReconciler.java new file mode 100644 index 0000000000..5996af2b57 --- /dev/null +++ b/src/main/java/run/halo/app/core/extension/reconciler/TagRouteReconciler.java @@ -0,0 +1,56 @@ +package run.halo.app.core.extension.reconciler; + +import lombok.RequiredArgsConstructor; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; +import run.halo.app.content.permalinks.TagPermalinkPolicy; +import run.halo.app.core.extension.content.Tag; +import run.halo.app.extension.ExtensionClient; +import run.halo.app.extension.controller.Controller; +import run.halo.app.extension.controller.ControllerBuilder; +import run.halo.app.extension.controller.Reconciler; + +@Component +@RequiredArgsConstructor +public class TagRouteReconciler implements Reconciler { + private final ExtensionClient client; + private final TagPermalinkPolicy tagPermalinkPolicy; + + @Override + public Result reconcile(Request request) { + client.fetch(Tag.class, request.name()) + .ifPresent(tag -> { + if (tag.getMetadata().getDeletionTimestamp() != null) { + // TagReconciler already did it, so there is no need to remove permalink + return; + } + + reconcilePermalinkRoute(request.name()); + }); + return new Result(false, null); + } + + private void reconcilePermalinkRoute(String tagName) { + client.fetch(Tag.class, tagName) + .ifPresent(tag -> { + final String oldPermalink = tag.getStatusOrDefault().getPermalink(); + + tagPermalinkPolicy.onPermalinkDelete(tag); + + String permalink = tagPermalinkPolicy.permalink(tag); + tag.getStatusOrDefault().setPermalink(permalink); + tagPermalinkPolicy.onPermalinkAdd(tag); + + if (!StringUtils.equals(permalink, oldPermalink)) { + client.update(tag); + } + }); + } + + @Override + public Controller setupWith(ControllerBuilder builder) { + return builder + .extension(new Tag()) + .build(); + } +} diff --git a/src/test/java/run/halo/app/core/extension/reconciler/TagReconcilerTest.java b/src/test/java/run/halo/app/core/extension/reconciler/TagReconcilerTest.java index 1a2cfec0bf..2edaa3dddf 100644 --- a/src/test/java/run/halo/app/core/extension/reconciler/TagReconcilerTest.java +++ b/src/test/java/run/halo/app/core/extension/reconciler/TagReconcilerTest.java @@ -40,32 +40,6 @@ class TagReconcilerTest { @InjectMocks private TagReconciler tagReconciler; - @Test - void reconcile() { - Tag tag = tag(); - when(client.fetch(eq(Tag.class), eq("fake-tag"))) - .thenReturn(Optional.of(tag)); - when(tagPermalinkPolicy.permalink(any())) - .thenAnswer(arg -> "/tags/" + tag.getSpec().getSlug()); - ArgumentCaptor captor = ArgumentCaptor.forClass(Tag.class); - - tagReconciler.reconcile(new TagReconciler.Request("fake-tag")); - - verify(client, times(3)).update(captor.capture()); - verify(tagPermalinkPolicy, times(1)).onPermalinkAdd(any()); - verify(tagPermalinkPolicy, times(1)).onPermalinkDelete(any()); - Tag capture = captor.getValue(); - assertThat(capture.getStatus().getPermalink()).isEqualTo("/tags/fake-slug"); - - // change slug - tag.getSpec().setSlug("new-slug"); - tagReconciler.reconcile(new TagReconciler.Request("fake-tag")); - verify(client, times(4)).update(captor.capture()); - verify(tagPermalinkPolicy, times(2)).onPermalinkAdd(any()); - verify(tagPermalinkPolicy, times(2)).onPermalinkDelete(any()); - assertThat(capture.getStatus().getPermalink()).isEqualTo("/tags/new-slug"); - } - @Test void reconcileDelete() { Tag tag = tag();