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
|
|
|
|
2013-10-29 13:39:28 +09:00
|
|
|
var modifyKarma = function(userId, karma){
|
|
|
|
Meteor.users.update({_id: userId}, {$inc: {karma: karma}});
|
2012-09-28 09:57:03 +09:00
|
|
|
};
|
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
var hasUpvotedItem= function(item, user){
|
|
|
|
return item.upvoters && item.upvoters.indexOf(user._id) != -1
|
2012-09-29 13:51:55 +09:00
|
|
|
}
|
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
var hasDownvotedItem= function(item, user){
|
|
|
|
return item.downvoters && item.downvoters.indexOf(user._id) != -1
|
2012-09-29 13:51:55 +09:00
|
|
|
}
|
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
var upvoteItem = function(collection, item) {
|
|
|
|
var user = Meteor.user(),
|
|
|
|
votePower = getVotePower(user);
|
2013-01-15 08:46:00 +09:00
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
// make sure user has rights to upvote first
|
2013-10-29 15:47:20 +09:00
|
|
|
if (!user || !canUpvote(user, collection, true) || hasUpvotedItem(item, user))
|
2012-09-27 13:13:55 -07:00
|
|
|
return false;
|
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
// if user is upvoting a previously downvoted item, cancel downvote first
|
|
|
|
if (hasDownvotedItem(item, user))
|
|
|
|
cancelDownvote(collection, item, user)
|
2012-09-29 13:51:55 +09:00
|
|
|
|
|
|
|
// Votes & Score
|
2013-10-29 15:06:50 +09:00
|
|
|
collection.update({_id: item._id},{
|
2012-12-24 10:59:13 +01:00
|
|
|
$addToSet: {upvoters: user._id},
|
2013-05-19 16:26:42 -04:00
|
|
|
$pull: {downvoters: user._id},
|
2013-10-29 15:06:50 +09:00
|
|
|
$inc: {votes: 1, baseScore: votePower},
|
2012-12-24 10:59:13 +01:00
|
|
|
$set: {inactive: false}
|
|
|
|
});
|
2013-10-29 13:30:35 +09:00
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
updateScore(item, collection, true);
|
2013-10-29 13:39:28 +09:00
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
// if the item is being upvoted by its own author, don't give karma
|
|
|
|
if (item.userId != user._id)
|
|
|
|
modifyKarma(item.userId, 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
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
var downvoteItem = function(collection, item) {
|
|
|
|
var user = Meteor.user(),
|
|
|
|
votePower = getVotePower(user);
|
2013-01-15 08:46:00 +09:00
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
// make sure user has rights to downvote first
|
2013-10-29 15:47:20 +09:00
|
|
|
if (!user || !canDownvote(user, collection, true) || hasDownvotedItem(item, user))
|
2012-09-27 13:13:55 -07:00
|
|
|
return false;
|
2013-05-19 16:26:42 -04:00
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
// if user is downvoting a previously upvoted item, cancel upvote first
|
|
|
|
if (hasUpvotedItem(item, user))
|
|
|
|
cancelUpvote(collection, item, user)
|
2012-09-29 13:51:55 +09:00
|
|
|
|
2012-09-29 13:54:47 +09:00
|
|
|
// Votes & Score
|
2012-12-24 10:59:13 +01:00
|
|
|
collection.update({_id: id},{
|
|
|
|
$addToSet: {downvoters: user._id},
|
2013-05-19 16:26:42 -04:00
|
|
|
$pull: {upvoters: user._id},
|
2013-10-29 15:06:50 +09:00
|
|
|
$inc: {votes: -1, baseScore: -votePower},
|
2012-12-24 10:59:13 +01:00
|
|
|
$set: {inactive: false}
|
|
|
|
});
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
updateScore(item, collection, true);
|
|
|
|
|
|
|
|
// if the item is being upvoted by its own author, don't give karma
|
|
|
|
if (item.userId != user._id)
|
|
|
|
modifyKarma(item.userId, 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
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
var cancelUpvote = function(collection, item) {
|
|
|
|
var user = Meteor.user();
|
|
|
|
votePower = getVotePower(user);
|
|
|
|
|
|
|
|
// if user isn't among the upvoters, abort
|
|
|
|
if(!hasUpvotedItem(item, user))
|
|
|
|
return false;
|
2013-01-15 08:46:00 +09:00
|
|
|
|
2012-09-29 13:51:55 +09:00
|
|
|
// Votes & Score
|
2012-12-24 10:59:13 +01:00
|
|
|
collection.update({_id: id},{
|
|
|
|
$pull: {upvoters: user._id},
|
|
|
|
$inc: {votes: -1, baseScore: -votePower},
|
|
|
|
$set: {inactive: false}
|
|
|
|
});
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
updateScore(collection, id, true);
|
|
|
|
|
|
|
|
// if the item is being upvoted by its own author, don't give karma
|
|
|
|
if (item.userId != user._id)
|
|
|
|
modifyKarma(item.userId, 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
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
var cancelDownvote = function(collection, item) {
|
|
|
|
var user = Meteor.user(),
|
|
|
|
votePower = getVotePower(user);
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
// if user isn't among the downvoters, abort
|
|
|
|
if(!hasUpvotedItem(item, user))
|
|
|
|
return false;
|
2012-09-28 15:07:25 +02:00
|
|
|
|
2012-09-29 13:51:55 +09:00
|
|
|
// Votes & Score
|
2012-12-24 10:59:13 +01:00
|
|
|
collection.update({_id: id},{
|
|
|
|
$pull: {downvoters: user._id},
|
|
|
|
$inc: {votes: 1, baseScore: votePower},
|
|
|
|
$set: {inactive: false}
|
|
|
|
});
|
2012-09-27 13:13:55 -07:00
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
updateScore(collection, id, true);
|
|
|
|
|
|
|
|
// if the item is being upvoted by its own author, don't give karma
|
|
|
|
if (item.userId != user._id)
|
|
|
|
modifyKarma(item.userId, 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
|
|
|
|
2013-10-29 15:06:50 +09:00
|
|
|
// note: doesn't actually seem very useful to enable admins to vote for other users. Remove this?
|
2013-02-04 12:04:01 +09:00
|
|
|
var getUser = function(user){
|
|
|
|
// only let admins specify different users for voting
|
|
|
|
// if no user is specified, use current user by default
|
|
|
|
return (isAdmin(Meteor.user()) && typeof user !== 'undefined') ? user : Meteor.user();
|
|
|
|
}
|
2013-01-15 08:46:00 +09:00
|
|
|
|
2012-09-18 10:04:26 +10:00
|
|
|
Meteor.methods({
|
2013-10-29 15:06:50 +09:00
|
|
|
upvotePost: function(post, user){
|
|
|
|
return upvoteItem.call(this, Posts, post);
|
2012-09-18 10:04:26 +10:00
|
|
|
},
|
2013-10-29 15:06:50 +09:00
|
|
|
downvotePost: function(post, user){
|
|
|
|
return downvoteItem.call(this, Posts, post);
|
2012-09-18 10:04:26 +10:00
|
|
|
},
|
2013-10-29 15:06:50 +09:00
|
|
|
cancelUpvotePost: function(post, user){
|
|
|
|
return cancelUpvote.call(this, Posts, postId);
|
2012-09-18 10:04:26 +10:00
|
|
|
},
|
2013-10-29 15:06:50 +09:00
|
|
|
cancelDownvotePost: function(post, user){
|
|
|
|
return cancelDownvote.call(this, Posts, post);
|
2012-09-18 10:04:26 +10:00
|
|
|
},
|
2013-10-29 15:06:50 +09:00
|
|
|
upvoteComment: function(comment, user){
|
|
|
|
return upvoteItem.call(this, Comments, comment);
|
2012-09-18 10:04:26 +10:00
|
|
|
},
|
2013-10-29 15:06:50 +09:00
|
|
|
downvoteComment: function(comment, user){
|
|
|
|
return upvoteItem.call(this, Comments, comment);
|
2012-09-18 10:04:26 +10:00
|
|
|
},
|
2013-10-29 15:06:50 +09:00
|
|
|
cancelUpvoteComment: function(comment, user){
|
|
|
|
return cancelUpvote.call(this, Comments, comment);
|
2012-09-18 10:04:26 +10:00
|
|
|
},
|
2013-10-29 15:06:50 +09:00
|
|
|
cancelDownvoteComment: function(comment, user){
|
|
|
|
return cancelDownvote.call(this, Comments, comment);
|
2012-09-18 09:23:42 +10:00
|
|
|
}
|
2012-09-18 10:04:26 +10:00
|
|
|
});
|
|
|
|
|
2012-09-28 09:40:34 +09:00
|
|
|
})();
|