mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-16 10:46:14 +01:00
refactor: make a single call to set widgets per template
This commit is contained in:
@@ -1,14 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const async = require('async');
|
|
||||||
const widgets = require('../../widgets');
|
const widgets = require('../../widgets');
|
||||||
|
|
||||||
const Widgets = module.exports;
|
const Widgets = module.exports;
|
||||||
|
|
||||||
Widgets.set = function (socket, data, callback) {
|
Widgets.set = async function (socket, data) {
|
||||||
if (!Array.isArray(data)) {
|
if (!Array.isArray(data)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
await widgets.setAreas(data);
|
||||||
async.eachSeries(data, widgets.setArea, callback);
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -167,6 +167,25 @@ widgets.setArea = async function (area) {
|
|||||||
await db.setObjectField(`widgets:${area.template}`, area.location, JSON.stringify(area.widgets));
|
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 () {
|
widgets.reset = async function () {
|
||||||
const defaultAreas = [
|
const defaultAreas = [
|
||||||
{ name: 'Draft Zone', template: 'global', location: 'header' },
|
{ name: 'Draft Zone', template: 'global', location: 'header' },
|
||||||
|
|||||||
Reference in New Issue
Block a user