2016-10-08 19:09:48 +03:00
|
|
|
'use strict';
|
2015-07-03 14:10:40 +02:00
|
|
|
|
2016-01-14 13:47:26 +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');
|
2019-07-10 14:29:11 -04:00
|
|
|
var topics = require('../topics');
|
2016-10-08 19:09:48 +03:00
|
|
|
var plugins = require('../plugins');
|
2018-01-09 14:33:23 -05:00
|
|
|
var privileges = require('../privileges');
|
2015-07-03 14:10:40 +02:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Posts) {
|
2016-10-08 19:09:48 +03:00
|
|
|
var votesInProgress = {};
|
2015-07-03 14:10:40 +02:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.upvote = function (pid, uid, callback) {
|
2018-10-21 16:47:51 -04:00
|
|
|
if (meta.config['reputation:disabled']) {
|
2015-07-03 14:10:40 +02:00
|
|
|
return callback(new Error('[[error:reputation-system-disabled]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 14:33:23 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
privileges.posts.can('posts:upvote', pid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (canUpvote, next) {
|
|
|
|
|
if (!canUpvote) {
|
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
2015-09-25 15:30:45 -04:00
|
|
|
|
2018-01-09 14:33:23 -05:00
|
|
|
if (voteInProgress(pid, uid)) {
|
|
|
|
|
return next(new Error('[[error:already-voting-for-this-post]]'));
|
|
|
|
|
}
|
2015-09-25 15:30:45 -04:00
|
|
|
|
2018-01-09 14:33:23 -05:00
|
|
|
putVoteInProgress(pid, uid);
|
|
|
|
|
|
|
|
|
|
toggleVote('upvote', pid, uid, function (err, data) {
|
|
|
|
|
clearVoteProgress(pid, uid);
|
|
|
|
|
next(err, data);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2015-07-03 14:10:40 +02:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.downvote = function (pid, uid, callback) {
|
2018-10-21 16:47:51 -04:00
|
|
|
if (meta.config['reputation:disabled']) {
|
2015-07-03 14:10:40 +02:00
|
|
|
return callback(new Error('[[error:reputation-system-disabled]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-21 16:47:51 -04:00
|
|
|
if (meta.config['downvote:disabled']) {
|
2015-07-03 14:10:40 +02:00
|
|
|
return callback(new Error('[[error:downvoting-disabled]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 14:33:23 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
privileges.posts.can('posts:downvote', pid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (canUpvote, next) {
|
|
|
|
|
if (!canUpvote) {
|
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
2015-09-25 15:30:45 -04:00
|
|
|
|
2018-01-09 14:33:23 -05:00
|
|
|
if (voteInProgress(pid, uid)) {
|
|
|
|
|
return next(new Error('[[error:already-voting-for-this-post]]'));
|
|
|
|
|
}
|
2015-09-25 15:30:45 -04:00
|
|
|
|
2018-01-09 14:33:23 -05:00
|
|
|
putVoteInProgress(pid, uid);
|
|
|
|
|
|
|
|
|
|
toggleVote('downvote', pid, uid, function (err, data) {
|
|
|
|
|
clearVoteProgress(pid, uid);
|
|
|
|
|
next(err, data);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2015-09-25 15:30:45 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02: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);
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.hasVoted = function (pid, uid, callback) {
|
2018-11-12 00:20:44 -05:00
|
|
|
if (parseInt(uid, 10) <= 0) {
|
|
|
|
|
return setImmediate(callback, null, { upvoted: false, downvoted: false });
|
2016-10-08 19:09:48 +03:00
|
|
|
}
|
2017-05-25 16:40:03 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (hasVoted, next) {
|
|
|
|
|
next(null, { upvoted: hasVoted[0], downvoted: hasVoted[1] });
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2016-10-08 19:09:48 +03:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Posts.getVoteStatusByPostIDs = function (pids, uid, callback) {
|
2018-11-12 00:20:44 -05:00
|
|
|
if (parseInt(uid, 10) <= 0) {
|
|
|
|
|
var data = pids.map(() => false);
|
|
|
|
|
return setImmediate(callback, null, { upvotes: data, downvotes: data });
|
2016-10-08 19:09:48 +03:00
|
|
|
}
|
|
|
|
|
var upvoteSets = [];
|
|
|
|
|
var downvoteSets = [];
|
|
|
|
|
|
2017-02-18 01:12:18 -07:00
|
|
|
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');
|
|
|
|
|
}
|
2018-11-12 00:20:44 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.isMemberOfSets(upvoteSets.concat(downvoteSets), uid, next);
|
2016-10-08 19:09:48 +03:00
|
|
|
},
|
2018-11-12 00:20:44 -05:00
|
|
|
function (data, next) {
|
|
|
|
|
next(null, {
|
|
|
|
|
upvotes: data.slice(0, pids.length),
|
|
|
|
|
downvotes: data.slice(pids.length, pids.length * 2),
|
|
|
|
|
});
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2018-11-12 00:20:44 -05:00
|
|
|
], callback);
|
2016-10-08 19:09:48 +03:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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) {
|
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])) {
|
|
|
|
|
var index = votesInProgress[uid].indexOf(parseInt(pid, 10));
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
votesInProgress[uid].splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toggleVote(type, pid, uid, callback) {
|
2017-05-25 16:40:03 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
unvote(pid, uid, type, function (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
vote(type, false, pid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2015-07-03 14:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unvote(pid, uid, command, callback) {
|
2017-05-25 16:40:03 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
async.parallel({
|
|
|
|
|
owner: function (next) {
|
|
|
|
|
Posts.getPostField(pid, 'uid', next);
|
|
|
|
|
},
|
|
|
|
|
voteStatus: function (next) {
|
|
|
|
|
Posts.hasVoted(pid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
reputation: function (next) {
|
|
|
|
|
user.getUserField(uid, 'reputation', next);
|
|
|
|
|
},
|
|
|
|
|
}, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2017-05-25 16:40:03 -04:00
|
|
|
function (results, next) {
|
|
|
|
|
if (parseInt(uid, 10) === parseInt(results.owner, 10)) {
|
2017-11-22 09:50:58 +05:00
|
|
|
return callback(new Error('[[error:self-vote]]'));
|
2017-05-25 16:40:03 -04:00
|
|
|
}
|
2015-07-03 14:10:40 +02:00
|
|
|
|
2018-10-21 16:47:51 -04:00
|
|
|
if (command === 'downvote' && results.reputation < meta.config['min:rep:downvote']) {
|
2017-05-25 16:40:03 -04:00
|
|
|
return callback(new Error('[[error:not-enough-reputation-to-downvote]]'));
|
|
|
|
|
}
|
2015-12-10 09:00:46 +02:00
|
|
|
|
2017-05-25 16:40:03 -04:00
|
|
|
var voteStatus = results.voteStatus;
|
|
|
|
|
var hook;
|
|
|
|
|
var 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';
|
|
|
|
|
}
|
2015-07-03 14:10:40 +02:00
|
|
|
|
2017-05-25 16:40:03 -04:00
|
|
|
plugins.fireHook('action:post.' + hook, {
|
|
|
|
|
pid: pid,
|
|
|
|
|
uid: uid,
|
|
|
|
|
owner: results.owner,
|
|
|
|
|
current: current,
|
|
|
|
|
});
|
2015-07-03 14:10:40 +02:00
|
|
|
|
2017-05-25 16:40:03 -04:00
|
|
|
if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2015-07-03 14:10:40 +02:00
|
|
|
|
2017-05-25 16:40:03 -04:00
|
|
|
vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2015-07-03 14:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-08 19:09:48 +03:00
|
|
|
function vote(type, unvote, pid, uid, callback) {
|
|
|
|
|
uid = parseInt(uid, 10);
|
|
|
|
|
|
2018-11-17 22:31:39 -05:00
|
|
|
if (uid <= 0) {
|
2016-10-08 19:09:48 +03:00
|
|
|
return callback(new Error('[[error:not-logged-in]]'));
|
2015-07-03 14:10:40 +02:00
|
|
|
}
|
2017-05-25 16:40:03 -04:00
|
|
|
var postData;
|
|
|
|
|
var newreputation;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
Posts.getPostFields(pid, ['pid', 'uid', 'tid'], next);
|
|
|
|
|
},
|
|
|
|
|
function (_postData, next) {
|
|
|
|
|
postData = _postData;
|
|
|
|
|
var now = Date.now();
|
2015-07-03 14:10:40 +02:00
|
|
|
|
2017-05-25 16:40:03 -04: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
|
|
|
|
2017-05-25 16:40:03 -04:00
|
|
|
if (type === 'upvote' || unvote) {
|
|
|
|
|
db.sortedSetRemove('uid:' + uid + ':downvote', pid);
|
|
|
|
|
} else {
|
|
|
|
|
db.sortedSetAdd('uid:' + uid + ':downvote', now, pid);
|
2016-10-08 19:09:48 +03:00
|
|
|
}
|
2015-07-03 14:10:40 +02:00
|
|
|
|
2017-05-25 16:40:03 -04:00
|
|
|
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, next);
|
|
|
|
|
},
|
|
|
|
|
function (_newreputation, next) {
|
|
|
|
|
newreputation = _newreputation;
|
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
|
|
|
|
2017-05-25 16:40:03 -04:00
|
|
|
adjustPostVotes(postData, uid, type, unvote, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
next(null, {
|
|
|
|
|
user: {
|
|
|
|
|
reputation: newreputation,
|
|
|
|
|
},
|
2017-11-21 12:14:14 -07:00
|
|
|
fromuid: uid,
|
2017-05-25 16:40:03 -04:00
|
|
|
post: postData,
|
|
|
|
|
upvote: type === 'upvote' && !unvote,
|
|
|
|
|
downvote: type === 'downvote' && !unvote,
|
2016-10-08 19:09:48 +03:00
|
|
|
});
|
2017-05-25 16:40:03 -04:00
|
|
|
},
|
|
|
|
|
], callback);
|
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');
|
2017-05-25 16:40:03 -04:00
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2017-05-25 16:40:03 -04:00
|
|
|
async.series([
|
|
|
|
|
function (next) {
|
|
|
|
|
if (unvote) {
|
|
|
|
|
db.setRemove('pid:' + postData.pid + ':' + type, uid, next);
|
|
|
|
|
} else {
|
|
|
|
|
db.setAdd('pid:' + postData.pid + ':' + type, uid, next);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
db.setRemove('pid:' + postData.pid + ':' + notType, uid, next);
|
|
|
|
|
},
|
|
|
|
|
], function (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
});
|
2015-07-03 14:10:40 +02:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2017-05-25 16:40:03 -04:00
|
|
|
async.parallel({
|
|
|
|
|
upvotes: function (next) {
|
|
|
|
|
db.setCount('pid:' + postData.pid + ':upvote', next);
|
|
|
|
|
},
|
|
|
|
|
downvotes: function (next) {
|
|
|
|
|
db.setCount('pid:' + postData.pid + ':downvote', next);
|
|
|
|
|
},
|
|
|
|
|
}, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2017-05-25 16:40:03 -04:00
|
|
|
function (results, next) {
|
2018-10-25 17:02:59 -04:00
|
|
|
postData.upvotes = results.upvotes;
|
|
|
|
|
postData.downvotes = results.downvotes;
|
2016-10-08 19:09:48 +03:00
|
|
|
postData.votes = postData.upvotes - postData.downvotes;
|
2017-05-25 16:40:03 -04:00
|
|
|
Posts.updatePostVoteCount(postData, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
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,
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
};
|