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
isekai-main
Barış Soner Uşaklı 3 years ago
parent e6185883ba
commit 079c487dcb

@ -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`, post.pid]);
bulkRemove.push([`cid:${oldCid}:uid:${post.uid}:pids:votes`, 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]); 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]); bulkAdd.push([`cid:${cid}:uid:${post.uid}:pids:votes`, post.votes, post.pid]);
} }
}); });

@ -57,8 +57,7 @@ const templateToData = {
return cids.map(c => `cid:${c}:uid:${userData.uid}:pids:votes`); return cids.map(c => `cid:${c}:uid:${userData.uid}:pids:votes`);
}, },
getTopics: async (sets, req, start, stop) => { getTopics: async (sets, req, start, stop) => {
let pids = await db.getSortedSetRevRangeByScore(sets, start, stop, '+inf', '1'); const pids = await db.getSortedSetRevRangeByScore(sets, start, stop - start + 1, '+inf', 1);
pids = await privileges.posts.filter('topics:read', pids, req.uid);
const postObjs = await posts.getPostSummaryByPids(pids, req.uid, { stripTags: false }); const postObjs = await posts.getPostSummaryByPids(pids, req.uid, { stripTags: false });
return { posts: postObjs, nextStart: stop + 1 }; return { posts: postObjs, nextStart: stop + 1 };
}, },
@ -72,8 +71,7 @@ const templateToData = {
return cids.map(c => `cid:${c}:uid:${userData.uid}:pids:votes`); return cids.map(c => `cid:${c}:uid:${userData.uid}:pids:votes`);
}, },
getTopics: async (sets, req, start, stop) => { getTopics: async (sets, req, start, stop) => {
let pids = await db.getSortedSetRangeByScore(sets, start, stop, '-inf', '-1'); const pids = await db.getSortedSetRangeByScore(sets, start, stop - start + 1, '-inf', -1);
pids = await privileges.posts.filter('topics:read', pids, req.uid);
const postObjs = await posts.getPostSummaryByPids(pids, req.uid, { stripTags: false }); const postObjs = await posts.getPostSummaryByPids(pids, req.uid, { stripTags: false });
return { posts: postObjs, nextStart: stop + 1 }; return { posts: postObjs, nextStart: stop + 1 };
}, },

@ -158,7 +158,7 @@ module.exports = function (Posts) {
bulkAdd.push([`uid:${toUid}:posts`, post.timestamp, post.pid]); bulkAdd.push([`uid:${toUid}:posts`, post.timestamp, post.pid]);
bulkAdd.push([`cid:${post.cid}:uid:${toUid}:pids`, 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]); bulkAdd.push([`cid:${post.cid}:uid:${toUid}:pids:votes`, post.votes, post.pid]);
} }
postsByUser[post.uid] = postsByUser[post.uid] || []; postsByUser[post.uid] = postsByUser[post.uid] || [];

@ -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}:pids`, postData.timestamp, postData.pid),
db.sortedSetAdd(`cid:${topicData[1].cid}:uid:${postData.uid}: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)); tasks.push(db.sortedSetAdd(`cid:${topicData[1].cid}:uid:${postData.uid}:pids:votes`, postData.votes, postData.pid));
} }

@ -10,25 +10,19 @@ module.exports = {
const posts = require('../../posts'); const posts = require('../../posts');
const { progress } = this; const { progress } = this;
await batch.processSortedSet('posts:pid', async (ids) => { await batch.processSortedSet('posts:pid', async (pids) => {
const postData = await posts.getPostsFields(ids, ['uid', 'downvotes']); const postData = await posts.getPostsFields(pids, ['pid', 'uid', 'upvotes', 'downvotes']);
const cids = await posts.getCidsByPids(ids); const cids = await posts.getCidsByPids(pids);
const promises = ids.reduce((memo, pid, idx) => { const bulkAdd = [];
const { uid, downvotes } = postData[idx]; postData.forEach((post, index) => {
const cid = cids[idx]; if (post.votes > 0 || post.votes < 0) {
const cid = cids[index];
if (!downvotes) { bulkAdd.push([`cid:${cid}:uid:${post.uid}:pids:votes`, post.votes, post.pid]);
progress.incr();
return memo;
} }
});
memo.push(db.sortedSetAdd(`cid:${cid}:uid:${uid}:pids:votes`, -downvotes, pid)); await db.sortedSetAddBulk(bulkAdd);
return memo; progress.incr(postData.length);
}, []);
await Promise.all(promises);
progress.incr(promises.length);
}, { }, {
progress, progress,
}); });

Loading…
Cancel
Save