2016-08-08 11:18:21 +09:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
2016-11-29 12:27:26 +01:00
|
|
|
import Users from 'meteor/nova:users';
|
2016-01-02 18:41:45 +01:00
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Update an item's (post or comment) score
|
2016-01-02 18:41:45 +01:00
|
|
|
* @param {object} item - The item being operated on
|
|
|
|
* @param {object} user - The user doing the operation
|
|
|
|
* @param {object} collection - The collection the item belongs to
|
|
|
|
* @param {string} operation - The operation being performed
|
|
|
|
*/
|
|
|
|
function updateScore (item, user, collection, operation) {
|
|
|
|
Telescope.updateScore({collection: collection, item: item, forceUpdate: true});
|
|
|
|
}
|
2016-02-24 18:23:21 +09:00
|
|
|
Telescope.callbacks.add("upvote.async", updateScore);
|
|
|
|
Telescope.callbacks.add("downvote.async", updateScore);
|
|
|
|
Telescope.callbacks.add("cancelUpvote.async", updateScore);
|
|
|
|
Telescope.callbacks.add("cancelDownvote.async", updateScore);
|
2016-01-02 18:41:45 +01:00
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Update the profile of the user doing the operation
|
2016-01-02 18:41:45 +01:00
|
|
|
* @param {object} item - The item being operated on
|
|
|
|
* @param {object} user - The user doing the operation
|
|
|
|
* @param {object} collection - The collection the item belongs to
|
|
|
|
* @param {string} operation - The operation being performed
|
|
|
|
*/
|
|
|
|
function updateUser (item, user, collection, operation) {
|
|
|
|
|
|
|
|
var update = {};
|
|
|
|
var votePower = Telescope.getVotePower(user);
|
|
|
|
var vote = {
|
|
|
|
itemId: item._id,
|
|
|
|
votedAt: new Date(),
|
|
|
|
power: votePower
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case "upvote":
|
2016-11-10 14:22:47 +01:00
|
|
|
update.$addToSet = {'__upvotedPosts': vote};
|
2016-01-02 18:41:45 +01:00
|
|
|
break;
|
|
|
|
case "downvote":
|
2016-11-10 14:22:47 +01:00
|
|
|
update.$addToSet = {'__downvotedPosts': vote};
|
2016-01-02 18:41:45 +01:00
|
|
|
break;
|
|
|
|
case "cancelUpvote":
|
2016-11-10 14:22:47 +01:00
|
|
|
update.$pull = {'__upvotedPosts': {itemId: item._id}};
|
2016-01-02 18:41:45 +01:00
|
|
|
break;
|
|
|
|
case "cancelDownvote":
|
2016-11-10 14:22:47 +01:00
|
|
|
update.$pull = {'__downvotedPosts': {itemId: item._id}};
|
2016-01-02 18:41:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-05 08:37:48 +02:00
|
|
|
Users.update({_id: user._id}, update);
|
2016-01-02 18:41:45 +01:00
|
|
|
|
|
|
|
}
|
2016-02-24 18:23:21 +09:00
|
|
|
Telescope.callbacks.add("upvote.async", updateUser);
|
|
|
|
Telescope.callbacks.add("downvote.async", updateUser);
|
|
|
|
Telescope.callbacks.add("cancelUpvote.async", updateUser);
|
|
|
|
Telescope.callbacks.add("cancelDownvote.async", updateUser);
|
2016-01-02 18:41:45 +01:00
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Update the karma of the item's owner
|
2016-01-02 18:41:45 +01:00
|
|
|
* @param {object} item - The item being operated on
|
|
|
|
* @param {object} user - The user doing the operation
|
|
|
|
* @param {object} collection - The collection the item belongs to
|
|
|
|
* @param {string} operation - The operation being performed
|
|
|
|
*/
|
|
|
|
function updateKarma (item, user, collection, operation) {
|
|
|
|
|
|
|
|
var votePower = Telescope.getVotePower(user);
|
|
|
|
var karmaAmount = (operation === "upvote" || operation === "cancelDownvote") ? votePower : -votePower;
|
|
|
|
|
|
|
|
// only update karma is the operation isn't done by the item's author
|
|
|
|
if (item.userId !== user._id) {
|
2016-11-10 14:22:47 +01:00
|
|
|
Users.update({_id: item.userId}, {$inc: {"__karma": karmaAmount}});
|
2016-01-02 18:41:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-02-24 18:23:21 +09:00
|
|
|
Telescope.callbacks.add("upvote.async", updateKarma);
|
|
|
|
Telescope.callbacks.add("downvote.async", updateKarma);
|
|
|
|
Telescope.callbacks.add("cancelUpvote.async", updateKarma);
|
|
|
|
Telescope.callbacks.add("cancelDownvote.async", updateKarma);
|