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

125 lines
4 KiB
JavaScript
Raw Normal View History

// add new post notification callback on post submit
Posts.hooks.afterSubmitMethodCallbacks.push(function (post) {
var adminIds = _.pluck(Meteor.users.find({'isAdmin': true}, {fields: {_id:1}}).fetch(), '_id');
var notifiedUserIds = _.pluck(Meteor.users.find({'profile.notifications.posts': 1}, {fields: {_id:1}}).fetch(), '_id');
2015-01-27 12:34:37 +09:00
// remove post author ID from arrays
var adminIds = _.without(adminIds, post.userId);
var notifiedUserIds = _.without(notifiedUserIds, post.userId);
if (post.status === Posts.config.STATUS_PENDING && !!adminIds.length) {
// if post is pending, only notify admins
Herald.createNotification(adminIds, {courier: 'newPendingPost', data: post});
2015-03-28 18:30:26 +09:00
} else if (!!notifiedUserIds.length) {
// if post is approved, notify everybody
Herald.createNotification(notifiedUserIds, {courier: 'newPost', data: post});
}
return post;
});
// notify users that their pending post has been approved
Posts.hooks.approvedCallbacks.push(function (post) {
Herald.createNotification(post.userId, {courier: 'postApproved', 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-12-31 17:44:21 +09:00
if(Meteor.isServer && !comment.disableNotifications){
2015-01-20 12:35:25 +09:00
var post = Posts.findOne(comment.postId),
notificationData = {
comment: _.pick(comment, '_id', 'userId', 'author', 'body'),
post: _.pick(post, '_id', 'userId', 'title', 'url')
},
2015-01-20 12:35:25 +09:00
userIdsNotified = [];
2014-09-20 10:42:42 +09:00
// 1. Notify author of post
2015-01-20 12:35:25 +09:00
// 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) {
2015-03-28 18:30:26 +09:00
var parentComment = Comments.findOne(comment.parentCommentId);
2015-03-28 18:30:26 +09:00
2015-01-20 12:35:25 +09:00
// do not notify author of parent comment if they're also post author or comment author
// (someone could be replying to their own comment)
2015-02-01 20:44:13 +09:00
if (parentComment.userId !== post.userId && parentComment.userId !== comment.userId) {
2015-03-28 18:30:26 +09:00
// add parent comment to notification data
notificationData.parentComment = _.pick(parentComment, '_id', 'userId', 'author');
2015-03-28 18:30:26 +09:00
2015-01-20 12:35:25 +09:00
Herald.createNotification(parentComment.userId, {courier: 'newReply', data: notificationData});
userIdsNotified.push(parentComment.userId);
2015-03-28 18:30:26 +09:00
}
}
2015-03-28 18:30:26 +09:00
// 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
2015-01-20 12:35:25 +09:00
// 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});
2015-01-20 12:35:25 +09:00
userIdsNotified = userIdsNotified.concat(subscriberIdsToNotify);
2015-03-28 18:30:26 +09:00
}
}
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
}
}
};
2015-03-28 18:30:26 +09:00
Settings.addToSchema(emailNotifications);
2014-12-31 17:44:21 +09:00
// 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;
}
Users.hooks.userCreatedCallbacks.push(setNotificationDefaults);