From d24a15410e873785828577fa940be47051e217b5 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Fri, 9 Jul 2021 17:55:48 +0800 Subject: [PATCH] send notification for important heartbeat --- server/model/monitor.js | 38 +++++++++++++++++++++++++-- server/server.js | 28 +++++++++++++++++--- src/components/NotificationDialog.vue | 25 ++++++++++++++++-- src/pages/EditMonitor.vue | 13 ++++++--- src/pages/Settings.vue | 18 ++++++++++--- 5 files changed, 108 insertions(+), 14 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 062f3406..f92e43e7 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -8,7 +8,7 @@ const axios = require("axios"); const {tcping, ping} = require("../util-server"); const {R} = require("redbean-node"); const {BeanModel} = require("redbean-node/dist/bean-model"); - +const {Notification} = require("../notification") /** * status: @@ -17,7 +17,18 @@ const {BeanModel} = require("redbean-node/dist/bean-model"); */ class Monitor extends BeanModel { - toJSON() { + async toJSON() { + + let notificationIDList = {}; + + let list = await R.find("monitor_notification", " monitor_id = ? ", [ + this.id + ]) + + for (let bean of list) { + notificationIDList[bean.notification_id] = true; + } + return { id: this.id, name: this.name, @@ -29,6 +40,7 @@ class Monitor extends BeanModel { type: this.type, interval: this.interval, keyword: this.keyword, + notificationIDList }; } @@ -96,6 +108,28 @@ class Monitor extends BeanModel { // Mark as important if status changed if (! previousBeat || previousBeat.status !== bean.status) { bean.important = true; + + let notificationList = await R.getAll(`SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? `, [ + this.id + ]) + + let promiseList = []; + + let text; + if (bean.status === 1) { + text = "✅ Up" + } else { + text = "🔴 Down" + } + + let msg = `[${this.name}] [${text}] ${bean.msg}`; + + for(let notification of notificationList) { + promiseList.push(Notification.send(JSON.parse(notification.config), msg)); + } + + await Promise.all(promiseList); + } else { bean.important = false; } diff --git a/server/server.js b/server/server.js index bf30ec81..27a4951d 100644 --- a/server/server.js +++ b/server/server.js @@ -111,12 +111,17 @@ let monitorList = {}; socket.on("add", async (monitor, callback) => { try { checkLogin(socket) - let bean = R.dispense("monitor") + + let notificationIDList = monitor.notificationIDList; + delete monitor.notificationIDList; + bean.import(monitor) bean.user_id = socket.userID await R.store(bean) + await updateMonitorNotification(bean.id, notificationIDList) + await startMonitor(socket.userID, bean.id); await sendMonitorList(socket); @@ -154,6 +159,8 @@ let monitorList = {}; await R.store(bean) + await updateMonitorNotification(bean.id, monitor.notificationIDList) + if (bean.active) { await restartMonitor(socket.userID, bean.id) } @@ -188,7 +195,7 @@ let monitorList = {}; callback({ ok: true, - monitor: bean.toJSON(), + monitor: await bean.toJSON(), }); } catch (e) { @@ -391,6 +398,21 @@ let monitorList = {}; })(); +async function updateMonitorNotification(monitorID, notificationIDList) { + R.exec("DELETE FROM monitor_notification WHERE monitor_id = ? ", [ + monitorID + ]) + + for (let notificationID in notificationIDList) { + if (notificationIDList[notificationID]) { + let relation = R.dispense("monitor_notification"); + relation.monitor_id = monitorID; + relation.notification_id = notificationID; + await R.store(relation) + } + } +} + async function checkOwner(userID, monitorID) { let row = await R.getRow("SELECT id FROM monitor WHERE id = ? AND user_id = ? ", [ monitorID, @@ -445,7 +467,7 @@ async function getMonitorJSONList(userID) { ]) for (let monitor of monitorList) { - result[monitor.id] = monitor.toJSON(); + result[monitor.id] = await monitor.toJSON(); } return result; diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index 1078195c..2dc58eb3 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -41,7 +41,11 @@
+ Support Direct Chat / Group / Channel's Chat ID + +

You can get your chat id by sending message to the bot and go to this url to view the chat_id: +

@@ -59,7 +63,7 @@

@@ -68,6 +72,8 @@ + + Are you sure want to delete this notification for all monitors?