Files
NodeBB/src/posts/votes.js

293 lines
8.9 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 flags = require('../flags');
2020-05-01 23:54:17 -04:00
const user = require('../user');
const topics = require('../topics');
const plugins = require('../plugins');
const privileges = require('../privileges');
const translator = require('../translator');
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 {
const voteStatus = await Posts.hasVoted(pid, uid);
return await unvote(pid, uid, 'unvote', voteStatus);
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
}
2021-02-03 23:59:08 -07:00
const hasVoted = await db.isMemberOfSets([`pid:${pid}:upvote`, `pid:${pid}:downvote`], uid);
2019-07-10 15:10:12 -04:00
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
}
2021-02-03 23:59:08 -07: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) {
2021-02-03 23:59:08 -07:00
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) {
const voteStatus = await Posts.hasVoted(pid, uid);
await unvote(pid, uid, type, voteStatus);
return await vote(type, false, pid, uid, voteStatus);
2015-07-03 14:10:40 +02:00
}
async function unvote(pid, uid, type, voteStatus) {
const owner = await Posts.getPostField(pid, 'uid');
2019-07-10 15:10:12 -04:00
if (parseInt(uid, 10) === parseInt(owner, 10)) {
throw new Error('[[error:self-vote]]');
}
if (type === 'downvote') {
2020-07-09 12:51:05 -04:00
await checkDownvoteLimitation(pid, uid);
2019-07-10 15:10:12 -04:00
}
if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) {
return;
}
return await vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, voteStatus);
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(
2021-02-03 23:59:08 -07:00
`uid:${uid}:downvote`, 0, -1, '+inf', Date.now() - oneDay
2020-07-09 12:51:05 -04:00
),
]);
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) {
2021-02-03 23:59:08 -07:00
throw new Error(`[[error:too-many-downvotes-today, ${meta.config.downvotesPerDay}]]`);
2020-07-09 12:51:05 -04:00
}
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) {
2021-02-03 23:59:08 -07:00
throw new Error(`[[error:too-many-downvotes-today-user, ${meta.config.downvotesPerUserPerDay}]]`);
2020-07-09 12:51:05 -04:00
}
}
}
async function vote(type, unvote, pid, uid, voteStatus) {
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) {
2021-02-03 23:59:08 -07:00
await db.sortedSetAdd(`uid:${uid}:upvote`, now, pid);
2019-07-10 15:10:12 -04:00
} else {
2021-02-03 23:59:08 -07:00
await db.sortedSetRemove(`uid:${uid}:upvote`, pid);
2019-07-10 15:10:12 -04:00
}
if (type === 'upvote' || unvote) {
2021-02-03 23:59:08 -07:00
await db.sortedSetRemove(`uid:${uid}:downvote`, pid);
2019-07-10 15:10:12 -04:00
} else {
2021-02-03 23:59:08 -07: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);
await fireVoteHook(postData, uid, type, unvote, voteStatus);
2019-07-10 15:10:12 -04:00
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
async function fireVoteHook(postData, uid, type, unvote, voteStatus) {
let hook = type;
let current = voteStatus.upvoted ? 'upvote' : 'downvote';
if (unvote) { // e.g. unvoting, removing a upvote or downvote
hook = 'unvote';
2021-11-18 16:42:18 -05:00
} else { // e.g. User *has not* voted, clicks upvote or downvote
current = 'unvote';
}
// action:post.upvote
// action:post.downvote
// action:post.unvote
plugins.hooks.fire(`action:post.${hook}`, {
pid: postData.pid,
uid: uid,
owner: postData.uid,
current: current,
});
}
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) {
2021-02-03 23:59:08 -07:00
await db.setRemove(`pid:${postData.pid}:${type}`, uid);
2019-07-10 15:10:12 -04:00
} else {
2021-02-03 23:59:08 -07:00
await db.setAdd(`pid:${postData.pid}:${type}`, uid);
2019-07-10 15:10:12 -04:00
}
2021-02-03 23:59:08 -07:00
await db.setRemove(`pid:${postData.pid}:${notType}`, uid);
2019-07-10 15:10:12 -04:00
const [upvotes, downvotes] = await Promise.all([
2021-02-03 23:59:08 -07:00
db.setCount(`pid:${postData.pid}:upvote`),
db.setCount(`pid:${postData.pid}:downvote`),
2019-07-10 15:10:12 -04:00
]);
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;
}
const threshold = meta.config['flags:autoFlagOnDownvoteThreshold'];
if (threshold && postData.votes <= (-threshold)) {
const adminUid = await user.getFirstAdminUid();
const reportMsg = await translator.translate(`[[flags:auto-flagged, ${-postData.votes}]]`);
const flagObj = await flags.create('post', postData.pid, adminUid, reportMsg, null, true);
await flags.notify(flagObj, adminUid, true);
}
2019-07-10 14:29:11 -04:00
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) {
2021-02-03 23:59:08 -07:00
await db.sortedSetAdd(`cid:${topicData.cid}:uid:${postData.uid}:pids:votes`, postData.votes, postData.pid);
2019-07-10 14:29:11 -04:00
} else {
2021-02-03 23:59:08 -07:00
await db.sortedSetRemove(`cid:${topicData.cid}:uid:${postData.uid}:pids:votes`, postData.pid);
2019-07-10 14:29:11 -04:00
}
}
if (parseInt(topicData.mainPid, 10) !== parseInt(postData.pid, 10)) {
2021-02-03 23:59:08 -07:00
return await db.sortedSetAdd(`tid:${postData.tid}:posts:votes`, postData.votes, postData.pid);
2019-07-10 14:29:11 -04:00
}
const promises = [
topics.setTopicFields(postData.tid, {
upvotes: postData.upvotes,
downvotes: postData.downvotes,
}),
db.sortedSetAdd('topics:votes', postData.votes, postData.tid),
];
if (!topicData.pinned) {
2021-02-03 23:59:08 -07:00
promises.push(db.sortedSetAdd(`cid:${topicData.cid}:tids:votes`, postData.votes, postData.tid));
2019-07-10 14:29:11 -04:00
}
await Promise.all(promises);
}
2016-10-08 19:09:48 +03:00
};