Vulcan/packages/nova-core/lib/callbacks.js

79 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-08-08 11:18:21 +09:00
import Telescope from 'meteor/nova:lib';
2016-01-02 18:41:45 +01: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});
}
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
/**
* @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":
update.$addToSet = {'__upvotedPosts': vote};
2016-01-02 18:41:45 +01:00
break;
case "downvote":
update.$addToSet = {'__downvotedPosts': vote};
2016-01-02 18:41:45 +01:00
break;
case "cancelUpvote":
update.$pull = {'__upvotedPosts': {itemId: item._id}};
2016-01-02 18:41:45 +01:00
break;
case "cancelDownvote":
update.$pull = {'__downvotedPosts': {itemId: item._id}};
2016-01-02 18:41:45 +01:00
break;
}
Users.update({_id: user._id}, update);
2016-01-02 18:41:45 +01: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
/**
* @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) {
Users.update({_id: item.userId}, {$inc: {"__karma": karmaAmount}});
2016-01-02 18:41:45 +01: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);