Merge branch 'master' into simple_pagination

This commit is contained in:
Adam Stachowicz 2021-07-18 15:37:32 +02:00
commit 2d943620c7
3 changed files with 68 additions and 42 deletions

View File

@ -34,6 +34,7 @@ if (newVersion) {
fs.writeFileSync("package.json", JSON.stringify(pkg, null, 4) + "\n") fs.writeFileSync("package.json", JSON.stringify(pkg, null, 4) + "\n")
// Process README.md // Process README.md
fs.writeFileSync("README.md", fs.readFileSync("README.md", 'utf8').replaceAll(oldVersion, newVersion)) if (fs.existsSync("README.md")) {
fs.writeFileSync("README.md", fs.readFileSync("README.md", 'utf8').replaceAll(oldVersion, newVersion))
}
} }

View File

@ -5,12 +5,18 @@ const nodemailer = require("nodemailer");
const child_process = require("child_process"); const child_process = require("child_process");
class Notification { class Notification {
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let res = { /**
ok: true, *
msg: "Sent Successfully" * @param notification
} * @param msg
* @param monitorJSON
* @param heartbeatJSON
* @returns {Promise<string>} Successful msg
* Throw Error with fail msg
*/
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully. ";
if (notification.type === "telegram") { if (notification.type === "telegram") {
try { try {
@ -20,15 +26,16 @@ class Notification {
text: msg, text: msg,
} }
}) })
return true; return okMsg;
} catch (error) { } catch (error) {
console.error(error) let msg = (error.response.data.description) ? error.response.data.description : "Error without description"
return false; throw new Error(msg)
} }
} else if (notification.type === "gotify") { } else if (notification.type === "gotify") {
try { try {
if (notification.gotifyserverurl.endsWith("/")) { if (notification.gotifyserverurl && notification.gotifyserverurl.endsWith("/")) {
notification.gotifyserverurl = notification.gotifyserverurl.slice(0, -1); notification.gotifyserverurl = notification.gotifyserverurl.slice(0, -1);
} }
await axios.post(`${notification.gotifyserverurl}/message?token=${notification.gotifyapplicationToken}`, { await axios.post(`${notification.gotifyserverurl}/message?token=${notification.gotifyapplicationToken}`, {
@ -36,15 +43,15 @@ class Notification {
"priority": notification.gotifyPriority || 8, "priority": notification.gotifyPriority || 8,
"title": "Uptime-Kuma" "title": "Uptime-Kuma"
}) })
return true;
return okMsg;
} catch (error) { } catch (error) {
console.error(error) throwGeneralAxiosError(error)
return false;
} }
} else if (notification.type === "webhook") { } else if (notification.type === "webhook") {
try { try {
let data = { let data = {
heartbeat: heartbeatJSON, heartbeat: heartbeatJSON,
monitor: monitorJSON, monitor: monitorJSON,
@ -66,10 +73,10 @@ class Notification {
} }
let res = await axios.post(notification.webhookURL, finalData, config) let res = await axios.post(notification.webhookURL, finalData, config)
return true; return okMsg;
} catch (error) { } catch (error) {
console.error(error) throwGeneralAxiosError(error)
return false;
} }
} else if (notification.type === "smtp") { } else if (notification.type === "smtp") {
@ -84,7 +91,7 @@ class Notification {
content: msg content: msg
} }
let res = await axios.post(notification.discordWebhookUrl, data) let res = await axios.post(notification.discordWebhookUrl, data)
return true; return okMsg;
} }
// If heartbeatJSON is not null, we go into the normal alerting loop. // If heartbeatJSON is not null, we go into the normal alerting loop.
if(heartbeatJSON['status'] == 0) { if(heartbeatJSON['status'] == 0) {
@ -110,10 +117,9 @@ class Notification {
}] }]
} }
let res = await axios.post(notification.discordWebhookUrl, data) let res = await axios.post(notification.discordWebhookUrl, data)
return true; return okMsg;
} catch(error) { } catch(error) {
console.error(error) throwGeneralAxiosError(error)
return false;
} }
} else if (notification.type === "signal") { } else if (notification.type === "signal") {
@ -126,10 +132,9 @@ class Notification {
let config = {}; let config = {};
let res = await axios.post(notification.signalURL, data, config) let res = await axios.post(notification.signalURL, data, config)
return true; return okMsg;
} catch (error) { } catch (error) {
console.error(error) throwGeneralAxiosError(error)
return false;
} }
} else if (notification.type === "slack") { } else if (notification.type === "slack") {
@ -137,7 +142,7 @@ class Notification {
if (heartbeatJSON == null) { if (heartbeatJSON == null) {
let data = {'text': "Uptime Kuma Slack testing successful.", 'channel': notification.slackchannel, 'username': notification.slackusername, 'icon_emoji': notification.slackiconemo} let data = {'text': "Uptime Kuma Slack testing successful.", 'channel': notification.slackchannel, 'username': notification.slackusername, 'icon_emoji': notification.slackiconemo}
let res = await axios.post(notification.slackwebhookURL, data) let res = await axios.post(notification.slackwebhookURL, data)
return true; return okMsg;
} }
const time = heartbeatJSON["time"]; const time = heartbeatJSON["time"];
@ -182,10 +187,9 @@ class Notification {
] ]
} }
let res = await axios.post(notification.slackwebhookURL, data) let res = await axios.post(notification.slackwebhookURL, data)
return true; return okMsg;
} catch (error) { } catch (error) {
console.error(error) throwGeneralAxiosError(error)
return false;
} }
} else if (notification.type === "pushover") { } else if (notification.type === "pushover") {
@ -196,7 +200,7 @@ class Notification {
'user': notification.pushoveruserkey, 'token': notification.pushoverapptoken, 'sound':notification.pushoversounds, 'user': notification.pushoveruserkey, 'token': notification.pushoverapptoken, 'sound':notification.pushoversounds,
'priority': notification.pushoverpriority, 'title':notification.pushovertitle, 'retry': "30", 'expire':"3600", 'html': 1} 'priority': notification.pushoverpriority, 'title':notification.pushovertitle, 'retry': "30", 'expire':"3600", 'html': 1}
let res = await axios.post(pushoverlink, data) let res = await axios.post(pushoverlink, data)
return true; return okMsg;
} }
let data = { let data = {
@ -211,10 +215,9 @@ class Notification {
"html": 1 "html": 1
} }
let res = await axios.post(pushoverlink, data) let res = await axios.post(pushoverlink, data)
return true; return okMsg;
} catch (error) { } catch (error) {
console.log(error) throwGeneralAxiosError(error)
return false;
} }
} else if (notification.type === "apprise") { } else if (notification.type === "apprise") {
@ -282,22 +285,24 @@ class Notification {
text: msg, text: msg,
}); });
return true; return "Sent Successfully.";
} }
static async apprise(notification, msg) { static async apprise(notification, msg) {
let s = child_process.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL]) let s = child_process.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL])
let output = s.stdout.toString();
console.log(output)
let output = (s.stdout) ? s.stdout.toString() : 'ERROR: maybe apprise not found';
if (output) { if (output) {
return {
ok: ! output.includes("ERROR"), if (! output.includes("ERROR")) {
msg: output return "Sent Successfully";
} else {
throw new Error(output)
} }
} else { } else {
return { } return ""
} }
} }
@ -306,6 +311,21 @@ class Notification {
let exists = commandExistsSync('apprise'); let exists = commandExistsSync('apprise');
return exists; return exists;
} }
}
function throwGeneralAxiosError(error) {
let msg = "Error: " + error + " ";
if (error.response && error.response.data) {
if (typeof error.response.data === "string") {
msg += error.response.data;
} else {
msg += JSON.stringify(error.response.data)
}
}
throw new Error(msg)
} }
module.exports = { module.exports = {

View File

@ -440,11 +440,16 @@ let needSetup = false;
try { try {
checkLogin(socket) checkLogin(socket)
let res = await Notification.send(notification, notification.name + " Testing") let msg = await Notification.send(notification, notification.name + " Testing")
callback(res); callback({
ok: true,
msg
});
} catch (e) { } catch (e) {
console.error(e)
callback({ callback({
ok: false, ok: false,
msg: e.message msg: e.message