cleanup and tests

v1.18.x
Baris Usakli 8 years ago
parent 82cab53508
commit 062bced3dd

@ -313,16 +313,13 @@ topicsController.teaser = function (req, res, next) {
} }
posts.getPostSummaryByPids([pid], req.uid, { stripTags: false }, next); posts.getPostSummaryByPids([pid], req.uid, { stripTags: false }, next);
}, },
], function (err, posts) { function (posts) {
if (err) {
return next(err);
}
if (!Array.isArray(posts) || !posts.length) { if (!Array.isArray(posts) || !posts.length) {
return res.status(404).json('not-found'); return res.status(404).json('not-found');
} }
res.json(posts[0]); res.json(posts[0]);
}); },
], next);
}; };
topicsController.pagination = function (req, res, callback) { topicsController.pagination = function (req, res, callback) {

@ -108,14 +108,15 @@ function expose(exposedField, method, field, req, res, next) {
if (!req.params.hasOwnProperty(field)) { if (!req.params.hasOwnProperty(field)) {
return next(); return next();
} }
method(req.params[field], function (err, id) { async.waterfall([
if (err) { function (next) {
return next(err); method(req.params[field], next);
} },
function (id, next) {
res.locals[exposedField] = id; res.locals[exposedField] = id;
next(); next();
}); },
], next);
} }
middleware.privateUploads = function (req, res, next) { middleware.privateUploads = function (req, res, next) {

@ -1,27 +1,31 @@
'use strict'; 'use strict';
var async = require('async');
var nconf = require('nconf'); var nconf = require('nconf');
var meta = require('../meta'); var meta = require('../meta');
var user = require('../user'); var user = require('../user');
module.exports = function (middleware) { module.exports = function (middleware) {
middleware.maintenanceMode = function (req, res, next) { middleware.maintenanceMode = function (req, res, callback) {
if (parseInt(meta.config.maintenanceMode, 10) !== 1) { if (parseInt(meta.config.maintenanceMode, 10) !== 1) {
return next(); return callback();
} }
var url = req.url.replace(nconf.get('relative_path'), ''); var url = req.url.replace(nconf.get('relative_path'), '');
if (url.startsWith('/login') || url.startsWith('/api/login')) { if (url.startsWith('/login') || url.startsWith('/api/login')) {
return next(); return callback();
} }
var data;
user.isAdministrator(req.uid, function (err, isAdmin) { async.waterfall([
if (err || isAdmin) { function (next) {
return next(err); user.isAdministrator(req.uid, next);
},
function (isAdmin, next) {
if (isAdmin) {
return callback();
} }
res.status(503); res.status(503);
var data = { data = {
site_title: meta.config.title || 'NodeBB', site_title: meta.config.title || 'NodeBB',
message: meta.config.maintenanceModeMessage, message: meta.config.maintenanceModeMessage,
}; };
@ -30,9 +34,11 @@ module.exports = function (middleware) {
return res.json(data); return res.json(data);
} }
middleware.buildHeader(req, res, function () { middleware.buildHeader(req, res, next);
},
function () {
res.render('503', data); res.render('503', data);
}); },
}); ], callback);
}; };
}; };

@ -18,8 +18,13 @@ module.exports = function (Posts) {
if (!parseInt(uid, 10)) { if (!parseInt(uid, 10)) {
return callback(new Error('[[error:not-logged-in]]')); return callback(new Error('[[error:not-logged-in]]'));
} }
var isBookmarking = type === 'bookmark';
var isBookmarking = type === 'bookmark';
var postData;
var hasBookmarked;
var owner;
async.waterfall([
function (next) {
async.parallel({ async.parallel({
owner: function (next) { owner: function (next) {
Posts.getPostField(pid, 'uid', next); Posts.getPostField(pid, 'uid', next);
@ -30,21 +35,21 @@ module.exports = function (Posts) {
hasBookmarked: function (next) { hasBookmarked: function (next) {
Posts.hasBookmarked(pid, uid, next); Posts.hasBookmarked(pid, uid, next);
}, },
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
} owner = results.owner;
postData = results.postData;
hasBookmarked = results.hasBookmarked;
if (isBookmarking && results.hasBookmarked) { if (isBookmarking && hasBookmarked) {
return callback(new Error('[[error:already-bookmarked]]')); return callback(new Error('[[error:already-bookmarked]]'));
} }
if (!isBookmarking && !results.hasBookmarked) { if (!isBookmarking && !hasBookmarked) {
return callback(new Error('[[error:already-unbookmarked]]')); return callback(new Error('[[error:already-unbookmarked]]'));
} }
async.waterfall([
function (next) {
if (isBookmarking) { if (isBookmarking) {
db.sortedSetAdd('uid:' + uid + ':bookmarks', Date.now(), pid, next); db.sortedSetAdd('uid:' + uid + ':bookmarks', Date.now(), pid, next);
} else { } else {
@ -58,29 +63,25 @@ module.exports = function (Posts) {
db.setCount('pid:' + pid + ':users_bookmarked', next); db.setCount('pid:' + pid + ':users_bookmarked', next);
}, },
function (count, next) { function (count, next) {
results.postData.bookmarks = count; postData.bookmarks = count;
Posts.setPostField(pid, 'bookmarks', count, next); Posts.setPostField(pid, 'bookmarks', count, next);
}, },
], function (err) { function (next) {
if (err) { var current = hasBookmarked ? 'bookmarked' : 'unbookmarked';
return callback(err);
}
var current = results.hasBookmarked ? 'bookmarked' : 'unbookmarked';
plugins.fireHook('action:post.' + type, { plugins.fireHook('action:post.' + type, {
pid: pid, pid: pid,
uid: uid, uid: uid,
owner: results.owner, owner: owner,
current: current, current: current,
}); });
callback(null, { next(null, {
post: results.postData, post: postData,
isBookmarked: isBookmarking, isBookmarked: isBookmarking,
}); });
}); },
}); ], callback);
} }
Posts.hasBookmarked = function (pid, uid, callback) { Posts.hasBookmarked = function (pid, uid, callback) {

@ -102,12 +102,12 @@ module.exports = function (Posts) {
db.incrObjectField('global', 'postCount', next); db.incrObjectField('global', 'postCount', next);
}, },
], function (err) { ], function (err) {
if (err) { next(err);
return next(err);
}
plugins.fireHook('filter:post.get', { post: postData, uid: data.uid }, next);
}); });
}, },
function (next) {
plugins.fireHook('filter:post.get', { post: postData, uid: data.uid }, next);
},
function (data, next) { function (data, next) {
data.post.isMain = isMain; data.post.isMain = isMain;
plugins.fireHook('action:post.save', { post: _.clone(data.post) }); plugins.fireHook('action:post.save', { post: _.clone(data.post) });

@ -216,40 +216,41 @@ module.exports = function (Posts) {
} }
function deletePostFromCategoryRecentPosts(pid, callback) { function deletePostFromCategoryRecentPosts(pid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) { async.waterfall([
if (err) { function (next) {
return callback(err); db.getSortedSetRange('categories:cid', 0, -1, next);
} },
function (cids, next) {
var sets = cids.map(function (cid) { var sets = cids.map(function (cid) {
return 'cid:' + cid + ':pids'; return 'cid:' + cid + ':pids';
}); });
db.sortedSetsRemove(sets, pid, callback); db.sortedSetsRemove(sets, pid, next);
}); },
], callback);
} }
function deletePostFromUsersBookmarks(pid, callback) { function deletePostFromUsersBookmarks(pid, callback) {
db.getSetMembers('pid:' + pid + ':users_bookmarked', function (err, uids) { async.waterfall([
if (err) { function (next) {
return callback(err); db.getSetMembers('pid:' + pid + ':users_bookmarked', next);
} },
function (uids, next) {
var sets = uids.map(function (uid) { var sets = uids.map(function (uid) {
return 'uid:' + uid + ':bookmarks'; return 'uid:' + uid + ':bookmarks';
}); });
db.sortedSetsRemove(sets, pid, function (err) { db.sortedSetsRemove(sets, pid, next);
if (err) { },
return callback(err); function (next) {
} db.delete('pid:' + pid + ':users_bookmarked', next);
},
db.delete('pid:' + pid + ':users_bookmarked', callback); ], callback);
});
});
} }
function deletePostFromUsersVotes(pid, callback) { function deletePostFromUsersVotes(pid, callback) {
async.waterfall([
function (next) {
async.parallel({ async.parallel({
upvoters: function (next) { upvoters: function (next) {
db.getSetMembers('pid:' + pid + ':upvote', next); db.getSetMembers('pid:' + pid + ':upvote', next);
@ -257,11 +258,9 @@ module.exports = function (Posts) {
downvoters: function (next) { downvoters: function (next) {
db.getSetMembers('pid:' + pid + ':downvote', next); db.getSetMembers('pid:' + pid + ':downvote', next);
}, },
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
}
var upvoterSets = results.upvoters.map(function (uid) { var upvoterSets = results.upvoters.map(function (uid) {
return 'uid:' + uid + ':upvote'; return 'uid:' + uid + ':upvote';
}); });
@ -280,23 +279,26 @@ module.exports = function (Posts) {
function (next) { function (next) {
db.deleteAll(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], next); db.deleteAll(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], next);
}, },
], next);
},
], callback); ], callback);
});
} }
function deletePostFromReplies(pid, callback) { function deletePostFromReplies(pid, callback) {
Posts.getPostField(pid, 'toPid', function (err, toPid) { async.waterfall([
if (err) { function (next) {
return callback(err); Posts.getPostField(pid, 'toPid', next);
} },
function (toPid, next) {
if (!parseInt(toPid, 10)) { if (!parseInt(toPid, 10)) {
return callback(null); return callback(null);
} }
async.parallel([ async.parallel([
async.apply(db.sortedSetRemove, 'pid:' + toPid + ':replies', pid), async.apply(db.sortedSetRemove, 'pid:' + toPid + ':replies', pid),
async.apply(db.decrObjectField, 'post:' + toPid, 'replies'), async.apply(db.decrObjectField, 'post:' + toPid, 'replies'),
], next);
},
], callback); ], callback);
});
} }
function deletePostFromGroups(pid, callback) { function deletePostFromGroups(pid, callback) {

@ -83,6 +83,10 @@ module.exports = function (Posts) {
var tid = postData.tid; var tid = postData.tid;
var title = data.title ? data.title.trim() : ''; var title = data.title ? data.title.trim() : '';
var topicData;
var results;
async.waterfall([
function (next) {
async.parallel({ async.parallel({
topic: function (next) { topic: function (next) {
topics.getTopicFields(tid, ['cid', 'title', 'timestamp'], next); topics.getTopicFields(tid, ['cid', 'title', 'timestamp'], next);
@ -90,11 +94,10 @@ module.exports = function (Posts) {
isMain: function (next) { isMain: function (next) {
Posts.isMain(data.pid, next); Posts.isMain(data.pid, next);
}, },
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (_results, next) {
} results = _results;
if (!results.isMain) { if (!results.isMain) {
return callback(null, { return callback(null, {
tid: tid, tid: tid,
@ -104,7 +107,7 @@ module.exports = function (Posts) {
}); });
} }
var topicData = { topicData = {
tid: tid, tid: tid,
cid: results.topic.cid, cid: results.topic.cid,
uid: postData.uid, uid: postData.uid,
@ -120,8 +123,6 @@ module.exports = function (Posts) {
data.tags = data.tags || []; data.tags = data.tags || [];
async.waterfall([
function (next) {
plugins.fireHook('filter:topic.edit', { req: data.req, topic: topicData, data: data }, next); plugins.fireHook('filter:topic.edit', { req: data.req, topic: topicData, data: data }, next);
}, },
function (results, next) { function (results, next) {
@ -140,7 +141,7 @@ module.exports = function (Posts) {
plugins.fireHook('action:topic.edit', { topic: topicData, uid: data.uid }); plugins.fireHook('action:topic.edit', { topic: topicData, uid: data.uid });
next(null, { next(null, {
tid: tid, tid: tid,
cid: results.topic.cid, cid: topicData.cid,
uid: postData.uid, uid: postData.uid,
title: validator.escape(String(title)), title: validator.escape(String(title)),
oldTitle: results.topic.title, oldTitle: results.topic.title,
@ -151,6 +152,5 @@ module.exports = function (Posts) {
}); });
}, },
], callback); ], callback);
});
} }
}; };

@ -89,34 +89,33 @@ module.exports = function (Posts) {
function parsePosts(posts, options, callback) { function parsePosts(posts, options, callback) {
async.map(posts, function (post, next) { async.map(posts, function (post, next) {
async.waterfall([
function (next) {
if (!post.content || !options.parse) { if (!post.content || !options.parse) {
if (options.stripTags) {
post.content = stripTags(post.content);
}
post.content = post.content ? validator.escape(String(post.content)) : post.content; post.content = post.content ? validator.escape(String(post.content)) : post.content;
return next(null, post); return next(null, post);
} }
Posts.parsePost(post, next);
Posts.parsePost(post, function (err, post) { },
if (err) { function (post, next) {
return next(err);
}
if (options.stripTags) { if (options.stripTags) {
post.content = stripTags(post.content); post.content = stripTags(post.content);
} }
next(null, post); next(null, post);
}); },
], next);
}, callback); }, callback);
} }
function getTopicAndCategories(tids, callback) { function getTopicAndCategories(tids, callback) {
topics.getTopicsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted', 'postcount', 'mainPid'], function (err, topics) { var topicsData;
if (err) { async.waterfall([
return callback(err); function (next) {
} topics.getTopicsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted', 'postcount', 'mainPid'], next);
},
var cids = topics.map(function (topic) { function (_topicsData, next) {
topicsData = _topicsData;
var cids = topicsData.map(function (topic) {
if (topic) { if (topic) {
topic.title = String(topic.title); topic.title = String(topic.title);
topic.deleted = parseInt(topic.deleted, 10) === 1; topic.deleted = parseInt(topic.deleted, 10) === 1;
@ -126,10 +125,12 @@ module.exports = function (Posts) {
return topic && array.indexOf(topic) === index; return topic && array.indexOf(topic) === index;
}); });
categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], function (err, categories) { categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], next);
callback(err, { topics: topics, categories: categories }); },
}); function (categoriesData, next) {
}); next(null, { topics: topicsData, categories: categoriesData });
},
], callback);
} }
function toObject(key, data) { function toObject(key, data) {

@ -25,11 +25,7 @@ module.exports = function (Posts) {
}); });
groups.getGroupsData(groupTitles, next); groups.getGroupsData(groupTitles, next);
}, },
], function (err, groupsData) { function (groupsData, next) {
if (err) {
return callback(err);
}
groupsData.forEach(function (group) { groupsData.forEach(function (group) {
if (group && group.userTitleEnabled) { if (group && group.userTitleEnabled) {
groupsMap[group.name] = { groupsMap[group.name] = {
@ -56,6 +52,8 @@ module.exports = function (Posts) {
}); });
async.map(userData, function (userData, next) { async.map(userData, function (userData, next) {
async.waterfall([
function (next) {
async.parallel({ async.parallel({
isMemberOfGroup: function (next) { isMemberOfGroup: function (next) {
if (!userData.groupTitle || !groupsMap[userData.groupTitle]) { if (!userData.groupTitle || !groupsMap[userData.groupTitle]) {
@ -73,11 +71,9 @@ module.exports = function (Posts) {
customProfileInfo: function (next) { customProfileInfo: function (next) {
plugins.fireHook('filter:posts.custom_profile_info', { profile: [], uid: userData.uid }, next); plugins.fireHook('filter:posts.custom_profile_info', { profile: [], uid: userData.uid }, next);
}, },
}, function (err, results) { }, next);
if (err) { },
return next(err); function (results, next) {
}
if (results.isMemberOfGroup && userData.groupTitle && groupsMap[userData.groupTitle]) { if (results.isMemberOfGroup && userData.groupTitle && groupsMap[userData.groupTitle]) {
userData.selectedGroup = groupsMap[userData.groupTitle]; userData.selectedGroup = groupsMap[userData.groupTitle];
} }
@ -85,9 +81,11 @@ module.exports = function (Posts) {
userData.custom_profile_info = results.customProfileInfo.profile; userData.custom_profile_info = results.customProfileInfo.profile;
plugins.fireHook('filter:posts.modifyUserInfo', userData, next); plugins.fireHook('filter:posts.modifyUserInfo', userData, next);
}); },
}, callback); ], next);
}); }, next);
},
], callback);
}; };
Posts.isOwner = function (pid, uid, callback) { Posts.isOwner = function (pid, uid, callback) {

@ -65,14 +65,14 @@ module.exports = function (Posts) {
if (!parseInt(uid, 10)) { if (!parseInt(uid, 10)) {
return callback(null, { upvoted: false, downvoted: false }); return callback(null, { upvoted: false, downvoted: false });
} }
async.waterfall([
db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, function (err, hasVoted) { function (next) {
if (err) { db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, next);
return callback(err); },
} function (hasVoted, next) {
next(null, { upvoted: hasVoted[0], downvoted: hasVoted[1] });
callback(null, { upvoted: hasVoted[0], downvoted: hasVoted[1] }); },
}); ], callback);
}; };
Posts.getVoteStatusByPostIDs = function (pids, uid, callback) { Posts.getVoteStatusByPostIDs = function (pids, uid, callback) {
@ -124,16 +124,21 @@ module.exports = function (Posts) {
} }
function toggleVote(type, pid, uid, callback) { function toggleVote(type, pid, uid, callback) {
async.waterfall([
function (next) {
unvote(pid, uid, type, function (err) { unvote(pid, uid, type, function (err) {
if (err) { next(err);
return callback(err);
}
vote(type, false, pid, uid, callback);
}); });
},
function (next) {
vote(type, false, pid, uid, next);
},
], callback);
} }
function unvote(pid, uid, command, callback) { function unvote(pid, uid, command, callback) {
async.waterfall([
function (next) {
async.parallel({ async.parallel({
owner: function (next) { owner: function (next) {
Posts.getPostField(pid, 'uid', next); Posts.getPostField(pid, 'uid', next);
@ -144,11 +149,9 @@ module.exports = function (Posts) {
reputation: function (next) { reputation: function (next) {
user.getUserField(uid, 'reputation', next); user.getUserField(uid, 'reputation', next);
}, },
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
}
if (parseInt(uid, 10) === parseInt(results.owner, 10)) { if (parseInt(uid, 10) === parseInt(results.owner, 10)) {
return callback(new Error('self-vote')); return callback(new Error('self-vote'));
} }
@ -181,22 +184,25 @@ module.exports = function (Posts) {
return callback(); return callback();
} }
vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, callback); vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, next);
}); },
], callback);
} }
function vote(type, unvote, pid, uid, callback) { function vote(type, unvote, pid, uid, callback) {
uid = parseInt(uid, 10); uid = parseInt(uid, 10);
if (uid === 0) { if (!uid) {
return callback(new Error('[[error:not-logged-in]]')); return callback(new Error('[[error:not-logged-in]]'));
} }
var postData;
Posts.getPostFields(pid, ['pid', 'uid', 'tid'], function (err, postData) { var newreputation;
if (err) { async.waterfall([
return callback(err); function (next) {
} Posts.getPostFields(pid, ['pid', 'uid', 'tid'], next);
},
function (_postData, next) {
postData = _postData;
var now = Date.now(); var now = Date.now();
if (type === 'upvote' && !unvote) { if (type === 'upvote' && !unvote) {
@ -211,17 +217,18 @@ module.exports = function (Posts) {
db.sortedSetAdd('uid:' + uid + ':downvote', now, pid); db.sortedSetAdd('uid:' + uid + ':downvote', now, pid);
} }
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, function (err, newreputation) { user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, next);
if (err) { },
return callback(err); function (_newreputation, next) {
} newreputation = _newreputation;
if (parseInt(postData.uid, 10)) { if (parseInt(postData.uid, 10)) {
db.sortedSetAdd('users:reputation', newreputation, postData.uid); db.sortedSetAdd('users:reputation', newreputation, postData.uid);
} }
adjustPostVotes(postData, uid, type, unvote, function (err) { adjustPostVotes(postData, uid, type, unvote, next);
callback(err, { },
function (next) {
next(null, {
user: { user: {
reputation: newreputation, reputation: newreputation,
}, },
@ -229,14 +236,14 @@ module.exports = function (Posts) {
upvote: type === 'upvote' && !unvote, upvote: type === 'upvote' && !unvote,
downvote: type === 'downvote' && !unvote, downvote: type === 'downvote' && !unvote,
}); });
}); },
}); ], callback);
});
} }
function adjustPostVotes(postData, uid, type, unvote, callback) { function adjustPostVotes(postData, uid, type, unvote, callback) {
var notType = (type === 'upvote' ? 'downvote' : 'upvote'); var notType = (type === 'upvote' ? 'downvote' : 'upvote');
async.waterfall([
function (next) {
async.series([ async.series([
function (next) { function (next) {
if (unvote) { if (unvote) {
@ -249,10 +256,10 @@ module.exports = function (Posts) {
db.setRemove('pid:' + postData.pid + ':' + notType, uid, next); db.setRemove('pid:' + postData.pid + ':' + notType, uid, next);
}, },
], function (err) { ], function (err) {
if (err) { next(err);
return callback(err); });
} },
function (next) {
async.parallel({ async.parallel({
upvotes: function (next) { upvotes: function (next) {
db.setCount('pid:' + postData.pid + ':upvote', next); db.setCount('pid:' + postData.pid + ':upvote', next);
@ -260,15 +267,14 @@ module.exports = function (Posts) {
downvotes: function (next) { downvotes: function (next) {
db.setCount('pid:' + postData.pid + ':downvote', next); db.setCount('pid:' + postData.pid + ':downvote', next);
}, },
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
}
postData.upvotes = parseInt(results.upvotes, 10); postData.upvotes = parseInt(results.upvotes, 10);
postData.downvotes = parseInt(results.downvotes, 10); postData.downvotes = parseInt(results.downvotes, 10);
postData.votes = postData.upvotes - postData.downvotes; postData.votes = postData.upvotes - postData.downvotes;
Posts.updatePostVoteCount(postData, callback); Posts.updatePostVoteCount(postData, next);
}); },
}); ], callback);
} }
}; };

@ -8,18 +8,18 @@ var plugins = require('../plugins');
var translator = require('../translator'); var translator = require('../translator');
var db = require('../database'); var db = require('../database');
var widgets = {}; var widgets = module.exports;
widgets.render = function (uid, area, req, res, callback) { widgets.render = function (uid, area, req, res, callback) {
if (!area.locations || !area.template) { if (!area.locations || !area.template) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
widgets.getAreas(['global', area.template], area.locations, function (err, data) { async.waterfall([
if (err) { function (next) {
return callback(err); widgets.getAreas(['global', area.template], area.locations, next);
} },
function (data, next) {
var widgetsByLocation = {}; var widgetsByLocation = {};
async.map(area.locations, function (location, done) { async.map(area.locations, function (location, done) {
@ -37,15 +37,29 @@ widgets.render = function (uid, area, req, res, callback) {
return next(); return next();
} }
renderWidget(widget, uid, area, req, res, next);
}, function (err, result) {
done(err, { location: location, widgets: result.filter(Boolean) });
});
}, next);
},
], callback);
};
function renderWidget(widget, uid, area, req, res, callback) {
async.waterfall([
function (next) {
plugins.fireHook('filter:widget.render:' + widget.widget, { plugins.fireHook('filter:widget.render:' + widget.widget, {
uid: uid, uid: uid,
area: area, area: area,
data: widget.data, data: widget.data,
req: req, req: req,
res: res, res: res,
}, function (err, data) { }, next);
if (err || data === null) { },
return next(err); function (data, next) {
if (!data) {
return callback();
} }
var html = data; var html = data;
if (typeof html !== 'string') { if (typeof html !== 'string') {
@ -66,23 +80,19 @@ widgets.render = function (uid, area, req, res, callback) {
} else { } else {
next(null, { html: html }); next(null, { html: html });
} }
}); },
}, function (err, result) { ], callback);
done(err, { location: location, widgets: result.filter(Boolean) }); }
});
}, callback);
});
};
widgets.getAreas = function (templates, locations, callback) { widgets.getAreas = function (templates, locations, callback) {
var keys = templates.map(function (tpl) { var keys = templates.map(function (tpl) {
return 'widgets:' + tpl; return 'widgets:' + tpl;
}); });
db.getObjectsFields(keys, locations, function (err, data) { async.waterfall([
if (err) { function (next) {
return callback(err); db.getObjectsFields(keys, locations, next);
} },
function (data, next) {
var returnData = {}; var returnData = {};
templates.forEach(function (template, index) { templates.forEach(function (template, index) {
@ -101,15 +111,17 @@ widgets.getAreas = function (templates, locations, callback) {
}); });
}); });
callback(null, returnData); next(null, returnData);
}); },
], callback);
}; };
widgets.getArea = function (template, location, callback) { widgets.getArea = function (template, location, callback) {
db.getObjectField('widgets:' + template, location, function (err, result) { async.waterfall([
if (err) { function (next) {
return callback(err); db.getObjectField('widgets:' + template, location, next);
} },
function (result, next) {
if (!result) { if (!result) {
return callback(null, []); return callback(null, []);
} }
@ -119,8 +131,9 @@ widgets.getArea = function (template, location, callback) {
return callback(err); return callback(err);
} }
callback(null, result); next(null, result);
}); },
], callback);
}; };
widgets.setArea = function (area, callback) { widgets.setArea = function (area, callback) {
@ -137,7 +150,9 @@ widgets.reset = function (callback) {
{ 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({ async.parallel({
areas: function (next) { areas: function (next) {
plugins.fireHook('filter:widgets.getAreas', defaultAreas, next); plugins.fireHook('filter:widgets.getAreas', defaultAreas, next);
@ -145,34 +160,32 @@ widgets.reset = function (callback) {
drafts: function (next) { drafts: function (next) {
widgets.getArea('global', 'drafts', next); widgets.getArea('global', 'drafts', next);
}, },
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
} drafts = results.drafts || [];
var drafts = results.drafts || [];
async.each(results.areas, function (area, next) { async.each(results.areas, function (area, next) {
widgets.getArea(area.template, area.location, function (err, areaData) { async.waterfall([
if (err) { function (next) {
return next(err); widgets.getArea(area.template, area.location, next);
} },
function (areaData, next) {
drafts = drafts.concat(areaData); drafts = drafts.concat(areaData);
area.widgets = []; area.widgets = [];
widgets.setArea(area, next); widgets.setArea(area, next);
}); },
}, function (err) { ], next);
if (err) { }, next);
return callback(err); },
} function (next) {
widgets.setArea({ widgets.setArea({
template: 'global', template: 'global',
location: 'drafts', location: 'drafts',
widgets: drafts, widgets: drafts,
}, callback); }, next);
}); },
}); ], callback);
}; };
module.exports = widgets; module.exports = widgets;

@ -138,6 +138,9 @@ function setupMockDefaults(callback) {
function (next) { function (next) {
db.emptydb(next); db.emptydb(next);
}, },
function (next) {
db.createIndices(next);
},
function (next) { function (next) {
winston.info('test_database flushed'); winston.info('test_database flushed');
setupDefaultConfigs(meta, next); setupDefaultConfigs(meta, next);

Loading…
Cancel
Save