Vulcan/packages/telescope-core/lib/vote.js

173 lines
5.8 KiB
JavaScript
Raw Normal View History

2016-01-02 18:41:45 +01:00
// The equation to determine voting power. Defaults to returning 1 for everybody
Telescope.getVotePower = function (user) {
return 1;
};
Telescope.upvoteItem = function (collection, itemId, user) {
user = typeof user === "undefined" ? Meteor.user() : user;
var item = collection.findOne(itemId);
2016-01-02 18:41:45 +01:00
var votePower = Telescope.getVotePower(user);
/*
we're testing if
1. the user exists
2. they can vote
3. they haven't previously performed the same operation on this item
*/
// make sure user has rights to upvote first
2016-01-02 18:41:45 +01:00
if (!user || !user.canVote() || user.hasUpvotedItem(item))
return false;
// ------------------------------ Callbacks ------------------------------ //
item = Telescope.callbacks.run("upvote", item, user);
2015-03-11 17:49:42 +09:00
// in case user is upvoting a previously downvoted item, cancel downvote first
Telescope.cancelDownvote(collection, itemId, user);
// Votes & Score
var result = collection.update({_id: item && item._id, upvoters: { $ne: user._id }},{
$addToSet: {upvoters: user._id},
$inc: {upvotes: 1, baseScore: votePower},
$set: {inactive: false}
});
if (result > 0) {
// extend item with baseScore to help calculate newScore
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
2015-01-21 10:38:59 +09:00
// --------------------- Server-Side Async Callbacks --------------------- //
2016-01-02 18:41:45 +01:00
Telescope.callbacks.runAsync("upvoteAsync", item, user, collection, "upvote");
}
2016-01-02 18:41:45 +01:00
return true;
};
Telescope.downvoteItem = function (collection, itemId, user) {
user = typeof user === "undefined" ? Meteor.user() : user;
var item = collection.findOne(itemId);
2016-01-02 18:41:45 +01:00
var votePower = Telescope.getVotePower(user);
// make sure user has rights to downvote first
2016-01-02 18:41:45 +01:00
if (!user || !user.canVote() || user.hasDownvotedItem(item))
return false;
// ------------------------------ Callbacks ------------------------------ //
item = Telescope.callbacks.run("downvote", item, user);
2015-03-28 18:30:26 +09:00
// in case user is downvoting a previously upvoted item, cancel upvote first
2015-05-06 12:17:07 +09:00
Telescope.cancelUpvote(collection, item, user);
// Votes & Score
var result = collection.update({_id: item && item._id, downvoters: { $ne: user._id }},{
$addToSet: {downvoters: user._id},
$inc: {downvotes: 1, baseScore: -votePower},
$set: {inactive: false}
});
if (result > 0) {
// extend item with baseScore to help calculate newScore
item = _.extend(item, {baseScore: (item.baseScore - votePower)});
2015-01-21 10:38:59 +09:00
// --------------------- Server-Side Async Callbacks --------------------- //
2016-01-02 18:41:45 +01:00
Telescope.callbacks.runAsync("downvoteAsync", item, user, collection, "downvote");
}
2016-01-02 18:41:45 +01:00
return true;
};
Telescope.cancelUpvote = function (collection, itemId, user) {
user = typeof user === "undefined" ? Meteor.user() : user;
var item = collection.findOne(itemId);
2016-01-02 18:41:45 +01:00
var votePower = Telescope.getVotePower(user);
// if user isn't among the upvoters, abort
2016-01-02 18:41:45 +01:00
if (!user.hasUpvotedItem(item))
return false;
// ------------------------------ Callbacks ------------------------------ //
2016-01-02 18:41:45 +01:00
item = Telescope.callbacks.run("cancelUpvote", item, user, collection, "cancelUpvote");
// Votes & Score
var result = collection.update({_id: item && item._id, upvoters: user._id},{
$pull: {upvoters: user._id},
$inc: {upvotes: -1, baseScore: -votePower},
$set: {inactive: false}
});
if (result > 0) {
// extend item with baseScore to help calculate newScore
item = _.extend(item, {baseScore: (item.baseScore - votePower)});
// --------------------- Server-Side Async Callbacks --------------------- //
2016-01-02 18:41:45 +01:00
Telescope.callbacks.runAsync("cancelUpvoteAsync", item, user, collection, "cancelDownvote");
}
// console.log(collection.findOne(item._id));
return true;
};
Telescope.cancelDownvote = function (collection, itemId, user) {
user = typeof user === "undefined" ? Meteor.user() : user;
var item = collection.findOne(itemId);
2016-01-02 18:41:45 +01:00
var votePower = Telescope.getVotePower(user);
// if user isn't among the downvoters, abort
2016-01-02 18:41:45 +01:00
if (!user.hasDownvotedItem(item))
return false;
// ------------------------------ Callbacks ------------------------------ //
item = Telescope.callbacks.run("cancelDownvote", item, user);
2015-03-28 18:30:26 +09:00
// Votes & Score
var result = collection.update({_id: item && item._id, downvoters: user._id},{
$pull: {downvoters: user._id},
2015-05-06 12:17:07 +09:00
$inc: {downvotes: -1, baseScore: votePower},
$set: {inactive: false}
2012-09-18 10:04:26 +10:00
});
if (result > 0) {
// extend item with baseScore to help calculate newScore
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
// --------------------- Server-Side Async Callbacks --------------------- //
Telescope.callbacks.runAsync("cancelDownvoteAsync", item, user);
}
2016-01-02 18:41:45 +01:00
return true;
};
Meteor.methods({
upvotePost: function (postId) {
check(postId, String);
return Telescope.upvoteItem.call(this, Posts, postId);
},
downvotePost: function (postId) {
check(postId, String);
return Telescope.downvoteItem.call(this, Posts, postId);
},
cancelUpvotePost: function (postId) {
check(postId, String);
return Telescope.cancelUpvote.call(this, Posts, postId);
},
cancelDownvotePost: function (postId) {
check(postId, String);
return Telescope.cancelDownvote.call(this, Posts, postId);
},
upvoteComment: function (commentId) {
check(commentId, String);
return Telescope.upvoteItem.call(this, Comments, commentId);
},
downvoteComment: function (commentId) {
check(commentId, String);
return Telescope.downvoteItem.call(this, Comments, commentId);
},
cancelUpvoteComment: function (commentId) {
check(commentId, String);
return Telescope.cancelUpvote.call(this, Comments, commentId);
},
cancelDownvoteComment: function (commentId) {
check(commentId, String);
return Telescope.cancelDownvote.call(this, Comments, commentId);
}
});