2015-04-22 07:50:11 +09:00
|
|
|
// add new post notification callback on post submit
|
2015-04-23 15:42:05 +09:00
|
|
|
function postSubmitNotification (post) {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2015-06-06 00:36:58 -04:00
|
|
|
var adminIds = _.pluck(Users.find({'isAdmin': true}, {fields: {_id:1}}).fetch(), '_id');
|
|
|
|
var notifiedUserIds = _.pluck(Users.find({'telescope.notifications.posts': true}, {fields: {_id:1}}).fetch(), '_id');
|
2015-04-22 07:50:11 +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});
|
|
|
|
} else if (!!notifiedUserIds.length) {
|
|
|
|
// if post is approved, notify everybody
|
|
|
|
Herald.createNotification(notifiedUserIds, {courier: 'newPost', data: post});
|
|
|
|
}
|
|
|
|
return post;
|
|
|
|
|
2015-04-23 15:42:05 +09:00
|
|
|
}
|
2015-05-17 15:38:02 +09:00
|
|
|
Telescope.callbacks.add("postSubmitAsync", postSubmitNotification);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2015-04-23 15:42:05 +09:00
|
|
|
function postApprovedNotification (post) {
|
2015-04-22 07:50:11 +09:00
|
|
|
Herald.createNotification(post.userId, {courier: 'postApproved', data: post});
|
|
|
|
return post;
|
2015-04-23 15:42:05 +09:00
|
|
|
}
|
2015-05-17 15:38:02 +09:00
|
|
|
Telescope.callbacks.add("postApprovedAsync", postApprovedNotification);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
// add new comment notification callback on comment submit
|
2015-04-23 11:24:59 +09:00
|
|
|
function addCommentNotification (comment) {
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
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')
|
|
|
|
},
|
2015-06-06 00:36:58 -04:00
|
|
|
postAuthor = Users.findOne(post.userId),
|
2015-04-22 07:50:11 +09:00
|
|
|
userIdsNotified = [];
|
|
|
|
|
2015-06-06 00:36:58 -04:00
|
|
|
// 1. Notify author of post (if they have new comment notifications turned on)
|
|
|
|
// but do not notify author of post if they're the ones posting the comment
|
|
|
|
if (!!postAuthor.telescope.notifications.comments && comment.userId !== postAuthor._id) {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
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) {
|
|
|
|
|
2015-06-06 00:36:58 -04:00
|
|
|
var parentCommentAuthor = Users.findOne(parentComment.userId);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2015-06-06 00:36:58 -04:00
|
|
|
// do not notify parent comment author if they have reply notifications turned off
|
|
|
|
if (!!parentCommentAuthor.telescope.notifications.replies) {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2015-06-06 00:36:58 -04:00
|
|
|
// 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);
|
|
|
|
}
|
2015-04-22 07:50:11 +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
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return comment;
|
|
|
|
|
2015-04-23 11:24:59 +09:00
|
|
|
}
|
|
|
|
|
2015-05-17 15:38:02 +09:00
|
|
|
Telescope.callbacks.add("commentSubmitAsync", addCommentNotification);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
var emailNotifications = {
|
2015-05-10 14:36:47 +09:00
|
|
|
fieldName: 'emailNotifications',
|
|
|
|
fieldSchema: {
|
2015-04-22 07:50:11 +09:00
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
defaultValue: true,
|
|
|
|
autoform: {
|
2015-04-25 12:39:07 +09:00
|
|
|
group: 'notifications',
|
2015-04-22 07:50:11 +09:00
|
|
|
instructions: 'Enable email notifications for new posts and new comments (requires restart).'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-05-17 15:38:02 +09:00
|
|
|
Settings.addField(emailNotifications);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
// make it possible to disable notifications on a per-comment basis
|
2015-05-17 15:38:02 +09:00
|
|
|
Comments.addField(
|
2015-04-22 07:50:11 +09:00
|
|
|
{
|
2015-05-10 14:36:47 +09:00
|
|
|
fieldName: 'disableNotifications',
|
|
|
|
fieldSchema: {
|
2015-04-22 07:50:11 +09:00
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
autoform: {
|
|
|
|
omit: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-06-06 00:35:51 -04:00
|
|
|
// Add notifications options to user profile settings
|
|
|
|
Users.addField({
|
|
|
|
fieldName: 'telescope.notifications.users',
|
|
|
|
fieldSchema: {
|
|
|
|
label: 'New users',
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
editableBy: ['admin'],
|
|
|
|
autoform: {
|
|
|
|
group: 'Email Notifications'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Users.addField({
|
|
|
|
fieldName: 'telescope.notifications.posts',
|
|
|
|
fieldSchema: {
|
|
|
|
label: 'New posts',
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
editableBy: ['admin', 'member'],
|
|
|
|
autoform: {
|
|
|
|
group: 'Email Notifications'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Users.addField({
|
|
|
|
fieldName: 'telescope.notifications.comments',
|
|
|
|
fieldSchema: {
|
|
|
|
label: 'Comments on my posts',
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
editableBy: ['admin', 'member'],
|
|
|
|
autoform: {
|
|
|
|
group: 'Email Notifications'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Users.addField({
|
|
|
|
fieldName: 'telescope.notifications.replies',
|
|
|
|
fieldSchema: {
|
|
|
|
label: 'Replies to my comments',
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
editableBy: ['admin', 'member'],
|
|
|
|
autoform: {
|
|
|
|
group: 'Email Notifications'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
function setNotificationDefaults (user) {
|
|
|
|
// set notifications default preferences
|
2015-04-28 15:54:19 +09:00
|
|
|
user.telescope.notifications = {
|
2015-04-22 07:50:11 +09:00
|
|
|
users: false,
|
|
|
|
posts: false,
|
|
|
|
comments: true,
|
|
|
|
replies: true
|
|
|
|
};
|
|
|
|
return user;
|
|
|
|
}
|
2015-05-17 15:38:02 +09:00
|
|
|
Telescope.callbacks.add("onCreateUser", setNotificationDefaults);
|