Vulcan/packages/telescope-notifications/lib/notifications.js

67 lines
2.2 KiB
JavaScript
Raw Normal View History

// add new post notification callback on post submit
2014-09-20 10:42:42 +09:00
postAfterSubmitMethodCallbacks.push(function (post) {
2014-10-07 13:45:48 +09:00
if(Meteor.isServer){
2014-10-02 16:42:31 -04:00
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
2014-09-20 10:42:42 +09:00
commentAfterSubmitMethodCallbacks.push(function (comment) {
2014-10-07 13:45:48 +09:00
if(Meteor.isServer){
var parentCommentId = comment.parentCommentId;
2014-09-20 10:42:42 +09:00
var user = Meteor.user();
var post = Posts.findOne(comment.postId);
var postUser = Meteor.users.findOne(post.userId);
2014-10-02 16:42:31 -04:00
var notificationData = {
comment: _.pick(comment, '_id', 'userId', 'author', 'body'),
post: _.pick(post, '_id', 'title', 'url')
};
if(parentCommentId){
// child comment
var parentComment = Comments.findOne(parentCommentId);
var parentUser = Meteor.users.findOne(parentComment.userId);
2014-10-02 16:42:31 -04:00
notificationData.parentComment = _.pick(parentComment, '_id', 'userId', 'author');
// reply notification
// do not notify users of their own actions (i.e. they're replying to themselves)
if(parentUser._id != user._id)
2014-10-02 16:42:31 -04:00
Herald.createNotification(parentUser._id, {courier: 'newReply', data: notificationData})
// comment notification
// if the original poster is different from the author of the parent comment, notify them too
if(postUser._id != user._id && parentComment.userId != post.userId)
2014-10-02 16:42:31 -04:00
Herald.createNotification(postUser._id, {courier: 'newComment', data: notificationData})
}else{
// root comment
// don't notify users of their own comments
if(postUser._id != user._id)
2014-10-02 16:42:31 -04:00
Herald.createNotification(postUser._id, {courier: 'newComment', data: notificationData})
}
}
2014-09-20 10:42:42 +09:00
return comment;
2014-09-29 10:15:21 +09:00
});
var emailNotifications = {
propertyName: 'emailNotifications',
propertySchema: {
type: Boolean,
optional: true,
defaultValue: true,
autoform: {
2014-11-30 12:36:56 +09:00
group: 'notifications_fieldset',
2014-10-07 13:45:48 +09:00
instructions: 'Enable email notifications for new posts and new comments (requires restart).'
2014-09-29 10:15:21 +09:00
}
}
}
addToSettingsSchema.push(emailNotifications);