Node.js based forum software built for the modern web
feat: move postercount to topic hash
Barış Soner Uşaklı0db0231cff09
diff --git a/public/openapi/components/schemas/TopicObject.yaml b/public/openapi/components/schemas/TopicObject.yamlindex 9481d81305..69662e0080 100644--- a/public/openapi/components/schemas/TopicObject.yaml+++ b/public/openapi/components/schemas/TopicObject.yaml@@ -26,6 +26,8 @@ TopicObject:type: numberviewcount:type: number+ postercount:+ type: numberteaserPid:oneOf:- type: numberdiff --git a/public/openapi/read/topic/tid/id.yaml b/public/openapi/read/topic/tid/id.yamlindex 4c83350b37..cdc638a1f5 100644--- a/public/openapi/read/topic/tid/id.yaml+++ b/public/openapi/read/topic/tid/id.yaml@@ -36,6 +36,8 @@ get:type: numberlastposttime:type: number+ postercount:+ type: numberpostcount:type: numberviewcount:diff --git a/public/openapi/read/topic/topic_id/slug/post_index.yaml b/public/openapi/read/topic/topic_id/slug/post_index.yamlindex 6d520ba1c1..d9374eff97 100644--- a/public/openapi/read/topic/topic_id/slug/post_index.yaml+++ b/public/openapi/read/topic/topic_id/slug/post_index.yaml@@ -53,7 +53,7 @@ get:type: numberviewcount:type: number- posterCount:+ postercount:type: numberdescription: The number of unique users who made a post in this topicmainPid:diff --git a/src/topics/data.js b/src/topics/data.jsindex 6b73163b4c..06476755ef 100644--- a/src/topics/data.js+++ b/src/topics/data.js@@ -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',];diff --git a/src/topics/index.js b/src/topics/index.jsindex 3ed54ce0e8..418797a3f7 100644--- a/src/topics/index.js+++ b/src/topics/index.js@@ -146,7 +146,6 @@ Topics.getTopicWithPosts = async function (topicData, set, uid, start, stop, revdeleter,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, revgetDeleter(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, revtopicData.mergedTimestampISO = utils.toISOString(topicData.mergedTimestamp);}topicData.related = related || [];- topicData.posterCount = posterCount;topicData.unreplied = topicData.postcount === 1;topicData.icons = [];diff --git a/src/topics/posts.js b/src/topics/posts.jsindex 7ecbf1fb86..883d66907f 100644--- a/src/topics/posts.js+++ b/src/topics/posts.js@@ -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);};diff --git a/src/upgrades/1.15.0/topic_poster_count.js b/src/upgrades/1.15.0/topic_poster_count.jsnew file mode 100644index 0000000000..b07111c800--- /dev/null+++ b/src/upgrades/1.15.0/topic_poster_count.js@@ -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,+ });+ },+};