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;
|
2014-12-20 17:34:15 +09:00
|
|
|
};
|
|
|
|
|
2015-07-10 11:05:13 +09:00
|
|
|
Telescope.upvoteItem = function (collection, itemId, user) {
|
|
|
|
|
2015-03-10 21:05:29 -06:00
|
|
|
user = typeof user === "undefined" ? Meteor.user() : user;
|
2015-07-10 11:05:13 +09:00
|
|
|
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
|
|
|
|
*/
|
2014-12-20 17:34:15 +09:00
|
|
|
|
|
|
|
// make sure user has rights to upvote first
|
2016-01-02 18:41:45 +01:00
|
|
|
if (!user || !user.canVote() || user.hasUpvotedItem(item))
|
2014-12-20 17:34:15 +09:00
|
|
|
return false;
|
|
|
|
|
2015-03-10 21:05:29 -06:00
|
|
|
// ------------------------------ Callbacks ------------------------------ //
|
2015-12-11 15:49:59 +09:00
|
|
|
item = Telescope.callbacks.run("upvote", item, user);
|
2015-03-11 17:49:42 +09:00
|
|
|
|
2014-12-20 17:34:15 +09:00
|
|
|
// in case user is upvoting a previously downvoted item, cancel downvote first
|
2015-09-08 09:48:39 +09:00
|
|
|
Telescope.cancelDownvote(collection, itemId, user);
|
2014-12-20 17:34:15 +09:00
|
|
|
|
|
|
|
// 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");
|
2014-12-20 17:34:15 +09:00
|
|
|
}
|
2016-01-02 18:41:45 +01:00
|
|
|
|
2014-12-20 17:34:15 +09:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2015-07-10 11:05:13 +09:00
|
|
|
Telescope.downvoteItem = function (collection, itemId, user) {
|
|
|
|
|
2015-03-10 21:05:29 -06:00
|
|
|
user = typeof user === "undefined" ? Meteor.user() : user;
|
2015-07-10 11:05:13 +09:00
|
|
|
var item = collection.findOne(itemId);
|
2016-01-02 18:41:45 +01:00
|
|
|
var votePower = Telescope.getVotePower(user);
|
2014-12-20 17:34:15 +09:00
|
|
|
|
|
|
|
// make sure user has rights to downvote first
|
2016-01-02 18:41:45 +01:00
|
|
|
if (!user || !user.canVote() || user.hasDownvotedItem(item))
|
2014-12-20 17:34:15 +09:00
|
|
|
return false;
|
|
|
|
|
2015-03-10 21:05:29 -06:00
|
|
|
// ------------------------------ Callbacks ------------------------------ //
|
2015-12-11 15:49:59 +09:00
|
|
|
item = Telescope.callbacks.run("downvote", item, user);
|
2015-03-28 18:30:26 +09:00
|
|
|
|
2014-12-20 17:34:15 +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);
|
2014-12-20 17:34:15 +09:00
|
|
|
|
|
|
|
// 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}
|
|
|
|
});
|
2014-08-11 10:14:53 +09:00
|
|
|
|
2014-12-20 17:34:15 +09:00
|
|
|
if (result > 0) {
|
|
|
|
// extend item with baseScore to help calculate newScore
|
2015-03-13 23:53:26 -03:00
|
|
|
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");
|
2014-12-20 17:34:15 +09:00
|
|
|
}
|
2016-01-02 18:41:45 +01:00
|
|
|
|
2014-12-20 17:34:15 +09:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2015-07-10 11:05:13 +09:00
|
|
|
Telescope.cancelUpvote = function (collection, itemId, user) {
|
|
|
|
|
2015-03-10 21:05:29 -06:00
|
|
|
user = typeof user === "undefined" ? Meteor.user() : user;
|
2015-07-10 11:05:13 +09:00
|
|
|
var item = collection.findOne(itemId);
|
2016-01-02 18:41:45 +01:00
|
|
|
var votePower = Telescope.getVotePower(user);
|
2014-12-20 17:34:15 +09:00
|
|
|
|
|
|
|
// if user isn't among the upvoters, abort
|
2016-01-02 18:41:45 +01:00
|
|
|
if (!user.hasUpvotedItem(item))
|
2014-12-20 17:34:15 +09:00
|
|
|
return false;
|
|
|
|
|
2015-03-10 21:05:29 -06:00
|
|
|
// ------------------------------ Callbacks ------------------------------ //
|
2016-01-02 18:41:45 +01:00
|
|
|
item = Telescope.callbacks.run("cancelUpvote", item, user, collection, "cancelUpvote");
|
2015-03-10 21:05:29 -06:00
|
|
|
|
2014-12-20 17:34:15 +09:00
|
|
|
// 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}
|
|
|
|
});
|
2013-10-29 15:06:50 +09:00
|
|
|
|
2014-12-20 17:34:15 +09:00
|
|
|
if (result > 0) {
|
|
|
|
// extend item with baseScore to help calculate newScore
|
2015-03-13 23:53:26 -03:00
|
|
|
item = _.extend(item, {baseScore: (item.baseScore - votePower)});
|
2015-03-07 21:54:32 -07:00
|
|
|
// --------------------- Server-Side Async Callbacks --------------------- //
|
2016-01-02 18:41:45 +01:00
|
|
|
Telescope.callbacks.runAsync("cancelUpvoteAsync", item, user, collection, "cancelDownvote");
|
2014-12-20 17:34:15 +09:00
|
|
|
}
|
|
|
|
// console.log(collection.findOne(item._id));
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2015-07-10 11:05:13 +09:00
|
|
|
Telescope.cancelDownvote = function (collection, itemId, user) {
|
|
|
|
|
2015-03-10 21:05:29 -06:00
|
|
|
user = typeof user === "undefined" ? Meteor.user() : user;
|
2015-07-10 11:05:13 +09:00
|
|
|
var item = collection.findOne(itemId);
|
2016-01-02 18:41:45 +01:00
|
|
|
var votePower = Telescope.getVotePower(user);
|
2014-12-20 17:34:15 +09:00
|
|
|
|
|
|
|
// if user isn't among the downvoters, abort
|
2016-01-02 18:41:45 +01:00
|
|
|
if (!user.hasDownvotedItem(item))
|
2014-12-20 17:34:15 +09:00
|
|
|
return false;
|
2015-01-07 08:22:46 +01:00
|
|
|
|
2015-03-10 21:05:29 -06:00
|
|
|
// ------------------------------ Callbacks ------------------------------ //
|
2015-12-11 15:49:59 +09:00
|
|
|
item = Telescope.callbacks.run("cancelDownvote", item, user);
|
2015-03-28 18:30:26 +09:00
|
|
|
|
2014-12-20 17:34:15 +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},
|
2014-12-20 17:34:15 +09:00
|
|
|
$set: {inactive: false}
|
2012-09-18 10:04:26 +10:00
|
|
|
});
|
2015-01-07 08:22:46 +01:00
|
|
|
|
2014-12-20 17:34:15 +09:00
|
|
|
if (result > 0) {
|
|
|
|
// extend item with baseScore to help calculate newScore
|
|
|
|
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
|
2015-03-07 21:54:32 -07:00
|
|
|
// --------------------- Server-Side Async Callbacks --------------------- //
|
2015-12-11 15:49:59 +09:00
|
|
|
Telescope.callbacks.runAsync("cancelDownvoteAsync", item, user);
|
2014-12-20 17:34:15 +09:00
|
|
|
}
|
2016-01-02 18:41:45 +01:00
|
|
|
|
2014-12-20 17:34:15 +09:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Meteor.methods({
|
2015-07-10 11:05:13 +09:00
|
|
|
upvotePost: function (postId) {
|
|
|
|
check(postId, String);
|
|
|
|
return Telescope.upvoteItem.call(this, Posts, postId);
|
2014-12-20 17:34:15 +09:00
|
|
|
},
|
2015-07-10 11:05:13 +09:00
|
|
|
downvotePost: function (postId) {
|
|
|
|
check(postId, String);
|
|
|
|
return Telescope.downvoteItem.call(this, Posts, postId);
|
2014-12-20 17:34:15 +09:00
|
|
|
},
|
2015-07-10 11:05:13 +09:00
|
|
|
cancelUpvotePost: function (postId) {
|
|
|
|
check(postId, String);
|
|
|
|
return Telescope.cancelUpvote.call(this, Posts, postId);
|
2014-12-20 17:34:15 +09:00
|
|
|
},
|
2015-07-10 11:05:13 +09:00
|
|
|
cancelDownvotePost: function (postId) {
|
|
|
|
check(postId, String);
|
|
|
|
return Telescope.cancelDownvote.call(this, Posts, postId);
|
2014-12-20 17:34:15 +09:00
|
|
|
},
|
2015-07-10 11:05:13 +09:00
|
|
|
upvoteComment: function (commentId) {
|
|
|
|
check(commentId, String);
|
|
|
|
return Telescope.upvoteItem.call(this, Comments, commentId);
|
2014-12-20 17:34:15 +09:00
|
|
|
},
|
2015-07-10 11:05:13 +09:00
|
|
|
downvoteComment: function (commentId) {
|
|
|
|
check(commentId, String);
|
|
|
|
return Telescope.downvoteItem.call(this, Comments, commentId);
|
2014-12-20 17:34:15 +09:00
|
|
|
},
|
2015-07-10 11:05:13 +09:00
|
|
|
cancelUpvoteComment: function (commentId) {
|
|
|
|
check(commentId, String);
|
|
|
|
return Telescope.cancelUpvote.call(this, Comments, commentId);
|
2014-12-20 17:34:15 +09:00
|
|
|
},
|
2015-07-10 11:05:13 +09:00
|
|
|
cancelDownvoteComment: function (commentId) {
|
|
|
|
check(commentId, String);
|
|
|
|
return Telescope.cancelDownvote.call(this, Comments, commentId);
|
2014-12-20 17:34:15 +09:00
|
|
|
}
|
|
|
|
});
|