Skip to content

Commit

Permalink
fix: #12444 add uncategorized topics counted stats to separate sorted…
Browse files Browse the repository at this point in the history
… set
  • Loading branch information
julianlam committed Mar 26, 2024
1 parent c2890a3 commit 8dcdf8e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/topics/create.js
Expand Up @@ -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) {
Expand All @@ -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'),
Expand Down
36 changes: 36 additions & 0 deletions 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),
]);
},
};
2 changes: 2 additions & 0 deletions 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');
Expand Down

0 comments on commit 8dcdf8e

Please sign in to comment.