From 8c98f4768849b5ae3f756d29e15fa56269daf1e8 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 17 Nov 2014 19:24:41 -0500 Subject: [PATCH] action:post.upvote / action:post.downvote now sends current status; new: action:post.unvote --- src/favourites.js | 42 +++++++++++++++++++++++++++--------------- src/socket.io/posts.js | 2 +- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/favourites.js b/src/favourites.js index 42d1afd788..ce64e57781 100644 --- a/src/favourites.js +++ b/src/favourites.js @@ -122,7 +122,7 @@ var async = require('async'), }); } - Favourites.upvote = function(pid, uid, callback) { + Favourites.upvote = function(pid, uid, commmand, callback) { if (parseInt(meta.config['reputation:disabled'], 10) === 1) { return callback(new Error('[[error:reputation-system-disabled]]')); } @@ -131,16 +131,12 @@ var async = require('async'), if (err) { return callback(err); } - plugins.fireHook('action:post.upvote', { - pid: pid, - uid: uid - }); callback(null, votes); }); }; - Favourites.downvote = function(pid, uid, callback) { + Favourites.downvote = function(pid, uid, command, callback) { if (parseInt(meta.config['reputation:disabled'], 10) === 1) { return callback(new Error('[[error:reputation-system-disabled]]')); } @@ -162,17 +158,14 @@ var async = require('async'), if (err) { return callback(err); } - plugins.fireHook('action:post.downvote', { - pid: pid, - uid: uid - }); + callback(null, votes); }); }); }; function toggleVote(type, pid, uid, callback) { - Favourites.unvote(pid, uid, function(err) { + Favourites.unvote(pid, uid, type, function(err) { if (err) { return callback(err); } @@ -181,24 +174,43 @@ var async = require('async'), }); } - Favourites.unvote = function(pid, uid, callback) { + Favourites.unvote = function(pid, uid, command, callback) { Favourites.hasVoted(pid, uid, function(err, voteStatus) { if (err) { return callback(err); } + var hook, + current = voteStatus.upvoted ? 'upvote' : 'downvote'; + + if (voteStatus.upvoted && command === 'downvote' || voteStatus.downvoted && command === 'upvote') { + hook = command; + } else if (voteStatus.upvoted || voteStatus.downvoted) { + hook = 'unvote'; + } else { + hook = command; + current = 'unvote'; + } + + 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); }); - }; + } Favourites.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); @@ -231,11 +243,11 @@ var async = require('async'), }, callback); }; - Favourites.favourite = function (pid, uid, callback) { + Favourites.favourite = function (pid, uid, command, callback) { toggleFavourite('favourite', pid, uid, callback); }; - Favourites.unfavourite = function(pid, uid, callback) { + Favourites.unfavourite = function(pid, uid, command, callback) { toggleFavourite('unfavourite', pid, uid, callback); }; diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 6db4f4b762..275c46de0f 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -115,7 +115,7 @@ function favouriteCommand(command, eventName, socket, data, callback) { return callback(new Error('[[error:post-deleted]]')); } - favourites[command](data.pid, socket.uid, function(err, result) { + favourites[command](data.pid, socket.uid, command, function(err, result) { if (err) { return callback(err); }