feat: #7743, posts/votes

v1.18.x
Baris Usakli 6 years ago
parent e1ecc36d04
commit 8ef75be3e3

@ -1,7 +1,5 @@
'use strict'; 'use strict';
var async = require('async');
var meta = require('../meta'); var meta = require('../meta');
var db = require('../database'); var db = require('../database');
var user = require('../user'); var user = require('../user');
@ -12,97 +10,80 @@ var privileges = require('../privileges');
module.exports = function (Posts) { module.exports = function (Posts) {
var votesInProgress = {}; var votesInProgress = {};
Posts.upvote = function (pid, uid, callback) { Posts.upvote = async function (pid, uid) {
if (meta.config['reputation:disabled']) { if (meta.config['reputation:disabled']) {
return callback(new Error('[[error:reputation-system-disabled]]')); 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]]');
} }
async.waterfall([ if (voteInProgress(pid, uid)) {
function (next) { throw new Error('[[error:already-voting-for-this-post]]');
privileges.posts.can('posts:upvote', pid, uid, next); }
}, putVoteInProgress(pid, uid);
function (canUpvote, next) {
if (!canUpvote) {
return next(new Error('[[error:no-privileges]]'));
}
if (voteInProgress(pid, uid)) {
return next(new Error('[[error:already-voting-for-this-post]]'));
}
putVoteInProgress(pid, uid);
toggleVote('upvote', pid, uid, function (err, data) { try {
clearVoteProgress(pid, uid); const data = await toggleVote('upvote', pid, uid);
next(err, data); return data;
}); } finally {
}, clearVoteProgress(pid, uid);
], callback); }
}; };
Posts.downvote = function (pid, uid, callback) { Posts.downvote = async function (pid, uid) {
if (meta.config['reputation:disabled']) { if (meta.config['reputation:disabled']) {
return callback(new Error('[[error:reputation-system-disabled]]')); throw new Error('[[error:reputation-system-disabled]]');
} }
if (meta.config['downvote:disabled']) { if (meta.config['downvote:disabled']) {
return callback(new Error('[[error:downvoting-disabled]]')); throw new Error('[[error:downvoting-disabled]]');
}
const canDownvote = await privileges.posts.can('posts:downvote', pid, uid);
if (!canDownvote) {
throw new Error('[[error:no-privileges]]');
} }
async.waterfall([ if (voteInProgress(pid, uid)) {
function (next) { throw new Error('[[error:already-voting-for-this-post]]');
privileges.posts.can('posts:downvote', pid, uid, next); }
},
function (canUpvote, next) {
if (!canUpvote) {
return next(new Error('[[error:no-privileges]]'));
}
if (voteInProgress(pid, uid)) {
return next(new Error('[[error:already-voting-for-this-post]]'));
}
putVoteInProgress(pid, uid);
toggleVote('downvote', pid, uid, function (err, data) { putVoteInProgress(pid, uid);
clearVoteProgress(pid, uid); try {
next(err, data); const data = toggleVote('downvote', pid, uid);
}); return data;
}, } finally {
], callback); clearVoteProgress(pid, uid);
}
}; };
Posts.unvote = function (pid, uid, callback) { Posts.unvote = async function (pid, uid) {
if (voteInProgress(pid, uid)) { if (voteInProgress(pid, uid)) {
return callback(new Error('[[error:already-voting-for-this-post]]')); throw new Error('[[error:already-voting-for-this-post]]');
} }
putVoteInProgress(pid, uid); putVoteInProgress(pid, uid);
try {
unvote(pid, uid, 'unvote', function (err, data) { const data = await unvote(pid, uid, 'unvote');
return data;
} finally {
clearVoteProgress(pid, uid); clearVoteProgress(pid, uid);
callback(err, data); }
});
}; };
Posts.hasVoted = function (pid, uid, callback) { Posts.hasVoted = async function (pid, uid) {
if (parseInt(uid, 10) <= 0) { if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, { upvoted: false, downvoted: false }); return { upvoted: false, downvoted: false };
} }
async.waterfall([ const hasVoted = await db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid);
function (next) { return { upvoted: hasVoted[0], downvoted: hasVoted[1] };
db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, next);
},
function (hasVoted, next) {
next(null, { upvoted: hasVoted[0], downvoted: hasVoted[1] });
},
], callback);
}; };
Posts.getVoteStatusByPostIDs = function (pids, uid, callback) { Posts.getVoteStatusByPostIDs = async function (pids, uid) {
if (parseInt(uid, 10) <= 0) { if (parseInt(uid, 10) <= 0) {
var data = pids.map(() => false); const data = pids.map(() => false);
return setImmediate(callback, null, { upvotes: data, downvotes: data }); return { upvotes: data, downvotes: data };
} }
var upvoteSets = []; var upvoteSets = [];
var downvoteSets = []; var downvoteSets = [];
@ -111,24 +92,15 @@ module.exports = function (Posts) {
upvoteSets.push('pid:' + pids[i] + ':upvote'); upvoteSets.push('pid:' + pids[i] + ':upvote');
downvoteSets.push('pid:' + pids[i] + ':downvote'); downvoteSets.push('pid:' + pids[i] + ':downvote');
} }
async.waterfall([ const data = await db.isMemberOfSets(upvoteSets.concat(downvoteSets), uid);
function (next) { return {
db.isMemberOfSets(upvoteSets.concat(downvoteSets), uid, next); upvotes: data.slice(0, pids.length),
}, downvotes: data.slice(pids.length, pids.length * 2),
function (data, next) { };
next(null, {
upvotes: data.slice(0, pids.length),
downvotes: data.slice(pids.length, pids.length * 2),
});
},
], callback);
}; };
Posts.getUpvotedUidsByPids = function (pids, callback) { Posts.getUpvotedUidsByPids = async function (pids) {
var sets = pids.map(function (pid) { return await db.getSetsMembers(pids.map(pid => 'pid:' + pid + ':upvote'));
return 'pid:' + pid + ':upvote';
});
db.getSetsMembers(sets, callback);
}; };
function voteInProgress(pid, uid) { function voteInProgress(pid, uid) {
@ -149,160 +121,108 @@ module.exports = function (Posts) {
} }
} }
function toggleVote(type, pid, uid, callback) { async function toggleVote(type, pid, uid) {
async.waterfall([ await unvote(pid, uid, type);
function (next) { return await vote(type, false, pid, uid);
unvote(pid, uid, type, function (err) {
next(err);
});
},
function (next) {
vote(type, false, pid, uid, next);
},
], callback);
} }
function unvote(pid, uid, command, callback) { async function unvote(pid, uid, command) {
async.waterfall([ const [owner, voteStatus, reputation] = await Promise.all([
function (next) { Posts.getPostField(pid, 'uid'),
async.parallel({ Posts.hasVoted(pid, uid),
owner: function (next) { user.getUserField(uid, 'reputation'),
Posts.getPostField(pid, 'uid', next); ]);
},
voteStatus: function (next) { if (parseInt(uid, 10) === parseInt(owner, 10)) {
Posts.hasVoted(pid, uid, next); throw new Error('[[error:self-vote]]');
}, }
reputation: function (next) {
user.getUserField(uid, 'reputation', next); if (command === 'downvote' && reputation < meta.config['min:rep:downvote']) {
}, throw new Error('[[error:not-enough-reputation-to-downvote]]');
}, next); }
},
function (results, next) { var hook;
if (parseInt(uid, 10) === parseInt(results.owner, 10)) { var current = voteStatus.upvoted ? 'upvote' : 'downvote';
return callback(new Error('[[error:self-vote]]'));
} if ((voteStatus.upvoted && command === 'downvote') || (voteStatus.downvoted && command === 'upvote')) { // e.g. User *has* upvoted, and clicks downvote
hook = command;
if (command === 'downvote' && results.reputation < meta.config['min:rep:downvote']) { } else if (voteStatus.upvoted || voteStatus.downvoted) { // e.g. User *has* upvoted, clicks upvote (so we "unvote")
return callback(new Error('[[error:not-enough-reputation-to-downvote]]')); hook = 'unvote';
} } else { // e.g. User *has not* voted, clicks upvote
hook = command;
var voteStatus = results.voteStatus; current = 'unvote';
var hook; }
var current = voteStatus.upvoted ? 'upvote' : 'downvote';
plugins.fireHook('action:post.' + hook, {
if ((voteStatus.upvoted && command === 'downvote') || (voteStatus.downvoted && command === 'upvote')) { // e.g. User *has* upvoted, and clicks downvote pid: pid,
hook = command; uid: uid,
} else if (voteStatus.upvoted || voteStatus.downvoted) { // e.g. User *has* upvoted, clicks upvote (so we "unvote") owner: owner,
hook = 'unvote'; current: current,
} else { // e.g. User *has not* voted, clicks upvote });
hook = command;
current = 'unvote'; if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) {
} return;
}
plugins.fireHook('action:post.' + hook, {
pid: pid, return await vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid);
uid: uid,
owner: results.owner,
current: current,
});
if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) {
return callback();
}
vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, next);
},
], callback);
} }
function vote(type, unvote, pid, uid, callback) { async function vote(type, unvote, pid, uid) {
uid = parseInt(uid, 10); uid = parseInt(uid, 10);
if (uid <= 0) { if (uid <= 0) {
return callback(new Error('[[error:not-logged-in]]')); throw new Error('[[error:not-logged-in]]');
} }
var postData; const postData = await Posts.getPostFields(pid, ['pid', 'uid', 'tid']);
var newreputation;
async.waterfall([
function (next) {
Posts.getPostFields(pid, ['pid', 'uid', 'tid'], next);
},
function (_postData, next) {
postData = _postData;
var now = Date.now();
if (type === 'upvote' && !unvote) {
db.sortedSetAdd('uid:' + uid + ':upvote', now, pid);
} else {
db.sortedSetRemove('uid:' + uid + ':upvote', pid);
}
if (type === 'upvote' || unvote) {
db.sortedSetRemove('uid:' + uid + ':downvote', pid);
} else {
db.sortedSetAdd('uid:' + uid + ':downvote', now, pid);
}
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, next);
},
function (_newreputation, next) {
newreputation = _newreputation;
if (parseInt(postData.uid, 10)) {
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
}
adjustPostVotes(postData, uid, type, unvote, next); var now = Date.now();
},
function (next) { if (type === 'upvote' && !unvote) {
next(null, { db.sortedSetAdd('uid:' + uid + ':upvote', now, pid);
user: { } else {
reputation: newreputation, db.sortedSetRemove('uid:' + uid + ':upvote', pid);
}, }
fromuid: uid,
post: postData, if (type === 'upvote' || unvote) {
upvote: type === 'upvote' && !unvote, db.sortedSetRemove('uid:' + uid + ':downvote', pid);
downvote: type === 'downvote' && !unvote, } else {
}); db.sortedSetAdd('uid:' + uid + ':downvote', now, pid);
}
const newReputation = await user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1);
if (parseInt(postData.uid, 10)) {
db.sortedSetAdd('users:reputation', newReputation, postData.uid);
}
await adjustPostVotes(postData, uid, type, unvote);
return {
user: {
reputation: newReputation,
}, },
], callback); fromuid: uid,
post: postData,
upvote: type === 'upvote' && !unvote,
downvote: type === 'downvote' && !unvote,
};
} }
function adjustPostVotes(postData, uid, type, unvote, callback) { async function adjustPostVotes(postData, uid, type, unvote) {
var notType = (type === 'upvote' ? 'downvote' : 'upvote'); var notType = (type === 'upvote' ? 'downvote' : 'upvote');
async.waterfall([ if (unvote) {
function (next) { await db.setRemove('pid:' + postData.pid + ':' + type, uid);
async.series([ } else {
function (next) { await db.setAdd('pid:' + postData.pid + ':' + type, uid);
if (unvote) { }
db.setRemove('pid:' + postData.pid + ':' + type, uid, next); await db.setRemove('pid:' + postData.pid + ':' + notType, uid);
} else {
db.setAdd('pid:' + postData.pid + ':' + type, uid, next); const [upvotes, downvotes] = await Promise.all([
} db.setCount('pid:' + postData.pid + ':upvote'),
}, db.setCount('pid:' + postData.pid + ':downvote'),
function (next) { ]);
db.setRemove('pid:' + postData.pid + ':' + notType, uid, next); postData.upvotes = upvotes;
}, postData.downvotes = downvotes;
], function (err) { postData.votes = postData.upvotes - postData.downvotes;
next(err); await Posts.updatePostVoteCount(postData);
});
},
function (next) {
async.parallel({
upvotes: function (next) {
db.setCount('pid:' + postData.pid + ':upvote', next);
},
downvotes: function (next) {
db.setCount('pid:' + postData.pid + ':downvote', next);
},
}, next);
},
function (results, next) {
postData.upvotes = results.upvotes;
postData.downvotes = results.downvotes;
postData.votes = postData.upvotes - postData.downvotes;
Posts.updatePostVoteCount(postData, next);
},
], callback);
} }
Posts.updatePostVoteCount = async function (postData) { Posts.updatePostVoteCount = async function (postData) {

Loading…
Cancel
Save