mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-13 17:35:46 +01:00
committed by
GitHub
parent
96f5312de9
commit
8ad9a103b3
@@ -63,7 +63,8 @@ module.exports = function (Posts) {
|
|||||||
|
|
||||||
putVoteInProgress(pid, uid);
|
putVoteInProgress(pid, uid);
|
||||||
try {
|
try {
|
||||||
return await unvote(pid, uid, 'unvote');
|
const voteStatus = await Posts.hasVoted(pid, uid);
|
||||||
|
return await unvote(pid, uid, 'unvote', voteStatus);
|
||||||
} finally {
|
} finally {
|
||||||
clearVoteProgress(pid, uid);
|
clearVoteProgress(pid, uid);
|
||||||
}
|
}
|
||||||
@@ -114,48 +115,26 @@ module.exports = function (Posts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function toggleVote(type, pid, uid) {
|
async function toggleVote(type, pid, uid) {
|
||||||
await unvote(pid, uid, type);
|
const voteStatus = await Posts.hasVoted(pid, uid);
|
||||||
return await vote(type, false, pid, uid);
|
await unvote(pid, uid, type, voteStatus);
|
||||||
|
return await vote(type, false, pid, uid, voteStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function unvote(pid, uid, command) {
|
async function unvote(pid, uid, type, voteStatus) {
|
||||||
const [owner, voteStatus] = await Promise.all([
|
const owner = await Posts.getPostField(pid, 'uid');
|
||||||
Posts.getPostField(pid, 'uid'),
|
|
||||||
Posts.hasVoted(pid, uid),
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (parseInt(uid, 10) === parseInt(owner, 10)) {
|
if (parseInt(uid, 10) === parseInt(owner, 10)) {
|
||||||
throw new Error('[[error:self-vote]]');
|
throw new Error('[[error:self-vote]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command === 'downvote') {
|
if (type === 'downvote') {
|
||||||
await checkDownvoteLimitation(pid, uid);
|
await checkDownvoteLimitation(pid, uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
let hook;
|
|
||||||
let current = voteStatus.upvoted ? 'upvote' : 'downvote';
|
|
||||||
|
|
||||||
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}`, {
|
|
||||||
pid: pid,
|
|
||||||
uid: uid,
|
|
||||||
owner: owner,
|
|
||||||
current: current,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) {
|
if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid);
|
return await vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, voteStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkDownvoteLimitation(pid, uid) {
|
async function checkDownvoteLimitation(pid, uid) {
|
||||||
@@ -185,7 +164,7 @@ module.exports = function (Posts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function vote(type, unvote, pid, uid) {
|
async function vote(type, unvote, pid, uid, voteStatus) {
|
||||||
uid = parseInt(uid, 10);
|
uid = parseInt(uid, 10);
|
||||||
if (uid <= 0) {
|
if (uid <= 0) {
|
||||||
throw new Error('[[error:not-logged-in]]');
|
throw new Error('[[error:not-logged-in]]');
|
||||||
@@ -209,6 +188,8 @@ module.exports = function (Posts) {
|
|||||||
|
|
||||||
await adjustPostVotes(postData, uid, type, unvote);
|
await adjustPostVotes(postData, uid, type, unvote);
|
||||||
|
|
||||||
|
await fireVoteHook(postData, uid, type, unvote, voteStatus);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
user: {
|
user: {
|
||||||
reputation: newReputation,
|
reputation: newReputation,
|
||||||
@@ -220,6 +201,25 @@ module.exports = function (Posts) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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';
|
||||||
|
} 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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function adjustPostVotes(postData, uid, type, unvote) {
|
async function adjustPostVotes(postData, uid, type, unvote) {
|
||||||
const notType = (type === 'upvote' ? 'downvote' : 'upvote');
|
const notType = (type === 'upvote' ? 'downvote' : 'upvote');
|
||||||
if (unvote) {
|
if (unvote) {
|
||||||
|
|||||||
Reference in New Issue
Block a user