filterPidsByCid

v1.18.x
barisusakli 9 years ago
parent 4576e7c7dd
commit 2e6fc4d045

@ -73,9 +73,6 @@ function getFlagData(req, callback) {
var stop = start + itemsPerPage - 1; var stop = start + itemsPerPage - 1;
var sets = [sortBy === 'count' ? 'posts:flags:count' : 'posts:flagged']; var sets = [sortBy === 'count' ? 'posts:flags:count' : 'posts:flagged'];
if (cid) {
sets.push('cid:' + cid + ':pids');
}
async.waterfall([ async.waterfall([
function(next) { function(next) {
@ -90,7 +87,7 @@ function getFlagData(req, callback) {
sets.push('uid:' + uid + ':flag:pids'); sets.push('uid:' + uid + ':flag:pids');
} }
posts.getFlags(sets, req.uid, start, stop, next); posts.getFlags(sets, cid, req.uid, start, stop, next);
} }
], callback); ], callback);
} }

@ -2,6 +2,8 @@
'use strict'; 'use strict';
var async = require('async'); var async = require('async');
var db = require('../database');
var topics = require('../topics'); var topics = require('../topics');
module.exports = function(Posts) { module.exports = function(Posts) {
@ -49,4 +51,19 @@ module.exports = function(Posts) {
}); });
}); });
}; };
Posts.filterPidsByCid = function(pids, cid, callback) {
if (!cid) {
return callback(null, pids);
}
db.isSortedSetMembers('cid:' + cid + ':pids', pids, function(err, isMembers) {
if (err) {
return callback(err);
}
pids = pids.filter(function(pid, index) {
return pid && isMembers[index];
});
callback(null, pids);
});
};
}; };

@ -153,31 +153,31 @@ module.exports = function(Posts) {
}); });
}; };
Posts.getFlags = function(set, uid, start, stop, callback) { Posts.getFlags = function(set, cid, uid, start, stop, callback) {
set = set.length > 1 ? set : set[0]; async.waterfall([
async.parallel({ function (next) {
count: function(next) {
if (Array.isArray(set)) { if (Array.isArray(set)) {
db.sortedSetIntersectCard(set, next); db.getSortedSetRevIntersect({sets: set, start: start, stop: -1, aggregate: 'MAX'}, next);
} else { } else {
db.sortedSetCard(set, next); db.getSortedSetRevRange(set, start, -1, next);
} }
}, },
posts: function(next) { function (pids, next) {
async.waterfall([ if (cid) {
function (next) { Posts.filterPidsByCid(pids, cid, next);
if (Array.isArray(set)) { } else {
db.getSortedSetRevIntersect({sets: set, start: start, stop: stop, aggregate: 'MAX'}, next); process.nextTick(next, null, pids);
} else { }
db.getSortedSetRevRange(set, start, stop, next); },
} function (pids, next) {
}, getFlaggedPostsWithReasons(pids, uid, next);
function (pids, next) { },
getFlaggedPostsWithReasons(pids, uid, next); function (posts, next) {
} var count = posts.length;
], next); var end = stop - start + 1;
next(null, {posts: posts.slice(0, stop === -1 ? undefined : end), count: count});
} }
}, callback); ], callback);
}; };
function getFlaggedPostsWithReasons(pids, uid, callback) { function getFlaggedPostsWithReasons(pids, uid, callback) {

Loading…
Cancel
Save