mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-08 17:13:08 +08:00
[status page] create incident
This commit is contained in:
@@ -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
17
server/model/incident.js
Normal 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;
|
@@ -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);
|
||||
|
@@ -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")
|
||||
|
61
server/socket-handlers/status-page-socket-handler.js
Normal file
61
server/socket-handlers/status-page-socket-handler.js
Normal 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,
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user