Skip to content

Commit

Permalink
Chore: General notification reformatting (#3182)
Browse files Browse the repository at this point in the history
- I unified where in file the name of `NotificationProvider.name` is placed
- I made sure that all the providers adhere to the signature of `NotificationProvider.send()`
- I made sure that all the providers use `okMsg` if returning success messages directly from the function.
  Here a discussion should be had:
  Should this be refactored into a constant of `NotificationProvider`? I could imagine that `NotificationProvider.SENDING_SUCCESSFULL`  could be a suitable alternative.
- I made sure all providers have the URL they `POST`/`GET` to be extraced into a variable.
  => refactored this way due to Nelsons suggestion
  • Loading branch information
CommanderStorm authored Mar 14, 2024
1 parent bfd65ab commit a9a1cf1
Show file tree
Hide file tree
Showing 50 changed files with 165 additions and 194 deletions.
1 change: 0 additions & 1 deletion server/monitor-types/monitor-type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class MonitorType {

name = undefined;

/**
Expand Down
10 changes: 4 additions & 6 deletions server/notification-providers/alerta.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ const { DOWN, UP } = require("../../src/util");
const axios = require("axios");

class Alerta extends NotificationProvider {

name = "alerta";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

try {
let alertaUrl = `${notification.alertaApiEndpoint}`;
let config = {
headers: {
"Content-Type": "application/json;charset=UTF-8",
Expand All @@ -40,7 +38,7 @@ class Alerta extends NotificationProvider {
resource: "Message",
}, data);

await axios.post(alertaUrl, postData, config);
await axios.post(notification.alertaApiEndpoint, postData, config);
} else {
let datadup = Object.assign( {
correlate: [ "service_up", "service_down" ],
Expand All @@ -52,11 +50,11 @@ class Alerta extends NotificationProvider {
if (heartbeatJSON["status"] === DOWN) {
datadup.severity = notification.alertaAlertState; // critical
datadup.text = "Service " + monitorJSON["type"] + " is down.";
await axios.post(alertaUrl, datadup, config);
await axios.post(notification.alertaApiEndpoint, datadup, config);
} else if (heartbeatJSON["status"] === UP) {
datadup.severity = notification.alertaRecoverState; // cleaned
datadup.text = "Service " + monitorJSON["type"] + " is up.";
await axios.post(alertaUrl, datadup, config);
await axios.post(notification.alertaApiEndpoint, datadup, config);
}
}
return okMsg;
Expand Down
4 changes: 2 additions & 2 deletions server/notification-providers/alertnow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const { setting } = require("../util-server");
const { getMonitorRelativeURL, UP, DOWN } = require("../../src/util");

class AlertNow extends NotificationProvider {

name = "AlertNow";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

try {
let textMsg = "";
let status = "open";
Expand Down
2 changes: 1 addition & 1 deletion server/notification-providers/aliyun-sms.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AliyunSMS extends NotificationProvider {
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

try {
if (heartbeatJSON != null) {
Expand Down
5 changes: 3 additions & 2 deletions server/notification-providers/apprise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ const NotificationProvider = require("./notification-provider");
const childProcessAsync = require("promisify-child-process");

class Apprise extends NotificationProvider {

name = "apprise";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
const okMsg = "Sent Successfully.";

const args = [ "-vv", "-b", msg, notification.appriseURL ];
if (notification.title) {
args.push("-t");
Expand All @@ -23,7 +24,7 @@ class Apprise extends NotificationProvider {
if (output) {

if (! output.includes("ERROR")) {
return "Sent Successfully";
return okMsg;
}

throw new Error(output);
Expand Down
23 changes: 11 additions & 12 deletions server/notification-providers/bark.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,29 @@ class Bark extends NotificationProvider {
}

/**
* Add additional parameter for Bark v1 endpoints
* Add additional parameter for Bark v1 endpoints.
* Leads to better on device styles (iOS 15 optimized)
* @param {BeanModel} notification Notification to send
* @param {string} postUrl URL to append parameters to
* @returns {string} Additional URL parameters
*/
appendAdditionalParameters(notification, postUrl) {
additionalParameters(notification) {
// set icon to uptime kuma icon, 11kb should be fine
postUrl += "?icon=" + barkNotificationAvatar;
let params = "?icon=" + barkNotificationAvatar;
// grouping all our notifications
if (notification.barkGroup != null) {
postUrl += "&group=" + notification.barkGroup;
params += "&group=" + notification.barkGroup;
} else {
// default name
postUrl += "&group=" + "UptimeKuma";
params += "&group=" + "UptimeKuma";
}
// picked a sound, this should follow system's mute status when arrival
if (notification.barkSound != null) {
postUrl += "&sound=" + notification.barkSound;
params += "&sound=" + notification.barkSound;
} else {
// default sound
postUrl += "&sound=" + "telegraph";
params += "&sound=" + "telegraph";
}
return postUrl;
return params;
}

/**
Expand Down Expand Up @@ -100,9 +100,8 @@ class Bark extends NotificationProvider {
// url encode title and subtitle
title = encodeURIComponent(title);
subtitle = encodeURIComponent(subtitle);
let postUrl = endpoint + "/" + title + "/" + subtitle;
postUrl = this.appendAdditionalParameters(notification, postUrl);
result = await axios.get(postUrl);
const params = this.additionalParameters(notification);
result = await axios.get(`${endpoint}/${title}/${subtitle}${params}`);
} else {
result = await axios.post(`${endpoint}/push`, {
title,
Expand Down
7 changes: 4 additions & 3 deletions server/notification-providers/clicksendsms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class ClickSendSMS extends NotificationProvider {

name = "clicksendsms";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";
const url = "https://rest.clicksend.com/v3/sms/send";

try {
let config = {
headers: {
Expand All @@ -28,7 +29,7 @@ class ClickSendSMS extends NotificationProvider {
}
]
};
let resp = await axios.post("https://rest.clicksend.com/v3/sms/send", data, config);
let resp = await axios.post(url, data, config);
if (resp.data.data.messages[0].status !== "SUCCESS") {
let error = "Something gone wrong. Api returned " + resp.data.data.messages[0].status + ".";
this.throwGeneralAxiosError(error);
Expand Down
2 changes: 1 addition & 1 deletion server/notification-providers/dingding.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DingDing extends NotificationProvider {
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

try {
if (heartbeatJSON != null) {
Expand Down
3 changes: 1 addition & 2 deletions server/notification-providers/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ const axios = require("axios");
const { DOWN, UP } = require("../../src/util");

class Discord extends NotificationProvider {

name = "discord";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

try {
const discordDisplayName = notification.discordUsername || "Uptime Kuma";
Expand Down
9 changes: 4 additions & 5 deletions server/notification-providers/feishu.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Feishu extends NotificationProvider {
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
let feishuWebHookUrl = notification.feishuWebHookUrl;
const okMsg = "Sent Successfully.";

try {
if (heartbeatJSON == null) {
Expand All @@ -20,7 +19,7 @@ class Feishu extends NotificationProvider {
text: msg,
},
};
await axios.post(feishuWebHookUrl, testdata);
await axios.post(notification.feishuWebHookUrl, testdata);
return okMsg;
}

Expand All @@ -46,7 +45,7 @@ class Feishu extends NotificationProvider {
},
},
};
await axios.post(feishuWebHookUrl, downdata);
await axios.post(notification.feishuWebHookUrl, downdata);
return okMsg;
}

Expand All @@ -72,7 +71,7 @@ class Feishu extends NotificationProvider {
},
},
};
await axios.post(feishuWebHookUrl, updata);
await axios.post(notification.feishuWebHookUrl, updata);
return okMsg;
}
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions server/notification-providers/freemobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class FreeMobile extends NotificationProvider {

name = "FreeMobile";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

try {
await axios.post(`https://smsapi.free-mobile.fr/sendmsg?msg=${encodeURIComponent(msg.replace("🔴", "⛔️"))}`, {
"user": notification.freemobileUser,
Expand Down
8 changes: 3 additions & 5 deletions server/notification-providers/goalert.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ const axios = require("axios");
const { UP } = require("../../src/util");

class GoAlert extends NotificationProvider {

name = "GoAlert";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

try {
let closeAction = "close";
let data = {
summary: msg,
};
if (heartbeatJSON != null && heartbeatJSON["status"] === UP) {
data["action"] = closeAction;
data["action"] = "close";
}
let headers = {
"Content-Type": "multipart/form-data",
Expand All @@ -27,7 +26,6 @@ class GoAlert extends NotificationProvider {
};
await axios.post(`${notification.goAlertBaseURL}/api/v2/generic/incoming?token=${notification.goAlertToken}`, data, config);
return okMsg;

} catch (error) {
let msg = (error.response.data) ? error.response.data : "Error without response";
throw new Error(msg);
Expand Down
4 changes: 2 additions & 2 deletions server/notification-providers/google-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const { getMonitorRelativeURL } = require("../../src/util");
const { DOWN, UP } = require("../../src/util");

class GoogleChat extends NotificationProvider {

name = "GoogleChat";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

try {
// Google Chat message formatting: https://developers.google.com/chat/api/guides/message-formats/basic

Expand Down
3 changes: 1 addition & 2 deletions server/notification-providers/gorush.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class Gorush extends NotificationProvider {

name = "gorush";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

let platformMapping = {
"ios": 1,
Expand Down
4 changes: 2 additions & 2 deletions server/notification-providers/gotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class Gotify extends NotificationProvider {

name = "gotify";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
const okMsg = "Sent Successfully.";

try {
if (notification.gotifyserverurl && notification.gotifyserverurl.endsWith("/")) {
notification.gotifyserverurl = notification.gotifyserverurl.slice(0, -1);
Expand Down
18 changes: 4 additions & 14 deletions server/notification-providers/grafana-oncall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,42 @@ const axios = require("axios");
const { DOWN, UP } = require("../../src/util");

class GrafanaOncall extends NotificationProvider {

name = "GrafanaOncall";

/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
const okMsg = "Sent Successfully.";

if (!notification.GrafanaOncallURL) {
throw new Error("GrafanaOncallURL cannot be empty");
}

let okMsg = "Sent Successfully.";
try {
if (heartbeatJSON === null) {
let grafanaupdata = {
title: "General notification",
message: msg,
state: "alerting",
};
await axios.post(
notification.GrafanaOncallURL,
grafanaupdata
);
await axios.post(notification.GrafanaOncallURL, grafanaupdata);
return okMsg;
} else if (heartbeatJSON["status"] === DOWN) {
let grafanadowndata = {
title: monitorJSON["name"] + " is down",
message: heartbeatJSON["msg"],
state: "alerting",
};
await axios.post(
notification.GrafanaOncallURL,
grafanadowndata
);
await axios.post(notification.GrafanaOncallURL, grafanadowndata);
return okMsg;
} else if (heartbeatJSON["status"] === UP) {
let grafanaupdata = {
title: monitorJSON["name"] + " is up",
message: heartbeatJSON["msg"],
state: "ok",
};
await axios.post(
notification.GrafanaOncallURL,
grafanaupdata
);
await axios.post(notification.GrafanaOncallURL, grafanaupdata);
return okMsg;
}
} catch (error) {
Expand Down
Loading

0 comments on commit a9a1cf1

Please sign in to comment.