mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
cleanup and tests
This commit is contained in:
@@ -313,16 +313,13 @@ topicsController.teaser = function (req, res, next) {
|
||||
}
|
||||
posts.getPostSummaryByPids([pid], req.uid, { stripTags: false }, next);
|
||||
},
|
||||
], function (err, posts) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
function (posts) {
|
||||
if (!Array.isArray(posts) || !posts.length) {
|
||||
return res.status(404).json('not-found');
|
||||
}
|
||||
res.json(posts[0]);
|
||||
});
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
||||
topicsController.pagination = function (req, res, callback) {
|
||||
|
||||
@@ -108,14 +108,15 @@ function expose(exposedField, method, field, req, res, next) {
|
||||
if (!req.params.hasOwnProperty(field)) {
|
||||
return next();
|
||||
}
|
||||
method(req.params[field], function (err, id) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
method(req.params[field], next);
|
||||
},
|
||||
function (id, next) {
|
||||
res.locals[exposedField] = id;
|
||||
next();
|
||||
});
|
||||
},
|
||||
], next);
|
||||
}
|
||||
|
||||
middleware.privateUploads = function (req, res, next) {
|
||||
|
||||
@@ -1,27 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
var meta = require('../meta');
|
||||
var user = require('../user');
|
||||
|
||||
module.exports = function (middleware) {
|
||||
middleware.maintenanceMode = function (req, res, next) {
|
||||
middleware.maintenanceMode = function (req, res, callback) {
|
||||
if (parseInt(meta.config.maintenanceMode, 10) !== 1) {
|
||||
return next();
|
||||
return callback();
|
||||
}
|
||||
var url = req.url.replace(nconf.get('relative_path'), '');
|
||||
|
||||
if (url.startsWith('/login') || url.startsWith('/api/login')) {
|
||||
return next();
|
||||
return callback();
|
||||
}
|
||||
|
||||
user.isAdministrator(req.uid, function (err, isAdmin) {
|
||||
if (err || isAdmin) {
|
||||
return next(err);
|
||||
var data;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.isAdministrator(req.uid, next);
|
||||
},
|
||||
function (isAdmin, next) {
|
||||
if (isAdmin) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
res.status(503);
|
||||
var data = {
|
||||
data = {
|
||||
site_title: meta.config.title || 'NodeBB',
|
||||
message: meta.config.maintenanceModeMessage,
|
||||
};
|
||||
@@ -30,9 +34,11 @@ module.exports = function (middleware) {
|
||||
return res.json(data);
|
||||
}
|
||||
|
||||
middleware.buildHeader(req, res, function () {
|
||||
middleware.buildHeader(req, res, next);
|
||||
},
|
||||
function () {
|
||||
res.render('503', data);
|
||||
});
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
};
|
||||
|
||||
@@ -18,8 +18,13 @@ module.exports = function (Posts) {
|
||||
if (!parseInt(uid, 10)) {
|
||||
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({
|
||||
owner: function (next) {
|
||||
Posts.getPostField(pid, 'uid', next);
|
||||
@@ -30,21 +35,21 @@ module.exports = function (Posts) {
|
||||
hasBookmarked: function (next) {
|
||||
Posts.hasBookmarked(pid, uid, next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
}, next);
|
||||
},
|
||||
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]]'));
|
||||
}
|
||||
|
||||
if (!isBookmarking && !results.hasBookmarked) {
|
||||
if (!isBookmarking && !hasBookmarked) {
|
||||
return callback(new Error('[[error:already-unbookmarked]]'));
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (isBookmarking) {
|
||||
db.sortedSetAdd('uid:' + uid + ':bookmarks', Date.now(), pid, next);
|
||||
} else {
|
||||
@@ -58,29 +63,25 @@ module.exports = function (Posts) {
|
||||
db.setCount('pid:' + pid + ':users_bookmarked', next);
|
||||
},
|
||||
function (count, next) {
|
||||
results.postData.bookmarks = count;
|
||||
postData.bookmarks = count;
|
||||
Posts.setPostField(pid, 'bookmarks', count, next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var current = results.hasBookmarked ? 'bookmarked' : 'unbookmarked';
|
||||
function (next) {
|
||||
var current = hasBookmarked ? 'bookmarked' : 'unbookmarked';
|
||||
|
||||
plugins.fireHook('action:post.' + type, {
|
||||
pid: pid,
|
||||
uid: uid,
|
||||
owner: results.owner,
|
||||
owner: owner,
|
||||
current: current,
|
||||
});
|
||||
|
||||
callback(null, {
|
||||
post: results.postData,
|
||||
next(null, {
|
||||
post: postData,
|
||||
isBookmarked: isBookmarking,
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
Posts.hasBookmarked = function (pid, uid, callback) {
|
||||
|
||||
@@ -102,12 +102,12 @@ module.exports = function (Posts) {
|
||||
db.incrObjectField('global', 'postCount', next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
plugins.fireHook('filter:post.get', { post: postData, uid: data.uid }, next);
|
||||
next(err);
|
||||
});
|
||||
},
|
||||
function (next) {
|
||||
plugins.fireHook('filter:post.get', { post: postData, uid: data.uid }, next);
|
||||
},
|
||||
function (data, next) {
|
||||
data.post.isMain = isMain;
|
||||
plugins.fireHook('action:post.save', { post: _.clone(data.post) });
|
||||
|
||||
@@ -216,40 +216,41 @@ module.exports = function (Posts) {
|
||||
}
|
||||
|
||||
function deletePostFromCategoryRecentPosts(pid, callback) {
|
||||
db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getSortedSetRange('categories:cid', 0, -1, next);
|
||||
},
|
||||
function (cids, next) {
|
||||
var sets = cids.map(function (cid) {
|
||||
return 'cid:' + cid + ':pids';
|
||||
});
|
||||
|
||||
db.sortedSetsRemove(sets, pid, callback);
|
||||
});
|
||||
db.sortedSetsRemove(sets, pid, next);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function deletePostFromUsersBookmarks(pid, callback) {
|
||||
db.getSetMembers('pid:' + pid + ':users_bookmarked', function (err, uids) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getSetMembers('pid:' + pid + ':users_bookmarked', next);
|
||||
},
|
||||
function (uids, next) {
|
||||
var sets = uids.map(function (uid) {
|
||||
return 'uid:' + uid + ':bookmarks';
|
||||
});
|
||||
|
||||
db.sortedSetsRemove(sets, pid, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
db.delete('pid:' + pid + ':users_bookmarked', callback);
|
||||
});
|
||||
});
|
||||
db.sortedSetsRemove(sets, pid, next);
|
||||
},
|
||||
function (next) {
|
||||
db.delete('pid:' + pid + ':users_bookmarked', next);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function deletePostFromUsersVotes(pid, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
upvoters: function (next) {
|
||||
db.getSetMembers('pid:' + pid + ':upvote', next);
|
||||
@@ -257,11 +258,9 @@ module.exports = function (Posts) {
|
||||
downvoters: function (next) {
|
||||
db.getSetMembers('pid:' + pid + ':downvote', next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
var upvoterSets = results.upvoters.map(function (uid) {
|
||||
return 'uid:' + uid + ':upvote';
|
||||
});
|
||||
@@ -280,23 +279,26 @@ module.exports = function (Posts) {
|
||||
function (next) {
|
||||
db.deleteAll(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], next);
|
||||
},
|
||||
], next);
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
}
|
||||
|
||||
function deletePostFromReplies(pid, callback) {
|
||||
Posts.getPostField(pid, 'toPid', function (err, toPid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Posts.getPostField(pid, 'toPid', next);
|
||||
},
|
||||
function (toPid, next) {
|
||||
if (!parseInt(toPid, 10)) {
|
||||
return callback(null);
|
||||
}
|
||||
async.parallel([
|
||||
async.apply(db.sortedSetRemove, 'pid:' + toPid + ':replies', pid),
|
||||
async.apply(db.decrObjectField, 'post:' + toPid, 'replies'),
|
||||
], next);
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
}
|
||||
|
||||
function deletePostFromGroups(pid, callback) {
|
||||
|
||||
@@ -83,6 +83,10 @@ module.exports = function (Posts) {
|
||||
var tid = postData.tid;
|
||||
var title = data.title ? data.title.trim() : '';
|
||||
|
||||
var topicData;
|
||||
var results;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
topic: function (next) {
|
||||
topics.getTopicFields(tid, ['cid', 'title', 'timestamp'], next);
|
||||
@@ -90,11 +94,10 @@ module.exports = function (Posts) {
|
||||
isMain: function (next) {
|
||||
Posts.isMain(data.pid, next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
}, next);
|
||||
},
|
||||
function (_results, next) {
|
||||
results = _results;
|
||||
if (!results.isMain) {
|
||||
return callback(null, {
|
||||
tid: tid,
|
||||
@@ -104,7 +107,7 @@ module.exports = function (Posts) {
|
||||
});
|
||||
}
|
||||
|
||||
var topicData = {
|
||||
topicData = {
|
||||
tid: tid,
|
||||
cid: results.topic.cid,
|
||||
uid: postData.uid,
|
||||
@@ -120,8 +123,6 @@ module.exports = function (Posts) {
|
||||
|
||||
data.tags = data.tags || [];
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
plugins.fireHook('filter:topic.edit', { req: data.req, topic: topicData, data: data }, next);
|
||||
},
|
||||
function (results, next) {
|
||||
@@ -140,7 +141,7 @@ module.exports = function (Posts) {
|
||||
plugins.fireHook('action:topic.edit', { topic: topicData, uid: data.uid });
|
||||
next(null, {
|
||||
tid: tid,
|
||||
cid: results.topic.cid,
|
||||
cid: topicData.cid,
|
||||
uid: postData.uid,
|
||||
title: validator.escape(String(title)),
|
||||
oldTitle: results.topic.title,
|
||||
@@ -151,6 +152,5 @@ module.exports = function (Posts) {
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -89,34 +89,33 @@ module.exports = function (Posts) {
|
||||
|
||||
function parsePosts(posts, options, callback) {
|
||||
async.map(posts, function (post, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (!post.content || !options.parse) {
|
||||
if (options.stripTags) {
|
||||
post.content = stripTags(post.content);
|
||||
}
|
||||
post.content = post.content ? validator.escape(String(post.content)) : post.content;
|
||||
return next(null, post);
|
||||
}
|
||||
|
||||
Posts.parsePost(post, function (err, post) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
Posts.parsePost(post, next);
|
||||
},
|
||||
function (post, next) {
|
||||
if (options.stripTags) {
|
||||
post.content = stripTags(post.content);
|
||||
}
|
||||
|
||||
next(null, post);
|
||||
});
|
||||
},
|
||||
], next);
|
||||
}, callback);
|
||||
}
|
||||
|
||||
function getTopicAndCategories(tids, callback) {
|
||||
topics.getTopicsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted', 'postcount', 'mainPid'], function (err, topics) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var cids = topics.map(function (topic) {
|
||||
var topicsData;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
topics.getTopicsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted', 'postcount', 'mainPid'], next);
|
||||
},
|
||||
function (_topicsData, next) {
|
||||
topicsData = _topicsData;
|
||||
var cids = topicsData.map(function (topic) {
|
||||
if (topic) {
|
||||
topic.title = String(topic.title);
|
||||
topic.deleted = parseInt(topic.deleted, 10) === 1;
|
||||
@@ -126,10 +125,12 @@ module.exports = function (Posts) {
|
||||
return topic && array.indexOf(topic) === index;
|
||||
});
|
||||
|
||||
categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], function (err, categories) {
|
||||
callback(err, { topics: topics, categories: categories });
|
||||
});
|
||||
});
|
||||
categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], next);
|
||||
},
|
||||
function (categoriesData, next) {
|
||||
next(null, { topics: topicsData, categories: categoriesData });
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function toObject(key, data) {
|
||||
|
||||
@@ -25,11 +25,7 @@ module.exports = function (Posts) {
|
||||
});
|
||||
groups.getGroupsData(groupTitles, next);
|
||||
},
|
||||
], function (err, groupsData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
function (groupsData, next) {
|
||||
groupsData.forEach(function (group) {
|
||||
if (group && group.userTitleEnabled) {
|
||||
groupsMap[group.name] = {
|
||||
@@ -56,6 +52,8 @@ module.exports = function (Posts) {
|
||||
});
|
||||
|
||||
async.map(userData, function (userData, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
isMemberOfGroup: function (next) {
|
||||
if (!userData.groupTitle || !groupsMap[userData.groupTitle]) {
|
||||
@@ -73,11 +71,9 @@ module.exports = function (Posts) {
|
||||
customProfileInfo: function (next) {
|
||||
plugins.fireHook('filter:posts.custom_profile_info', { profile: [], uid: userData.uid }, next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
if (results.isMemberOfGroup && userData.groupTitle && groupsMap[userData.groupTitle]) {
|
||||
userData.selectedGroup = groupsMap[userData.groupTitle];
|
||||
}
|
||||
@@ -85,9 +81,11 @@ module.exports = function (Posts) {
|
||||
userData.custom_profile_info = results.customProfileInfo.profile;
|
||||
|
||||
plugins.fireHook('filter:posts.modifyUserInfo', userData, next);
|
||||
});
|
||||
}, callback);
|
||||
});
|
||||
},
|
||||
], next);
|
||||
}, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Posts.isOwner = function (pid, uid, callback) {
|
||||
|
||||
@@ -65,14 +65,14 @@ module.exports = function (Posts) {
|
||||
if (!parseInt(uid, 10)) {
|
||||
return callback(null, { upvoted: false, downvoted: false });
|
||||
}
|
||||
|
||||
db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, function (err, hasVoted) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
callback(null, { upvoted: hasVoted[0], downvoted: hasVoted[1] });
|
||||
});
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, next);
|
||||
},
|
||||
function (hasVoted, next) {
|
||||
next(null, { upvoted: hasVoted[0], downvoted: hasVoted[1] });
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Posts.getVoteStatusByPostIDs = function (pids, uid, callback) {
|
||||
@@ -124,16 +124,21 @@ module.exports = function (Posts) {
|
||||
}
|
||||
|
||||
function toggleVote(type, pid, uid, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
unvote(pid, uid, type, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
vote(type, false, pid, uid, callback);
|
||||
next(err);
|
||||
});
|
||||
},
|
||||
function (next) {
|
||||
vote(type, false, pid, uid, next);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function unvote(pid, uid, command, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
owner: function (next) {
|
||||
Posts.getPostField(pid, 'uid', next);
|
||||
@@ -144,11 +149,9 @@ module.exports = function (Posts) {
|
||||
reputation: function (next) {
|
||||
user.getUserField(uid, 'reputation', next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
if (parseInt(uid, 10) === parseInt(results.owner, 10)) {
|
||||
return callback(new Error('self-vote'));
|
||||
}
|
||||
@@ -181,22 +184,25 @@ module.exports = function (Posts) {
|
||||
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) {
|
||||
uid = parseInt(uid, 10);
|
||||
|
||||
if (uid === 0) {
|
||||
if (!uid) {
|
||||
return callback(new Error('[[error:not-logged-in]]'));
|
||||
}
|
||||
|
||||
Posts.getPostFields(pid, ['pid', 'uid', 'tid'], function (err, postData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var postData;
|
||||
var newreputation;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Posts.getPostFields(pid, ['pid', 'uid', 'tid'], next);
|
||||
},
|
||||
function (_postData, next) {
|
||||
postData = _postData;
|
||||
var now = Date.now();
|
||||
|
||||
if (type === 'upvote' && !unvote) {
|
||||
@@ -211,17 +217,18 @@ module.exports = function (Posts) {
|
||||
db.sortedSetAdd('uid:' + uid + ':downvote', now, pid);
|
||||
}
|
||||
|
||||
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, function (err, newreputation) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, next);
|
||||
},
|
||||
function (_newreputation, next) {
|
||||
newreputation = _newreputation;
|
||||
if (parseInt(postData.uid, 10)) {
|
||||
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
|
||||
}
|
||||
|
||||
adjustPostVotes(postData, uid, type, unvote, function (err) {
|
||||
callback(err, {
|
||||
adjustPostVotes(postData, uid, type, unvote, next);
|
||||
},
|
||||
function (next) {
|
||||
next(null, {
|
||||
user: {
|
||||
reputation: newreputation,
|
||||
},
|
||||
@@ -229,14 +236,14 @@ module.exports = function (Posts) {
|
||||
upvote: type === 'upvote' && !unvote,
|
||||
downvote: type === 'downvote' && !unvote,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function adjustPostVotes(postData, uid, type, unvote, callback) {
|
||||
var notType = (type === 'upvote' ? 'downvote' : 'upvote');
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.series([
|
||||
function (next) {
|
||||
if (unvote) {
|
||||
@@ -249,10 +256,10 @@ module.exports = function (Posts) {
|
||||
db.setRemove('pid:' + postData.pid + ':' + notType, uid, next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
next(err);
|
||||
});
|
||||
},
|
||||
function (next) {
|
||||
async.parallel({
|
||||
upvotes: function (next) {
|
||||
db.setCount('pid:' + postData.pid + ':upvote', next);
|
||||
@@ -260,15 +267,14 @@ module.exports = function (Posts) {
|
||||
downvotes: function (next) {
|
||||
db.setCount('pid:' + postData.pid + ':downvote', next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
postData.upvotes = parseInt(results.upvotes, 10);
|
||||
postData.downvotes = parseInt(results.downvotes, 10);
|
||||
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 db = require('../database');
|
||||
|
||||
var widgets = {};
|
||||
var widgets = module.exports;
|
||||
|
||||
widgets.render = function (uid, area, req, res, callback) {
|
||||
if (!area.locations || !area.template) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
widgets.getAreas(['global', area.template], area.locations, function (err, data) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
widgets.getAreas(['global', area.template], area.locations, next);
|
||||
},
|
||||
function (data, next) {
|
||||
var widgetsByLocation = {};
|
||||
|
||||
async.map(area.locations, function (location, done) {
|
||||
@@ -37,15 +37,29 @@ widgets.render = function (uid, area, req, res, callback) {
|
||||
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, {
|
||||
uid: uid,
|
||||
area: area,
|
||||
data: widget.data,
|
||||
req: req,
|
||||
res: res,
|
||||
}, function (err, data) {
|
||||
if (err || data === null) {
|
||||
return next(err);
|
||||
}, next);
|
||||
},
|
||||
function (data, next) {
|
||||
if (!data) {
|
||||
return callback();
|
||||
}
|
||||
var html = data;
|
||||
if (typeof html !== 'string') {
|
||||
@@ -66,23 +80,19 @@ widgets.render = function (uid, area, req, res, callback) {
|
||||
} else {
|
||||
next(null, { html: html });
|
||||
}
|
||||
});
|
||||
}, function (err, result) {
|
||||
done(err, { location: location, widgets: result.filter(Boolean) });
|
||||
});
|
||||
}, callback);
|
||||
});
|
||||
};
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getObjectsFields(keys, locations, next);
|
||||
},
|
||||
function (data, next) {
|
||||
var returnData = {};
|
||||
|
||||
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) {
|
||||
db.getObjectField('widgets:' + template, location, function (err, result) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getObjectField('widgets:' + template, location, next);
|
||||
},
|
||||
function (result, next) {
|
||||
if (!result) {
|
||||
return callback(null, []);
|
||||
}
|
||||
@@ -119,8 +131,9 @@ widgets.getArea = function (template, location, callback) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
callback(null, result);
|
||||
});
|
||||
next(null, result);
|
||||
},
|
||||
], 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: 'sidebar' },
|
||||
];
|
||||
|
||||
var drafts;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
areas: function (next) {
|
||||
plugins.fireHook('filter:widgets.getAreas', defaultAreas, next);
|
||||
@@ -145,34 +160,32 @@ widgets.reset = function (callback) {
|
||||
drafts: function (next) {
|
||||
widgets.getArea('global', 'drafts', next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var drafts = results.drafts || [];
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
drafts = results.drafts || [];
|
||||
|
||||
async.each(results.areas, function (area, next) {
|
||||
widgets.getArea(area.template, area.location, function (err, areaData) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
widgets.getArea(area.template, area.location, next);
|
||||
},
|
||||
function (areaData, next) {
|
||||
drafts = drafts.concat(areaData);
|
||||
area.widgets = [];
|
||||
widgets.setArea(area, next);
|
||||
});
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
},
|
||||
], next);
|
||||
}, next);
|
||||
},
|
||||
function (next) {
|
||||
widgets.setArea({
|
||||
template: 'global',
|
||||
location: 'drafts',
|
||||
widgets: drafts,
|
||||
}, callback);
|
||||
});
|
||||
});
|
||||
}, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
module.exports = widgets;
|
||||
|
||||
@@ -138,6 +138,9 @@ function setupMockDefaults(callback) {
|
||||
function (next) {
|
||||
db.emptydb(next);
|
||||
},
|
||||
function (next) {
|
||||
db.createIndices(next);
|
||||
},
|
||||
function (next) {
|
||||
winston.info('test_database flushed');
|
||||
setupDefaultConfigs(meta, next);
|
||||
|
||||
Reference in New Issue
Block a user