Files
NodeBB/src/posts/votes.js

284 lines
8.4 KiB
JavaScript
Raw Normal View History

2016-10-08 19:09:48 +03:00
'use strict';
2015-07-03 14:10:40 +02:00
2020-05-01 23:54:17 -04:00
const meta = require('../meta');
const db = require('../database');
const user = require('../user');
const topics = require('../topics');
const plugins = require('../plugins');
const privileges = require('../privileges');
2015-07-03 14:10:40 +02:00
module.exports = function (Posts) {
2020-05-01 23:54:17 -04:00
const votesInProgress = {};
2015-07-03 14:10:40 +02:00
2019-07-10 15:10:12 -04:00
Posts.upvote = async function (pid, uid) {
if (meta.config['reputation:disabled']) {
2019-07-10 15:10:12 -04:00
throw new Error('[[error:reputation-system-disabled]]');
}
const canUpvote = await privileges.posts.can('posts:upvote', pid, uid);
if (!canUpvote) {
throw new Error('[[error:no-privileges]]');
2015-07-03 14:10:40 +02:00
}
2019-07-10 15:10:12 -04:00
if (voteInProgress(pid, uid)) {
throw new Error('[[error:already-voting-for-this-post]]');
}
putVoteInProgress(pid, uid);
2018-01-09 14:33:23 -05:00
2019-07-10 15:10:12 -04:00
try {
2020-05-01 23:54:17 -04:00
return await toggleVote('upvote', pid, uid);
2019-07-10 15:10:12 -04:00
} finally {
clearVoteProgress(pid, uid);
}
2015-07-03 14:10:40 +02:00
};
2019-07-10 15:10:12 -04:00
Posts.downvote = async function (pid, uid) {
if (meta.config['reputation:disabled']) {
2019-07-10 15:10:12 -04:00
throw new Error('[[error:reputation-system-disabled]]');
2015-07-03 14:10:40 +02:00
}
if (meta.config['downvote:disabled']) {
2019-07-10 15:10:12 -04:00
throw new Error('[[error:downvoting-disabled]]');
}
const canDownvote = await privileges.posts.can('posts:downvote', pid, uid);
if (!canDownvote) {
throw new Error('[[error:no-privileges]]');
2015-07-03 14:10:40 +02:00
}
2019-07-10 15:10:12 -04:00
if (voteInProgress(pid, uid)) {
throw new Error('[[error:already-voting-for-this-post]]');
}
2018-01-09 14:33:23 -05:00
2019-07-10 15:10:12 -04:00
putVoteInProgress(pid, uid);
try {
2020-05-01 23:54:17 -04:00
return await toggleVote('downvote', pid, uid);
2019-07-10 15:10:12 -04:00
} finally {
clearVoteProgress(pid, uid);
}
2015-09-25 15:30:45 -04:00
};
2019-07-10 15:10:12 -04:00
Posts.unvote = async function (pid, uid) {
2015-09-25 15:30:45 -04:00
if (voteInProgress(pid, uid)) {
2019-07-10 15:10:12 -04:00
throw new Error('[[error:already-voting-for-this-post]]');
2015-09-25 15:30:45 -04:00
}
putVoteInProgress(pid, uid);
2019-07-10 15:10:12 -04:00
try {
2020-05-01 23:54:17 -04:00
return await unvote(pid, uid, 'unvote');
2019-07-10 15:10:12 -04:00
} finally {
2015-09-25 15:30:45 -04:00
clearVoteProgress(pid, uid);
2019-07-10 15:10:12 -04:00
}
2015-07-03 14:10:40 +02:00
};
2019-07-10 15:10:12 -04:00
Posts.hasVoted = async function (pid, uid) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
2019-07-10 15:10:12 -04:00
return { upvoted: false, downvoted: false };
2016-10-08 19:09:48 +03:00
}
2019-07-10 15:10:12 -04:00
const hasVoted = await db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid);
return { upvoted: hasVoted[0], downvoted: hasVoted[1] };
2016-10-08 19:09:48 +03:00
};
2019-07-10 15:10:12 -04:00
Posts.getVoteStatusByPostIDs = async function (pids, uid) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
2019-07-10 15:10:12 -04:00
const data = pids.map(() => false);
return { upvotes: data, downvotes: data };
2016-10-08 19:09:48 +03:00
}
2020-05-01 23:54:17 -04:00
const upvoteSets = pids.map(pid => 'pid:' + pid + ':upvote');
const downvoteSets = pids.map(pid => 'pid:' + pid + ':downvote');
2019-07-10 15:10:12 -04:00
const data = await db.isMemberOfSets(upvoteSets.concat(downvoteSets), uid);
return {
upvotes: data.slice(0, pids.length),
downvotes: data.slice(pids.length, pids.length * 2),
};
2016-10-08 19:09:48 +03:00
};
2019-07-10 15:10:12 -04:00
Posts.getUpvotedUidsByPids = async function (pids) {
return await db.getSetsMembers(pids.map(pid => 'pid:' + pid + ':upvote'));
2016-10-08 19:09:48 +03:00
};
2015-07-03 14:10:40 +02:00
function voteInProgress(pid, uid) {
2018-01-09 14:33:23 -05:00
return Array.isArray(votesInProgress[uid]) && votesInProgress[uid].includes(parseInt(pid, 10));
2015-07-03 14:10:40 +02:00
}
function putVoteInProgress(pid, uid) {
votesInProgress[uid] = votesInProgress[uid] || [];
votesInProgress[uid].push(parseInt(pid, 10));
}
function clearVoteProgress(pid, uid) {
if (Array.isArray(votesInProgress[uid])) {
2020-05-01 23:54:17 -04:00
const index = votesInProgress[uid].indexOf(parseInt(pid, 10));
2015-07-03 14:10:40 +02:00
if (index !== -1) {
votesInProgress[uid].splice(index, 1);
}
}
}
2019-07-10 15:10:12 -04:00
async function toggleVote(type, pid, uid) {
await unvote(pid, uid, type);
return await vote(type, false, pid, uid);
2015-07-03 14:10:40 +02:00
}
2019-07-10 15:10:12 -04:00
async function unvote(pid, uid, command) {
2020-07-09 12:51:05 -04:00
const [owner, voteStatus] = await Promise.all([
2019-07-10 15:10:12 -04:00
Posts.getPostField(pid, 'uid'),
Posts.hasVoted(pid, uid),
]);
if (parseInt(uid, 10) === parseInt(owner, 10)) {
throw new Error('[[error:self-vote]]');
}
2020-07-09 12:51:05 -04:00
if (command === 'downvote') {
await checkDownvoteLimitation(pid, uid);
2019-07-10 15:10:12 -04:00
}
2020-05-01 23:54:17 -04:00
let hook;
let current = voteStatus.upvoted ? 'upvote' : 'downvote';
2019-07-10 15:10:12 -04:00
if ((voteStatus.upvoted && command === 'downvote') || (voteStatus.downvoted && command === 'upvote')) { // e.g. User *has* upvoted, and clicks downvote
hook = command;
} else if (voteStatus.upvoted || voteStatus.downvoted) { // e.g. User *has* upvoted, clicks upvote (so we "unvote")
hook = 'unvote';
} else { // e.g. User *has not* voted, clicks upvote
hook = command;
current = 'unvote';
}
plugins.hooks.fire('action:post.' + hook, {
2019-07-10 15:10:12 -04:00
pid: pid,
uid: uid,
owner: owner,
current: current,
});
if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) {
return;
}
return await vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid);
2015-07-03 14:10:40 +02:00
}
2020-07-09 12:51:05 -04:00
async function checkDownvoteLimitation(pid, uid) {
const oneDay = 86400000;
const [reputation, targetUid, downvotedPids] = await Promise.all([
user.getUserField(uid, 'reputation'),
Posts.getPostField(pid, 'uid'),
db.getSortedSetRevRangeByScore(
'uid:' + uid + ':downvote', 0, -1, '+inf', Date.now() - oneDay
),
]);
if (reputation < meta.config['min:rep:downvote']) {
throw new Error('[[error:not-enough-reputation-to-downvote]]');
}
if (meta.config.downvotesPerDay && downvotedPids.length >= meta.config.downvotesPerDay) {
throw new Error('[[error:too-many-downvotes-today, ' + meta.config.downvotesPerDay + ']]');
}
if (meta.config.downvotesPerUserPerDay) {
const postData = await Posts.getPostsFields(downvotedPids, ['uid']);
const targetDownvotes = postData.filter(p => p.uid === targetUid).length;
if (targetDownvotes >= meta.config.downvotesPerUserPerDay) {
throw new Error('[[error:too-many-downvotes-today-user, ' + meta.config.downvotesPerUserPerDay + ']]');
}
}
}
2019-07-10 15:10:12 -04:00
async function vote(type, unvote, pid, uid) {
2016-10-08 19:09:48 +03:00
uid = parseInt(uid, 10);
2018-11-17 22:31:39 -05:00
if (uid <= 0) {
2019-07-10 15:10:12 -04:00
throw new Error('[[error:not-logged-in]]');
2015-07-03 14:10:40 +02:00
}
2020-05-01 23:54:17 -04:00
const now = Date.now();
2019-07-10 15:10:12 -04:00
if (type === 'upvote' && !unvote) {
2020-05-01 23:54:17 -04:00
await db.sortedSetAdd('uid:' + uid + ':upvote', now, pid);
2019-07-10 15:10:12 -04:00
} else {
2020-05-01 23:54:17 -04:00
await db.sortedSetRemove('uid:' + uid + ':upvote', pid);
2019-07-10 15:10:12 -04:00
}
if (type === 'upvote' || unvote) {
2020-05-01 23:54:17 -04:00
await db.sortedSetRemove('uid:' + uid + ':downvote', pid);
2019-07-10 15:10:12 -04:00
} else {
2020-05-01 23:54:17 -04:00
await db.sortedSetAdd('uid:' + uid + ':downvote', now, pid);
2019-07-10 15:10:12 -04:00
}
2020-05-01 23:54:17 -04:00
const postData = await Posts.getPostFields(pid, ['pid', 'uid', 'tid']);
2020-05-02 13:34:19 -04:00
const newReputation = await user.incrementUserReputationBy(postData.uid, type === 'upvote' ? 1 : -1);
2019-07-10 15:10:12 -04:00
await adjustPostVotes(postData, uid, type, unvote);
return {
user: {
reputation: newReputation,
2017-05-25 16:40:03 -04:00
},
2019-07-10 15:10:12 -04:00
fromuid: uid,
post: postData,
upvote: type === 'upvote' && !unvote,
downvote: type === 'downvote' && !unvote,
};
2016-10-08 19:09:48 +03:00
}
2015-07-03 14:10:40 +02:00
2019-07-10 15:10:12 -04:00
async function adjustPostVotes(postData, uid, type, unvote) {
2020-05-01 23:54:17 -04:00
const notType = (type === 'upvote' ? 'downvote' : 'upvote');
2019-07-10 15:10:12 -04:00
if (unvote) {
await db.setRemove('pid:' + postData.pid + ':' + type, uid);
} else {
await db.setAdd('pid:' + postData.pid + ':' + type, uid);
}
await db.setRemove('pid:' + postData.pid + ':' + notType, uid);
const [upvotes, downvotes] = await Promise.all([
db.setCount('pid:' + postData.pid + ':upvote'),
db.setCount('pid:' + postData.pid + ':downvote'),
]);
postData.upvotes = upvotes;
postData.downvotes = downvotes;
postData.votes = postData.upvotes - postData.downvotes;
await Posts.updatePostVoteCount(postData);
2015-07-03 14:10:40 +02:00
}
2019-07-10 14:29:11 -04:00
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,
}),
]);
plugins.hooks.fire('action:post.updatePostVoteCount', { post: postData });
2019-07-10 14:29:11 -04:00
};
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);
}
2016-10-08 19:09:48 +03:00
};