Skip to content

Commit

Permalink
Move post events into api modules
Browse files Browse the repository at this point in the history
Signed-off-by: JohnNiang <[email protected]>
  • Loading branch information
JohnNiang committed Jun 7, 2024
1 parent ccbe185 commit a715a00
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 121 deletions.
22 changes: 22 additions & 0 deletions api/src/main/java/run/halo/app/event/post/PostDeletedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package run.halo.app.event.post;

import run.halo.app.core.extension.content.Post;

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;
}
}
27 changes: 27 additions & 0 deletions api/src/main/java/run/halo/app/event/post/PostEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
12 changes: 12 additions & 0 deletions api/src/main/java/run/halo/app/event/post/PostPublishedEvent.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package run.halo.app.event.post;

public class PostUnpublishedEvent extends PostEvent {

public PostUnpublishedEvent(Object source, String postName) {
super(source, postName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package run.halo.app.event.post;

public class PostUpdatedEvent extends PostEvent {

public PostUpdatedEvent(Object source, String postName) {
super(source, postName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package run.halo.app.event.post;

import lombok.Getter;
import org.springframework.lang.Nullable;
import run.halo.app.core.extension.content.Post;

@Getter
public class PostVisibleChangedEvent extends PostEvent {

@Nullable
private final Post.VisibleEnum oldVisible;

private final Post.VisibleEnum newVisible;

public PostVisibleChangedEvent(Object source, String postName,
Post.VisibleEnum oldVisible, Post.VisibleEnum newVisible) {
super(source, postName);
this.oldVisible = oldVisible;
this.newVisible = newVisible;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* <p>It is a symbolic annotation.</p>
* <p>When the event marked with {@link SharedEvent} annotation is published, it will be
* broadcast to the application context of the plugin by
* {@link PluginApplicationEventBridgeDispatcher}.</p>
* {@link SharedEventListenerRegistry}.</p>
*
* @author guqing
* @since 2.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -88,7 +89,7 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {

@Override
public Result reconcile(Request request) {
var events = new HashSet<ApplicationEvent>();
var events = new LinkedHashSet<ApplicationEvent>();
client.fetch(Post.class, request.name())
.ifPresent(post -> {
if (ExtensionOperator.isDeleted(post)) {
Expand All @@ -104,7 +105,7 @@ public Result reconcile(Request request) {
}
addFinalizers(post.getMetadata(), Set.of(FINALIZER_NAME));

populateLabels(post);
populateLabels(post, events);

schedulePublishIfNecessary(post);

Expand Down Expand Up @@ -195,16 +196,15 @@ public Result reconcile(Request request) {
return Result.doNotRetry();
}

private void populateLabels(Post post) {
private void populateLabels(Post post, Set<ApplicationEvent> events) {
var labels = nullSafeLabels(post);
labels.put(Post.DELETED_LABEL, String.valueOf(isTrue(post.getSpec().getDeleted())));

var expectVisible = defaultIfNull(post.getSpec().getVisible(), VisibleEnum.PUBLIC);
var oldVisible = VisibleEnum.from(labels.get(Post.VISIBLE_LABEL));
if (!Objects.equals(oldVisible, expectVisible)) {
var postName = post.getMetadata().getName();
eventPublisher.publishEvent(
new PostVisibleChangedEvent(postName, oldVisible, expectVisible));
events.add(new PostVisibleChangedEvent(this, postName, oldVisible, expectVisible));
}
labels.put(Post.VISIBLE_LABEL, expectVisible.toString());

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit a715a00

Please sign in to comment.