uptime-kuma/server/socket-handlers/status-page-socket-handler.js

162 lines
4.8 KiB
JavaScript
Raw Normal View History

2021-09-16 14:48:28 +00:00
const { R } = require("redbean-node");
2021-09-20 08:22:18 +00:00
const { checkLogin, setSettings } = require("../util-server");
2021-09-16 14:48:28 +00:00
const dayjs = require("dayjs");
2021-11-15 16:52:28 +00:00
const { log, log_info, log_debug, log_error } = require("../../src/util");
const ImageDataURI = require("../image-data-uri");
const Database = require("../database");
const apicache = require("../modules/apicache");
2021-09-16 14:48:28 +00:00
module.exports.statusPageSocketHandler = (socket) => {
// Post or edit incident
socket.on("postIncident", async (incident, callback) => {
try {
checkLogin(socket);
await R.exec("UPDATE incident SET pin = 0 ");
let incidentBean;
if (incident.id) {
incidentBean = await R.findOne("incident", " id = ?", [
incident.id
]);
}
if (incidentBean == null) {
incidentBean = R.dispense("incident");
}
incidentBean.title = incident.title;
incidentBean.content = incident.content;
incidentBean.style = incident.style;
incidentBean.pin = true;
if (incident.id) {
incidentBean.lastUpdatedDate = R.isoDateTime(dayjs.utc());
} else {
incidentBean.createdDate = R.isoDateTime(dayjs.utc());
}
2021-09-16 14:48:28 +00:00
await R.store(incidentBean);
callback({
ok: true,
incident: incidentBean.toPublicJSON(),
});
2021-09-16 14:48:28 +00:00
} catch (error) {
callback({
ok: false,
msg: error.message,
});
2021-09-16 14:48:28 +00:00
}
});
socket.on("unpinIncident", async (callback) => {
try {
checkLogin(socket);
await R.exec("UPDATE incident SET pin = 0 WHERE pin = 1");
callback({
ok: true,
});
2021-09-16 14:48:28 +00:00
} catch (error) {
callback({
ok: false,
msg: error.message,
});
2021-09-16 14:48:28 +00:00
}
});
// Save Status Page
// imgDataUrl Only Accept PNG!
2021-09-20 08:22:18 +00:00
socket.on("saveStatusPage", async (config, imgDataUrl, publicGroupList, callback) => {
try {
checkLogin(socket);
apicache.clear();
const header = "data:image/png;base64,";
// Check logo format
// If is image data url, convert to png file
// Else assume it is a url, nothing to do
if (imgDataUrl.startsWith("data:")) {
if (! imgDataUrl.startsWith(header)) {
throw new Error("Only allowed PNG logo.");
}
// Convert to file
await ImageDataURI.outputFile(imgDataUrl, Database.uploadDir + "logo.png");
config.logo = "/upload/logo.png?t=" + Date.now();
} else {
config.icon = imgDataUrl;
}
2021-09-20 12:44:07 +00:00
// Save Config
await setSettings("statusPage", config);
2021-09-22 07:10:08 +00:00
// Save Public Group List
const groupIDList = [];
let groupOrder = 1;
for (let group of publicGroupList) {
let groupBean;
if (group.id) {
groupBean = await R.findOne("group", " id = ? AND public = 1 ", [
group.id
]);
2021-09-22 07:10:08 +00:00
} else {
groupBean = R.dispense("group");
}
groupBean.name = group.name;
groupBean.public = true;
groupBean.weight = groupOrder++;
await R.store(groupBean);
await R.exec("DELETE FROM monitor_group WHERE group_id = ? ", [
groupBean.id
]);
2021-09-22 07:10:08 +00:00
let monitorOrder = 1;
2021-11-15 16:52:28 +00:00
log_info("socket", group.monitorList);
2021-09-22 07:23:58 +00:00
2021-09-22 07:10:08 +00:00
for (let monitor of group.monitorList) {
let relationBean = R.dispense("monitor_group");
relationBean.weight = monitorOrder++;
relationBean.group_id = groupBean.id;
relationBean.monitor_id = monitor.id;
await R.store(relationBean);
}
2021-09-22 07:10:08 +00:00
groupIDList.push(groupBean.id);
group.id = groupBean.id;
}
2021-11-11 11:31:28 +00:00
// Delete groups that are not in the list
2021-11-15 16:52:28 +00:00
log_debug("socket", "Delete groups that are not in the list");
2021-09-22 07:10:08 +00:00
const slots = groupIDList.map(() => "?").join(",");
await R.exec(`DELETE FROM \`group\` WHERE id NOT IN (${slots})`, groupIDList);
2021-09-22 07:10:08 +00:00
callback({
ok: true,
publicGroupList,
});
2021-09-22 07:10:08 +00:00
} catch (error) {
2021-11-15 16:52:28 +00:00
log_error("socket", error);
callback({
ok: false,
msg: error.message,
});
}
});
};