Fix bad karma

This commit is contained in:
Julien Chaumond 2012-09-28 15:07:25 +02:00
parent b6e49111aa
commit 23fccbd082

View file

@ -9,9 +9,8 @@
Meteor.users.update({_id: userId}, {$inc: {karma: karma}}); Meteor.users.update({_id: userId}, {$inc: {karma: karma}});
}; };
var giveKarmaForPost = function(postId, karma){ var giveKarmaForItem = function(votedItem, karma){
var votedPost=Posts.findOne(postId); modifyKarma(votedItem.user_id, karma);
modifyKarma(votedPost.user_id, karma);
}; };
var upvote = function(collection, id) { var upvote = function(collection, id) {
@ -19,15 +18,21 @@
return false; return false;
var votePower=getVotePower(this.userId()); 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 // only modify the document if my userId isn't already in upvoters
collection.update({_id: id, upvoters: {$ne: this.userId()}}, collection.update({_id: id, upvoters: {$ne: this.userId()}},
{$push: {upvoters: this.userId()}, {$push: {upvoters: this.userId()},
$inc: {votes: 1, baseScore: votePower}}); $inc: {votes: 1, baseScore: votePower}});
// user's posts and comments do not impact his own karma: if (ack) {
if (id != this.userId) { var votedItem = collection.findOne(id);
giveKarmaForPost(id, votePower); // user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, votePower);
}
} }
if (!this.isSimulation) if (!this.isSimulation)
@ -42,14 +47,20 @@
var votePower=getVotePower(this.userId()); 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 // only modify the document if my userId isn't already in downvoters
collection.update({_id: id, downvoters: {$ne: this.userId()}}, collection.update({_id: id, downvoters: {$ne: this.userId()}},
{$push: {downvoters: this.userId()}, {$push: {downvoters: this.userId()},
$inc: {votes: -1, baseScore: -votePower}}); $inc: {votes: -1, baseScore: -votePower}});
// user's posts and comments do not impact his own karma: if (ack) {
if (id != this.userId) { var votedItem = collection.findOne(id);
giveKarmaForPost(id, -votePower); // user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, -votePower);
}
} }
if (!this.isSimulation) if (!this.isSimulation)
@ -63,15 +74,21 @@
return false; return false;
var votePower=getVotePower(this.userId()); 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 // only modify the document if i am a recorded voter
collection.update({_id: id, upvoters: this.userId()}, collection.update({_id: id, upvoters: this.userId()},
{$pull: {upvoters: this.userId()}, {$pull: {upvoters: this.userId()},
$inc: {votes: -1, baseScore: -votePower}}); $inc: {votes: -1, baseScore: -votePower}});
// user's posts and comments do not impact his own karma: if (ack) {
if (id != this.userId) { var votedItem = collection.findOne(id);
giveKarmaForPost(id, -votePower); // user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, -votePower);
}
} }
if (!this.isSimulation) if (!this.isSimulation)
@ -85,15 +102,21 @@
return false; return false;
var votePower=getVotePower(this.userId()); 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 // only modify the document if i am a recorded voter
collection.update({_id: id, downvoters: this.userId()}, collection.update({_id: id, downvoters: this.userId()},
{$pull: {downvoters: this.userId()}, {$pull: {downvoters: this.userId()},
$inc: {votes: 1, baseScore: votePower}}); $inc: {votes: 1, baseScore: votePower}});
// user's posts and comments do not impact his own karma: if (ack) {
if (id != this.userId) { var votedItem = collection.findOne(id);
giveKarmaForPost(id, votePower); // user's posts and comments do not impact his own karma:
if (votedItem.user_id != this.userId()) {
giveKarmaForItem(votedItem, votePower);
}
} }
if (!this.isSimulation) if (!this.isSimulation)