1
0
mirror of https://github.com/louislam/uptime-kuma.git synced 2024-12-14 10:23:16 +02:00
uptime-kuma/server/notification.js

167 lines
5.3 KiB
JavaScript
Raw Normal View History

2021-07-27 19:47:13 +02:00
const { R } = require("redbean-node");
2021-09-07 16:42:46 +02:00
const Apprise = require("./notification-providers/apprise");
const Discord = require("./notification-providers/discord");
const Gotify = require("./notification-providers/gotify");
const Line = require("./notification-providers/line");
const LunaSea = require("./notification-providers/lunasea");
const Mattermost = require("./notification-providers/mattermost");
2021-10-05 20:03:56 +02:00
const Matrix = require("./notification-providers/matrix");
2021-09-07 16:42:46 +02:00
const Octopush = require("./notification-providers/octopush");
const PromoSMS = require("./notification-providers/promosms");
const ClickSendSMS = require("./notification-providers/clicksendsms");
2021-09-07 16:42:46 +02:00
const Pushbullet = require("./notification-providers/pushbullet");
const Pushover = require("./notification-providers/pushover");
const Pushy = require("./notification-providers/pushy");
const RocketChat = require("./notification-providers/rocket-chat");
const Signal = require("./notification-providers/signal");
const Slack = require("./notification-providers/slack");
const SMTP = require("./notification-providers/smtp");
const Teams = require("./notification-providers/teams");
2021-09-07 16:42:46 +02:00
const Telegram = require("./notification-providers/telegram");
const Webhook = require("./notification-providers/webhook");
2021-10-11 11:20:09 +02:00
const Feishu = require("./notification-providers/feishu");
2021-10-13 05:55:01 +02:00
const AliyunSms = require("./notification-providers/aliyun-sms");
2021-10-13 10:13:46 +02:00
const DingDing = require("./notification-providers/dingding");
const Bark = require("./notification-providers/bark");
2021-11-11 13:31:28 +02:00
const { log } = require("../src/util");
2021-07-09 08:14:03 +02:00
class Notification {
2021-07-18 12:51:58 +02:00
2021-09-07 16:42:46 +02:00
providerList = {};
static init() {
2021-11-11 13:31:28 +02:00
log("notification", "Prepare Notification Providers");
2021-09-07 16:42:46 +02:00
this.providerList = {};
const list = [
new Apprise(),
2021-10-13 05:55:01 +02:00
new AliyunSms(),
2021-10-13 10:13:46 +02:00
new DingDing(),
2021-09-07 16:42:46 +02:00
new Discord(),
new Teams(),
2021-09-07 16:42:46 +02:00
new Gotify(),
new Line(),
new LunaSea(),
2021-10-11 11:20:09 +02:00
new Feishu(),
2021-09-07 16:42:46 +02:00
new Mattermost(),
2021-10-05 20:03:56 +02:00
new Matrix(),
2021-09-07 16:42:46 +02:00
new Octopush(),
new PromoSMS(),
new ClickSendSMS(),
2021-09-07 16:42:46 +02:00
new Pushbullet(),
new Pushover(),
new Pushy(),
new RocketChat(),
new Signal(),
new Slack(),
new SMTP(),
new Telegram(),
new Webhook(),
new Bark(),
2021-09-07 16:42:46 +02:00
];
for (let item of list) {
if (! item.name) {
throw new Error("Notification provider without name");
}
if (this.providerList[item.name]) {
throw new Error("Duplicate notification provider name");
}
this.providerList[item.name] = item;
}
}
2021-07-18 14:49:46 +02:00
/**
*
2021-09-07 16:42:46 +02:00
* @param notification : BeanModel
* @param msg : string General Message
* @param monitorJSON : object Monitor details (For Up/Down only)
* @param heartbeatJSON : object Heartbeat details (For Up/Down only)
2021-07-18 14:49:46 +02:00
* @returns {Promise<string>} Successful msg
* Throw Error with fail msg
*/
2021-07-09 13:33:22 +02:00
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
2021-09-07 16:42:46 +02:00
if (this.providerList[notification.type]) {
return this.providerList[notification.type].send(notification, msg, monitorJSON, heartbeatJSON);
2021-07-09 08:14:03 +02:00
} else {
2021-09-07 16:42:46 +02:00
throw new Error("Notification type is not supported");
2021-07-09 08:14:03 +02:00
}
}
static async save(notification, notificationID, userID) {
let bean
if (notificationID) {
bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
notificationID,
userID,
])
if (! bean) {
throw new Error("notification not found")
}
} else {
bean = R.dispense("notification")
}
bean.name = notification.name;
bean.user_id = userID;
bean.config = JSON.stringify(notification);
2021-09-09 14:22:32 +02:00
bean.is_default = notification.isDefault || false;
2021-07-09 08:14:03 +02:00
await R.store(bean)
if (notification.applyExisting) {
await applyNotificationEveryMonitor(bean.id, userID);
}
return bean;
2021-07-09 08:14:03 +02:00
}
static async delete(notificationID, userID) {
let bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
notificationID,
userID,
])
if (! bean) {
throw new Error("notification not found")
}
await R.trash(bean)
}
2021-07-09 19:08:08 +02:00
2021-07-18 12:51:58 +02:00
static checkApprise() {
2021-07-27 19:47:13 +02:00
let commandExistsSync = require("command-exists").sync;
let exists = commandExistsSync("apprise");
2021-07-18 12:51:58 +02:00
return exists;
2021-07-10 05:38:00 +02:00
}
2021-07-18 14:49:46 +02:00
}
2021-07-10 05:38:00 +02:00
async function applyNotificationEveryMonitor(notificationID, userID) {
let monitors = await R.getAll("SELECT id FROM monitor WHERE user_id = ?", [
userID
]);
for (let i = 0; i < monitors.length; i++) {
let checkNotification = await R.findOne("monitor_notification", " monitor_id = ? AND notification_id = ? ", [
monitors[i].id,
notificationID,
])
if (! checkNotification) {
let relation = R.dispense("monitor_notification");
relation.monitor_id = monitors[i].id;
relation.notification_id = notificationID;
await R.store(relation)
}
}
}
2021-07-09 08:14:03 +02:00
module.exports = {
Notification,
}