Files
NodeBB/src/posts/votes.js

275 lines
7.2 KiB
JavaScript
Raw Normal View History

2016-10-08 19:09:48 +03:00
'use strict';
2015-07-03 14:10:40 +02:00
var async = require('async');
2015-07-03 14:10:40 +02:00
2016-10-08 19:09:48 +03:00
var meta = require('../meta');
var db = require('../database');
var user = require('../user');
var plugins = require('../plugins');
2015-07-03 14:10:40 +02:00
module.exports = function (Posts) {
2016-10-08 19:09:48 +03:00
var votesInProgress = {};
2015-07-03 14:10:40 +02:00
Posts.upvote = function (pid, uid, callback) {
2015-07-03 14:10:40 +02:00
if (parseInt(meta.config['reputation:disabled'], 10) === 1) {
return callback(new Error('[[error:reputation-system-disabled]]'));
}
2015-09-25 15:30:45 -04:00
if (voteInProgress(pid, uid)) {
return callback(new Error('[[error:already-voting-for-this-post]]'));
}
putVoteInProgress(pid, uid);
toggleVote('upvote', pid, uid, function (err, data) {
2015-09-25 15:30:45 -04:00
clearVoteProgress(pid, uid);
callback(err, data);
});
2015-07-03 14:10:40 +02:00
};
Posts.downvote = function (pid, uid, callback) {
2015-07-03 14:10:40 +02:00
if (parseInt(meta.config['reputation:disabled'], 10) === 1) {
return callback(new Error('[[error:reputation-system-disabled]]'));
}
if (parseInt(meta.config['downvote:disabled'], 10) === 1) {
return callback(new Error('[[error:downvoting-disabled]]'));
}
2015-09-25 15:30:45 -04:00
if (voteInProgress(pid, uid)) {
return callback(new Error('[[error:already-voting-for-this-post]]'));
}
putVoteInProgress(pid, uid);
toggleVote('downvote', pid, uid, function (err, data) {
2015-12-10 09:00:46 +02:00
clearVoteProgress(pid, uid);
callback(err, data);
2015-09-25 15:30:45 -04:00
});
};
Posts.unvote = function (pid, uid, callback) {
2015-09-25 15:30:45 -04:00
if (voteInProgress(pid, uid)) {
return callback(new Error('[[error:already-voting-for-this-post]]'));
}
putVoteInProgress(pid, uid);
unvote(pid, uid, 'unvote', function (err, data) {
2015-09-25 15:30:45 -04:00
clearVoteProgress(pid, uid);
callback(err, data);
2015-07-03 14:10:40 +02:00
});
};
Posts.hasVoted = function (pid, uid, callback) {
2016-10-08 19:09:48 +03:00
if (!parseInt(uid, 10)) {
return callback(null, {upvoted: false, downvoted: false});
}
db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, function (err, hasVoted) {
2016-10-08 19:09:48 +03:00
if (err) {
return callback(err);
}
2017-02-17 22:17:10 -07:00
callback(null, {upvoted: hasVoted[0], downvoted: hasVoted[1]});
2016-10-08 19:09:48 +03:00
});
};
Posts.getVoteStatusByPostIDs = function (pids, uid, callback) {
2016-10-08 19:09:48 +03:00
if (!parseInt(uid, 10)) {
var data = pids.map(function () { return false; });
2016-10-08 19:09:48 +03:00
return callback(null, {upvotes: data, downvotes: data});
}
var upvoteSets = [];
var downvoteSets = [];
for (var i = 0; i < pids.length; i += 1) {
2016-10-08 19:09:48 +03:00
upvoteSets.push('pid:' + pids[i] + ':upvote');
downvoteSets.push('pid:' + pids[i] + ':downvote');
}
async.parallel({
upvotes: function (next) {
2016-10-08 19:09:48 +03:00
db.isMemberOfSets(upvoteSets, uid, next);
},
downvotes: function (next) {
2016-10-08 19:09:48 +03:00
db.isMemberOfSets(downvoteSets, uid, next);
2017-02-17 19:31:21 -07:00
},
2016-10-08 19:09:48 +03:00
}, callback);
};
Posts.getUpvotedUidsByPids = function (pids, callback) {
var sets = pids.map(function (pid) {
2016-10-08 19:09:48 +03:00
return 'pid:' + pid + ':upvote';
});
db.getSetsMembers(sets, callback);
};
2015-07-03 14:10:40 +02:00
function voteInProgress(pid, uid) {
return Array.isArray(votesInProgress[uid]) && votesInProgress[uid].indexOf(parseInt(pid, 10)) !== -1;
}
function putVoteInProgress(pid, uid) {
votesInProgress[uid] = votesInProgress[uid] || [];
votesInProgress[uid].push(parseInt(pid, 10));
}
function clearVoteProgress(pid, uid) {
if (Array.isArray(votesInProgress[uid])) {
var index = votesInProgress[uid].indexOf(parseInt(pid, 10));
if (index !== -1) {
votesInProgress[uid].splice(index, 1);
}
}
}
function toggleVote(type, pid, uid, callback) {
unvote(pid, uid, type, function (err) {
2015-07-03 14:10:40 +02:00
if (err) {
2015-09-25 15:30:45 -04:00
return callback(err);
2015-07-03 14:10:40 +02:00
}
2015-09-25 15:30:45 -04:00
vote(type, false, pid, uid, callback);
2015-07-03 14:10:40 +02:00
});
}
function unvote(pid, uid, command, callback) {
async.parallel({
owner: function (next) {
2016-10-08 19:09:48 +03:00
Posts.getPostField(pid, 'uid', next);
2015-07-03 14:10:40 +02:00
},
voteStatus: function (next) {
2016-10-08 19:09:48 +03:00
Posts.hasVoted(pid, uid, next);
2015-12-10 09:00:46 +02:00
},
reputation: function (next) {
2015-12-10 09:00:46 +02:00
user.getUserField(uid, 'reputation', next);
2017-02-17 19:31:21 -07:00
},
}, function (err, results) {
2015-07-03 14:10:40 +02:00
if (err) {
return callback(err);
}
if (parseInt(uid, 10) === parseInt(results.owner, 10)) {
return callback(new Error('self-vote'));
2015-07-03 14:10:40 +02:00
}
2015-12-10 09:00:46 +02:00
if (command === 'downvote' && parseInt(results.reputation) < parseInt(meta.config['privileges:downvote'], 10)) {
return callback(new Error('[[error:not-enough-reputation-to-downvote]]'));
}
2017-02-17 20:20:42 -07:00
var voteStatus = results.voteStatus;
var hook;
var current = voteStatus.upvoted ? 'upvote' : 'downvote';
2015-07-03 14:10:40 +02:00
2017-02-18 01:51:11 -07:00
if ((voteStatus.upvoted && command === 'downvote') || (voteStatus.downvoted && command === 'upvote')) { // e.g. User *has* upvoted, and clicks downvote
2015-07-03 14:10:40 +02:00
hook = command;
2015-10-22 16:59:25 -04:00
} else if (voteStatus.upvoted || voteStatus.downvoted) { // e.g. User *has* upvoted, clicks upvote (so we "unvote")
2015-07-03 14:10:40 +02:00
hook = 'unvote';
} else { // e.g. User *has not* voted, clicks upvote
hook = command;
current = 'unvote';
}
plugins.fireHook('action:post.' + hook, {
pid: pid,
uid: uid,
owner: results.owner,
2017-02-17 19:31:21 -07:00
current: current,
2015-07-03 14:10:40 +02:00
});
if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) {
return callback();
}
vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, callback);
});
}
2016-10-08 19:09:48 +03:00
function vote(type, unvote, pid, uid, callback) {
uid = parseInt(uid, 10);
if (uid === 0) {
return callback(new Error('[[error:not-logged-in]]'));
2015-07-03 14:10:40 +02:00
}
2016-10-08 19:09:48 +03:00
Posts.getPostFields(pid, ['pid', 'uid', 'tid'], function (err, postData) {
2015-07-03 14:10:40 +02:00
if (err) {
return callback(err);
}
2016-10-08 19:09:48 +03:00
var now = Date.now();
2015-07-03 14:10:40 +02:00
2016-10-08 19:09:48 +03:00
if (type === 'upvote' && !unvote) {
db.sortedSetAdd('uid:' + uid + ':upvote', now, pid);
} else {
db.sortedSetRemove('uid:' + uid + ':upvote', pid);
}
2015-07-03 14:10:40 +02:00
2016-10-08 19:09:48 +03:00
if (type === 'upvote' || unvote) {
db.sortedSetRemove('uid:' + uid + ':downvote', pid);
} else {
db.sortedSetAdd('uid:' + uid + ':downvote', now, pid);
2015-07-03 14:10:40 +02:00
}
2016-10-08 19:09:48 +03:00
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, function (err, newreputation) {
if (err) {
return callback(err);
}
2015-07-03 14:10:40 +02:00
2016-10-08 19:09:48 +03:00
if (parseInt(postData.uid, 10)) {
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
}
2015-07-03 14:10:40 +02:00
adjustPostVotes(postData, uid, type, unvote, function (err) {
2016-10-08 19:09:48 +03:00
callback(err, {
user: {
2017-02-17 19:31:21 -07:00
reputation: newreputation,
2016-10-08 19:09:48 +03:00
},
post: postData,
upvote: type === 'upvote' && !unvote,
2017-02-17 19:31:21 -07:00
downvote: type === 'downvote' && !unvote,
2016-10-08 19:09:48 +03:00
});
});
});
});
}
2015-07-03 14:10:40 +02:00
2016-10-08 19:09:48 +03:00
function adjustPostVotes(postData, uid, type, unvote, callback) {
var notType = (type === 'upvote' ? 'downvote' : 'upvote');
async.series([
function (next) {
2016-10-08 19:09:48 +03:00
if (unvote) {
db.setRemove('pid:' + postData.pid + ':' + type, uid, next);
} else {
db.setAdd('pid:' + postData.pid + ':' + type, uid, next);
}
2015-07-03 14:10:40 +02:00
},
function (next) {
2016-10-08 19:09:48 +03:00
db.setRemove('pid:' + postData.pid + ':' + notType, uid, next);
2017-02-17 19:31:21 -07:00
},
], function (err) {
2015-07-03 14:10:40 +02:00
if (err) {
return callback(err);
}
2016-10-08 19:09:48 +03:00
async.parallel({
upvotes: function (next) {
2016-10-08 19:09:48 +03:00
db.setCount('pid:' + postData.pid + ':upvote', next);
2015-07-03 14:10:40 +02:00
},
downvotes: function (next) {
2016-10-08 19:09:48 +03:00
db.setCount('pid:' + postData.pid + ':downvote', next);
2017-02-17 19:31:21 -07:00
},
}, function (err, results) {
2015-07-03 14:10:40 +02:00
if (err) {
return callback(err);
}
2016-10-08 19:09:48 +03:00
postData.upvotes = parseInt(results.upvotes, 10);
postData.downvotes = parseInt(results.downvotes, 10);
postData.votes = postData.upvotes - postData.downvotes;
Posts.updatePostVoteCount(postData, callback);
2015-07-03 14:10:40 +02:00
});
});
}
2016-10-08 19:09:48 +03:00
};