2016-11-14 17:17:44 +09:00
|
|
|
|
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';
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
|
|
// ------------------------------------- posts.remove.sync -------------------------------- //
|
|
|
|
|
|
|
|
|
|
function PostsRemoveOperations (post) {
|
2017-01-18 10:18:33 +09:00
|
|
|
|
Users.update({_id: post.userId}, {$inc: {"postCount": -1}});
|
2016-11-14 17:17:44 +09:00
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
|
addCallback("posts.remove.sync", PostsRemoveOperations);
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
|
|
// ------------------------------------- posts.approve.async -------------------------------- //
|
|
|
|
|
|
2016-11-30 16:54:58 +09:00
|
|
|
|
/**
|
|
|
|
|
* @summary set postedAt when a post is approved
|
|
|
|
|
*/
|
|
|
|
|
function PostsSetPostedAt (modifier, post) {
|
|
|
|
|
modifier.$set.postedAt = new Date();
|
|
|
|
|
return modifier;
|
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
|
addCallback("posts.approve.sync", PostsSetPostedAt);
|
2016-11-30 16:54:58 +09:00
|
|
|
|
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
|
|
// ------------------------------------- users.remove.async -------------------------------- //
|
|
|
|
|
|
|
|
|
|
function UsersRemoveDeletePosts (user, options) {
|
|
|
|
|
if (options.deletePosts) {
|
2017-02-02 15:15:51 +01:00
|
|
|
|
Posts.remove({userId: user._id});
|
2016-11-14 17:17:44 +09:00
|
|
|
|
} else {
|
|
|
|
|
// not sure if anything should be done in that scenario yet
|
|
|
|
|
// Posts.update({userId: userId}, {$set: {author: "\[deleted\]"}}, {multi: true});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-02 15:15:51 +01:00
|
|
|
|
addCallback("users.remove.async", UsersRemoveDeletePosts);
|
2017-03-18 15:59:31 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * @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);
|
|
|
|
|
});
|