mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
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:
@@ -255,18 +255,29 @@ module.exports = function (Categories) {
|
|||||||
notifications.push(notification, followers);
|
notifications.push(notification, followers);
|
||||||
};
|
};
|
||||||
|
|
||||||
Categories.sortTidsBySet = async (tids, cid, sort) => {
|
Categories.sortTidsBySet = async (tids, sort) => {
|
||||||
sort = sort || meta.config.categoryTopicSort || 'recently_replied';
|
let cids = await topics.getTopicsFields(tids, ['cid']);
|
||||||
const sortToSet = {
|
cids = cids.map(({ cid }) => cid);
|
||||||
recently_replied: `cid:${cid}:tids`,
|
|
||||||
recently_created: `cid:${cid}:tids:create`,
|
function getSet(cid, sort) {
|
||||||
most_posts: `cid:${cid}:tids:posts`,
|
sort = sort || meta.config.categoryTopicSort || 'recently_replied';
|
||||||
most_votes: `cid:${cid}:tids:votes`,
|
const sortToSet = {
|
||||||
most_views: `cid:${cid}:tids:views`,
|
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
|
const sorted = tids
|
||||||
.map((tid, idx) => [tid, scores[idx]])
|
.map((tid, idx) => [tid, scores[idx]])
|
||||||
.sort(([, a], [, b]) => b - a)
|
.sort(([, a], [, b]) => b - a)
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ controller.list = async function (req, res) {
|
|||||||
delete data.children;
|
delete data.children;
|
||||||
|
|
||||||
let tids = await categories.getTopicIds(cidQuery);
|
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.topicCount = tids.length;
|
||||||
data.topics = await topics.getTopicsByTids(tids, { uid: req.uid });
|
data.topics = await topics.getTopicsByTids(tids, { uid: req.uid });
|
||||||
topics.calculateTopicIndices(data.topics, start);
|
topics.calculateTopicIndices(data.topics, start);
|
||||||
|
|||||||
Reference in New Issue
Block a user