diff --git a/src/favourites.js b/src/favourites.js index 11d6ba6721..4f5ff90274 100644 --- a/src/favourites.js +++ b/src/favourites.js @@ -17,15 +17,11 @@ var async = require('async'), return callback(new Error('[[error:not-logged-in]]')); } - posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function (err, postData) { + posts.getPostFields(pid, ['pid', 'uid'], function (err, postData) { if (err) { return callback(err); } - if (uid === parseInt(postData.uid, 10)) { - return callback(new Error('[[error:cant-vote-self-post]]')); - } - var now = Date.now(); if(type === 'upvote' && !unvote) { @@ -167,16 +163,24 @@ var async = require('async'), }; function unvote(pid, uid, command, callback) { - Favourites.hasVoted(pid, uid, function(err, voteStatus) { + async.parallel({ + owner: function(next) { + posts.getPostField(pid, 'uid', next); + }, + voteStatus: function(next) { + Favourites.hasVoted(pid, uid, next); + } + }, function(err, results) { if (err) { return callback(err); } - if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) { - return callback(); + if (parseInt(uid, 10) === parseInt(results.owner, 10)) { + return callback(new Error('[[error:cant-vote-self-post]]')); } - var hook, + var voteStatus = results.voteStatus, + hook, current = voteStatus.upvoted ? 'upvote' : 'downvote'; if (voteStatus.upvoted && command === 'downvote' || voteStatus.downvoted && command === 'upvote') { @@ -188,17 +192,17 @@ var async = require('async'), current = 'unvote'; } - vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, function(err, data) { - if (err) { - return callback(err); - } - plugins.fireHook('action:post.' + hook, { - pid: pid, - uid: uid, - current: current - }); - callback(null, data); + plugins.fireHook('action:post.' + hook, { + pid: pid, + uid: uid, + current: current }); + + if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) { + return callback(); + } + + vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, callback); }); }