This commit is contained in:
SachaG 2017-04-18 09:56:48 +09:00
parent 778ad9b64d
commit b98c86afa4
2 changed files with 73 additions and 79 deletions

View file

@ -14,7 +14,7 @@ import './parameters.js';
const Movies = createCollection({
collectionName: 'movies',
collectionName: 'Movies',
typeName: 'Movie',

View file

@ -10,101 +10,95 @@ const Comments = Package['vulcan:comments'] ? Package['vulcan:comments'].default
const Posts = Package['vulcan:posts'] ? Package['vulcan:posts'].default : null;
const Categories = Package['vulcan:categories'] ? Package['vulcan:categories'].default : null;
/**
* @summary Notify users subscribed to 'another user' whenever another user posts
*/
function SubscribedCategoriesNotifications (post) {
if (Meteor.isServer && !!post.categories && !!post.categories.length) {
// get the subscribers of the different categories from the post's categories
const subscribers = post.categories
// find the category from its id
.map(categoryId => Categories.findOne({_id: categoryId}))
// clean the array if none subscribe to this category
.filter(category => typeof category.subscribers !== 'undefined' || !!category.subscribers)
// build the subscribers list interested in these categories
.reduce((subscribersList, category) => [...subscribersList, ...category.subscribers], []);
let userIdsNotified = [];
const notificationData = {
post: _.pick(post, '_id', 'userId', 'title', 'url', 'author')
};
if (!!subscribers && !!subscribers.length) {
// remove userIds of users that have already been notified and of post's author
let subscriberIdsToNotify = _.uniq(_.difference(subscribers, userIdsNotified, [post.userId]));
createNotification(subscriberIdsToNotify, 'newPost', notificationData);
userIdsNotified = userIdsNotified.concat(subscriberIdsToNotify);
}
}
}
/**
* @summary Notify users subscribed to the comment's thread
*/
if (!!Posts && !!Comments) {
const SubscribedPostNotifications = (comment) => {
function SubscribedPostNotifications (comment) {
// note: dummy content has disableNotifications set to true
if (Meteor.isServer && !comment.disableNotifications) {
if (Meteor.isServer && !comment.disableNotifications) {
const post = Posts.findOne(comment.postId);
const post = Posts.findOne(comment.postId);
let userIdsNotified = [];
const notificationData = {
comment: _.pick(comment, '_id', 'userId', 'author', 'htmlBody', 'postId'),
post: _.pick(post, '_id', 'userId', 'title', 'url')
};
let userIdsNotified = [];
const notificationData = {
comment: _.pick(comment, '_id', 'userId', 'author', 'htmlBody', 'postId'),
post: _.pick(post, '_id', 'userId', 'title', 'url')
};
if (!!post.subscribers && !!post.subscribers.length) {
// remove userIds of users that have already been notified
// and of comment author (they could be replying in a thread they're subscribed to)
let subscriberIdsToNotify = _.difference(post.subscribers, userIdsNotified, [comment.userId]);
createNotification(subscriberIdsToNotify, 'newCommentSubscribed', notificationData);
if (!!post.subscribers && !!post.subscribers.length) {
// remove userIds of users that have already been notified
// and of comment author (they could be replying in a thread they're subscribed to)
let subscriberIdsToNotify = _.difference(post.subscribers, userIdsNotified, [comment.userId]);
createNotification(subscriberIdsToNotify, 'newCommentSubscribed', notificationData);
userIdsNotified = userIdsNotified.concat(subscriberIdsToNotify);
}
userIdsNotified = userIdsNotified.concat(subscriberIdsToNotify);
}
};
}
}
/**
* @summary Notify users subscribed to 'another user' whenever another user posts
*/
function SubscribedUsersNotifications (post) {
if (Meteor.isServer) {
let userIdsNotified = [];
const notificationData = {
post: _.pick(post, '_id', 'userId', 'title', 'url', 'author')
};
const user = Users.findOne({_id: post.userId});
if (!!user.subscribers && !!user.subscribers.length) {
// remove userIds of users that have already been notified and of post's author
let subscriberIdsToNotify = _.difference(user.subscribers, userIdsNotified, [user._id]);
createNotification(subscriberIdsToNotify, 'newPost', notificationData);
userIdsNotified = userIdsNotified.concat(subscriberIdsToNotify);
}
}
}
if (!!Posts && !!Comments) {
addCallback("comments.new.async", SubscribedPostNotifications);
}
/**
* @summary Notify users subscribed to 'another user' whenever another user posts
*/
if (!!Posts) {
const SubscribedUsersNotifications = (post) => {
if (Meteor.isServer) {
let userIdsNotified = [];
const notificationData = {
post: _.pick(post, '_id', 'userId', 'title', 'url', 'author')
};
const user = Users.findOne({_id: post.userId});
if (!!user.subscribers && !!user.subscribers.length) {
// remove userIds of users that have already been notified and of post's author
let subscriberIdsToNotify = _.difference(user.subscribers, userIdsNotified, [user._id]);
createNotification(subscriberIdsToNotify, 'newPost', notificationData);
userIdsNotified = userIdsNotified.concat(subscriberIdsToNotify);
}
}
};
addCallback("posts.new.async", SubscribedUsersNotifications);
}
/**
* @summary Notify users subscribed to 'another user' whenever another user posts
*/
if (!!Posts && !!Categories) {
const SubscribedCategoriesNotifications = (post) => {
if (Meteor.isServer && !!post.categories && !!post.categories.length) {
// get the subscribers of the different categories from the post's categories
const subscribers = post.categories
// find the category from its id
.map(categoryId => Categories.findOne({_id: categoryId}))
// clean the array if none subscribe to this category
.filter(category => typeof category.subscribers !== 'undefined' || !!category.subscribers)
// build the subscribers list interested in these categories
.reduce((subscribersList, category) => [...subscribersList, ...category.subscribers], []);
let userIdsNotified = [];
const notificationData = {
post: _.pick(post, '_id', 'userId', 'title', 'url', 'author')
};
if (!!subscribers && !!subscribers.length) {
// remove userIds of users that have already been notified and of post's author
let subscriberIdsToNotify = _.uniq(_.difference(subscribers, userIdsNotified, [post.userId]));
createNotification(subscriberIdsToNotify, 'newPost', notificationData);
userIdsNotified = userIdsNotified.concat(subscriberIdsToNotify);
}
}
};
if (!!Posts && !!Categories) {
addCallback("posts.new.async", SubscribedCategoriesNotifications);
}