From 03709d2ef48f3a49609c9bb2e25ea2948c38fa1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 20 Mar 2024 10:37:18 -0400 Subject: [PATCH 1/2] feat: on vote calculate new order --- library.js | 23 +++++++++++++++++++++-- plugin.json | 3 ++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/library.js b/library.js index 6a34e70..df90c0b 100644 --- a/library.js +++ b/library.js @@ -3,8 +3,9 @@ const winston = require.main.require('winston'); const db = require.main.require('./src/database'); +const topics = require.main.require('./src/topics'); -const plugin = {}; +const plugin = module.exports; plugin.getTopics = async (hookData) => { let voteData; @@ -26,4 +27,22 @@ plugin.getTopics = async (hookData) => { return hookData; }; -module.exports = plugin; +plugin.updatePostVoteCount = async (hookData) => { + const { tid } = hookData.post; + const topicData = await topics.getTopicFields(tid, ['cid', 'upvotes', 'downvotes', 'pinned']); + const voteData = await db.getSortedSetMembersWithScores(`tid:${tid}:posts:votes`); + + let votes = topicData.votes; + for (const { score } of voteData) { + votes += score; + } + + const promises = [ + db.sortedSetAdd('topics:votes', votes, tid) + ]; + + if (!topicData.pinned) { + promises.push(db.sortedSetAdd(`cid:${topicData.cid}:tids:votes`, votes, tid)); + } + await Promise.all(promises); +}; diff --git a/plugin.json b/plugin.json index 8691ff7..e60a91e 100644 --- a/plugin.json +++ b/plugin.json @@ -3,6 +3,7 @@ "url": "https://github.com/oplik0/nodebb-plugin-total-vote-count", "library": "./library.js", "hooks": [ - { "hook": "filter:topics.get", "method": "getTopics" } + { "hook": "filter:topics.get", "method": "getTopics" }, + { "hook": "action:post.updatePostVoteCount", "method": "updatePostVoteCount" } ] } \ No newline at end of file From 186c77279140e113d92b1760a48bcc6b670e6c7f Mon Sep 17 00:00:00 2001 From: Opliko Date: Wed, 20 Mar 2024 16:56:47 +0100 Subject: [PATCH 2/2] fix: resolve eslint issues --- library.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library.js b/library.js index df90c0b..4a47f0d 100644 --- a/library.js +++ b/library.js @@ -32,13 +32,13 @@ plugin.updatePostVoteCount = async (hookData) => { const topicData = await topics.getTopicFields(tid, ['cid', 'upvotes', 'downvotes', 'pinned']); const voteData = await db.getSortedSetMembersWithScores(`tid:${tid}:posts:votes`); - let votes = topicData.votes; + let { votes } = topicData; for (const { score } of voteData) { votes += score; } const promises = [ - db.sortedSetAdd('topics:votes', votes, tid) + db.sortedSetAdd('topics:votes', votes, tid), ]; if (!topicData.pinned) {