2022-03-10 08:34:30 -05:00
|
|
|
const { BeanModel } = require("redbean-node/dist/bean-model");
|
|
|
|
const { R } = require("redbean-node");
|
|
|
|
|
|
|
|
class StatusPage extends BeanModel {
|
|
|
|
|
2022-04-06 10:43:22 -04:00
|
|
|
static domainMappingList = { };
|
|
|
|
|
|
|
|
/**
|
2022-04-21 13:30:04 -04:00
|
|
|
* Loads domain mapping from DB
|
2022-04-06 10:43:22 -04:00
|
|
|
* Return object like this: { "test-uptime.kuma.pet": "default" }
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async loadDomainMappingList() {
|
2022-04-09 12:25:27 -04:00
|
|
|
StatusPage.domainMappingList = await R.getAssoc(`
|
2022-04-06 10:43:22 -04:00
|
|
|
SELECT domain, slug
|
|
|
|
FROM status_page, status_page_cname
|
|
|
|
WHERE status_page.id = status_page_cname.status_page_id
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:30:04 -04:00
|
|
|
/**
|
|
|
|
* Send status page list to client
|
|
|
|
* @param {Server} io io Socket server instance
|
|
|
|
* @param {Socket} socket Socket.io instance
|
|
|
|
* @returns {Promise<Bean[]>}
|
|
|
|
*/
|
2022-03-10 08:34:30 -05:00
|
|
|
static async sendStatusPageList(io, socket) {
|
|
|
|
let result = {};
|
|
|
|
|
|
|
|
let list = await R.findAll("status_page", " ORDER BY title ");
|
|
|
|
|
|
|
|
for (let item of list) {
|
|
|
|
result[item.id] = await item.toJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
io.to(socket.userID).emit("statusPageList", result);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:30:04 -04:00
|
|
|
/**
|
|
|
|
* Update list of domain names
|
2022-04-21 15:02:18 -04:00
|
|
|
* @param {string[]} domainNameList
|
2022-04-21 13:30:04 -04:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2022-04-09 12:25:27 -04:00
|
|
|
async updateDomainNameList(domainNameList) {
|
|
|
|
|
|
|
|
if (!Array.isArray(domainNameList)) {
|
|
|
|
throw new Error("Invalid array");
|
|
|
|
}
|
|
|
|
|
|
|
|
let trx = await R.begin();
|
|
|
|
|
|
|
|
await trx.exec("DELETE FROM status_page_cname WHERE status_page_id = ?", [
|
|
|
|
this.id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (let domain of domainNameList) {
|
|
|
|
if (typeof domain !== "string") {
|
|
|
|
throw new Error("Invalid domain");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domain.trim() === "") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the domain name is used in another status page, delete it
|
|
|
|
await trx.exec("DELETE FROM status_page_cname WHERE domain = ?", [
|
|
|
|
domain,
|
|
|
|
]);
|
|
|
|
|
|
|
|
let mapping = trx.dispense("status_page_cname");
|
|
|
|
mapping.status_page_id = this.id;
|
|
|
|
mapping.domain = domain;
|
|
|
|
await trx.store(mapping);
|
|
|
|
}
|
|
|
|
await trx.commit();
|
|
|
|
} catch (error) {
|
|
|
|
await trx.rollback();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:30:04 -04:00
|
|
|
/**
|
|
|
|
* Get list of domain names
|
2022-04-21 15:02:18 -04:00
|
|
|
* @returns {Object[]}
|
2022-04-21 13:30:04 -04:00
|
|
|
*/
|
2022-04-09 12:25:27 -04:00
|
|
|
getDomainNameList() {
|
2022-04-06 10:43:22 -04:00
|
|
|
let domainList = [];
|
|
|
|
for (let domain in StatusPage.domainMappingList) {
|
|
|
|
let s = StatusPage.domainMappingList[domain];
|
|
|
|
|
|
|
|
if (this.slug === s) {
|
|
|
|
domainList.push(domain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return domainList;
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:30:04 -04:00
|
|
|
/**
|
2022-04-22 14:10:13 -04:00
|
|
|
* Return an object that ready to parse to JSON
|
2022-04-21 13:30:04 -04:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-03-10 08:34:30 -05:00
|
|
|
async toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
slug: this.slug,
|
|
|
|
title: this.title,
|
2022-03-16 02:14:47 -04:00
|
|
|
description: this.description,
|
2022-03-17 04:42:26 -04:00
|
|
|
icon: this.getIcon(),
|
2022-03-10 08:34:30 -05:00
|
|
|
theme: this.theme,
|
|
|
|
published: !!this.published,
|
|
|
|
showTags: !!this.show_tags,
|
2022-04-09 12:25:27 -04:00
|
|
|
domainNameList: this.getDomainNameList(),
|
2022-04-17 02:53:13 -04:00
|
|
|
customCSS: this.custom_css,
|
|
|
|
footerText: this.footer_text,
|
|
|
|
showPoweredBy: !!this.show_powered_by,
|
2022-03-10 08:34:30 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:30:04 -04:00
|
|
|
/**
|
2022-04-22 14:10:13 -04:00
|
|
|
* Return an object that ready to parse to JSON for public
|
2022-04-21 13:30:04 -04:00
|
|
|
* Only show necessary data to public
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-03-10 08:34:30 -05:00
|
|
|
async toPublicJSON() {
|
|
|
|
return {
|
|
|
|
slug: this.slug,
|
|
|
|
title: this.title,
|
2022-03-16 02:14:47 -04:00
|
|
|
description: this.description,
|
2022-03-17 04:42:26 -04:00
|
|
|
icon: this.getIcon(),
|
2022-03-10 08:34:30 -05:00
|
|
|
theme: this.theme,
|
|
|
|
published: !!this.published,
|
|
|
|
showTags: !!this.show_tags,
|
2022-04-17 02:53:13 -04:00
|
|
|
customCSS: this.custom_css,
|
|
|
|
footerText: this.footer_text,
|
|
|
|
showPoweredBy: !!this.show_powered_by,
|
2022-03-10 08:34:30 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:30:04 -04:00
|
|
|
/**
|
|
|
|
* Convert slug to status page ID
|
|
|
|
* @param {string} slug
|
|
|
|
*/
|
2022-03-16 03:38:10 -04:00
|
|
|
static async slugToID(slug) {
|
|
|
|
return await R.getCell("SELECT id FROM status_page WHERE slug = ? ", [
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:30:04 -04:00
|
|
|
/**
|
|
|
|
* Get path to the icon for the page
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-03-17 04:42:26 -04:00
|
|
|
getIcon() {
|
|
|
|
if (!this.icon) {
|
|
|
|
return "/icon.svg";
|
|
|
|
} else {
|
|
|
|
return this.icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 08:34:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = StatusPage;
|