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 sets = [sortBy === 'count' ? 'posts:flags:count' : 'posts:flagged'];
if (cid) {
sets.push('cid:' + cid + ':pids');
}
async.waterfall([
function(next) {
@ -90,7 +87,7 @@ function getFlagData(req, callback) {
sets.push('uid:' + uid + ':flag:pids');
}
posts.getFlags(sets, req.uid, start, stop, next);
posts.getFlags(sets, cid, req.uid, start, stop, next);
}
], callback);
}

@ -2,6 +2,8 @@
'use strict';
var async = require('async');
var db = require('../database');
var topics = require('../topics');
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) {
set = set.length > 1 ? set : set[0];
async.parallel({
count: function(next) {
Posts.getFlags = function(set, cid, uid, start, stop, callback) {
async.waterfall([
function (next) {
if (Array.isArray(set)) {
db.sortedSetIntersectCard(set, next);
db.getSortedSetRevIntersect({sets: set, start: start, stop: -1, aggregate: 'MAX'}, next);
} else {
db.sortedSetCard(set, next);
db.getSortedSetRevRange(set, start, -1, next);
}
},
posts: function(next) {
async.waterfall([
function (next) {
if (Array.isArray(set)) {
db.getSortedSetRevIntersect({sets: set, start: start, stop: stop, aggregate: 'MAX'}, next);
} else {
db.getSortedSetRevRange(set, start, stop, next);
}
},
function (pids, next) {
getFlaggedPostsWithReasons(pids, uid, next);
}
], next);
function (pids, next) {
if (cid) {
Posts.filterPidsByCid(pids, cid, next);
} else {
process.nextTick(next, null, pids);
}
},
function (pids, next) {
getFlaggedPostsWithReasons(pids, uid, next);
},
function (posts, next) {
var count = posts.length;
var end = stop - start + 1;
next(null, {posts: posts.slice(0, stop === -1 ? undefined : end), count: count});
}
}, callback);
], callback);
};
function getFlaggedPostsWithReasons(pids, uid, callback) {

Loading…
Cancel
Save