2012-09-18 10:04:26 +10:00
|
|
|
(function() {
|
2012-09-27 13:13:55 -07:00
|
|
|
// returns how much "power" a user's votes have
|
2012-10-04 12:30:39 +10:00
|
|
|
var getVotePower = function(user){
|
2012-09-27 13:13:55 -07:00
|
|
|
return (user && user.isAdmin) ? 5 : 1;
|
2012-09-18 10:04:26 +10:00
|
|
|
};
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-10-04 12:33:15 +10:00
|
|
|
var modifyKarma = function(user_id, karma){
|
|
|
|
Meteor.users.update({_id: user_id}, {$inc: {karma: karma}});
|
2012-09-28 09:57:03 +09:00
|
|
|
};
|
|
|
|
|
2012-10-04 12:30:39 +10:00
|
|
|
var hasUpvotedItem= function(user, collection, id){
|
2012-09-29 13:51:55 +09:00
|
|
|
// see http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails
|
2012-09-29 14:14:57 +09:00
|
|
|
// 'is there an item with this id which contains this userId in its upvoters?'
|
|
|
|
// if such an item exists, it means we have voted.
|
2012-10-04 12:30:39 +10:00
|
|
|
return collection.findOne({_id: id, upvoters: user._id}) !== undefined;
|
2012-09-29 13:51:55 +09:00
|
|
|
}
|
|
|
|
|
2012-10-04 12:30:39 +10:00
|
|
|
var hasDownvotedItem= function(user, collection, id){
|
|
|
|
return collection.findOne({_id: id, downvoters: user._id}) !== undefined;
|
2012-09-29 13:51:55 +09:00
|
|
|
}
|
|
|
|
|
2012-09-18 10:04:26 +10:00
|
|
|
var upvote = function(collection, id) {
|
2012-10-04 12:30:39 +10:00
|
|
|
var user = Meteor.user();
|
2012-10-05 22:46:20 +09:00
|
|
|
if (!user || !canUpvote(user, collection, true) || hasUpvotedItem(user, collection, id))
|
2012-09-27 13:13:55 -07:00
|
|
|
return false;
|
|
|
|
|
2012-10-04 12:30:39 +10:00
|
|
|
var votePower=getVotePower(user);
|
2012-09-29 13:54:47 +09:00
|
|
|
var votedItem = collection.findOne(id);
|
2012-09-29 13:51:55 +09:00
|
|
|
|
|
|
|
// Votes & Score
|
2012-09-29 14:14:57 +09:00
|
|
|
collection.update({_id: id},
|
2012-10-04 12:30:39 +10:00
|
|
|
{$addToSet: {upvoters: user._id},
|
2012-09-28 09:57:03 +09:00
|
|
|
$inc: {votes: 1, baseScore: votePower}});
|
2012-09-29 14:14:57 +09:00
|
|
|
if(!this.isSimulation)
|
|
|
|
updateScore(collection, id);
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-09-29 13:54:47 +09:00
|
|
|
// Karma
|
2012-09-29 13:51:55 +09:00
|
|
|
// user's posts and comments do not impact his own karma:
|
2012-10-04 12:30:39 +10:00
|
|
|
if (votedItem.user_id != user._id) {
|
2012-09-29 14:14:57 +09:00
|
|
|
modifyKarma(votedItem.user_id, votePower);
|
2012-09-29 13:51:55 +09:00
|
|
|
}
|
|
|
|
|
2012-09-27 13:13:55 -07:00
|
|
|
return true;
|
2012-09-18 10:04:26 +10:00
|
|
|
};
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-09-18 10:04:26 +10:00
|
|
|
var downvote = function(collection, id) {
|
2012-10-04 12:30:39 +10:00
|
|
|
var user = Meteor.user();
|
2012-10-05 22:46:20 +09:00
|
|
|
if (! user || !canDownvote(user, collection, 'redirect') || hasDownvotedItem(user, collection, id))
|
2012-09-27 13:13:55 -07:00
|
|
|
return false;
|
2012-10-04 12:30:39 +10:00
|
|
|
|
|
|
|
var votePower=getVotePower(user);
|
2012-09-29 13:54:47 +09:00
|
|
|
var votedItem = collection.findOne(id);
|
2012-09-29 13:51:55 +09:00
|
|
|
|
2012-09-29 13:54:47 +09:00
|
|
|
// Votes & Score
|
2012-09-29 14:14:57 +09:00
|
|
|
collection.update({_id: id},
|
2012-10-04 12:30:39 +10:00
|
|
|
{$addToSet: {downvoters: user._id},
|
2012-09-28 09:57:03 +09:00
|
|
|
$inc: {votes: -1, baseScore: -votePower}});
|
2012-09-29 14:14:57 +09:00
|
|
|
if(!this.isSimulation)
|
|
|
|
updateScore(collection, id);
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-09-29 13:51:55 +09:00
|
|
|
// Karma
|
|
|
|
// user's posts and comments do not impact his own karma:
|
2012-10-04 12:30:39 +10:00
|
|
|
if (votedItem.user_id != user._id) {
|
2012-09-29 14:14:57 +09:00
|
|
|
modifyKarma(votedItem.user_id, -votePower);
|
2012-09-29 13:51:55 +09:00
|
|
|
}
|
|
|
|
|
2012-09-27 13:13:55 -07:00
|
|
|
return true;
|
2012-09-18 10:04:26 +10:00
|
|
|
};
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-09-18 10:04:26 +10:00
|
|
|
var cancelUpvote = function(collection, id) {
|
2012-10-04 12:30:39 +10:00
|
|
|
var user = Meteor.user();
|
2012-10-05 22:46:20 +09:00
|
|
|
if (! user || !canUpvote(user, collection, 'redirect') || ! hasUpvotedItem(user, collection, id))
|
2012-10-04 12:30:39 +10:00
|
|
|
return false
|
|
|
|
|
|
|
|
var votePower=getVotePower(user);
|
2012-09-29 13:54:47 +09:00
|
|
|
var votedItem = collection.findOne(id);
|
|
|
|
|
2012-09-29 13:51:55 +09:00
|
|
|
// Votes & Score
|
2012-09-29 14:14:57 +09:00
|
|
|
collection.update({_id: id},
|
2012-10-04 12:30:39 +10:00
|
|
|
{$pull: {upvoters: user._id},
|
2012-09-28 09:57:03 +09:00
|
|
|
$inc: {votes: -1, baseScore: -votePower}});
|
2012-09-29 14:14:57 +09:00
|
|
|
if(!this.isSimulation)
|
|
|
|
updateScore(collection, id);
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-09-29 13:51:55 +09:00
|
|
|
// Karma
|
|
|
|
// user's posts and comments do not impact his own karma:
|
2012-10-04 12:30:39 +10:00
|
|
|
if (votedItem.user_id != user._id) {
|
2012-09-29 14:14:57 +09:00
|
|
|
modifyKarma(votedItem.user_id, -votePower);
|
2012-09-29 13:51:55 +09:00
|
|
|
}
|
|
|
|
|
2012-09-27 13:13:55 -07:00
|
|
|
return true;
|
2012-09-18 10:04:26 +10:00
|
|
|
};
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-09-18 10:04:26 +10:00
|
|
|
var cancelDownvote = function(collection, id) {
|
2012-10-04 12:30:39 +10:00
|
|
|
var user = Meteor.user();
|
2012-10-05 22:46:20 +09:00
|
|
|
if (! user || !canDownvote(user, collection, 'redirect') || ! hasDownvotedItem(user, collection, id))
|
2012-10-04 12:30:39 +10:00
|
|
|
return false
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-10-04 12:30:39 +10:00
|
|
|
var votePower=getVotePower(user);
|
2012-09-29 13:54:47 +09:00
|
|
|
var votedItem = collection.findOne(id);
|
2012-09-28 15:07:25 +02:00
|
|
|
|
2012-09-29 13:51:55 +09:00
|
|
|
// Votes & Score
|
2012-09-29 14:14:57 +09:00
|
|
|
collection.update({_id: id},
|
2012-10-04 12:30:39 +10:00
|
|
|
{$pull: {downvoters: user._id},
|
2012-09-28 09:57:03 +09:00
|
|
|
$inc: {votes: 1, baseScore: votePower}});
|
2012-09-29 14:14:57 +09:00
|
|
|
if(!this.isSimulation)
|
|
|
|
updateScore(collection, id);
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-09-29 13:51:55 +09:00
|
|
|
// Karma
|
|
|
|
// user's posts and comments do not impact his own karma:
|
2012-10-04 12:30:39 +10:00
|
|
|
if (votedItem.user_id != user._id) {
|
2012-09-29 14:14:57 +09:00
|
|
|
modifyKarma(votedItem.user_id, votePower);
|
2012-09-29 13:51:55 +09:00
|
|
|
}
|
|
|
|
|
2012-09-27 13:13:55 -07:00
|
|
|
return true;
|
2012-09-18 10:04:26 +10:00
|
|
|
};
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2012-09-18 10:04:26 +10:00
|
|
|
Meteor.methods({
|
|
|
|
upvotePost: function(postId){
|
|
|
|
return upvote.call(this, Posts, postId);
|
|
|
|
},
|
|
|
|
downvotePost: function(postId){
|
|
|
|
return downvote.call(this, Posts, postId);
|
|
|
|
},
|
|
|
|
cancelUpvotePost: function(postId){
|
|
|
|
return cancelUpvote.call(this, Posts, postId);
|
|
|
|
},
|
|
|
|
cancelDownvotePost: function(postId){
|
|
|
|
return cancelDownvote.call(this, Posts, postId);
|
|
|
|
},
|
2012-09-27 16:03:27 -07:00
|
|
|
|
2012-09-18 10:04:26 +10:00
|
|
|
upvoteComment: function(commentId){
|
|
|
|
return upvote.call(this, Comments, commentId);
|
|
|
|
},
|
|
|
|
downvoteComment: function(commentId){
|
|
|
|
return downvote.call(this, Comments, commentId);
|
|
|
|
},
|
|
|
|
cancelUpvoteComment: function(commentId){
|
|
|
|
return cancelUpvote.call(this, Comments, commentId);
|
|
|
|
},
|
|
|
|
cancelDownvoteComment: function(commentId){
|
|
|
|
return cancelDownvote.call(this, Comments, commentId);
|
2012-09-18 09:23:42 +10:00
|
|
|
}
|
2012-09-18 10:04:26 +10:00
|
|
|
});
|
|
|
|
|
2012-09-28 09:40:34 +09:00
|
|
|
})();
|