Skip to content

Node.js based forum software built for the modern web

refactor: make a single call to set widgets per template

Barış Soner Uşaklı8750ee04a6e3
diff --git a/src/socket.io/admin/widgets.js b/src/socket.io/admin/widgets.js
index 7dfc8e7ab7..b2a4032f49 100644
--- a/src/socket.io/admin/widgets.js
+++ b/src/socket.io/admin/widgets.js
@@ -1,14 +1,12 @@
'use strict';
-const async = require('async');
const widgets = require('../../widgets');
const Widgets = module.exports;
-Widgets.set = function (socket, data, callback) {
+Widgets.set = async function (socket, data) {
if (!Array.isArray(data)) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
-
- async.eachSeries(data, widgets.setArea, callback);
+ await widgets.setAreas(data);
};
diff --git a/src/widgets/index.js b/src/widgets/index.js
index 572121b021..bda9682736 100644
--- a/src/widgets/index.js
+++ b/src/widgets/index.js
@@ -167,6 +167,25 @@ widgets.setArea = async function (area) {
await db.setObjectField(`widgets:${area.template}`, area.location, JSON.stringify(area.widgets));
};
+widgets.setAreas = async function (areas) {
+ const templates = {};
+ areas.forEach((area) => {
+ if (!area.location || !area.template) {
+ throw new Error('Missing location and template data');
+ }
+ templates[area.template] = templates[area.template] || {};
+ templates[area.template][area.location] = JSON.stringify(area.widgets);
+ });
+
+ const keys = [];
+ const data = [];
+ Object.keys(templates).forEach((tpl) => {
+ keys.push(`widgets:${tpl}`);
+ data.push(templates[tpl]);
+ });
+ await db.setObjectBulk(keys, data);
+};
+
widgets.reset = async function () {
const defaultAreas = [
{ name: 'Draft Zone', template: 'global', location: 'header' },