mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: widgets/index.js
enable widget-essentials in tests fix widget test
This commit is contained in:
@@ -1,180 +1,144 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const async = require('async');
|
||||||
var winston = require('winston');
|
const winston = require('winston');
|
||||||
var _ = require('lodash');
|
const _ = require('lodash');
|
||||||
var Benchpress = require('benchpressjs');
|
const Benchpress = require('benchpressjs');
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
var plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
var groups = require('../groups');
|
const groups = require('../groups');
|
||||||
var translator = require('../translator');
|
const translator = require('../translator');
|
||||||
var db = require('../database');
|
const db = require('../database');
|
||||||
var apiController = require('../controllers/api');
|
const apiController = require('../controllers/api');
|
||||||
var meta = require('../meta');
|
const loadConfigAsync = util.promisify(apiController.loadConfig);
|
||||||
|
const meta = require('../meta');
|
||||||
|
|
||||||
var widgets = module.exports;
|
const widgets = module.exports;
|
||||||
|
|
||||||
widgets.render = function (uid, options, callback) {
|
widgets.render = async function (uid, options) {
|
||||||
if (!options.template) {
|
if (!options.template) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
const data = await widgets.getWidgetDataForTemplates(['global', options.template]);
|
||||||
|
delete data.global.drafts;
|
||||||
|
|
||||||
async.waterfall([
|
const locations = _.uniq(Object.keys(data.global).concat(Object.keys(data[options.template])));
|
||||||
function (next) {
|
|
||||||
widgets.getWidgetDataForTemplates(['global', options.template], next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
var widgetsByLocation = {};
|
|
||||||
|
|
||||||
delete data.global.drafts;
|
const widgetData = await Promise.all(locations.map(location => renderLocation(location, data, uid, options)));
|
||||||
|
|
||||||
var locations = _.uniq(Object.keys(data.global).concat(Object.keys(data[options.template])));
|
const returnData = {};
|
||||||
|
locations.forEach(function (location, i) {
|
||||||
|
if (Array.isArray(widgetData[i]) && widgetData[i].length) {
|
||||||
|
returnData[location] = widgetData[i].filter(Boolean);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
var returnData = {};
|
return returnData;
|
||||||
|
|
||||||
async.each(locations, function (location, done) {
|
|
||||||
widgetsByLocation[location] = (data[options.template][location] || []).concat(data.global[location] || []);
|
|
||||||
|
|
||||||
if (!widgetsByLocation[location].length) {
|
|
||||||
return done(null, { location: location, widgets: [] });
|
|
||||||
}
|
|
||||||
|
|
||||||
async.map(widgetsByLocation[location], function (widget, next) {
|
|
||||||
renderWidget(widget, uid, options, next);
|
|
||||||
}, function (err, renderedWidgets) {
|
|
||||||
if (err) {
|
|
||||||
return done(err);
|
|
||||||
}
|
|
||||||
renderedWidgets = renderedWidgets.filter(Boolean);
|
|
||||||
returnData[location] = renderedWidgets.length ? renderedWidgets : undefined;
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
}, function (err) {
|
|
||||||
next(err, returnData);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function renderWidget(widget, uid, options, callback) {
|
async function renderLocation(location, data, uid, options) {
|
||||||
var userLang;
|
const widgetsAtLocation = (data[options.template][location] || []).concat(data.global[location] || []);
|
||||||
|
|
||||||
if (!widget || !widget.data || (!!widget.data['hide-mobile'] && options.req.useragent.isMobile)) {
|
if (!widgetsAtLocation.length) {
|
||||||
return setImmediate(callback);
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
const renderedWidgets = await Promise.all(widgetsAtLocation.map(widget => renderWidget(widget, uid, options)));
|
||||||
function (next) {
|
return renderedWidgets;
|
||||||
if (!widget.data.groups.length) {
|
|
||||||
return next(null, true);
|
|
||||||
}
|
|
||||||
groups.isMemberOfAny(uid, widget.data.groups, next);
|
|
||||||
},
|
|
||||||
function (isVisible, next) {
|
|
||||||
if (!isVisible) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.res.locals.isAPI) {
|
|
||||||
apiController.loadConfig(options.req, next);
|
|
||||||
} else {
|
|
||||||
next(null, options.res.locals.config || {});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
function (config, next) {
|
|
||||||
userLang = config.userLang || meta.config.defaultLang || 'en-GB';
|
|
||||||
var templateData = _.assign({ }, options.templateData, { config: config });
|
|
||||||
plugins.fireHook('filter:widget.render:' + widget.widget, {
|
|
||||||
uid: uid,
|
|
||||||
area: options,
|
|
||||||
templateData: templateData,
|
|
||||||
data: widget.data,
|
|
||||||
req: options.req,
|
|
||||||
res: options.res,
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
if (!data) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
var html = data;
|
|
||||||
if (typeof html !== 'string') {
|
|
||||||
html = data.html;
|
|
||||||
} else {
|
|
||||||
winston.warn('[widgets.render] passing a string is deprecated!, filter:widget.render:' + widget.widget + '. Please set hookData.html in your plugin.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (widget.data.container && widget.data.container.match('{body}')) {
|
|
||||||
Benchpress.compileParse(widget.data.container, {
|
|
||||||
title: widget.data.title,
|
|
||||||
body: html,
|
|
||||||
template: data.templateData && data.templateData.template,
|
|
||||||
}, next);
|
|
||||||
} else {
|
|
||||||
next(null, html);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
function (html, next) {
|
|
||||||
translator.translate(html, userLang, function (translatedHtml) {
|
|
||||||
next(null, { html: translatedHtml });
|
|
||||||
});
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
widgets.getWidgetDataForTemplates = function (templates, callback) {
|
async function renderWidget(widget, uid, options) {
|
||||||
async.waterfall([
|
if (!widget || !widget.data || (!!widget.data['hide-mobile'] && options.req.useragent.isMobile)) {
|
||||||
function (next) {
|
return;
|
||||||
const keys = templates.map(tpl => 'widgets:' + tpl);
|
}
|
||||||
db.getObjects(keys, next);
|
let isVisible = true;
|
||||||
},
|
if (widget.data.groups.length) {
|
||||||
function (data, next) {
|
isVisible = await groups.isMemberOfAny(uid, widget.data.groups);
|
||||||
var returnData = {};
|
}
|
||||||
|
if (!isVisible) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let config = options.res.locals.config || {};
|
||||||
|
if (options.res.locals.isAPI) {
|
||||||
|
config = await loadConfigAsync(options.req);
|
||||||
|
}
|
||||||
|
|
||||||
templates.forEach(function (template, index) {
|
const userLang = config.userLang || meta.config.defaultLang || 'en-GB';
|
||||||
returnData[template] = returnData[template] || {};
|
const templateData = _.assign({ }, options.templateData, { config: config });
|
||||||
|
const data = await plugins.fireHook('filter:widget.render:' + widget.widget, {
|
||||||
|
uid: uid,
|
||||||
|
area: options,
|
||||||
|
templateData: templateData,
|
||||||
|
data: widget.data,
|
||||||
|
req: options.req,
|
||||||
|
res: options.res,
|
||||||
|
});
|
||||||
|
|
||||||
var templateWidgetData = data[index] || {};
|
if (!data) {
|
||||||
var locations = Object.keys(templateWidgetData);
|
return;
|
||||||
|
}
|
||||||
|
let html = data;
|
||||||
|
if (typeof html !== 'string') {
|
||||||
|
html = data.html;
|
||||||
|
} else {
|
||||||
|
winston.warn('[widgets.render] passing a string is deprecated!, filter:widget.render:' + widget.widget + '. Please set hookData.html in your plugin.');
|
||||||
|
}
|
||||||
|
|
||||||
locations.forEach(function (location) {
|
if (widget.data.container && widget.data.container.match('{body}')) {
|
||||||
if (templateWidgetData && templateWidgetData[location]) {
|
html = await Benchpress.compileRender(widget.data.container, {
|
||||||
try {
|
title: widget.data.title,
|
||||||
returnData[template][location] = parseWidgetData(templateWidgetData[location]);
|
body: html,
|
||||||
} catch (err) {
|
template: data.templateData && data.templateData.template,
|
||||||
winston.error('can not parse widget data. template: ' + template + ' location: ' + location);
|
});
|
||||||
returnData[template][location] = [];
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
returnData[template][location] = [];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
next(null, returnData);
|
if (html !== undefined) {
|
||||||
},
|
html = await translator.translate(html, userLang);
|
||||||
], callback);
|
}
|
||||||
|
|
||||||
|
return { html: html };
|
||||||
|
}
|
||||||
|
|
||||||
|
widgets.getWidgetDataForTemplates = async function (templates) {
|
||||||
|
const keys = templates.map(tpl => 'widgets:' + tpl);
|
||||||
|
const data = await db.getObjects(keys);
|
||||||
|
|
||||||
|
const returnData = {};
|
||||||
|
|
||||||
|
templates.forEach(function (template, index) {
|
||||||
|
returnData[template] = returnData[template] || {};
|
||||||
|
|
||||||
|
const templateWidgetData = data[index] || {};
|
||||||
|
const locations = Object.keys(templateWidgetData);
|
||||||
|
|
||||||
|
locations.forEach(function (location) {
|
||||||
|
if (templateWidgetData && templateWidgetData[location]) {
|
||||||
|
try {
|
||||||
|
returnData[template][location] = parseWidgetData(templateWidgetData[location]);
|
||||||
|
} catch (err) {
|
||||||
|
winston.error('can not parse widget data. template: ' + template + ' location: ' + location);
|
||||||
|
returnData[template][location] = [];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
returnData[template][location] = [];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return returnData;
|
||||||
};
|
};
|
||||||
|
|
||||||
widgets.getArea = function (template, location, callback) {
|
widgets.getArea = async function (template, location) {
|
||||||
async.waterfall([
|
const result = await db.getObjectField('widgets:' + template, location);
|
||||||
function (next) {
|
if (!result) {
|
||||||
db.getObjectField('widgets:' + template, location, next);
|
return [];
|
||||||
},
|
}
|
||||||
function (result, next) {
|
try {
|
||||||
if (!result) {
|
return parseWidgetData(result);
|
||||||
return callback(null, []);
|
} catch (err) {
|
||||||
}
|
throw err;
|
||||||
try {
|
}
|
||||||
result = parseWidgetData(result);
|
|
||||||
} catch (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
next(null, result);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function parseWidgetData(data) {
|
function parseWidgetData(data) {
|
||||||
@@ -190,84 +154,62 @@ function parseWidgetData(data) {
|
|||||||
return widgets;
|
return widgets;
|
||||||
}
|
}
|
||||||
|
|
||||||
widgets.setArea = function (area, callback) {
|
widgets.setArea = async function (area) {
|
||||||
if (!area.location || !area.template) {
|
if (!area.location || !area.template) {
|
||||||
return callback(new Error('Missing location and template data'));
|
throw new Error('Missing location and template data');
|
||||||
}
|
}
|
||||||
|
|
||||||
db.setObjectField('widgets:' + area.template, area.location, JSON.stringify(area.widgets), callback);
|
await db.setObjectField('widgets:' + area.template, area.location, JSON.stringify(area.widgets));
|
||||||
};
|
};
|
||||||
|
|
||||||
widgets.reset = function (callback) {
|
widgets.reset = async function () {
|
||||||
var defaultAreas = [
|
const defaultAreas = [
|
||||||
{ name: 'Draft Zone', template: 'global', location: 'header' },
|
{ name: 'Draft Zone', template: 'global', location: 'header' },
|
||||||
{ name: 'Draft Zone', template: 'global', location: 'footer' },
|
{ name: 'Draft Zone', template: 'global', location: 'footer' },
|
||||||
{ name: 'Draft Zone', template: 'global', location: 'sidebar' },
|
{ name: 'Draft Zone', template: 'global', location: 'sidebar' },
|
||||||
];
|
];
|
||||||
var drafts;
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
async.parallel({
|
|
||||||
areas: function (next) {
|
|
||||||
plugins.fireHook('filter:widgets.getAreas', defaultAreas, next);
|
|
||||||
},
|
|
||||||
drafts: function (next) {
|
|
||||||
widgets.getArea('global', 'drafts', next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
drafts = results.drafts || [];
|
|
||||||
|
|
||||||
async.eachSeries(results.areas, function (area, next) {
|
const [areas, drafts] = await Promise.all([
|
||||||
async.waterfall([
|
plugins.fireHook('filter:widgets.getAreas', defaultAreas),
|
||||||
function (next) {
|
widgets.getArea('global', 'drafts'),
|
||||||
widgets.getArea(area.template, area.location, next);
|
]);
|
||||||
},
|
|
||||||
function (areaData, next) {
|
let saveDrafts = drafts || [];
|
||||||
drafts = drafts.concat(areaData);
|
for (const area of areas) {
|
||||||
area.widgets = [];
|
/* eslint-disable no-await-in-loop */
|
||||||
widgets.setArea(area, next);
|
const areaData = await widgets.getArea(area.template, area.location);
|
||||||
},
|
saveDrafts = saveDrafts.concat(areaData);
|
||||||
], next);
|
area.widgets = [];
|
||||||
}, next);
|
await widgets.setArea(area);
|
||||||
},
|
}
|
||||||
function (next) {
|
|
||||||
widgets.setArea({
|
await widgets.setArea({
|
||||||
template: 'global',
|
template: 'global',
|
||||||
location: 'drafts',
|
location: 'drafts',
|
||||||
widgets: drafts,
|
widgets: saveDrafts,
|
||||||
}, next);
|
});
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
widgets.resetTemplate = function (template, callback) {
|
widgets.resetTemplate = async function (template) {
|
||||||
var toBeDrafted = [];
|
let toBeDrafted = [];
|
||||||
async.waterfall([
|
const area = await db.getObject('widgets:' + template + '.tpl');
|
||||||
function (next) {
|
for (var location in area) {
|
||||||
db.getObject('widgets:' + template + '.tpl', next);
|
if (area.hasOwnProperty(location)) {
|
||||||
},
|
toBeDrafted = toBeDrafted.concat(JSON.parse(area[location]));
|
||||||
function (area, next) {
|
}
|
||||||
for (var location in area) {
|
}
|
||||||
if (area.hasOwnProperty(location)) {
|
await db.delete('widgets:' + template + '.tpl');
|
||||||
toBeDrafted = toBeDrafted.concat(JSON.parse(area[location]));
|
let draftWidgets = await db.getObjectField('widgets:global', 'drafts');
|
||||||
}
|
draftWidgets = JSON.parse(draftWidgets).concat(toBeDrafted);
|
||||||
}
|
await db.setObjectField('widgets:global', 'drafts', JSON.stringify(draftWidgets));
|
||||||
db.delete('widgets:' + template + '.tpl', next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
db.getObjectField('widgets:global', 'drafts', next);
|
|
||||||
},
|
|
||||||
function (draftWidgets, next) {
|
|
||||||
draftWidgets = JSON.parse(draftWidgets).concat(toBeDrafted);
|
|
||||||
db.setObjectField('widgets:global', 'drafts', JSON.stringify(draftWidgets), next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
widgets.resetTemplates = function (templates, callback) {
|
widgets.resetTemplates = async function (templates) {
|
||||||
async.eachSeries(templates, widgets.resetTemplate, callback);
|
async.eachSeries(templates, widgets.resetTemplate);
|
||||||
|
for (const template of templates) {
|
||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
|
await widgets.resetTemplate(template);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
require('../promisify')(widgets);
|
require('../promisify')(widgets);
|
||||||
|
|||||||
@@ -886,14 +886,11 @@ describe('Controllers', function () {
|
|||||||
widgets: [
|
widgets: [
|
||||||
{
|
{
|
||||||
widget: 'html',
|
widget: 'html',
|
||||||
data: [{
|
data: {
|
||||||
widget: 'html',
|
html: 'test',
|
||||||
data: {
|
title: '',
|
||||||
html: 'test',
|
container: '',
|
||||||
title: '',
|
},
|
||||||
container: '',
|
|
||||||
},
|
|
||||||
}],
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
@@ -920,6 +917,7 @@ describe('Controllers', function () {
|
|||||||
assert.equal(res.statusCode, 200);
|
assert.equal(res.statusCode, 200);
|
||||||
assert(body.widgets);
|
assert(body.widgets);
|
||||||
assert(body.widgets.sidebar);
|
assert(body.widgets.sidebar);
|
||||||
|
assert.equal(body.widgets.sidebar[0].html, 'test');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -257,6 +257,7 @@ function enableDefaultPlugins(callback) {
|
|||||||
var defaultEnabled = [
|
var defaultEnabled = [
|
||||||
'nodebb-plugin-dbsearch',
|
'nodebb-plugin-dbsearch',
|
||||||
'nodebb-plugin-soundpack-default',
|
'nodebb-plugin-soundpack-default',
|
||||||
|
'nodebb-widget-essentials',
|
||||||
];
|
];
|
||||||
|
|
||||||
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
|
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
|
||||||
|
|||||||
Reference in New Issue
Block a user