2021-09-16 15:48:28 +01:00
|
|
|
const { R } = require("redbean-node");
|
2021-09-20 09:22:18 +01:00
|
|
|
const { checkLogin, setSettings } = require("../util-server");
|
2021-09-16 15:48:28 +01:00
|
|
|
const dayjs = require("dayjs");
|
2021-09-19 12:04:51 +01:00
|
|
|
const { debug } = require("../../src/util");
|
2021-09-21 14:22:35 +01:00
|
|
|
const ImageDataURI = require("../image-data-uri");
|
|
|
|
const Database = require("../database");
|
2021-09-21 17:58:22 +01:00
|
|
|
const apicache = require("../modules/apicache");
|
2022-03-16 07:38:10 +00:00
|
|
|
const StatusPage = require("../model/status_page");
|
2021-09-16 15:48:28 +01:00
|
|
|
|
|
|
|
module.exports.statusPageSocketHandler = (socket) => {
|
|
|
|
|
|
|
|
// Post or edit incident
|
2022-03-16 07:38:10 +00:00
|
|
|
socket.on("postIncident", async (slug, incident, callback) => {
|
2021-09-16 15:48:28 +01:00
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
2022-03-16 07:38:10 +00:00
|
|
|
let statusPageID = await StatusPage.slugToID(slug);
|
|
|
|
|
|
|
|
if (!statusPageID) {
|
|
|
|
throw new Error("slug is not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
await R.exec("UPDATE incident SET pin = 0 WHERE status_page_id = ? ", [
|
|
|
|
statusPageID
|
|
|
|
]);
|
2021-09-16 15:48:28 +01:00
|
|
|
|
|
|
|
let incidentBean;
|
|
|
|
|
|
|
|
if (incident.id) {
|
2022-03-16 07:38:10 +00:00
|
|
|
incidentBean = await R.findOne("incident", " id = ? AND status_page_id = ? ", [
|
|
|
|
incident.id,
|
|
|
|
statusPageID
|
2021-09-16 15:48:28 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (incidentBean == null) {
|
|
|
|
incidentBean = R.dispense("incident");
|
|
|
|
}
|
|
|
|
|
|
|
|
incidentBean.title = incident.title;
|
|
|
|
incidentBean.content = incident.content;
|
|
|
|
incidentBean.style = incident.style;
|
|
|
|
incidentBean.pin = true;
|
2022-03-16 07:38:10 +00:00
|
|
|
incidentBean.status_page_id = statusPageID;
|
2021-09-19 12:04:51 +01:00
|
|
|
|
|
|
|
if (incident.id) {
|
|
|
|
incidentBean.lastUpdatedDate = R.isoDateTime(dayjs.utc());
|
|
|
|
} else {
|
|
|
|
incidentBean.createdDate = R.isoDateTime(dayjs.utc());
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:48:28 +01:00
|
|
|
await R.store(incidentBean);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
incident: incidentBean.toPublicJSON(),
|
2021-09-16 15:57:34 +01:00
|
|
|
});
|
2021-09-16 15:48:28 +01:00
|
|
|
} catch (error) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: error.message,
|
2021-09-16 15:57:34 +01:00
|
|
|
});
|
2021-09-16 15:48:28 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-16 07:38:10 +00:00
|
|
|
socket.on("unpinIncident", async (slug, callback) => {
|
2021-09-16 15:48:28 +01:00
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
2022-03-16 07:38:10 +00:00
|
|
|
let statusPageID = await StatusPage.slugToID(slug);
|
|
|
|
|
|
|
|
await R.exec("UPDATE incident SET pin = 0 WHERE pin = 1 AND status_page_id = ? ", [
|
|
|
|
statusPageID
|
|
|
|
]);
|
2021-09-16 15:48:28 +01:00
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
2021-09-16 15:57:34 +01:00
|
|
|
});
|
2021-09-16 15:48:28 +01:00
|
|
|
} catch (error) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: error.message,
|
2021-09-16 15:57:34 +01:00
|
|
|
});
|
2021-09-16 15:48:28 +01:00
|
|
|
}
|
|
|
|
});
|
2021-09-19 12:04:51 +01:00
|
|
|
|
|
|
|
// Save Status Page
|
2021-09-21 14:22:35 +01:00
|
|
|
// imgDataUrl Only Accept PNG!
|
2022-03-15 04:00:29 +00:00
|
|
|
socket.on("saveStatusPage", async (slug, config, imgDataUrl, publicGroupList, callback) => {
|
2021-09-19 12:04:51 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
2021-09-21 17:58:22 +01:00
|
|
|
apicache.clear();
|
|
|
|
|
2022-03-17 08:42:26 +00:00
|
|
|
// Save Config
|
|
|
|
let statusPage = await R.findOne("status_page", " slug = ? ", [
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!statusPage) {
|
|
|
|
throw new Error("No slug?");
|
|
|
|
}
|
|
|
|
|
2021-09-21 14:22:35 +01:00
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
2022-03-17 08:42:26 +00:00
|
|
|
const filename = `logo${statusPage.id}.png`;
|
|
|
|
|
2021-09-21 14:22:35 +01:00
|
|
|
// Convert to file
|
2022-03-17 08:42:26 +00:00
|
|
|
await ImageDataURI.outputFile(imgDataUrl, Database.uploadDir + filename);
|
|
|
|
config.logo = `/upload/${filename}?t=` + Date.now();
|
2021-09-21 14:22:35 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
config.icon = imgDataUrl;
|
|
|
|
}
|
|
|
|
|
2022-03-15 04:00:29 +00:00
|
|
|
statusPage.slug = config.slug;
|
|
|
|
statusPage.title = config.title;
|
2022-03-16 06:14:47 +00:00
|
|
|
statusPage.description = config.description;
|
2022-03-15 04:00:29 +00:00
|
|
|
statusPage.icon = config.logo;
|
|
|
|
statusPage.theme = config.theme;
|
|
|
|
//statusPage.published = ;
|
|
|
|
//statusPage.search_engine_index = ;
|
|
|
|
statusPage.show_tags = config.showTags;
|
|
|
|
//statusPage.password = null;
|
2022-03-16 06:14:47 +00:00
|
|
|
statusPage.modified_date = R.isoDateTime();
|
2022-03-15 04:00:29 +00:00
|
|
|
|
|
|
|
await R.store(statusPage);
|
2021-09-20 13:44:07 +01:00
|
|
|
|
2021-09-22 08:10:08 +01:00
|
|
|
// Save Public Group List
|
|
|
|
const groupIDList = [];
|
|
|
|
let groupOrder = 1;
|
|
|
|
|
|
|
|
for (let group of publicGroupList) {
|
|
|
|
let groupBean;
|
|
|
|
if (group.id) {
|
2022-03-16 07:38:10 +00:00
|
|
|
groupBean = await R.findOne("group", " id = ? AND public = 1 AND status_page_id = ? ", [
|
|
|
|
group.id,
|
|
|
|
statusPage.id
|
2021-09-19 12:04:51 +01:00
|
|
|
]);
|
2021-09-22 08:10:08 +01:00
|
|
|
} else {
|
|
|
|
groupBean = R.dispense("group");
|
|
|
|
}
|
|
|
|
|
2022-03-16 07:38:10 +00:00
|
|
|
groupBean.status_page_id = statusPage.id;
|
2021-09-22 08:10:08 +01:00
|
|
|
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-19 12:04:51 +01:00
|
|
|
|
2021-09-22 08:10:08 +01:00
|
|
|
let monitorOrder = 1;
|
2021-09-22 08:23:58 +01:00
|
|
|
|
2021-09-22 08:10:08 +01: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-19 12:04:51 +01:00
|
|
|
}
|
|
|
|
|
2021-09-22 08:10:08 +01:00
|
|
|
groupIDList.push(groupBean.id);
|
|
|
|
group.id = groupBean.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete groups that not in the list
|
|
|
|
debug("Delete groups that not in the list");
|
|
|
|
const slots = groupIDList.map(() => "?").join(",");
|
|
|
|
await R.exec(`DELETE FROM \`group\` WHERE id NOT IN (${slots})`, groupIDList);
|
2021-09-19 12:04:51 +01:00
|
|
|
|
2021-09-22 08:10:08 +01:00
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
publicGroupList,
|
2021-09-19 12:04:51 +01:00
|
|
|
});
|
2021-09-22 08:10:08 +01:00
|
|
|
|
2021-09-19 12:04:51 +01:00
|
|
|
} catch (error) {
|
2021-09-20 09:22:18 +01:00
|
|
|
console.log(error);
|
2021-09-19 12:04:51 +01:00
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: error.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-16 15:57:34 +01:00
|
|
|
};
|