This commit is contained in:
Baris Usakli
2019-07-10 14:29:11 -04:00
parent 0ac49d63d9
commit e1ecc36d04
3 changed files with 125 additions and 218 deletions

View File

@@ -5,6 +5,7 @@ var async = require('async');
var meta = require('../meta');
var db = require('../database');
var user = require('../user');
var topics = require('../topics');
var plugins = require('../plugins');
var privileges = require('../privileges');
@@ -303,4 +304,45 @@ module.exports = function (Posts) {
},
], callback);
}
Posts.updatePostVoteCount = async function (postData) {
if (!postData || !postData.pid || !postData.tid) {
return;
}
await Promise.all([
updateTopicVoteCount(postData),
db.sortedSetAdd('posts:votes', postData.votes, postData.pid),
Posts.setPostFields(postData.pid, {
upvotes: postData.upvotes,
downvotes: postData.downvotes,
}),
]);
};
async function updateTopicVoteCount(postData) {
const topicData = await topics.getTopicFields(postData.tid, ['mainPid', 'cid', 'pinned']);
if (postData.uid) {
if (postData.votes > 0) {
await db.sortedSetAdd('cid:' + topicData.cid + ':uid:' + postData.uid + ':pids:votes', postData.votes, postData.pid);
} else {
await db.sortedSetRemove('cid:' + topicData.cid + ':uid:' + postData.uid + ':pids:votes', postData.pid);
}
}
if (parseInt(topicData.mainPid, 10) !== parseInt(postData.pid, 10)) {
return await db.sortedSetAdd('tid:' + postData.tid + ':posts:votes', postData.votes, postData.pid);
}
const promises = [
topics.setTopicFields(postData.tid, {
upvotes: postData.upvotes,
downvotes: postData.downvotes,
}),
db.sortedSetAdd('topics:votes', postData.votes, postData.tid),
];
if (!topicData.pinned) {
promises.push(db.sortedSetAdd('cid:' + topicData.cid + ':tids:votes', postData.votes, postData.tid));
}
await Promise.all(promises);
}
};