Skip to content

Node.js based forum software built for the modern web

fix: #12444 add uncategorized topics counted stats to separate sorted set

Julian Lam8dcdf8ef3b7b
diff --git a/src/topics/create.js b/src/topics/create.js
index 79d9d7675c..581017eff5 100644
--- a/src/topics/create.js
+++ b/src/topics/create.js
@@ -50,6 +50,12 @@ module.exports = function (Topics) {
`cid:${topicData.cid}:tids:create`,
`cid:${topicData.cid}:uid:${topicData.uid}:tids`,
];
+ const countedSortedSetKeys = [
+ ...['views', 'posts', 'votes'].map(prop => `${topicData.cid === -1 ? 'topicsRemote' : 'topics'}:${prop}`),
+ `cid:${topicData.cid}:tids:votes`,
+ `cid:${topicData.cid}:tids:posts`,
+ `cid:${topicData.cid}:tids:views`,
+ ];
const scheduled = timestamp > Date.now();
if (scheduled) {
@@ -58,12 +64,7 @@ module.exports = function (Topics) {
await Promise.all([
db.sortedSetsAdd(timestampedSortedSetKeys, timestamp, topicData.tid),
- db.sortedSetsAdd([
- 'topics:views', 'topics:posts', 'topics:votes',
- `cid:${topicData.cid}:tids:votes`,
- `cid:${topicData.cid}:tids:posts`,
- `cid:${topicData.cid}:tids:views`,
- ], 0, topicData.tid),
+ db.sortedSetsAdd(countedSortedSetKeys, 0, topicData.tid),
user.addTopicIdToUser(topicData.uid, topicData.tid, timestamp),
db.incrObjectField(`category:${topicData.cid}`, 'topic_count'),
db.incrObjectField('global', 'topicCount'),
diff --git a/src/upgrades/4.0.0/fix_topic_zsets_for_uncategorized.js b/src/upgrades/4.0.0/fix_topic_zsets_for_uncategorized.js
new file mode 100644
index 0000000000..4103cd81e1
--- /dev/null
+++ b/src/upgrades/4.0.0/fix_topic_zsets_for_uncategorized.js
@@ -0,0 +1,36 @@
+// REMOVE THIS PRIOR TO 4.0 ALPHA
+
+'use strict';
+
+const db = require('../../database');
+
+module.exports = {
+ name: 'Fix topic sorted sets for uncategorized topics',
+ timestamp: Date.UTC(2024, 2, 26),
+ method: async function () {
+ const props = ['views', 'posts', 'votes'];
+ const { progress } = this;
+ const tids = await db.getSortedSetMembers('cid:-1:tids');
+ progress.total = tids.length;
+
+ const remove = [];
+ const add = [];
+ await Promise.all(props.map(async (prop) => {
+ const set = `topics:${prop}`;
+ const newSet = `topicsRemote:${prop}`;
+
+ const scores = await db.sortedSetScores(set, tids);
+ scores.forEach((score, idx) => {
+ if (score !== null) {
+ remove.push([set, tids[idx]]);
+ add.push([newSet, score, tids[idx]]);
+ }
+ });
+ }));
+
+ await Promise.all([
+ db.sortedSetRemoveBulk(remove),
+ db.sortedSetAddBulk(add),
+ ]);
+ },
+};
diff --git a/src/upgrades/4.0.0/searchable_remote_users.js b/src/upgrades/4.0.0/searchable_remote_users.js
index 65b9fceed0..7cc9abbfb8 100644
--- a/src/upgrades/4.0.0/searchable_remote_users.js
+++ b/src/upgrades/4.0.0/searchable_remote_users.js
@@ -1,3 +1,5 @@
+// REMOVE THIS PRIOR TO 4.0 ALPHA
+
'use strict';
const db = require('../../database');