From 23fccbd082fc46c5a0730fa52d2381975402007a Mon Sep 17 00:00:00 2001 From: Julien Chaumond Date: Fri, 28 Sep 2012 15:07:25 +0200 Subject: [PATCH] Fix bad karma --- lib/vote.js | 59 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/lib/vote.js b/lib/vote.js index d99857c4a..43f38629e 100644 --- a/lib/vote.js +++ b/lib/vote.js @@ -9,9 +9,8 @@ Meteor.users.update({_id: userId}, {$inc: {karma: karma}}); }; - var giveKarmaForPost = function(postId, karma){ - var votedPost=Posts.findOne(postId); - modifyKarma(votedPost.user_id, karma); + var giveKarmaForItem = function(votedItem, karma){ + modifyKarma(votedItem.user_id, karma); }; var upvote = function(collection, id) { @@ -19,15 +18,21 @@ return false; var votePower=getVotePower(this.userId()); + + // Is my userId already in up/downvoters? + var ack = (collection.findOne({_id: id, upvoters: {$ne: this.userId()}}) !== undefined); // only modify the document if my userId isn't already in upvoters collection.update({_id: id, upvoters: {$ne: this.userId()}}, {$push: {upvoters: this.userId()}, $inc: {votes: 1, baseScore: votePower}}); - - // user's posts and comments do not impact his own karma: - if (id != this.userId) { - giveKarmaForPost(id, votePower); + + if (ack) { + var votedItem = collection.findOne(id); + // user's posts and comments do not impact his own karma: + if (votedItem.user_id != this.userId()) { + giveKarmaForItem(votedItem, votePower); + } } if (!this.isSimulation) @@ -42,14 +47,20 @@ var votePower=getVotePower(this.userId()); + // Is my userId already in up/downvoters? + var ack = (collection.findOne({_id: id, downvoters: {$ne: this.userId()}}) !== undefined); + // only modify the document if my userId isn't already in downvoters collection.update({_id: id, downvoters: {$ne: this.userId()}}, {$push: {downvoters: this.userId()}, $inc: {votes: -1, baseScore: -votePower}}); - // user's posts and comments do not impact his own karma: - if (id != this.userId) { - giveKarmaForPost(id, -votePower); + if (ack) { + var votedItem = collection.findOne(id); + // user's posts and comments do not impact his own karma: + if (votedItem.user_id != this.userId()) { + giveKarmaForItem(votedItem, -votePower); + } } if (!this.isSimulation) @@ -63,15 +74,21 @@ return false; var votePower=getVotePower(this.userId()); - + + // Is my userId already in up/downvoters? + var ack = (collection.findOne({_id: id, upvoters: {$ne: this.userId()}}) !== undefined); + // only modify the document if i am a recorded voter collection.update({_id: id, upvoters: this.userId()}, {$pull: {upvoters: this.userId()}, $inc: {votes: -1, baseScore: -votePower}}); - // user's posts and comments do not impact his own karma: - if (id != this.userId) { - giveKarmaForPost(id, -votePower); + if (ack) { + var votedItem = collection.findOne(id); + // user's posts and comments do not impact his own karma: + if (votedItem.user_id != this.userId()) { + giveKarmaForItem(votedItem, -votePower); + } } if (!this.isSimulation) @@ -85,15 +102,21 @@ return false; var votePower=getVotePower(this.userId()); - + + // Is my userId already in up/downvoters? + var ack = (collection.findOne({_id: id, downvoters: {$ne: this.userId()}}) !== undefined); + // only modify the document if i am a recorded voter collection.update({_id: id, downvoters: this.userId()}, {$pull: {downvoters: this.userId()}, $inc: {votes: 1, baseScore: votePower}}); - // user's posts and comments do not impact his own karma: - if (id != this.userId) { - giveKarmaForPost(id, votePower); + if (ack) { + var votedItem = collection.findOne(id); + // user's posts and comments do not impact his own karma: + if (votedItem.user_id != this.userId()) { + giveKarmaForItem(votedItem, votePower); + } } if (!this.isSimulation)