diff --git a/src/topics/recent.js b/src/topics/recent.js index 359dd5a6cd..b331ed4535 100644 --- a/src/topics/recent.js +++ b/src/topics/recent.js @@ -32,12 +32,15 @@ module.exports = function (Topics) { return { topics: topics, nextStart: options.stop + 1 }; }; - Topics.getLatestTidsFromSet = async function (set, start, stop, term) { - let since = terms.day; - if (terms[term]) { - since = terms[term]; + Topics.getSinceFromTerm = function (term) { + if (terms.hasOwnProperty(term)) { + return terms[term]; } + return terms.day; + }; + Topics.getLatestTidsFromSet = async function (set, start, stop, term) { + const since = Topics.getSinceFromTerm(term); const count = parseInt(stop, 10) === -1 ? stop : stop - start + 1; return await db.getSortedSetRevRangeByScore(set, start, count, '+inf', Date.now() - since); }; diff --git a/src/topics/sorted.js b/src/topics/sorted.js index 8348f4ceaa..92df164828 100644 --- a/src/topics/sorted.js +++ b/src/topics/sorted.js @@ -44,7 +44,12 @@ module.exports = function (Topics) { } let tids = []; if (params.term !== 'alltime') { - tids = await Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term); + if (params.sort === 'posts') { + tids = await getTidsWithMostPostsInTerm(params.term); + } else { + tids = await Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term); + } + if (params.filter === 'watched') { tids = await Topics.filterWatchedTids(tids, params.uid); } @@ -63,6 +68,18 @@ module.exports = function (Topics) { return tids; } + async function getTidsWithMostPostsInTerm(term) { + const pids = await db.getSortedSetRevRangeByScore('posts:pid', 0, -1, '+inf', Date.now() - Topics.getSinceFromTerm(term)); + const postObjs = await db.getObjectsFields(pids.map(pid => `post:${pid}`), ['tid']); + const tidToCount = {}; + postObjs.forEach((post) => { + tidToCount[post.tid] = tidToCount[post.tid] || 0; + tidToCount[post.tid] += 1; + }); + + return _.uniq(postObjs.map(post => post.tid)).sort((t1, t2) => tidToCount[t2] - tidToCount[t1]); + } + async function getWatchedTopics(params) { const sortSet = ['recent', 'old'].includes(params.sort) ? 'topics:recent' : `topics:${params.sort}`; const method = params.sort === 'old' ? 'getSortedSetIntersect' : 'getSortedSetRevIntersect'; @@ -123,6 +140,11 @@ module.exports = function (Topics) { if (params.term === 'alltime' && !params.cids && !params.tags.length && params.filter !== 'watched' && !params.floatPinned) { return tids; } + + if (params.sort === 'posts' && params.term !== 'alltime') { + return tids; + } + const topicData = await Topics.getTopicsFields(tids, [ 'tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount', 'pinned', ]);