From f764a3ea5c018db9686ef6c8a77804db793ba076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 25 Apr 2024 11:15:23 -0400 Subject: [PATCH] use `cid::pids` instead of posts:pid --- src/topics/sorted.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/topics/sorted.js b/src/topics/sorted.js index c2f916718e..98292f0ddb 100644 --- a/src/topics/sorted.js +++ b/src/topics/sorted.js @@ -45,7 +45,7 @@ module.exports = function (Topics) { let tids = []; if (params.term !== 'alltime') { if (params.sort === 'posts') { - tids = await getTidsWithMostPostsInTerm(params.term); + tids = await getTidsWithMostPostsInTerm(params.cids, params.uid, params.term); } else { tids = await Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term); } @@ -84,8 +84,20 @@ module.exports = function (Topics) { return 'topics:recent'; } - async function getTidsWithMostPostsInTerm(term) { - const pids = await db.getSortedSetRevRangeByScore('posts:pid', 0, 1000, '+inf', Date.now() - Topics.getSinceFromTerm(term)); + async function getTidsWithMostPostsInTerm(cids, uid, term) { + if (Array.isArray(cids)) { + cids = await privileges.categories.filterCids('topics:read', cids, uid); + } else { + cids = await categories.getCidsByPrivilege('categories:cid', uid, 'topics:read'); + } + + const pids = await db.getSortedSetRevRangeByScore( + cids.map(cid => `cid:${cid}:pids`), + 0, + 1000, + '+inf', + Date.now() - Topics.getSinceFromTerm(term) + ); const postObjs = await db.getObjectsFields(pids.map(pid => `post:${pid}`), ['tid']); const tidToCount = {}; postObjs.forEach((post) => { @@ -93,7 +105,8 @@ module.exports = function (Topics) { tidToCount[post.tid] += 1; }); - return _.uniq(postObjs.map(post => String(post.tid))).sort((t1, t2) => tidToCount[t2] - tidToCount[t1]); + return _.uniq(postObjs.map(post => String(post.tid))) + .sort((t1, t2) => tidToCount[t2] - tidToCount[t1]); } async function getWatchedTopics(params) {