adminsettings: Promisify

This commit is contained in:
Richard Hansen 2021-11-10 18:24:54 -05:00
parent 4e2f816665
commit 46ad93024d

View file

@ -1,12 +1,12 @@
'use strict';
const eejs = require('../../eejs');
const fs = require('fs');
const fsp = require('fs').promises;
const hooks = require('../../../static/js/pluginfw/hooks');
const plugins = require('../../../static/js/pluginfw/plugins');
const settings = require('../../utils/Settings');
exports.expressCreateServer = (hookName, {app}, cb) => {
exports.expressCreateServer = (hookName, {app}) => {
app.get('/admin/settings', (req, res) => {
res.send(eejs.require('ep_etherpad-lite/templates/admin/settings.html', {
req,
@ -14,34 +14,31 @@ exports.expressCreateServer = (hookName, {app}, cb) => {
errors: [],
}));
});
return cb();
};
exports.socketio = (hookName, {io}, cb) => {
exports.socketio = (hookName, {io}) => {
io.of('/settings').on('connection', (socket) => {
const {session: {user: {is_admin: isAdmin} = {}} = {}} = socket.conn.request;
if (!isAdmin) return;
socket.on('load', (query) => {
fs.readFile('settings.json', 'utf8', (err, data) => {
if (err) {
return console.log(err);
}
// if showSettingsInAdminPage is set to false, then return NOT_ALLOWED in the result
if (settings.showSettingsInAdminPage === false) {
socket.emit('settings', {results: 'NOT_ALLOWED'});
} else {
socket.emit('settings', {results: data});
}
});
socket.on('load', async (query) => {
let data;
try {
data = await fsp.readFile('settings.json', 'utf8');
} catch (err) {
return console.log(err);
}
// if showSettingsInAdminPage is set to false, then return NOT_ALLOWED in the result
if (settings.showSettingsInAdminPage === false) {
socket.emit('settings', {results: 'NOT_ALLOWED'});
} else {
socket.emit('settings', {results: data});
}
});
socket.on('saveSettings', (settings) => {
fs.writeFile('settings.json', settings, (err) => {
if (err) throw err;
socket.emit('saveprogress', 'saved');
});
socket.on('saveSettings', async (settings) => {
await fsp.writeFile('settings.json', settings);
socket.emit('saveprogress', 'saved');
});
socket.on('restartServer', async () => {
@ -52,5 +49,4 @@ exports.socketio = (hookName, {io}, cb) => {
await hooks.aCallAll('restartServer');
});
});
return cb();
};