Skip to content

Commit

Permalink
init app
Browse files Browse the repository at this point in the history
  • Loading branch information
monyuan committed Jan 2, 2024
1 parent ad33810 commit b6a5440
Show file tree
Hide file tree
Showing 22 changed files with 770 additions and 205 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id "run.halo.plugin.devtools" version "0.0.7"
}

group 'run.halo.starter'
group 'notifyme.plugin.halo.run'
sourceCompatibility = JavaVersion.VERSION_17

repositories {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
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
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pluginManagement {
gradlePluginPortal()
}
}
rootProject.name = 'plugin-starter'
rootProject.name = 'NotifyMe'

33 changes: 33 additions & 0 deletions src/main/java/run/halo/notifyme/NotifyMePlugin.java
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("插件停止!");
}
}
41 changes: 41 additions & 0 deletions src/main/java/run/halo/notifyme/domain/NotifyMe.java
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; // 文章审核通知开关

}
14 changes: 14 additions & 0 deletions src/main/java/run/halo/notifyme/domain/PostDo.java
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;
}
10 changes: 10 additions & 0 deletions src/main/java/run/halo/notifyme/domain/PushDo.java
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 src/main/java/run/halo/notifyme/event/NotifyBaseEvent.java
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 src/main/java/run/halo/notifyme/event/PushEventNotify.java
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 src/main/java/run/halo/notifyme/listener/NotifyListener.java
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 src/main/java/run/halo/notifyme/strategy/CommentStrategy.java
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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/run/halo/notifyme/strategy/NotifyStrategy.java
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);
}
Loading

0 comments on commit b6a5440

Please sign in to comment.