generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
770 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
version=1.0.0-SNAPSHOT | ||
org.gradle.java.home=/Users/mon/Library/Java/JavaVirtualMachines/corretto-17.0.9/Contents/Home |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ pluginManagement { | |
gradlePluginPortal() | ||
} | ||
} | ||
rootProject.name = 'plugin-starter' | ||
rootProject.name = 'NotifyMe' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package run.halo.notifyme; | ||
|
||
import org.pf4j.PluginWrapper; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.stereotype.Component; | ||
import run.halo.app.extension.Scheme; | ||
import run.halo.app.plugin.BasePlugin; | ||
import run.halo.app.extension.SchemeManager; | ||
import run.halo.notifyme.domain.NotifyMe; | ||
|
||
@Component | ||
@EnableAsync | ||
public class NotifyMePlugin extends BasePlugin { | ||
private final SchemeManager schemeManager; | ||
|
||
public NotifyMePlugin(PluginWrapper wrapper, SchemeManager schemeManager) { | ||
super(wrapper); | ||
this.schemeManager = schemeManager; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
schemeManager.register(NotifyMe.class); | ||
System.out.println("插件启动成功!"); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
Scheme notifyMeScheme = schemeManager.get(NotifyMe.class); | ||
schemeManager.unregister(notifyMeScheme); | ||
System.out.println("插件停止!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package run.halo.notifyme.domain; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import run.halo.app.extension.AbstractExtension; | ||
import run.halo.app.extension.GVK; | ||
|
||
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
@GVK(kind = "NotifyMe", group = "notifyme.plugin.halo.run", | ||
version = "v1alpha1", singular = "notifyme", plural = "notifymes") | ||
public class NotifyMe extends AbstractExtension { | ||
|
||
@Schema(requiredMode = REQUIRED, minLength = 10) | ||
private String apiKey; | ||
|
||
@Schema(requiredMode = REQUIRED, minLength = 4) | ||
private String channel; | ||
|
||
@Schema(requiredMode = REQUIRED) | ||
private String siteUrl; //站点文章的路径 | ||
|
||
@Schema(defaultValue = "false") | ||
private Boolean status; // 通知总开关 | ||
|
||
@Schema(defaultValue = "true") | ||
private Boolean commentStatus; // 评论通知开关 | ||
|
||
@Schema(defaultValue = "false") | ||
private Boolean commentAuditsStatus; // 评论审核通知 | ||
|
||
@Schema(defaultValue = "false") | ||
private Boolean postStatus; // 发布文章开关 | ||
|
||
@Schema(defaultValue = "false") | ||
private Boolean postAuditsStatus; // 文章审核通知开关 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package run.halo.notifyme.domain; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class PostDo { | ||
private String title; | ||
|
||
private String slug; | ||
|
||
private String owner; | ||
|
||
private String createTime; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package run.halo.notifyme.domain; | ||
|
||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class PushDo { | ||
private String title; | ||
private String content; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/run/halo/notifyme/event/NotifyBaseEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package run.halo.notifyme.event; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
import run.halo.app.extension.Extension; | ||
|
||
@Getter | ||
public class NotifyBaseEvent extends ApplicationEvent { | ||
private final Extension extension; | ||
|
||
public NotifyBaseEvent(Object source, Extension extension) { | ||
super(source); | ||
this.extension = extension; | ||
} | ||
|
||
|
||
public static NotifyBaseEvent getEx(Object o, Extension extension) { | ||
return new NotifyBaseEvent(o, extension); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/run/halo/notifyme/event/PushEventNotify.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package run.halo.notifyme.event; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.stereotype.Component; | ||
import run.halo.app.extension.Extension; | ||
import run.halo.app.extension.ExtensionClient; | ||
import run.halo.app.extension.Watcher; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class PushEventNotify implements Watcher, InitializingBean { | ||
|
||
private final ExtensionClient client; | ||
private final ApplicationEventPublisher eventPublisher; | ||
|
||
private Runnable disposeHook; | ||
private volatile boolean disposed = false; | ||
|
||
@Override | ||
public void onAdd(Extension extension) { | ||
eventPublisher.publishEvent(NotifyBaseEvent.getEx(this, extension)); | ||
} | ||
|
||
@Override | ||
public void registerDisposeHook(Runnable dispose) { | ||
this.disposeHook = dispose; | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return this.disposed; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
client.watch(this); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
if (isDisposed()) { | ||
return; | ||
} | ||
this.disposed = true; | ||
if (this.disposeHook != null) { | ||
this.disposeHook.run(); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/run/halo/notifyme/listener/NotifyListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package run.halo.notifyme.listener; | ||
|
||
|
||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import run.halo.app.core.extension.content.Comment; | ||
import run.halo.app.core.extension.content.Post; | ||
import run.halo.app.extension.Extension; | ||
import run.halo.app.extension.ExtensionClient; | ||
import run.halo.notifyme.domain.NotifyMe; | ||
import run.halo.notifyme.event.NotifyBaseEvent; | ||
import run.halo.notifyme.strategy.CommentStrategy; | ||
import run.halo.notifyme.strategy.NotifyStrategy; | ||
import run.halo.notifyme.strategy.PostStrategy; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@Async | ||
@Component | ||
@RequiredArgsConstructor | ||
public class NotifyListener implements ApplicationListener<NotifyBaseEvent> { | ||
|
||
|
||
private final PostStrategy postStrategy; | ||
private final CommentStrategy commentStrategy; | ||
private final ExtensionClient client; | ||
|
||
|
||
@Override | ||
public void onApplicationEvent(@NonNull NotifyBaseEvent event) { | ||
NotifyStrategy strategy = getStrategyForExtension(event.getExtension()); | ||
if (strategy != null) { | ||
Optional<NotifyMe> config = client.fetch(NotifyMe.class, "notify-config"); | ||
config.ifPresent(c -> strategy.process(event, c)); | ||
} | ||
} | ||
|
||
private NotifyStrategy getStrategyForExtension(Extension extension) { | ||
if (extension instanceof Post) { | ||
return postStrategy; | ||
} | ||
if (extension instanceof Comment) { | ||
return commentStrategy; | ||
} | ||
return null; | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/run/halo/notifyme/strategy/CommentStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package run.halo.notifyme.strategy; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import run.halo.app.core.extension.content.Comment; | ||
import run.halo.app.core.extension.content.Post; | ||
import run.halo.notifyme.domain.NotifyMe; | ||
import run.halo.notifyme.domain.PushDo; | ||
import run.halo.notifyme.event.NotifyBaseEvent; | ||
import java.util.Optional; | ||
import run.halo.app.extension.ExtensionClient; | ||
import run.halo.notifyme.util.PushUtil; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CommentStrategy implements NotifyStrategy { | ||
private final ExtensionClient client; | ||
private final PushUtil pushUtil; | ||
|
||
@Override | ||
public void process(NotifyBaseEvent event, NotifyMe setting) { | ||
Comment commentInfo = (Comment) event.getExtension(); | ||
Comment.CommentSpec commentSpec = commentInfo.getSpec(); | ||
if (!commentSpec.getOwner().getName().equals("admin")) { | ||
// 不是管理员的话就推送通知 | ||
String postMetaName = commentSpec.getSubjectRef().getName(); | ||
Optional<Post> postInfo = client.fetch(Post.class, postMetaName); | ||
postInfo.ifPresent(post -> { | ||
if (!commentSpec.getApproved()) { //等待审核的 | ||
audits(post, setting, commentSpec); | ||
} else { | ||
publish(post, setting, commentSpec); | ||
} | ||
}); | ||
|
||
} | ||
} | ||
|
||
private void publish(Post post, NotifyMe setting, Comment.CommentSpec commentSpec) { // 评论发布通知 | ||
if (setting.getCommentStatus()) { | ||
String title = String.format("《%s》上有新评论", post.getSpec().getTitle()); | ||
String content = String.format("%s说: %s \n\n[查看评论](%s)", | ||
commentSpec.getOwner().getDisplayName(), | ||
commentSpec.getContent(), | ||
setting.getSiteUrl() + post.getStatus().getPermalink() | ||
); | ||
push(title, content, setting); | ||
} | ||
} | ||
|
||
private void audits(Post post, NotifyMe setting, Comment.CommentSpec commentSpec) { // 评论审核通知 | ||
if (setting.getCommentAuditsStatus()) { | ||
String title = String.format("《%s》上有新评论等待审核", post.getSpec().getTitle()); | ||
String content = String.format("%s说: %s \n\n[现在去审核](%s)", | ||
commentSpec.getOwner().getDisplayName(), | ||
commentSpec.getContent(), | ||
setting.getSiteUrl() + "/console/comments" | ||
); | ||
push(title, content, setting); | ||
} | ||
} | ||
|
||
private void push(String title, String content, NotifyMe setting) { // 推送 | ||
PushDo pushDo = new PushDo(); | ||
pushDo.setTitle(title); | ||
pushDo.setContent(content); | ||
pushUtil.sendRequest(pushDo, setting); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package run.halo.notifyme.strategy; | ||
|
||
import run.halo.notifyme.domain.NotifyMe; | ||
import run.halo.notifyme.event.NotifyBaseEvent; | ||
|
||
public interface NotifyStrategy { | ||
void process(NotifyBaseEvent event, | ||
NotifyMe setting); | ||
} |
Oops, something went wrong.