Skip to content

Node.js based forum software built for the modern web

refactor: categories.sortTidsBySet to not take cid, retrieve from tids themselves

Julian Lambfc7daf25537
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);