feat: move postercount to topic hash

v1.18.x
Barış Soner Uşaklı 4 years ago
parent 203db47b30
commit 0db0231cff

@ -26,6 +26,8 @@ TopicObject:
type: number
viewcount:
type: number
postercount:
type: number
teaserPid:
oneOf:
- type: number

@ -36,6 +36,8 @@ get:
type: number
lastposttime:
type: number
postercount:
type: number
postcount:
type: number
viewcount:

@ -53,7 +53,7 @@ get:
type: number
viewcount:
type: number
posterCount:
postercount:
type: number
description: The number of unique users who made a post in this topic
mainPid:

@ -10,7 +10,7 @@ const plugins = require('../plugins');
const intFields = [
'tid', 'cid', 'uid', 'mainPid', 'postcount',
'viewcount', 'deleted', 'locked', 'pinned',
'viewcount', 'postercount', 'deleted', 'locked', 'pinned',
'timestamp', 'upvotes', 'downvotes', 'lastposttime',
'deleterUid',
];

@ -146,7 +146,6 @@ Topics.getTopicWithPosts = async function (topicData, set, uid, start, stop, rev
deleter,
merger,
related,
posterCount,
] = await Promise.all([
getMainPostAndReplies(topicData, set, uid, start, stop, reverse),
categories.getCategoryData(topicData.cid),
@ -158,7 +157,6 @@ Topics.getTopicWithPosts = async function (topicData, set, uid, start, stop, rev
getDeleter(topicData),
getMerger(topicData),
getRelated(topicData, uid),
db.sortedSetCard('tid:' + topicData.tid + ':posters'),
]);
topicData.posts = posts;
@ -181,7 +179,6 @@ Topics.getTopicWithPosts = async function (topicData, set, uid, start, stop, rev
topicData.mergedTimestampISO = utils.toISOString(topicData.mergedTimestamp);
}
topicData.related = related || [];
topicData.posterCount = posterCount;
topicData.unreplied = topicData.postcount === 1;
topicData.icons = [];

@ -174,6 +174,8 @@ module.exports = function (Topics) {
}
await Topics.increasePostCount(tid);
await db.sortedSetIncrBy('tid:' + tid + ':posters', 1, postData.uid);
const posterCount = await db.sortedSetCard('tid:' + tid + ':posters');
await Topics.setTopicField(tid, 'postercount', posterCount);
await Topics.updateTeaser(tid);
};
@ -184,6 +186,9 @@ module.exports = function (Topics) {
], postData.pid);
await Topics.decreasePostCount(tid);
await db.sortedSetIncrBy('tid:' + tid + ':posters', -1, postData.uid);
await db.sortedSetsRemoveRangeByScore(['tid:' + tid + ':posters'], '-inf', 0);
const posterCount = await db.sortedSetCard('tid:' + tid + ':posters');
await Topics.setTopicField(tid, 'postercount', posterCount);
await Topics.updateTeaser(tid);
};

@ -0,0 +1,29 @@
'use strict';
const db = require('../../database');
const batch = require('../../batch');
module.exports = {
name: 'Store poster count in topic hash',
timestamp: Date.UTC(2020, 9, 24),
method: async function () {
const progress = this.progress;
await batch.processSortedSet('topics:tid', async function (tids) {
progress.incr(tids.length);
const keys = tids.map(tid => 'tid:' + tid + ':posters');
await db.sortedSetsRemoveRangeByScore(keys, '-inf', 0);
const counts = await db.sortedSetsCard(keys);
for (let i = 0; i < tids.length; i++) {
if (counts[i] > 0) {
// eslint-disable-next-line no-await-in-loop
await db.setObjectField('topic:' + tids[i], 'postercount', counts[i]);
}
}
}, {
progress: progress,
batchSize: 500,
});
},
};
Loading…
Cancel
Save