From 2e6fc4d04514988b7c4f209040619bb1cf47faff Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 16 Sep 2016 18:39:45 +0300 Subject: [PATCH] filterPidsByCid --- src/controllers/admin/flags.js | 5 +---- src/posts/category.js | 17 +++++++++++++++ src/posts/flags.js | 40 +++++++++++++++++----------------- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/controllers/admin/flags.js b/src/controllers/admin/flags.js index 4b44b1d951..75a82d1e55 100644 --- a/src/controllers/admin/flags.js +++ b/src/controllers/admin/flags.js @@ -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); } diff --git a/src/posts/category.js b/src/posts/category.js index 2d8fd97cc0..0f9ee7fd67 100644 --- a/src/posts/category.js +++ b/src/posts/category.js @@ -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); + }); + }; }; \ No newline at end of file diff --git a/src/posts/flags.js b/src/posts/flags.js index 0d4a782ee8..8acc453399 100644 --- a/src/posts/flags.js +++ b/src/posts/flags.js @@ -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) {