Vulcan/lib/vote.js

87 lines
2.7 KiB
JavaScript
Raw Normal View History

2012-09-18 10:04:26 +10:00
(function() {
var prepareVote = function(collection, id, fn) {
// ensure we are logged in
var userId = this.userId();
if(!userId) return false;
2012-09-18 10:04:26 +10:00
// now do the real work
fn(userId);
2012-09-18 10:04:26 +10:00
// now update scores
if (!this.is_simulation) {
// now update the post's score
2012-09-18 10:04:26 +10:00
var object = collection.findOne(id);
Scoring.updateObject(object);
collection.update(id, {$set: {score: object.score}});
2012-09-18 09:29:31 +10:00
}
return true;
2012-09-18 10:04:26 +10:00
};
2012-09-18 09:29:31 +10:00
2012-09-18 10:04:26 +10:00
var upvote = function(collection, id) {
return prepareVote.call(this, collection, id, function(userId) {
var votePower=getVotesPower(userId);
2012-09-18 10:04:26 +10:00
var query = {_id: id, upvoters: {$ne: userId}};
var update = {$push: {upvoters: userId}, $inc: {votes: 1}, $inc: {baseScore: votePower}};
2012-09-18 10:04:26 +10:00
collection.update(query, update);
});
};
var downvote = function(collection, id) {
return prepareVote.call(this, collection, id, function(userId) {
var votePower=getVotesPower(userId);
2012-09-18 10:04:26 +10:00
var query = {_id: id, downvoters: {$ne: userId}};
var update = {$push: {downvoters: userId}, $inc: {votes: -1}, $inc: {baseScore: -votePower}};
2012-09-18 10:04:26 +10:00
collection.update(query, update);
});
};
var cancelUpvote = function(collection, id) {
return prepareVote.call(this, collection, id, function(userId) {
var votePower=getVotesPower(userId);
2012-09-18 10:04:26 +10:00
var query = {_id: id, upvoters: userId};
var update = {$pull: {upvoters: userId}, $inc: {votes: -1}, $inc: {baseScore: -votePower}};
2012-09-18 10:04:26 +10:00
collection.update(query, update);
});
};
var cancelDownvote = function(collection, id) {
return prepareVote.call(this, collection, id, function(userId) {
var votePower=getVotesPower(userId);
2012-09-18 10:04:26 +10:00
var query = {_id: id, downvoters: userId};
var update = {$pull: {downvoters: userId}, $inc: {votes: 1}, $inc: {baseScore: -votePower}};
2012-09-18 10:04:26 +10:00
collection.update(query, update);
});
};
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);
},
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 10:04:26 +10:00
});
})();