posts/recent.js refactor

v1.18.x
barisusakli 10 years ago
parent ef35b83806
commit c18de5cdd2

@ -1,6 +1,7 @@
'use strict'; 'use strict';
var db = require('../database'), var async = require('async'),
db = require('../database'),
privileges = require('../privileges'); privileges = require('../privileges');
@ -19,44 +20,35 @@ module.exports = function(Posts) {
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1; var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', Date.now() - since, function(err, pids) { async.waterfall([
if (err) { function(next) {
return callback(err); 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);
} }
], callback);
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);
});
});
}; };
Posts.getRecentPosterUids = function(start, end, callback) { Posts.getRecentPosterUids = function(start, end, callback) {
db.getSortedSetRevRange('posts:pid', start, end, function(err, pids) { async.waterfall([
if (err) { function(next) {
return callback(err); db.getSortedSetRevRange('posts:pid', start, end, next);
} },
function(pids, next) {
Posts.getPostsFields(pids, ['uid'], function(err, postData) { Posts.getPostsFields(pids, ['uid'], next);
if (err) { },
return callback(err); function(postData, next) {
}
postData = postData.map(function(post) { postData = postData.map(function(post) {
return post && post.uid; return post && post.uid;
}).filter(function(value, index, array) { }).filter(function(value, index, array) {
return value && array.indexOf(value) === index; return value && array.indexOf(value) === index;
}); });
next(null, postData);
callback(null, postData); }
}); ], callback);
}); };
};
}; };

@ -24,13 +24,9 @@ module.exports = function(Posts) {
return callback(null, []); return callback(null, []);
} }
var keys = pids.map(function(pid) {
return 'post:' + pid;
});
var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'].concat(options.extraFields); 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) { if (err) {
return callback(err); return callback(err);
} }

@ -72,7 +72,7 @@ module.exports = function(privileges) {
}; };
privileges.posts.filter = function(privilege, pids, uid, callback) { privileges.posts.filter = function(privilege, pids, uid, callback) {
if (!pids.length) { if (!Array.isArray(pids) || !pids.length) {
return callback(null, []); return callback(null, []);
} }
posts.getCidsByPids(pids, function(err, cids) { posts.getCidsByPids(pids, function(err, cids) {

Loading…
Cancel
Save