'use strict'; var async = require('async'); var meta = require('../meta'); var db = require('../database'); var user = require('../user'); var plugins = require('../plugins'); module.exports = function(Posts) { var votesInProgress = {}; Posts.upvote = function(pid, uid, callback) { if (parseInt(meta.config['reputation:disabled'], 10) === 1) { return callback(new Error('[[error:reputation-system-disabled]]')); } if (voteInProgress(pid, uid)) { return callback(new Error('[[error:already-voting-for-this-post]]')); } putVoteInProgress(pid, uid); toggleVote('upvote', pid, uid, function(err, data) { clearVoteProgress(pid, uid); callback(err, data); }); }; Posts.downvote = function(pid, uid, callback) { 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]]')); } if (voteInProgress(pid, uid)) { return callback(new Error('[[error:already-voting-for-this-post]]')); } putVoteInProgress(pid, uid); toggleVote('downvote', pid, uid, function(err, data) { clearVoteProgress(pid, uid); callback(err, data); }); }; Posts.unvote = function(pid, uid, callback) { if (voteInProgress(pid, uid)) { return callback(new Error('[[error:already-voting-for-this-post]]')); } putVoteInProgress(pid, uid); unvote(pid, uid, 'unvote', function(err, data) { clearVoteProgress(pid, uid); callback(err, data); }); }; Posts.hasVoted = function(pid, uid, callback) { if (!parseInt(uid, 10)) { return callback(null, {upvoted: false, downvoted: false}); } db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, function(err, hasVoted) { if (err) { return callback(err); } callback (null, {upvoted: hasVoted[0], downvoted: hasVoted[1]}); }); }; Posts.getVoteStatusByPostIDs = function(pids, uid, callback) { if (!parseInt(uid, 10)) { var data = pids.map(function() { return false; }); return callback(null, {upvotes: data, downvotes: data}); } var upvoteSets = []; var downvoteSets = []; for (var i=0; i