Skip to content

Commit

Permalink
fix: #7102
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Dec 12, 2018
1 parent 85a07e9 commit d117df7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/database/redis/sorted.js
Expand Up @@ -29,6 +29,9 @@ module.exports = function (redisClient, module) {

function sortedSetRange(method, key, start, stop, withScores, callback) {
if (Array.isArray(key)) {
if (!key.length) {
return setImmediate(callback, null, []);
}
const batch = redisClient.batch();
key.forEach((key) => {
batch[method]([key, start, stop, 'WITHSCORES']);
Expand Down
30 changes: 20 additions & 10 deletions src/topics/sorted.js
Expand Up @@ -47,12 +47,11 @@ module.exports = function (Topics) {
async.waterfall([
function (next) {
if (params.term === 'alltime') {
var key = 'topics:' + params.sort;
if (params.cids) {
key = getCidSets(params.cids, params.sort);
getCidTids(params.cids, params.sort, next);
} else {
db.getSortedSetRevRange('topics:' + params.sort, 0, 199, next);
}

db.getSortedSetRevRange(key, 0, 199, next);
} else {
Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term, next);
}
Expand All @@ -70,17 +69,28 @@ module.exports = function (Topics) {
], callback);
}

function getCidSets(cids, sort) {
const keys = [];
function getCidTids(cids, sort, callback) {
const sets = [];
const pinnedSets = [];
cids.forEach(function (cid) {
if (sort === 'recent') {
keys.push('cid:' + cid + ':tids:lastposttime');
sets.push('cid:' + cid + ':tids:lastposttime');
return;
}
keys.push('cid:' + cid + ':tids' + (sort ? ':' + sort : ''));
keys.push('cid:' + cid + ':tids:pinned');
sets.push('cid:' + cid + ':tids' + (sort ? ':' + sort : ''));
pinnedSets.push('cid:' + cid + ':tids:pinned');
});
return keys;
async.waterfall([
function (next) {
async.parallel({
tids: async.apply(db.getSortedSetRevRange, sets, 0, 199),
pinnedTids: async.apply(db.getSortedSetRevRange, pinnedSets, 0, -1),
}, next);
},
function (results, next) {
next(null, results.pinnedTids.concat(results.tids));
},
], callback);
}

function sortTids(tids, params, callback) {
Expand Down

0 comments on commit d117df7

Please sign in to comment.