2022-09-17 04:54:21 -04:00
|
|
|
const { checkLogin } = require("../util-server");
|
|
|
|
const { log } = require("../../src/util");
|
|
|
|
const { R } = require("redbean-node");
|
|
|
|
const apicache = require("../modules/apicache");
|
|
|
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
2022-09-24 07:18:24 -04:00
|
|
|
const Maintenance = require("../model/maintenance");
|
2022-09-17 04:54:21 -04:00
|
|
|
const server = UptimeKumaServer.getInstance();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handlers for Maintenance
|
|
|
|
* @param {Socket} socket Socket.io instance
|
2023-08-11 03:46:41 -04:00
|
|
|
* @returns {void}
|
2022-09-17 04:54:21 -04:00
|
|
|
*/
|
|
|
|
module.exports.maintenanceSocketHandler = (socket) => {
|
|
|
|
// Add a new maintenance
|
2022-10-10 08:48:11 -04:00
|
|
|
socket.on("addMaintenance", async (maintenance, callback) => {
|
2022-09-17 04:54:21 -04:00
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
2022-09-24 07:18:24 -04:00
|
|
|
log.debug("maintenance", maintenance);
|
|
|
|
|
2023-03-30 16:04:17 -04:00
|
|
|
let bean = await Maintenance.jsonToBean(R.dispense("maintenance"), maintenance);
|
2022-09-17 04:54:21 -04:00
|
|
|
bean.user_id = socket.userID;
|
|
|
|
let maintenanceID = await R.store(bean);
|
2023-03-30 16:04:17 -04:00
|
|
|
|
|
|
|
server.maintenanceList[maintenanceID] = bean;
|
2023-03-31 08:25:37 -04:00
|
|
|
await bean.run(true);
|
2022-09-17 04:54:21 -04:00
|
|
|
|
|
|
|
await server.sendMaintenanceList(socket);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
2023-09-26 16:53:14 -04:00
|
|
|
msg: "successAdded",
|
|
|
|
msgi18n: true,
|
2022-09-17 04:54:21 -04:00
|
|
|
maintenanceID,
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Edit a maintenance
|
2022-10-10 08:48:11 -04:00
|
|
|
socket.on("editMaintenance", async (maintenance, callback) => {
|
2022-09-17 04:54:21 -04:00
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
2023-03-30 16:04:17 -04:00
|
|
|
let bean = server.getMaintenance(maintenance.id);
|
2022-09-17 04:54:21 -04:00
|
|
|
|
|
|
|
if (bean.user_id !== socket.userID) {
|
|
|
|
throw new Error("Permission denied.");
|
|
|
|
}
|
|
|
|
|
2023-03-30 16:04:17 -04:00
|
|
|
await Maintenance.jsonToBean(bean, maintenance);
|
2022-09-17 04:54:21 -04:00
|
|
|
await R.store(bean);
|
2023-03-31 08:25:37 -04:00
|
|
|
await bean.run(true);
|
2022-09-17 04:54:21 -04:00
|
|
|
await server.sendMaintenanceList(socket);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
msg: "Saved.",
|
2023-09-01 08:51:28 -04:00
|
|
|
msgi18n: true,
|
2022-09-17 04:54:21 -04:00
|
|
|
maintenanceID: bean.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add a new monitor_maintenance
|
|
|
|
socket.on("addMonitorMaintenance", async (maintenanceID, monitors, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
await R.exec("DELETE FROM monitor_maintenance WHERE maintenance_id = ?", [
|
|
|
|
maintenanceID
|
|
|
|
]);
|
|
|
|
|
|
|
|
for await (const monitor of monitors) {
|
|
|
|
let bean = R.dispense("monitor_maintenance");
|
|
|
|
|
|
|
|
bean.import({
|
|
|
|
monitor_id: monitor.id,
|
|
|
|
maintenance_id: maintenanceID
|
|
|
|
});
|
|
|
|
await R.store(bean);
|
|
|
|
}
|
|
|
|
|
|
|
|
apicache.clear();
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
2023-09-26 16:53:14 -04:00
|
|
|
msg: "successAdded",
|
|
|
|
msgi18n: true,
|
2022-09-17 04:54:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add a new monitor_maintenance
|
|
|
|
socket.on("addMaintenanceStatusPage", async (maintenanceID, statusPages, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
await R.exec("DELETE FROM maintenance_status_page WHERE maintenance_id = ?", [
|
|
|
|
maintenanceID
|
|
|
|
]);
|
|
|
|
|
|
|
|
for await (const statusPage of statusPages) {
|
|
|
|
let bean = R.dispense("maintenance_status_page");
|
|
|
|
|
|
|
|
bean.import({
|
|
|
|
status_page_id: statusPage.id,
|
|
|
|
maintenance_id: maintenanceID
|
|
|
|
});
|
|
|
|
await R.store(bean);
|
|
|
|
}
|
|
|
|
|
|
|
|
apicache.clear();
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
2023-09-26 16:53:14 -04:00
|
|
|
msg: "successAdded",
|
|
|
|
msgi18n: true,
|
2022-09-17 04:54:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-10 08:48:11 -04:00
|
|
|
socket.on("getMaintenance", async (maintenanceID, callback) => {
|
2022-09-17 04:58:08 -04:00
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
log.debug("maintenance", `Get Maintenance: ${maintenanceID} User ID: ${socket.userID}`);
|
|
|
|
|
|
|
|
let bean = await R.findOne("maintenance", " id = ? AND user_id = ? ", [
|
|
|
|
maintenanceID,
|
|
|
|
socket.userID,
|
|
|
|
]);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
2022-10-10 08:48:11 -04:00
|
|
|
maintenance: await bean.toJSON(),
|
2022-09-17 04:58:08 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-17 04:54:21 -04:00
|
|
|
socket.on("getMaintenanceList", async (callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
await server.sendMaintenanceList(socket);
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("getMonitorMaintenance", async (maintenanceID, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
2022-09-17 04:58:08 -04:00
|
|
|
log.debug("maintenance", `Get Monitors for Maintenance: ${maintenanceID} User ID: ${socket.userID}`);
|
2022-09-17 04:54:21 -04:00
|
|
|
|
2023-01-28 08:02:10 -05:00
|
|
|
let monitors = await R.getAll("SELECT monitor.id FROM monitor_maintenance mm JOIN monitor ON mm.monitor_id = monitor.id WHERE mm.maintenance_id = ? ", [
|
2022-09-17 04:54:21 -04:00
|
|
|
maintenanceID,
|
|
|
|
]);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
monitors,
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("getMaintenanceStatusPage", async (maintenanceID, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
2022-09-17 04:58:08 -04:00
|
|
|
log.debug("maintenance", `Get Status Pages for Maintenance: ${maintenanceID} User ID: ${socket.userID}`);
|
2022-09-17 04:54:21 -04:00
|
|
|
|
|
|
|
let statusPages = await R.getAll("SELECT status_page.id, status_page.title FROM maintenance_status_page msp JOIN status_page ON msp.status_page_id = status_page.id WHERE msp.maintenance_id = ? ", [
|
|
|
|
maintenanceID,
|
|
|
|
]);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
statusPages,
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("deleteMaintenance", async (maintenanceID, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
2022-09-17 04:58:08 -04:00
|
|
|
log.debug("maintenance", `Delete Maintenance: ${maintenanceID} User ID: ${socket.userID}`);
|
2022-09-17 04:54:21 -04:00
|
|
|
|
|
|
|
if (maintenanceID in server.maintenanceList) {
|
2023-03-30 16:04:17 -04:00
|
|
|
server.maintenanceList[maintenanceID].stop();
|
2022-09-17 04:54:21 -04:00
|
|
|
delete server.maintenanceList[maintenanceID];
|
|
|
|
}
|
|
|
|
|
|
|
|
await R.exec("DELETE FROM maintenance WHERE id = ? AND user_id = ? ", [
|
|
|
|
maintenanceID,
|
|
|
|
socket.userID,
|
|
|
|
]);
|
|
|
|
|
2023-01-01 08:43:39 -05:00
|
|
|
apicache.clear();
|
|
|
|
|
2022-09-17 04:54:21 -04:00
|
|
|
callback({
|
|
|
|
ok: true,
|
2023-09-26 16:53:14 -04:00
|
|
|
msg: "successDeleted",
|
|
|
|
msgi18n: true,
|
2022-09-17 04:54:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
await server.sendMaintenanceList(socket);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-10-11 06:23:17 -04:00
|
|
|
|
|
|
|
socket.on("pauseMaintenance", async (maintenanceID, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
log.debug("maintenance", `Pause Maintenance: ${maintenanceID} User ID: ${socket.userID}`);
|
|
|
|
|
2023-03-30 16:04:17 -04:00
|
|
|
let maintenance = server.getMaintenance(maintenanceID);
|
|
|
|
|
|
|
|
if (!maintenance) {
|
|
|
|
throw new Error("Maintenance not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
maintenance.active = false;
|
|
|
|
await R.store(maintenance);
|
|
|
|
maintenance.stop();
|
2022-10-11 06:23:17 -04:00
|
|
|
|
2023-01-01 08:43:39 -05:00
|
|
|
apicache.clear();
|
|
|
|
|
2022-10-11 06:23:17 -04:00
|
|
|
callback({
|
|
|
|
ok: true,
|
2023-09-26 16:53:14 -04:00
|
|
|
msg: "successPaused",
|
|
|
|
msgi18n: true,
|
2022-10-11 06:23:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
await server.sendMaintenanceList(socket);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("resumeMaintenance", async (maintenanceID, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
log.debug("maintenance", `Resume Maintenance: ${maintenanceID} User ID: ${socket.userID}`);
|
|
|
|
|
2023-03-30 16:04:17 -04:00
|
|
|
let maintenance = server.getMaintenance(maintenanceID);
|
|
|
|
|
|
|
|
if (!maintenance) {
|
|
|
|
throw new Error("Maintenance not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
maintenance.active = true;
|
|
|
|
await R.store(maintenance);
|
|
|
|
await maintenance.run();
|
2022-10-11 06:23:17 -04:00
|
|
|
|
2023-01-01 08:43:39 -05:00
|
|
|
apicache.clear();
|
|
|
|
|
2022-10-11 06:23:17 -04:00
|
|
|
callback({
|
|
|
|
ok: true,
|
2023-09-26 16:53:14 -04:00
|
|
|
msg: "successResumed",
|
|
|
|
msgi18n: true,
|
2022-10-11 06:23:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
await server.sendMaintenanceList(socket);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-09-17 04:54:21 -04:00
|
|
|
};
|