diff --git a/src/posts/recent.js b/src/posts/recent.js index f3a0508a79..4a970a6435 100644 --- a/src/posts/recent.js +++ b/src/posts/recent.js @@ -1,6 +1,7 @@ 'use strict'; -var db = require('../database'), +var async = require('async'), + db = require('../database'), privileges = require('../privileges'); @@ -19,44 +20,35 @@ module.exports = function(Posts) { var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1; - db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', Date.now() - since, function(err, pids) { - if (err) { - return callback(err); + async.waterfall([ + function(next) { + db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', Date.now() - since, next); + }, + function(pids, next) { + privileges.posts.filter('read', pids, uid, next); + }, + function(pids, next) { + Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, next); } - - if (!Array.isArray(pids) || !pids.length) { - return callback(null, []); - } - - privileges.posts.filter('read', pids, uid, function(err, pids) { - if (err) { - return callback(err); - } - Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback); - }); - }); + ], callback); }; Posts.getRecentPosterUids = function(start, end, callback) { - db.getSortedSetRevRange('posts:pid', start, end, function(err, pids) { - if (err) { - return callback(err); - } - - Posts.getPostsFields(pids, ['uid'], function(err, postData) { - if (err) { - return callback(err); - } - + async.waterfall([ + function(next) { + db.getSortedSetRevRange('posts:pid', start, end, next); + }, + function(pids, next) { + Posts.getPostsFields(pids, ['uid'], next); + }, + function(postData, next) { postData = postData.map(function(post) { return post && post.uid; }).filter(function(value, index, array) { return value && array.indexOf(value) === index; }); - - callback(null, postData); - }); - }); - }; - + next(null, postData); + } + ], callback); + }; }; diff --git a/src/posts/summary.js b/src/posts/summary.js index 3e1fae403b..9b1d880176 100644 --- a/src/posts/summary.js +++ b/src/posts/summary.js @@ -24,13 +24,9 @@ module.exports = function(Posts) { return callback(null, []); } - var keys = pids.map(function(pid) { - return 'post:' + pid; - }); - var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'].concat(options.extraFields); - db.getObjectsFields(keys, fields, function(err, posts) { + Posts.getPostsFields(pids, fields, function(err, posts) { if (err) { return callback(err); } diff --git a/src/privileges/posts.js b/src/privileges/posts.js index e5035ceb4b..ec61bf826c 100644 --- a/src/privileges/posts.js +++ b/src/privileges/posts.js @@ -72,7 +72,7 @@ module.exports = function(privileges) { }; privileges.posts.filter = function(privilege, pids, uid, callback) { - if (!pids.length) { + if (!Array.isArray(pids) || !pids.length) { return callback(null, []); } posts.getCidsByPids(pids, function(err, cids) {