Vulcan/packages/vulcan-posts/lib/callbacks/callbacks_other.js

67 lines
2.2 KiB
JavaScript
Raw Normal View History

import Posts from '../collection.js'
2017-03-23 16:27:59 +09:00
import Users from 'meteor/vulcan:users';
import { addCallback } from 'meteor/vulcan:core';
import Events from 'meteor/vulcan:events';
// ------------------------------------- posts.remove.sync -------------------------------- //
function PostsRemoveOperations (post) {
Users.update({_id: post.userId}, {$inc: {"postCount": -1}});
}
addCallback("posts.remove.sync", PostsRemoveOperations);
// ------------------------------------- posts.approve.async -------------------------------- //
/**
* @summary set postedAt when a post is approved
*/
function PostsSetPostedAt (modifier, post) {
modifier.$set.postedAt = new Date();
return modifier;
}
addCallback("posts.approve.sync", PostsSetPostedAt);
// ------------------------------------- users.remove.async -------------------------------- //
function UsersRemoveDeletePosts (user, options) {
if (options.deletePosts) {
Posts.remove({userId: user._id});
} else {
// not sure if anything should be done in that scenario yet
// Posts.update({userId: userId}, {$set: {author: "\[deleted\]"}}, {multi: true});
}
}
addCallback("users.remove.async", UsersRemoveDeletePosts);
// /**
// * @summary Increase the number of clicks on a post
// * @param {string} postId the ID of the post being edited
// * @param {string} ip the IP of the current user
// */
Posts.increaseClicks = (postId, ip) => {
const clickEvent = {
name: 'click',
properties: {
postId: postId,
ip: ip
}
};
// make sure this IP hasn't previously clicked on this post
const existingClickEvent = Events.findOne({name: 'click', 'properties.postId': postId, 'properties.ip': ip});
if(!existingClickEvent) {
Events.log(clickEvent);
return Posts.update(postId, { $inc: { clickCount: 1 }}, {validate: false, bypassCollection2:true});
}
};
// track links clicked, locally in Events collection
// note: this event is not sent to segment cause we cannot access the current user
// in our server-side route /out -> sending an event would create a new anonymous
// user: the free limit of 1,000 unique users per month would be reached quickly
addCallback('posts.click.async', function PostsClickTracking(postId, ip) {
return Posts.increaseClicks(postId, ip);
});