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 type: number
viewcount: viewcount:
type: number type: number
postercount:
type: number
teaserPid: teaserPid:
oneOf: oneOf:
- type: number - type: number

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

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

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

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

@ -174,6 +174,8 @@ module.exports = function (Topics) {
} }
await Topics.increasePostCount(tid); await Topics.increasePostCount(tid);
await db.sortedSetIncrBy('tid:' + tid + ':posters', 1, postData.uid); 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); await Topics.updateTeaser(tid);
}; };
@ -184,6 +186,9 @@ module.exports = function (Topics) {
], postData.pid); ], postData.pid);
await Topics.decreasePostCount(tid); await Topics.decreasePostCount(tid);
await db.sortedSetIncrBy('tid:' + tid + ':posters', -1, postData.uid); 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); 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