From fbedd7290aade00c4254eb1a0bcba66edcfd4f64 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 19 Mar 2025 22:18:47 -0400 Subject: [PATCH] refactor: categories.sortTidsBySet to not take cid, retrieve from tids themselves re: ##13255, this fixes the issue with topics outside of cid -1 in /world being sorted incorrectly --- src/categories/topics.js | 33 ++++++++++++++++++--------- src/controllers/activitypub/topics.js | 2 +- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/categories/topics.js b/src/categories/topics.js index ebb3136360..5169998910 100644 --- a/src/categories/topics.js +++ b/src/categories/topics.js @@ -255,18 +255,29 @@ module.exports = function (Categories) { notifications.push(notification, followers); }; - Categories.sortTidsBySet = async (tids, cid, sort) => { - sort = sort || meta.config.categoryTopicSort || 'recently_replied'; - const sortToSet = { - recently_replied: `cid:${cid}:tids`, - recently_created: `cid:${cid}:tids:create`, - most_posts: `cid:${cid}:tids:posts`, - most_votes: `cid:${cid}:tids:votes`, - most_views: `cid:${cid}:tids:views`, - }; + Categories.sortTidsBySet = async (tids, sort) => { + let cids = await topics.getTopicsFields(tids, ['cid']); + cids = cids.map(({ cid }) => cid); + + function getSet(cid, sort) { + sort = sort || meta.config.categoryTopicSort || 'recently_replied'; + const sortToSet = { + recently_replied: `cid:${cid}:tids`, + recently_created: `cid:${cid}:tids:create`, + most_posts: `cid:${cid}:tids:posts`, + most_votes: `cid:${cid}:tids:votes`, + most_views: `cid:${cid}:tids:views`, + }; + + return sortToSet[sort]; + } + + const scores = await Promise.all(tids.map(async (tid, idx) => { + const cid = cids[idx]; + const orderBy = getSet(cid, sort); + return await db.sortedSetScore(orderBy, tid); + })); - const orderBy = sortToSet[sort]; - const scores = await db.sortedSetScores(orderBy, tids); const sorted = tids .map((tid, idx) => [tid, scores[idx]]) .sort(([, a], [, b]) => b - a) diff --git a/src/controllers/activitypub/topics.js b/src/controllers/activitypub/topics.js index afcc7eb942..b14abcc512 100644 --- a/src/controllers/activitypub/topics.js +++ b/src/controllers/activitypub/topics.js @@ -52,7 +52,7 @@ controller.list = async function (req, res) { delete data.children; let tids = await categories.getTopicIds(cidQuery); - tids = await categories.sortTidsBySet(tids, -1, sort); // sorting not handled if cid is -1 + tids = await categories.sortTidsBySet(tids, sort); // sorting not handled if cid is -1 data.topicCount = tids.length; data.topics = await topics.getTopicsByTids(tids, { uid: req.uid }); topics.calculateTopicIndices(data.topics, start);