2025-03-14 12:51:07 +01:00
|
|
|
const { Liquid } = require("liquidjs");
|
|
|
|
const { DOWN } = require("../../src/util");
|
|
|
|
|
2021-09-07 22:42:46 +08:00
|
|
|
class NotificationProvider {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification Provider Name
|
2023-08-11 09:46:41 +02:00
|
|
|
* @type {string}
|
2021-09-07 22:42:46 +08:00
|
|
|
*/
|
|
|
|
name = undefined;
|
|
|
|
|
|
|
|
/**
|
2022-04-16 20:24:53 +01:00
|
|
|
* Send a notification
|
2023-08-11 09:46:41 +02:00
|
|
|
* @param {BeanModel} notification Notification to send
|
2022-04-16 20:24:53 +01:00
|
|
|
* @param {string} msg General Message
|
2023-08-11 09:46:41 +02:00
|
|
|
* @param {?object} monitorJSON Monitor details (For Up/Down only)
|
|
|
|
* @param {?object} heartbeatJSON Heartbeat details (For Up/Down only)
|
2021-09-07 22:42:46 +08:00
|
|
|
* @returns {Promise<string>} Return Successful Message
|
2022-04-16 20:24:53 +01:00
|
|
|
* @throws Error with fail msg
|
2021-09-07 22:42:46 +08:00
|
|
|
*/
|
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
|
|
throw new Error("Have to override Notification.send(...)");
|
|
|
|
}
|
|
|
|
|
2024-06-13 17:00:11 +02:00
|
|
|
/**
|
|
|
|
* Extracts the address from a monitor JSON object based on its type.
|
|
|
|
* @param {?object} monitorJSON Monitor details (For Up/Down only)
|
|
|
|
* @returns {string} The extracted address based on the monitor type.
|
|
|
|
*/
|
2024-10-09 18:24:32 -05:00
|
|
|
extractAddress(monitorJSON) {
|
2024-06-13 17:00:11 +02:00
|
|
|
if (!monitorJSON) {
|
2024-06-13 17:04:38 +02:00
|
|
|
return "";
|
2024-06-13 17:00:11 +02:00
|
|
|
}
|
|
|
|
switch (monitorJSON["type"]) {
|
|
|
|
case "push":
|
|
|
|
return "Heartbeat";
|
|
|
|
case "ping":
|
|
|
|
return monitorJSON["hostname"];
|
|
|
|
case "port":
|
|
|
|
case "dns":
|
|
|
|
case "gamedig":
|
|
|
|
case "steam":
|
|
|
|
if (monitorJSON["port"]) {
|
|
|
|
return monitorJSON["hostname"] + ":" + monitorJSON["port"];
|
|
|
|
}
|
2024-06-13 17:04:38 +02:00
|
|
|
return monitorJSON["hostname"];
|
2024-06-13 17:00:11 +02:00
|
|
|
default:
|
|
|
|
if (![ "https://", "http://", "" ].includes(monitorJSON["url"])) {
|
|
|
|
return monitorJSON["url"];
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-14 12:51:07 +01:00
|
|
|
/**
|
|
|
|
* Renders a message template with notification context
|
|
|
|
* @param {string} template the template
|
|
|
|
* @param {string} msg the message that will be included in the context
|
|
|
|
* @param {?object} monitorJSON Monitor details (For Up/Down/Cert-Expiry only)
|
|
|
|
* @param {?object} heartbeatJSON Heartbeat details (For Up/Down only)
|
|
|
|
* @returns {Promise<string>} rendered template
|
|
|
|
*/
|
|
|
|
async renderTemplate(template, msg, monitorJSON, heartbeatJSON) {
|
|
|
|
const engine = new Liquid();
|
|
|
|
const parsedTpl = engine.parse(template);
|
|
|
|
|
|
|
|
// Let's start with dummy values to simplify code
|
|
|
|
let monitorName = "Monitor Name not available";
|
|
|
|
let monitorHostnameOrURL = "testing.hostname";
|
|
|
|
|
|
|
|
if (monitorJSON !== null) {
|
|
|
|
monitorName = monitorJSON["name"];
|
|
|
|
monitorHostnameOrURL = this.extractAddress(monitorJSON);
|
|
|
|
}
|
|
|
|
|
|
|
|
let serviceStatus = "⚠️ Test";
|
|
|
|
if (heartbeatJSON !== null) {
|
|
|
|
serviceStatus = (heartbeatJSON["status"] === DOWN) ? "🔴 Down" : "✅ Up";
|
|
|
|
}
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
// for v1 compatibility, to be removed in v3
|
|
|
|
"STATUS": serviceStatus,
|
|
|
|
"NAME": monitorName,
|
|
|
|
"HOSTNAME_OR_URL": monitorHostnameOrURL,
|
|
|
|
|
|
|
|
// variables which are officially supported
|
|
|
|
"status": serviceStatus,
|
|
|
|
"name": monitorName,
|
|
|
|
"hostnameOrURL": monitorHostnameOrURL,
|
|
|
|
monitorJSON,
|
|
|
|
heartbeatJSON,
|
|
|
|
msg,
|
|
|
|
};
|
|
|
|
|
|
|
|
return engine.render(parsedTpl, context);
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:53 +01:00
|
|
|
/**
|
|
|
|
* Throws an error
|
|
|
|
* @param {any} error The error to throw
|
2023-08-11 09:46:41 +02:00
|
|
|
* @returns {void}
|
2022-04-16 20:24:53 +01:00
|
|
|
* @throws {any} The error specified
|
|
|
|
*/
|
2021-09-07 22:42:46 +08:00
|
|
|
throwGeneralAxiosError(error) {
|
|
|
|
let msg = "Error: " + error + " ";
|
|
|
|
|
|
|
|
if (error.response && error.response.data) {
|
|
|
|
if (typeof error.response.data === "string") {
|
|
|
|
msg += error.response.data;
|
|
|
|
} else {
|
2022-04-14 00:30:32 +08:00
|
|
|
msg += JSON.stringify(error.response.data);
|
2021-09-07 22:42:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:30:32 +08:00
|
|
|
throw new Error(msg);
|
2021-09-07 22:42:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = NotificationProvider;
|