|
|
|
@ -54,31 +54,36 @@ module.exports = function (Posts) {
|
|
|
|
|
|
|
|
|
|
Posts.filterPidsByCid = function (pids, cid, callback) {
|
|
|
|
|
if (!cid) {
|
|
|
|
|
return callback(null, pids);
|
|
|
|
|
return setImmediate(callback, null, pids);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(cid) || cid.length === 1) {
|
|
|
|
|
// Single cid
|
|
|
|
|
db.isSortedSetMembers('cid:' + parseInt(cid, 10) + ':pids', pids, function (err, isMembers) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
return filterPidsBySingleCid(pids, cid, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
async.map(cid, function (cid, next) {
|
|
|
|
|
Posts.filterPidsByCid(pids, cid, next);
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function (pidsArr, next) {
|
|
|
|
|
next(null, _.union.apply(_, pidsArr));
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function filterPidsBySingleCid(pids, cid, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.isSortedSetMembers('cid:' + parseInt(cid, 10) + ':pids', pids, next);
|
|
|
|
|
},
|
|
|
|
|
function (isMembers, next) {
|
|
|
|
|
pids = pids.filter(function (pid, index) {
|
|
|
|
|
return pid && isMembers[index];
|
|
|
|
|
});
|
|
|
|
|
callback(null, pids);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// Multiple cids
|
|
|
|
|
async.map(cid, function (cid, next) {
|
|
|
|
|
Posts.filterPidsByCid(pids, cid, next);
|
|
|
|
|
}, function (err, pidsArr) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, _.union.apply(_, pidsArr));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
next(null, pids);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|