From 079c487dcb977c80a4e3fb582b6e495eabe44333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 4 Feb 2022 21:52:44 -0500 Subject: [PATCH] fix: controversial posts/bests posts not showing anything fix upgrade script so posts with negative votes are stored, a post can have 10 upvotes and 2 downvotes fix missing negative votes checks remove unnecessary pids flters since the cids are only already filtered by topics:read --- src/categories/recentreplies.js | 2 +- src/controllers/accounts/posts.js | 6 ++-- src/posts/user.js | 2 +- src/topics/fork.js | 2 +- .../1.19.2/store_downvoted_posts_in_zset.js | 28 ++++++++----------- 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index 935ef80642..0e5618b4fe 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -176,7 +176,7 @@ module.exports = function (Categories) { bulkRemove.push([`cid:${oldCid}:uid:${post.uid}:pids`, post.pid]); bulkRemove.push([`cid:${oldCid}:uid:${post.uid}:pids:votes`, post.pid]); bulkAdd.push([`cid:${cid}:uid:${post.uid}:pids`, post.timestamp, post.pid]); - if (post.votes > 0) { + if (post.votes > 0 || post.votes < 0) { bulkAdd.push([`cid:${cid}:uid:${post.uid}:pids:votes`, post.votes, post.pid]); } }); diff --git a/src/controllers/accounts/posts.js b/src/controllers/accounts/posts.js index d7e2cf000f..9455c1322b 100644 --- a/src/controllers/accounts/posts.js +++ b/src/controllers/accounts/posts.js @@ -57,8 +57,7 @@ const templateToData = { return cids.map(c => `cid:${c}:uid:${userData.uid}:pids:votes`); }, getTopics: async (sets, req, start, stop) => { - let pids = await db.getSortedSetRevRangeByScore(sets, start, stop, '+inf', '1'); - pids = await privileges.posts.filter('topics:read', pids, req.uid); + const pids = await db.getSortedSetRevRangeByScore(sets, start, stop - start + 1, '+inf', 1); const postObjs = await posts.getPostSummaryByPids(pids, req.uid, { stripTags: false }); return { posts: postObjs, nextStart: stop + 1 }; }, @@ -72,8 +71,7 @@ const templateToData = { return cids.map(c => `cid:${c}:uid:${userData.uid}:pids:votes`); }, getTopics: async (sets, req, start, stop) => { - let pids = await db.getSortedSetRangeByScore(sets, start, stop, '-inf', '-1'); - pids = await privileges.posts.filter('topics:read', pids, req.uid); + const pids = await db.getSortedSetRangeByScore(sets, start, stop - start + 1, '-inf', -1); const postObjs = await posts.getPostSummaryByPids(pids, req.uid, { stripTags: false }); return { posts: postObjs, nextStart: stop + 1 }; }, diff --git a/src/posts/user.js b/src/posts/user.js index d94e5e7966..c92c197f69 100644 --- a/src/posts/user.js +++ b/src/posts/user.js @@ -158,7 +158,7 @@ module.exports = function (Posts) { bulkAdd.push([`uid:${toUid}:posts`, post.timestamp, post.pid]); bulkAdd.push([`cid:${post.cid}:uid:${toUid}:pids`, post.timestamp, post.pid]); - if (post.votes > 0) { + if (post.votes > 0 || post.votes < 0) { bulkAdd.push([`cid:${post.cid}:uid:${toUid}:pids:votes`, post.votes, post.pid]); } postsByUser[post.uid] = postsByUser[post.uid] || []; diff --git a/src/topics/fork.js b/src/topics/fork.js index 59eb95b783..97b5316656 100644 --- a/src/topics/fork.js +++ b/src/topics/fork.js @@ -146,7 +146,7 @@ module.exports = function (Topics) { db.sortedSetAdd(`cid:${topicData[1].cid}:pids`, postData.timestamp, postData.pid), db.sortedSetAdd(`cid:${topicData[1].cid}:uid:${postData.uid}:pids`, postData.timestamp, postData.pid), ]; - if (postData.votes > 0) { + if (postData.votes > 0 || postData.votes < 0) { tasks.push(db.sortedSetAdd(`cid:${topicData[1].cid}:uid:${postData.uid}:pids:votes`, postData.votes, postData.pid)); } diff --git a/src/upgrades/1.19.2/store_downvoted_posts_in_zset.js b/src/upgrades/1.19.2/store_downvoted_posts_in_zset.js index f1752f8974..6b402677b7 100644 --- a/src/upgrades/1.19.2/store_downvoted_posts_in_zset.js +++ b/src/upgrades/1.19.2/store_downvoted_posts_in_zset.js @@ -10,25 +10,19 @@ module.exports = { const posts = require('../../posts'); const { progress } = this; - await batch.processSortedSet('posts:pid', async (ids) => { - const postData = await posts.getPostsFields(ids, ['uid', 'downvotes']); - const cids = await posts.getCidsByPids(ids); + await batch.processSortedSet('posts:pid', async (pids) => { + const postData = await posts.getPostsFields(pids, ['pid', 'uid', 'upvotes', 'downvotes']); + const cids = await posts.getCidsByPids(pids); - const promises = ids.reduce((memo, pid, idx) => { - const { uid, downvotes } = postData[idx]; - const cid = cids[idx]; - - if (!downvotes) { - progress.incr(); - return memo; + const bulkAdd = []; + postData.forEach((post, index) => { + if (post.votes > 0 || post.votes < 0) { + const cid = cids[index]; + bulkAdd.push([`cid:${cid}:uid:${post.uid}:pids:votes`, post.votes, post.pid]); } - - memo.push(db.sortedSetAdd(`cid:${cid}:uid:${uid}:pids:votes`, -downvotes, pid)); - return memo; - }, []); - - await Promise.all(promises); - progress.incr(promises.length); + }); + await db.sortedSetAddBulk(bulkAdd); + progress.incr(postData.length); }, { progress, });