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
This commit is contained in:
Julian Lam
2025-03-19 22:18:47 -04:00
parent 53dc79a1bd
commit bfc7daf255
2 changed files with 23 additions and 12 deletions

View File

@@ -255,7 +255,11 @@ module.exports = function (Categories) {
notifications.push(notification, followers);
};
Categories.sortTidsBySet = async (tids, cid, sort) => {
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`,
@@ -265,8 +269,15 @@ module.exports = function (Categories) {
most_views: `cid:${cid}:tids:views`,
};
const orderBy = sortToSet[sort];
const scores = await db.sortedSetScores(orderBy, tids);
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 sorted = tids
.map((tid, idx) => [tid, scores[idx]])
.sort(([, a], [, b]) => b - a)

View File

@@ -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);