Move post events into api modules (#6052)

Signed-off-by: JohnNiang <johnniang@foxmail.com>
pull/6059/head
John Niang 2024-06-07 18:34:09 +08:00 committed by GitHub
parent ccbe18567f
commit b692db1f57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 123 additions and 122 deletions

View File

@ -0,0 +1,24 @@
package run.halo.app.event.post;
import run.halo.app.core.extension.content.Post;
import run.halo.app.plugin.SharedEvent;
@SharedEvent
public class PostDeletedEvent extends PostEvent {
private final Post post;
public PostDeletedEvent(Object source, Post post) {
super(source, post.getMetadata().getName());
this.post = post;
}
/**
* Get original post.
*
* @return original post.
*/
public Post getPost() {
return post;
}
}

View File

@ -0,0 +1,27 @@
package run.halo.app.event.post;
import org.springframework.context.ApplicationEvent;
/**
* An abstract class for post events.
*
* @author johnniang
*/
public abstract class PostEvent extends ApplicationEvent {
private final String name;
public PostEvent(Object source, String name) {
super(source);
this.name = name;
}
/**
* Gets post metadata name.
*
* @return post metadata name
*/
public String getName() {
return name;
}
}

View File

@ -0,0 +1,12 @@
package run.halo.app.event.post;
import run.halo.app.plugin.SharedEvent;
@SharedEvent
public class PostPublishedEvent extends PostEvent {
public PostPublishedEvent(Object source, String postName) {
super(source, postName);
}
}

View File

@ -0,0 +1,12 @@
package run.halo.app.event.post;
import run.halo.app.plugin.SharedEvent;
@SharedEvent
public class PostUnpublishedEvent extends PostEvent {
public PostUnpublishedEvent(Object source, String postName) {
super(source, postName);
}
}

View File

@ -0,0 +1,12 @@
package run.halo.app.event.post;
import run.halo.app.plugin.SharedEvent;
@SharedEvent
public class PostUpdatedEvent extends PostEvent {
public PostUpdatedEvent(Object source, String postName) {
super(source, postName);
}
}

View File

@ -0,0 +1,30 @@
package run.halo.app.event.post;
import org.springframework.lang.Nullable;
import run.halo.app.core.extension.content.Post;
import run.halo.app.plugin.SharedEvent;
@SharedEvent
public class PostVisibleChangedEvent extends PostEvent {
@Nullable
private final Post.VisibleEnum oldVisible;
private final Post.VisibleEnum newVisible;
public PostVisibleChangedEvent(Object source, String postName,
@Nullable Post.VisibleEnum oldVisible, Post.VisibleEnum newVisible) {
super(source, postName);
this.oldVisible = oldVisible;
this.newVisible = newVisible;
}
@Nullable
public Post.VisibleEnum getOldVisible() {
return oldVisible;
}
public Post.VisibleEnum getNewVisible() {
return newVisible;
}
}

View File

@ -9,8 +9,7 @@ import java.lang.annotation.Target;
/** /**
* <p>It is a symbolic annotation.</p> * <p>It is a symbolic annotation.</p>
* <p>When the event marked with {@link SharedEvent} annotation is published, it will be * <p>When the event marked with {@link SharedEvent} annotation is published, it will be
* broadcast to the application context of the plugin by * broadcast to the application context of the plugin.
* {@link PluginApplicationEventBridgeDispatcher}.</p>
* *
* @author guqing * @author guqing
* @since 2.0.0 * @since 2.0.0

View File

@ -15,6 +15,7 @@ import com.google.common.hash.Hashing;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -88,7 +89,7 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
@Override @Override
public Result reconcile(Request request) { public Result reconcile(Request request) {
var events = new HashSet<ApplicationEvent>(); var events = new LinkedHashSet<ApplicationEvent>();
client.fetch(Post.class, request.name()) client.fetch(Post.class, request.name())
.ifPresent(post -> { .ifPresent(post -> {
if (ExtensionOperator.isDeleted(post)) { if (ExtensionOperator.isDeleted(post)) {
@ -104,7 +105,7 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
} }
addFinalizers(post.getMetadata(), Set.of(FINALIZER_NAME)); addFinalizers(post.getMetadata(), Set.of(FINALIZER_NAME));
populateLabels(post); populateLabels(post, events);
schedulePublishIfNecessary(post); schedulePublishIfNecessary(post);
@ -195,7 +196,7 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
return Result.doNotRetry(); return Result.doNotRetry();
} }
private void populateLabels(Post post) { 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())));
@ -203,8 +204,7 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
var oldVisible = VisibleEnum.from(labels.get(Post.VISIBLE_LABEL)); var oldVisible = VisibleEnum.from(labels.get(Post.VISIBLE_LABEL));
if (!Objects.equals(oldVisible, expectVisible)) { if (!Objects.equals(oldVisible, expectVisible)) {
var postName = post.getMetadata().getName(); var postName = post.getMetadata().getName();
eventPublisher.publishEvent( events.add(new PostVisibleChangedEvent(this, postName, oldVisible, expectVisible));
new PostVisibleChangedEvent(postName, oldVisible, expectVisible));
} }
labels.put(Post.VISIBLE_LABEL, expectVisible.toString()); labels.put(Post.VISIBLE_LABEL, expectVisible.toString());

View File

@ -1,21 +0,0 @@
package run.halo.app.event.post;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
import run.halo.app.core.extension.content.Post;
@Getter
public class PostDeletedEvent extends ApplicationEvent implements PostEvent {
private final Post post;
public PostDeletedEvent(Object source, Post post) {
super(source);
this.post = post;
}
@Override
public String getName() {
return post.getMetadata().getName();
}
}

View File

@ -1,7 +0,0 @@
package run.halo.app.event.post;
public interface PostEvent {
String getName();
}

View File

@ -1,21 +0,0 @@
package run.halo.app.event.post;
import org.springframework.context.ApplicationEvent;
import run.halo.app.plugin.SharedEvent;
@SharedEvent
public class PostPublishedEvent extends ApplicationEvent implements PostEvent {
private final String postName;
public PostPublishedEvent(Object source, String postName) {
super(source);
this.postName = postName;
}
@Override
public String getName() {
return postName;
}
}

View File

@ -1,19 +0,0 @@
package run.halo.app.event.post;
import org.springframework.context.ApplicationEvent;
public class PostUnpublishedEvent extends ApplicationEvent implements PostEvent {
private final String postName;
public PostUnpublishedEvent(Object source, String postName) {
super(source);
this.postName = postName;
}
@Override
public String getName() {
return postName;
}
}

View File

@ -1,18 +0,0 @@
package run.halo.app.event.post;
import org.springframework.context.ApplicationEvent;
public class PostUpdatedEvent extends ApplicationEvent implements PostEvent {
private final String postName;
public PostUpdatedEvent(Object source, String postName) {
super(source);
this.postName = postName;
}
@Override
public String getName() {
return postName;
}
}

View File

@ -1,29 +0,0 @@
package run.halo.app.event.post;
import lombok.Data;
import org.springframework.lang.Nullable;
import run.halo.app.core.extension.content.Post;
@Data
public class PostVisibleChangedEvent implements PostEvent {
private final String postName;
@Nullable
private final Post.VisibleEnum oldVisible;
private final Post.VisibleEnum newVisible;
public PostVisibleChangedEvent(String postName, Post.VisibleEnum oldVisible,
Post.VisibleEnum newVisible) {
this.postName = postName;
this.oldVisible = oldVisible;
this.newVisible = newVisible;
}
@Override
public String getName() {
return postName;
}
}