2016-12-09 09:11:20 +01:00
|
|
|
import Users from 'meteor/nova:users';
|
2016-12-12 10:24:34 +09:00
|
|
|
import { Callbacks } from 'meteor/nova:core';
|
2016-12-12 11:17:13 +00:00
|
|
|
import { updateScore } from './scoring.js';
|
2016-12-12 16:43:23 +09:00
|
|
|
import { operateOnItem, getVotePower } from '../vote.js';
|
|
|
|
import Posts from 'meteor/nova:posts';
|
|
|
|
import Comments from 'meteor/nova:comments';
|
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
|
|
|
|
*/
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-12-12 16:43:23 +09:00
|
|
|
function updateScoreCallback (item, user, collection, operation) {
|
|
|
|
updateScore({collection: collection, item: item, forceUpdate: true});
|
2016-01-02 18:41:45 +01:00
|
|
|
}
|
2016-12-09 09:11:20 +01:00
|
|
|
|
2016-12-12 16:43:23 +09:00
|
|
|
Callbacks.add("upvote.async", updateScoreCallback);
|
|
|
|
Callbacks.add("downvote.async", updateScoreCallback);
|
|
|
|
Callbacks.add("cancelUpvote.async", updateScoreCallback);
|
|
|
|
Callbacks.add("cancelDownvote.async", updateScoreCallback);
|
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
|
|
|
|
*/
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-01-02 18:41:45 +01:00
|
|
|
function updateUser (item, user, collection, operation) {
|
|
|
|
|
|
|
|
var update = {};
|
2016-12-12 16:43:23 +09:00
|
|
|
var votePower = getVotePower(user);
|
2016-01-02 18:41:45 +01:00
|
|
|
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;
|
2016-11-26 02:46:55 +08:00
|
|
|
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;
|
2016-11-26 02:46:55 +08:00
|
|
|
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-12-12 10:24:34 +09:00
|
|
|
Callbacks.add("upvote.async", updateUser);
|
|
|
|
Callbacks.add("downvote.async", updateUser);
|
|
|
|
Callbacks.add("cancelUpvote.async", updateUser);
|
|
|
|
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
|
|
|
|
*/
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-12-09 09:11:20 +01:00
|
|
|
|
2016-01-02 18:41:45 +01:00
|
|
|
function updateKarma (item, user, collection, operation) {
|
|
|
|
|
2016-12-12 16:43:23 +09:00
|
|
|
var votePower = getVotePower(user);
|
2016-01-02 18:41:45 +01:00
|
|
|
var karmaAmount = (operation === "upvote" || operation === "cancelDownvote") ? votePower : -votePower;
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-01-02 18:41:45 +01:00
|
|
|
// 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-12-12 10:24:34 +09:00
|
|
|
Callbacks.add("upvote.async", updateKarma);
|
|
|
|
Callbacks.add("downvote.async", updateKarma);
|
|
|
|
Callbacks.add("cancelUpvote.async", updateKarma);
|
|
|
|
Callbacks.add("cancelDownvote.async", updateKarma);
|
2016-12-12 16:43:23 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Make users upvote their own new posts
|
|
|
|
*/
|
|
|
|
function PostsNewUpvoteOwnPost (post) {
|
|
|
|
var postAuthor = Users.findOne(post.userId);
|
|
|
|
operateOnItem(Posts, post, postAuthor, "upvote");
|
|
|
|
}
|
|
|
|
Callbacks.add("posts.new.async", PostsNewUpvoteOwnPost);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Make users upvote their own new comments
|
|
|
|
*/
|
|
|
|
function CommentsNewUpvoteOwnComment (comment) {
|
|
|
|
var commentAuthor = Users.findOne(comment.userId);
|
|
|
|
operateOnItem(Comments, comment, commentAuthor, "upvote");
|
|
|
|
return comment;
|
|
|
|
}
|
|
|
|
Callbacks.add("comments.new.async", CommentsNewUpvoteOwnComment);
|