[status page] create incident

This commit is contained in:
LouisLam
2021-09-16 22:48:28 +08:00
parent 8230cfe13f
commit 2955abb5d9
13 changed files with 273 additions and 60 deletions

View File

@@ -34,6 +34,7 @@ class Database {
"patch-2fa.sql": true,
"patch-add-retry-interval-monitor.sql": true,
"patch-monitor-public-weight.sql": true,
"patch-incident-table.sql": true,
}
/**

17
server/model/incident.js Normal file
View File

@@ -0,0 +1,17 @@
const { BeanModel } = require("redbean-node/dist/bean-model");
class Incident extends BeanModel {
toPublicJSON() {
return {
id: this.id,
style: this.style,
title: this.title,
content: this.content,
pin: this.pin,
createdDate: this.createdDate,
};
}
}
module.exports = Incident;

View File

@@ -38,7 +38,10 @@ router.get("/api/status-page/incident", async (_, response) => {
try {
await checkPublished();
// TODO:
response.json({
ok: true,
incident: (await R.findOne("incident", " pin = 1 AND active = 1")).toPublicJSON(),
})
} catch (error) {
send403(response, error.message);

View File

@@ -35,7 +35,7 @@ console.log("Importing this project modules");
debug("Importing Monitor");
const Monitor = require("./model/monitor");
debug("Importing Settings");
const { getSettings, setSettings, setting, initJWTSecret, genSecret, allowDevAllOrigin } = require("./util-server");
const { getSettings, setSettings, setting, initJWTSecret, genSecret, allowDevAllOrigin, checkLogin } = require("./util-server");
debug("Importing Notification");
const { Notification } = require("./notification");
@@ -91,6 +91,7 @@ module.exports.io = io;
// Must be after io instantiation
const { sendNotificationList, sendHeartbeatList, sendImportantHeartbeatList } = require("./client");
const { statusPageSocketHandler } = require("./socket-handlers/status-page-socket-handler");
app.use(express.json());
@@ -1104,7 +1105,10 @@ exports.entryPage = "dashboard";
}
});
debug("added all socket handlers")
// Status Page Socket Handler for admin only
statusPageSocketHandler(socket);
debug("added all socket handlers");
// ***************************
// Better do anything after added all socket handlers here
@@ -1208,12 +1212,6 @@ async function getMonitorJSONList(userID) {
return result;
}
function checkLogin(socket) {
if (! socket.userID) {
throw new Error("You are not logged in.");
}
}
async function initDatabase() {
if (! fs.existsSync(Database.path)) {
console.log("Copying Database")

View File

@@ -0,0 +1,61 @@
const { R } = require("redbean-node");
const { checkLogin } = require("../util-server");
const dayjs = require("dayjs");
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;
incidentBean.createdDate = R.isoDateTime(dayjs.utc());
await R.store(incidentBean);
callback({
ok: true,
incident: incidentBean.toPublicJSON(),
})
} catch (error) {
callback({
ok: false,
msg: error.message,
})
}
});
socket.on("unpinIncident", async (callback) => {
try {
checkLogin(socket);
await R.exec("UPDATE incident SET pin = 0 WHERE pin = 1");
callback({
ok: true,
})
} catch (error) {
callback({
ok: false,
msg: error.message,
})
}
});
}

View File

@@ -293,3 +293,9 @@ exports.allowAllOrigin = (res) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}
exports.checkLogin = (socket) => {
if (! socket.userID) {
throw new Error("You are not logged in.");
}
}