|
| 1 | +package run.halo.app.content; |
| 2 | + |
| 3 | +import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; |
| 4 | +import static run.halo.app.extension.index.query.QueryFactory.and; |
| 5 | +import static run.halo.app.extension.index.query.QueryFactory.equal; |
| 6 | +import static run.halo.app.extension.index.query.QueryFactory.isNull; |
| 7 | + |
| 8 | +import com.google.common.collect.Sets; |
| 9 | +import java.time.Duration; |
| 10 | +import java.time.Instant; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.Set; |
| 14 | +import org.apache.commons.lang3.StringUtils; |
| 15 | +import org.springframework.context.SmartLifecycle; |
| 16 | +import org.springframework.context.event.EventListener; |
| 17 | +import org.springframework.stereotype.Component; |
| 18 | +import run.halo.app.core.extension.content.Post; |
| 19 | +import run.halo.app.core.extension.content.Tag; |
| 20 | +import run.halo.app.core.extension.content.Tag.TagStatus; |
| 21 | +import run.halo.app.event.post.PostDeletedEvent; |
| 22 | +import run.halo.app.event.post.PostEvent; |
| 23 | +import run.halo.app.event.post.PostUpdatedEvent; |
| 24 | +import run.halo.app.extension.ExtensionClient; |
| 25 | +import run.halo.app.extension.ListOptions; |
| 26 | +import run.halo.app.extension.MetadataUtil; |
| 27 | +import run.halo.app.extension.PageRequestImpl; |
| 28 | +import run.halo.app.extension.controller.Controller; |
| 29 | +import run.halo.app.extension.controller.ControllerBuilder; |
| 30 | +import run.halo.app.extension.controller.DefaultController; |
| 31 | +import run.halo.app.extension.controller.DefaultQueue; |
| 32 | +import run.halo.app.extension.controller.Reconciler; |
| 33 | +import run.halo.app.extension.controller.RequestQueue; |
| 34 | +import run.halo.app.extension.router.selector.FieldSelector; |
| 35 | +import run.halo.app.extension.router.selector.LabelSelector; |
| 36 | +import run.halo.app.infra.utils.JsonUtils; |
| 37 | + |
| 38 | +/** |
| 39 | + * Update {@link TagStatus#postCount} when post related to tag is updated. |
| 40 | + * |
| 41 | + * @author guqing |
| 42 | + * @since 2.13.0 |
| 43 | + */ |
| 44 | +@Component |
| 45 | +public class TagPostCountUpdater |
| 46 | + implements Reconciler<TagPostCountUpdater.PostRelatedTags>, SmartLifecycle { |
| 47 | + |
| 48 | + private final RequestQueue<PostRelatedTags> tagQueue; |
| 49 | + |
| 50 | + private final Controller postEventController; |
| 51 | + |
| 52 | + private final ExtensionClient client; |
| 53 | + |
| 54 | + private volatile boolean running = false; |
| 55 | + |
| 56 | + /** |
| 57 | + * Construct a {@link TagPostCountUpdater} with the given {@link ExtensionClient}. |
| 58 | + */ |
| 59 | + public TagPostCountUpdater(ExtensionClient client) { |
| 60 | + this.client = client; |
| 61 | + |
| 62 | + this.tagQueue = new DefaultQueue<>(Instant::now); |
| 63 | + this.postEventController = this.setupWith(null); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Result reconcile(PostRelatedTags postRelatedTags) { |
| 68 | + for (var tag : postRelatedTags.tags()) { |
| 69 | + updateTagRelatedPostCount(tag); |
| 70 | + } |
| 71 | + |
| 72 | + // Update last associated tags when handled |
| 73 | + client.fetch(Post.class, postRelatedTags.postName()).ifPresent(post -> { |
| 74 | + var tags = defaultIfNull(post.getSpec().getTags(), List.<String>of()); |
| 75 | + var annotations = MetadataUtil.nullSafeAnnotations(post); |
| 76 | + var tagAnno = JsonUtils.objectToJson(tags); |
| 77 | + var oldTagAnno = annotations.get(Post.LAST_ASSOCIATED_TAGS_ANNO); |
| 78 | + |
| 79 | + if (!tagAnno.equals(oldTagAnno)) { |
| 80 | + annotations.put(Post.LAST_ASSOCIATED_TAGS_ANNO, tagAnno); |
| 81 | + client.update(post); |
| 82 | + } |
| 83 | + }); |
| 84 | + return Result.doNotRetry(); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + @Override |
| 89 | + public Controller setupWith(ControllerBuilder builder) { |
| 90 | + return new DefaultController<>( |
| 91 | + this.getClass().getName(), |
| 92 | + this, |
| 93 | + tagQueue, |
| 94 | + null, |
| 95 | + Duration.ofMillis(100), |
| 96 | + Duration.ofMinutes(10) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void start() { |
| 102 | + postEventController.start(); |
| 103 | + running = true; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void stop() { |
| 108 | + running = false; |
| 109 | + postEventController.dispose(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean isRunning() { |
| 114 | + return running; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Listen to post event to calculate post related to tag for updating. |
| 119 | + */ |
| 120 | + @EventListener(PostEvent.class) |
| 121 | + public void onPostUpdated(PostEvent postEvent) { |
| 122 | + var postName = postEvent.getName(); |
| 123 | + if (postEvent instanceof PostUpdatedEvent) { |
| 124 | + var tagsToUpdate = calcTagsToUpdate(postEvent.getName()); |
| 125 | + tagQueue.addImmediately(new PostRelatedTags(postName, tagsToUpdate)); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + if (postEvent instanceof PostDeletedEvent deletedEvent) { |
| 130 | + var tags = defaultIfNull(deletedEvent.getPost().getSpec().getTags(), |
| 131 | + List.<String>of()); |
| 132 | + tagQueue.addImmediately(new PostRelatedTags(postName, Sets.newHashSet(tags))); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private Set<String> calcTagsToUpdate(String postName) { |
| 137 | + var post = client.fetch(Post.class, postName).orElseThrow(); |
| 138 | + var annotations = MetadataUtil.nullSafeAnnotations(post); |
| 139 | + var oldTags = Optional.ofNullable(annotations.get(Post.LAST_ASSOCIATED_TAGS_ANNO)) |
| 140 | + .filter(StringUtils::isNotBlank) |
| 141 | + .map(tagsJson -> JsonUtils.jsonToObject(tagsJson, String[].class)) |
| 142 | + .orElse(new String[0]); |
| 143 | + |
| 144 | + var tagsToUpdate = Sets.newHashSet(oldTags); |
| 145 | + var newTags = post.getSpec().getTags(); |
| 146 | + if (newTags != null) { |
| 147 | + tagsToUpdate.addAll(newTags); |
| 148 | + } |
| 149 | + return tagsToUpdate; |
| 150 | + } |
| 151 | + |
| 152 | + public record PostRelatedTags(String postName, Set<String> tags) { |
| 153 | + } |
| 154 | + |
| 155 | + private void updateTagRelatedPostCount(String tagName) { |
| 156 | + client.fetch(Tag.class, tagName).ifPresent(tag -> { |
| 157 | + var commonFieldQuery = and( |
| 158 | + equal("spec.tags", tag.getMetadata().getName()), |
| 159 | + isNull("metadata.deletionTimestamp") |
| 160 | + ); |
| 161 | + // Update post count |
| 162 | + var allPostOptions = new ListOptions(); |
| 163 | + allPostOptions.setFieldSelector(FieldSelector.of(commonFieldQuery)); |
| 164 | + var result = client.listBy(Post.class, allPostOptions, PageRequestImpl.ofSize(1)); |
| 165 | + tag.getStatusOrDefault().setPostCount((int) result.getTotal()); |
| 166 | + |
| 167 | + // Update visible post count |
| 168 | + var publicPostOptions = new ListOptions(); |
| 169 | + publicPostOptions.setLabelSelector(LabelSelector.builder() |
| 170 | + .eq(Post.PUBLISHED_LABEL, "true") |
| 171 | + .build()); |
| 172 | + publicPostOptions.setFieldSelector(FieldSelector.of( |
| 173 | + and( |
| 174 | + commonFieldQuery, |
| 175 | + equal("spec.deleted", "false"), |
| 176 | + equal("spec.visible", Post.VisibleEnum.PUBLIC.name()) |
| 177 | + ) |
| 178 | + )); |
| 179 | + var publicPosts = |
| 180 | + client.listBy(Post.class, publicPostOptions, PageRequestImpl.ofSize(1)); |
| 181 | + tag.getStatusOrDefault().setVisiblePostCount((int) publicPosts.getTotal()); |
| 182 | + |
| 183 | + client.update(tag); |
| 184 | + }); |
| 185 | + } |
| 186 | +} |
0 commit comments