diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index 3fee97fa2f..cd90546e39 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -39,7 +39,11 @@ module.exports = function (db, module) { if (!key.length) { return setImmediate(callback, null, []); } - key = { $in: key }; + if (key.length > 1) { + key = { $in: key }; + } else { + key = key[0]; + } } var query = { _key: key }; diff --git a/src/topics/sorted.js b/src/topics/sorted.js index b5328d0c6d..3ace0585c2 100644 --- a/src/topics/sorted.js +++ b/src/topics/sorted.js @@ -49,16 +49,7 @@ module.exports = function (Topics) { if (params.term === 'alltime') { var key = 'topics:' + params.sort; if (params.cids) { - key = params.cids.map(function (cid) { - if (params.sort === 'recent') { - return 'cid:' + cid + ':tids:lastposttime'; - } else if (params.sort === 'votes') { - return 'cid:' + cid + ':tids:votes'; - } else if (params.sort === 'posts') { - return 'cid:' + cid + ':tids:posts'; - } - return 'cid:' + cid + ':tids'; - }); + key = getCidSets(params.cids, params.sort); } db.getSortedSetRevRange(key, 0, 199, next); @@ -67,7 +58,7 @@ module.exports = function (Topics) { } }, function (tids, next) { - if (params.term !== 'alltime') { + if (params.term !== 'alltime' || (params.cids && params.sort !== 'recent')) { sortTids(tids, params, next); } else { next(null, tids); @@ -79,6 +70,19 @@ module.exports = function (Topics) { ], callback); } + function getCidSets(cids, sort) { + const keys = []; + cids.forEach(function (cid) { + if (sort === 'recent') { + keys.push('cid:' + cid + ':tids:lastposttime'); + return; + } + keys.push('cid:' + cid + ':tids' + (sort ? ':' + sort : '')); + keys.push('cid:' + cid + ':tids:pinned'); + }); + return keys; + } + function sortTids(tids, params, callback) { async.waterfall([ function (next) {