From ba90b5d113e21624a8a29a3a217b53476a968ab7 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 21 Sep 2014 18:56:36 -0400 Subject: [PATCH] widget render change --- src/categories.js | 3 ++ src/controllers/api.js | 24 +++++-------- src/widgets.js | 81 +++++++++++++++++++++++++++--------------- 3 files changed, 64 insertions(+), 44 deletions(-) diff --git a/src/categories.js b/src/categories.js index db0729c288..2a80c4a67b 100644 --- a/src/categories.js +++ b/src/categories.js @@ -238,6 +238,9 @@ var db = require('./database'), Categories.getModerators = function(cid, callback) { Groups.get('cid:' + cid + ':privileges:mods', {}, function(err, groupObj) { + if (err && err === 'group-not-found') { + return callback(null, []); + } if (err) { return callback(err); } diff --git a/src/controllers/api.js b/src/controllers/api.js index b15422c157..a43678d22a 100644 --- a/src/controllers/api.js +++ b/src/controllers/api.js @@ -98,21 +98,15 @@ apiController.renderWidgets = function(req, res, next) { return res.json(200, {}); } - async.each(areas.locations, function(location, next) { - widgets.render(uid, { - template: areas.template, - url: areas.url, - location: location - }, function(err, widgets) { - renderedWidgets.push({ - location: location, - widgets: widgets - }); - - next(); - }); - }, function(err) { - res.json(200, renderedWidgets); + widgets.render(uid, { + template: areas.template, + url: areas.url, + locations: areas.locations + }, function(err, widgets) { + if (err) { + return next(err); + } + res.json(200, widgets); }); }; diff --git a/src/widgets.js b/src/widgets.js index efcc768646..1755ab9cb5 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -9,52 +9,75 @@ var async = require('async'), (function(Widgets) { + Widgets.render = function(uid, area, callback) { - if (!area.location || !area.template) { + if (!area.locations || !area.template) { callback({ error: 'Missing location and template data' }); } - var rendered = []; + Widgets.getAreas(['global', area.template], area.locations, function(err, data) { - async.parallel({ - global: function(next) { - Widgets.getArea('global', area.location, next); - }, - local: function(next) { - Widgets.getArea(area.template, area.location, next); - } - }, function(err, data) { - var widgets = data.global.concat(data.local); + var widgetsByLocation = {}; - async.eachSeries(widgets, function(widget, next) { + async.map(area.locations, function(location, done) { + widgetsByLocation[location] = data.global[location].concat(data[area.template][location]); - if (!widget || !widget.data || (!!widget.data['registered-only'] && uid === 0)) { - return next(); + if (!widgetsByLocation[location].length) { + return done(null, {location: location, widgets: []}); } - plugins.fireHook('filter:widget.render:' + widget.widget, { - uid: uid, - area: area, - data: widget.data - }, function(err, html){ - if (widget.data.container && widget.data.container.match('{body}')) { - html = templates.parse(widget.data.container, { - title: widget.data.title, - body: html - }); + async.map(widgetsByLocation[location], function(widget, next) { + + if (!widget || !widget.data || (!!widget.data['registered-only'] && uid === 0)) { + return next(); } - rendered.push({ - html: html + plugins.fireHook('filter:widget.render:' + widget.widget, { + uid: uid, + area: area, + data: widget.data + }, function(err, html) { + if (widget.data.container && widget.data.container.match('{body}')) { + html = templates.parse(widget.data.container, { + title: widget.data.title, + body: html + }); + } + + next(err, {html: html}); }); + }, function(err, widgets) { + done(err, {location: location, widgets: widgets.filter(Boolean)}); + }); + }, callback); + }); + }; - next(err); + Widgets.getAreas = function(templates, locations, callback) { + var keys = templates.map(function(tpl) { + return 'widgets:' + tpl; + }); + db.getObjectsFields(keys, locations, function(err, data) { + if (err) { + return callback(err); + } + + var returnData = {}; + + templates.forEach(function(template, index) { + returnData[template] = returnData[template] || {}; + locations.forEach(function(location) { + if (data && data[index] && data[index][location]) { + returnData[template][location] = JSON.parse(data[index][location]); + } else { + returnData[template][location] = []; + } }); - }, function(err) { - callback(err, rendered); }); + + callback(null, returnData); }); };