From f2f9000b70dda2b80d2884bdfc556c01068129ce Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 25 Sep 2015 15:30:45 -0400 Subject: [PATCH] fix vote progress --- src/favourites.js | 53 ++++++++++++++++++++----------- src/socket.io/posts/favourites.js | 5 ++- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/favourites.js b/src/favourites.js index 5a8aaf7b85..137c47464e 100644 --- a/src/favourites.js +++ b/src/favourites.js @@ -106,7 +106,16 @@ var async = require('async'), return callback(new Error('[[error:reputation-system-disabled]]')); } - toggleVote('upvote', pid, uid, callback); + 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); + }); }; Favourites.downvote = function(pid, uid, callback) { @@ -118,6 +127,12 @@ var async = require('async'), return callback(new Error('[[error:downvoting-disabled]]')); } + if (voteInProgress(pid, uid)) { + return callback(new Error('[[error:already-voting-for-this-post]]')); + } + + putVoteInProgress(pid, uid); + user.getUserField(uid, 'reputation', function(err, reputation) { if (err) { return callback(err); @@ -127,7 +142,23 @@ var async = require('async'), return callback(new Error('[[error:not-enough-reputation-to-downvote]]')); } - toggleVote('downvote', pid, uid, callback); + toggleVote('downvote', pid, uid, function(err, data) { + clearVoteProgress(pid, uid); + callback(err, data); + }); + }); + }; + + Favourites.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); }); }; @@ -150,29 +181,15 @@ var async = require('async'), } function toggleVote(type, pid, uid, callback) { - function done(err, data) { - clearVoteProgress(pid, uid); - callback(err, data); - } - - if (voteInProgress(pid, uid)) { - return callback(new Error('[[error:already-voting-for-this-post]]')); - } - putVoteInProgress(pid, uid); - unvote(pid, uid, type, function(err) { if (err) { - return done(err); + return callback(err); } - vote(type, false, pid, uid, done); + vote(type, false, pid, uid, callback); }); } - Favourites.unvote = function(pid, uid, callback) { - unvote(pid, uid, 'unvote', callback); - }; - function unvote(pid, uid, command, callback) { async.parallel({ owner: function(next) { diff --git a/src/socket.io/posts/favourites.js b/src/socket.io/posts/favourites.js index f29ff25f50..e24564bb1d 100644 --- a/src/socket.io/posts/favourites.js +++ b/src/socket.io/posts/favourites.js @@ -142,13 +142,12 @@ module.exports = function(SocketPosts) { return callback(err); } - socket.emit('posts.' + command, result); - if (result && eventName) { + socket.emit('posts.' + command, result); websockets.in(data.room_id).emit('event:' + eventName, result); } - if (notification) { + if (result && notification) { SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, notification); } callback();