From b38c6cda3fab534374e0a45ac8904252e5d03d4c Mon Sep 17 00:00:00 2001 From: Tarang Patel Date: Mon, 11 Nov 2013 23:06:12 +0200 Subject: [PATCH] Fix downvoting, cancelling upvoting & cancelling downvoting Additionally removed the `(function() {})();` encapsulation since Meteor already adds this in since they started scoping files --- lib/vote.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/vote.js b/lib/vote.js index d4de04c11..9bd35e1f4 100644 --- a/lib/vote.js +++ b/lib/vote.js @@ -1,4 +1,4 @@ -(function() { + // returns how much "power" a user's votes have var getVotePower = function(user){ // return (user && user.isAdmin) ? 5 : 1; @@ -30,7 +30,7 @@ cancelDownvote(collection, item, user) // Votes & Score - var result = collection.update({_id: item._id},{ + var result = collection.update({_id: item && item._id},{ $addToSet: {upvoters: user._id}, $pull: {downvoters: user._id}, $inc: {votes: 1, baseScore: votePower}, @@ -71,7 +71,7 @@ cancelUpvote(collection, item, user) // Votes & Score - collection.update({_id: id},{ + collection.update({_id: item && item._id},{ $addToSet: {downvoters: user._id}, $pull: {upvoters: user._id}, $inc: {votes: -1, baseScore: -votePower}, @@ -98,7 +98,7 @@ return false; // Votes & Score - collection.update({_id: id},{ + collection.update({_id: item && item._id},{ $pull: {upvoters: user._id}, $inc: {votes: -1, baseScore: -votePower}, $set: {inactive: false} @@ -106,7 +106,7 @@ // extend item with baseScore to help calculate newScore item = _.extend(item, {baseScore: (item.baseScore + votePower)}); - updateScore(collection, id, true); + updateScore(item && item._id, collection, true); // if the item is being upvoted by its own author, don't give karma if (item.userId != user._id) @@ -124,7 +124,7 @@ return false; // Votes & Score - collection.update({_id: id},{ + collection.update({_id: item && item._id},{ $pull: {downvoters: user._id}, $inc: {votes: 1, baseScore: votePower}, $set: {inactive: false} @@ -132,7 +132,7 @@ // extend item with baseScore to help calculate newScore item = _.extend(item, {baseScore: (item.baseScore + votePower)}); - updateScore(collection, id, true); + updateScore(item && item._id, collection, true); // if the item is being upvoted by its own author, don't give karma if (item.userId != user._id) @@ -174,5 +174,3 @@ return cancelDownvote.call(this, Comments, comment); } }); - -})(); \ No newline at end of file