diff --git a/server/notification.js b/server/notification.js index c60160a8a..221378122 100644 --- a/server/notification.js +++ b/server/notification.js @@ -1,5 +1,5 @@ const axios = require("axios"); -const {R} = require("redbean-node"); +const { R } = require("redbean-node"); const FormData = require('form-data'); const nodemailer = require("nodemailer"); @@ -54,61 +54,61 @@ class Notification { } else if (notification.type === "discord") { try { - // If heartbeatJSON is null, assume we're testing. - if(heartbeatJSON == null) { + // If heartbeatJSON is null, assume we're testing. + if (heartbeatJSON == null) { + let data = { + username: 'Uptime-Kuma', + content: msg + } + let res = await axios.post(notification.discordWebhookUrl, data) + return true; + } + // If heartbeatJSON is not null, we go into the normal alerting loop. + if (heartbeatJSON['status'] == 0) { + var alertColor = "16711680"; + } else if (heartbeatJSON['status'] == 1) { + var alertColor = "65280"; + } let data = { - username: 'Uptime-Kuma', - content: msg + username: 'Uptime-Kuma', + embeds: [{ + title: "Uptime-Kuma Alert", + color: alertColor, + fields: [ + { + name: "Time (UTC)", + value: heartbeatJSON["time"] + }, + { + name: "Message", + value: msg + } + ] + }] } let res = await axios.post(notification.discordWebhookUrl, data) return true; - } - // If heartbeatJSON is not null, we go into the normal alerting loop. - if(heartbeatJSON['status'] == 0) { - var alertColor = "16711680"; - } else if(heartbeatJSON['status'] == 1) { - var alertColor = "65280"; - } - let data = { - username: 'Uptime-Kuma', - embeds: [{ - title: "Uptime-Kuma Alert", - color: alertColor, - fields: [ - { - name: "Time (UTC)", - value: heartbeatJSON["time"] - }, - { - name: "Message", - value: msg - } - ] - }] - } - let res = await axios.post(notification.discordWebhookUrl, data) - return true; - } catch(error) { - console.log(error) - return false; + } catch (error) { + console.log(error) + return false; } return await Notification.discord(notification, msg) } else if (notification.type === "signal") { - try { - let data = { - "message": msg, - "number": notification.signalNumber, - "recipients": notification.signalRecipients.replace(/\s/g, '').split(",") - }; - let config = {}; + try { + let data = { + "message": msg, + "number": notification.signalNumber, + "recipients": notification.signalRecipients.replace(/\s/g, '').split(",") + }; + let config = {}; - let res = await axios.post(notification.signalURL, data, config) - return true; - } catch (error) { - console.log(error) - return false; - } + let res = await axios.post(notification.signalURL, data, config) + return true; + } catch (error) { + console.log(error) + return false; + } } else { throw new Error("Notification type is not supported") @@ -124,7 +124,7 @@ class Notification { userID, ]) - if (! bean) { + if (!bean) { throw new Error("notification not found") } @@ -144,7 +144,7 @@ class Notification { userID, ]) - if (! bean) { + if (!bean) { throw new Error("notification not found") } diff --git a/server/server.js b/server/server.js index c335b25f2..a402c765b 100644 --- a/server/server.js +++ b/server/server.js @@ -5,13 +5,13 @@ const server = http.createServer(app); const { Server } = require("socket.io"); const io = new Server(server); const dayjs = require("dayjs"); -const {R} = require("redbean-node"); +const { R } = require("redbean-node"); const passwordHash = require('password-hash'); const jwt = require('jsonwebtoken'); const Monitor = require("./model/monitor"); const fs = require("fs"); -const {getSettings} = require("./util-server"); -const {Notification} = require("./notification") +const { getSettings } = require("./util-server"); +const { Notification } = require("./notification") const args = require('args-parser')(process.argv); const version = require('../package.json').version; @@ -30,7 +30,7 @@ let needSetup = false; app.use('/', express.static("dist")); - app.get('*', function(request, response, next) { + app.get('*', function (request, response, next) { response.sendFile(process.cwd() + '/dist/index.html'); }); @@ -190,7 +190,7 @@ let needSetup = false; try { checkLogin(socket) - let bean = await R.findOne("monitor", " id = ? ", [ monitor.id ]) + let bean = await R.findOne("monitor", " id = ? ", [monitor.id]) if (bean.user_id !== socket.userID) { throw new Error("Permission denied.") @@ -328,7 +328,7 @@ let needSetup = false; try { checkLogin(socket) - if (! password.currentPassword) { + if (!password.currentPassword) { throw new Error("Invalid new password") } @@ -467,7 +467,7 @@ async function checkOwner(userID, monitorID) { userID, ]) - if (! row) { + if (!row) { throw new Error("You do not own this monitor."); } } @@ -522,7 +522,7 @@ async function getMonitorJSONList(userID) { } function checkLogin(socket) { - if (! socket.userID) { + if (!socket.userID) { throw new Error("You are not logged in."); } } @@ -530,7 +530,7 @@ function checkLogin(socket) { async function initDatabase() { const path = './data/kuma.db'; - if (! fs.existsSync(path)) { + if (!fs.existsSync(path)) { console.log("Copy Database") fs.copyFileSync("./db/kuma.db", path); } @@ -547,7 +547,7 @@ async function initDatabase() { "jwtSecret" ]); - if (! jwtSecretBean) { + if (!jwtSecretBean) { console.log("JWT secret is not found, generate one.") jwtSecretBean = R.dispense("setting") jwtSecretBean.key = "jwtSecret" @@ -634,7 +634,7 @@ async function sendHeartbeatList(socket, monitorID) { let result = []; for (let bean of list) { - result.unshift(bean.toJSON()) + result.unshift(bean.toJSON()) } socket.emit("heartbeatList", monitorID, result)