Переглянути джерело

更新 NotifyService, NotifyReceiver 不存在时不发送通知

reghao 2 роки тому
батько
коміт
8a5341b4dd

+ 22 - 3
manager/src/main/java/cn/reghao/devops/manager/notification/service/NotifyService.java

@@ -1,5 +1,8 @@
 package cn.reghao.devops.manager.notification.service;
 
+import cn.reghao.devops.manager.notification.db.repository.NotifyReceiverRepository;
+import cn.reghao.devops.manager.notification.model.NotifyType;
+import cn.reghao.devops.manager.notification.model.po.NotifyReceiver;
 import cn.reghao.devops.manager.notification.service.notifier.Notify;
 import cn.reghao.devops.manager.notification.service.notifier.ding.DingMsg;
 import cn.reghao.devops.manager.notification.service.notifier.ding.DingNotify;
@@ -23,15 +26,31 @@ public class NotifyService {
     private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("notify-service");
     private final DingNotify dingNotify;
     private final EmailNotify emailNotify;
+    private final NotifyReceiverRepository notifyReceiverRepository;
 
-    public NotifyService(DingNotify dingNotify, EmailNotify emailNotify) {
+    public NotifyService(DingNotify dingNotify, EmailNotify emailNotify, NotifyReceiverRepository notifyReceiverRepository) {
         this.dingNotify = dingNotify;
         this.emailNotify = emailNotify;
+        this.notifyReceiverRepository = notifyReceiverRepository;
     }
 
     public <T> void notify(T msg) {
-        String receiver = "";
-        notify(receiver, msg);
+        String receiver;
+        if (msg instanceof DingMsg) {
+            NotifyReceiver notifyReceiver = notifyReceiverRepository.findByType(NotifyType.webhook.getName());
+            if (notifyReceiver != null) {
+                receiver = notifyReceiver.getUrl();
+                DingMsg dingMsg = (DingMsg) msg;
+                threadPool.execute(new NotifyTask<>(dingNotify, receiver, dingMsg));
+            }
+        } else if (msg instanceof EmailMsg) {
+            NotifyReceiver notifyReceiver = notifyReceiverRepository.findByType(NotifyType.email.getName());
+            if (notifyReceiver != null) {
+                receiver = notifyReceiver.getUrl();
+                EmailMsg emailMsg = (EmailMsg) msg;
+                threadPool.execute(new NotifyTask<>(emailNotify, receiver, emailMsg));
+            }
+        }
     }
 
     public <T> void notify(String receiver, T msg) {