mirror of
https://github.com/vale981/Vulcan
synced 2025-03-10 04:26:41 -04:00
111 lines
3.4 KiB
JavaScript
111 lines
3.4 KiB
JavaScript
// add new post notification callback on post submit
|
|
postAfterSubmitMethodCallbacks.push(function (post) {
|
|
if(Meteor.isServer){
|
|
var userIds = Meteor.users.find({'profile.notifications.posts': 1}, {fields: {}}).map(function (user) {
|
|
return user._id;
|
|
});
|
|
Herald.createNotification(userIds, {courier: 'newPost', data: post});
|
|
}
|
|
return post;
|
|
});
|
|
|
|
// add new comment notification callback on comment submit
|
|
commentAfterSubmitMethodCallbacks.push(function (comment) {
|
|
if(Meteor.isServer && !comment.disableNotifications){
|
|
|
|
var post = Posts.findOne(comment.postId),
|
|
notificationData = {
|
|
comment: _.pick(comment, '_id', 'userId', 'author', 'body'),
|
|
post: _.pick(post, '_id', 'userId', 'title', 'url')
|
|
},
|
|
userIdsNotified = [];
|
|
|
|
// 1. Notify author of post
|
|
// do not notify author of post if they're the ones posting the comment
|
|
if (comment.userId !== post.userId) {
|
|
|
|
Herald.createNotification(post.userId, {courier: 'newComment', data: notificationData});
|
|
userIdsNotified.push(post.userId);
|
|
|
|
}
|
|
|
|
// 2. Notify author of comment being replied to
|
|
if (!!comment.parentCommentId) {
|
|
|
|
var parentComment = Comments.findOne(comment.parentCommentId);
|
|
|
|
// do not notify author of parent comment if they're also post author or comment author
|
|
// (someone could be replying to their own comment)
|
|
if (parentComment.userId !== post.userId || parentComment.userId !== comment.userId) {
|
|
|
|
// add parent comment to notification data
|
|
notificationData.parentComment = _.pick(parentComment, '_id', 'userId', 'author');
|
|
|
|
Herald.createNotification(parentComment.userId, {courier: 'newReply', data: notificationData});
|
|
userIdsNotified.push(parentComment.userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 3. Notify users subscribed to the thread
|
|
// TODO: ideally this would be injected from the telescope-subscribe-to-posts package
|
|
if (!!post.subscribers) {
|
|
|
|
// remove userIds of users that have already been notified
|
|
// and of comment author (they could be replying in a thread they're subscribed to)
|
|
var subscriberIdsToNotify = _.difference(post.subscribers, userIdsNotified, [comment.userId]);
|
|
Herald.createNotification(subscriberIdsToNotify, {courier: 'newCommentSubscribed', data: notificationData});
|
|
|
|
userIdsNotified = userIdsNotified.concat(subscriberIdsToNotify);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(userIdsNotified)
|
|
console.log(comment.body)
|
|
|
|
return comment;
|
|
|
|
});
|
|
|
|
var emailNotifications = {
|
|
propertyName: 'emailNotifications',
|
|
propertySchema: {
|
|
type: Boolean,
|
|
optional: true,
|
|
defaultValue: true,
|
|
autoform: {
|
|
group: 'notifications_fieldset',
|
|
instructions: 'Enable email notifications for new posts and new comments (requires restart).'
|
|
}
|
|
}
|
|
};
|
|
addToSettingsSchema.push(emailNotifications);
|
|
|
|
// make it possible to disable notifications on a per-comment basis
|
|
addToCommentsSchema.push(
|
|
{
|
|
propertyName: 'disableNotifications',
|
|
propertySchema: {
|
|
type: Boolean,
|
|
optional: true,
|
|
autoform: {
|
|
omit: true
|
|
}
|
|
}
|
|
}
|
|
);
|
|
|
|
function setNotificationDefaults (user) {
|
|
// set notifications default preferences
|
|
user.profile.notifications = {
|
|
users: false,
|
|
posts: false,
|
|
comments: true,
|
|
replies: true
|
|
};
|
|
return user;
|
|
}
|
|
userCreatedCallbacks.push(setNotificationDefaults);
|