From 0b572df3d0a4f011b6db57252c8d8578296c2026 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Mon, 20 Sep 2021 16:22:18 +0800 Subject: [PATCH] [status page] store config --- .../status-page-socket-handler.js | 12 ++++- server/util-server.js | 51 +++++++++---------- src/pages/StatusPage.vue | 23 ++++++--- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/server/socket-handlers/status-page-socket-handler.js b/server/socket-handlers/status-page-socket-handler.js index 34f9b6c63..0a4bf05cf 100644 --- a/server/socket-handlers/status-page-socket-handler.js +++ b/server/socket-handlers/status-page-socket-handler.js @@ -1,5 +1,5 @@ const { R } = require("redbean-node"); -const { checkLogin } = require("../util-server"); +const { checkLogin, setSettings } = require("../util-server"); const dayjs = require("dayjs"); const { debug } = require("../../src/util"); @@ -67,12 +67,19 @@ module.exports.statusPageSocketHandler = (socket) => { }); // Save Status Page - socket.on("saveStatusPage", async (publicGroupList, callback) => { + socket.on("saveStatusPage", async (config, imgDataUrl, publicGroupList, callback) => { try { checkLogin(socket); await R.transaction(async (trx) => { + // Save Config + //TODO + await setSettings("statusPage", config); + + // Save Icon + + // Save Public Group List const groupIDList = []; let groupOrder = 1; @@ -120,6 +127,7 @@ module.exports.statusPageSocketHandler = (socket) => { }); }); } catch (error) { + console.log(error); callback({ ok: false, diff --git a/server/util-server.js b/server/util-server.js index 7dbbb6dfa..4d2b6cbe1 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -5,7 +5,6 @@ const { debug } = require("../src/util"); const passwordHash = require("./password-hash"); const dayjs = require("dayjs"); const { Resolver } = require("dns"); -const { allowAllOrigin } = require("./util-server"); /** * Init or reset JWT secret @@ -24,7 +23,7 @@ exports.initJWTSecret = async () => { jwtSecretBean.value = passwordHash.generate(dayjs() + ""); await R.store(jwtSecretBean); return jwtSecretBean; -} +}; exports.tcping = function (hostname, port) { return new Promise((resolve, reject) => { @@ -45,7 +44,7 @@ exports.tcping = function (hostname, port) { resolve(Math.round(data.max)); }); }); -} +}; exports.ping = async (hostname) => { try { @@ -58,7 +57,7 @@ exports.ping = async (hostname) => { throw e; } } -} +}; exports.pingAsync = function (hostname, ipv6 = false) { return new Promise((resolve, reject) => { @@ -70,13 +69,13 @@ exports.pingAsync = function (hostname, ipv6 = false) { if (err) { reject(err); } else if (ms === null) { - reject(new Error(stdout)) + reject(new Error(stdout)); } else { - resolve(Math.round(ms)) + resolve(Math.round(ms)); } }); }); -} +}; exports.dnsResolve = function (hostname, resolver_server, rrtype) { const resolver = new Resolver(); @@ -99,8 +98,8 @@ exports.dnsResolve = function (hostname, resolver_server, rrtype) { } }); } - }) -} + }); +}; exports.setting = async function (key) { let value = await R.getCell("SELECT `value` FROM setting WHERE `key` = ? ", [ @@ -109,29 +108,29 @@ exports.setting = async function (key) { try { const v = JSON.parse(value); - debug(`Get Setting: ${key}: ${v}`) + debug(`Get Setting: ${key}: ${v}`); return v; } catch (e) { return value; } -} +}; exports.setSetting = async function (key, value) { let bean = await R.findOne("setting", " `key` = ? ", [ key, - ]) + ]); if (!bean) { - bean = R.dispense("setting") + bean = R.dispense("setting"); bean.key = key; } bean.value = JSON.stringify(value); - await R.store(bean) -} + await R.store(bean); +}; exports.getSettings = async function (type) { let list = await R.getAll("SELECT `key`, `value` FROM setting WHERE `type` = ? ", [ type, - ]) + ]); let result = {}; @@ -144,7 +143,7 @@ exports.getSettings = async function (type) { } return result; -} +}; exports.setSettings = async function (type, data) { let keyList = Object.keys(data); @@ -164,12 +163,12 @@ exports.setSettings = async function (type, data) { if (bean.type === type) { bean.value = JSON.stringify(data[key]); - promiseList.push(R.store(bean)) + promiseList.push(R.store(bean)); } } await Promise.all(promiseList); -} +}; // ssl-checker by @dyaa // param: res - response object from axios @@ -219,7 +218,7 @@ exports.checkCertificate = function (res) { issuer, fingerprint, }; -} +}; // Check if the provided status code is within the accepted ranges // Param: status - the status code to check @@ -248,7 +247,7 @@ exports.checkStatusCode = function (status, accepted_codes) { } return false; -} +}; exports.getTotalClientInRoom = (io, roomName) => { @@ -271,7 +270,7 @@ exports.getTotalClientInRoom = (io, roomName) => { } else { return 0; } -} +}; exports.genSecret = () => { let secret = ""; @@ -281,21 +280,21 @@ exports.genSecret = () => { secret += chars.charAt(Math.floor(Math.random() * charsLength)); } return secret; -} +}; exports.allowDevAllOrigin = (res) => { if (process.env.NODE_ENV === "development") { exports.allowAllOrigin(res); } -} +}; 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."); } -} +}; diff --git a/src/pages/StatusPage.vue b/src/pages/StatusPage.vue index 191cd0b19..38cadeb3c 100644 --- a/src/pages/StatusPage.vue +++ b/src/pages/StatusPage.vue @@ -1,5 +1,5 @@ @@ -190,7 +194,6 @@