mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-16 02:36:16 +01:00
refactor: make a single call to set widgets per template
This commit is contained in:
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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' },
|
||||
|
||||
Reference in New Issue
Block a user