Skip to content

Node.js based forum software built for the modern web

fix: #7102

Barış Soner Uşaklıd117df77f79e
diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js
index 471d896852..43b797e2f9 100644
--- a/src/database/redis/sorted.js
+++ b/src/database/redis/sorted.js
@@ -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']);
diff --git a/src/topics/sorted.js b/src/topics/sorted.js
index 3ace0585c2..ae9a255da8 100644
--- a/src/topics/sorted.js
+++ b/src/topics/sorted.js
@@ -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);
}
@@ -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) {