Vulcan/packages/vulcan-notifications/lib/callbacks.js

88 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-03-23 16:27:59 +09:00
import Users from 'meteor/vulcan:users';
import { addCallback } from 'meteor/vulcan:core';
import { createNotification } from './notifications.js';
// note: leverage weak dependencies on packages
2017-03-23 16:27:59 +09:00
const Comments = Package['vulcan:comments'] ? Package['vulcan:comments'].default : null;
const Posts = Package['vulcan:posts'] ? Package['vulcan:posts'].default : null;
/**
* @summary Add notification callback when a post is approved
*/
function PostsApprovedNotification (post) {
2017-08-24 13:16:50 +09:00
createNotification(post.userId, 'postApproved', {documentId: post._id});
}
/**
* @summary Add new post notification callback on post submit
*/
function PostsNewNotifications (post) {
let adminIds = _.pluck(Users.adminUsers({fields: {_id:1}}), '_id');
let notifiedUserIds = _.pluck(Users.find({'notifications_posts': true}, {fields: {_id:1}}).fetch(), '_id');
// remove post author ID from arrays
adminIds = _.without(adminIds, post.userId);
notifiedUserIds = _.without(notifiedUserIds, post.userId);
if (post.status === Posts.config.STATUS_PENDING && !!adminIds.length) {
// if post is pending, only notify admins
2017-08-24 13:16:50 +09:00
createNotification(adminIds, 'newPendingPost', {documentId: post._id});
} else if (!!notifiedUserIds.length) {
// if post is approved, notify everybody
2017-08-24 13:16:50 +09:00
createNotification(notifiedUserIds, 'newPost', {documentId: post._id});
}
}
if (!!Posts) {
addCallback("posts.approve.async", PostsApprovedNotification);
addCallback("posts.new.async", PostsNewNotifications);
}
2017-03-27 14:50:07 -04:00
// add new comment notification callback on comment submit
function CommentsNewNotifications (comment) {
// note: dummy content has disableNotifications set to true
if(Meteor.isServer && !comment.disableNotifications) {
2017-03-27 14:50:07 -04:00
const post = Posts.findOne(comment.postId);
const postAuthor = Users.findOne(post.userId);
2017-08-24 13:16:50 +09:00
let userIdsNotified = [];
// 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 (Users.getSetting(postAuthor, "notifications_comments", false) && comment.userId !== postAuthor._id) {
2017-08-24 13:16:50 +09:00
createNotification(post.userId, 'newComment', {documentId: comment._id});
userIdsNotified.push(post.userId);
}
2017-03-27 14:50:07 -04:00
// 2. Notify author of comment being replied to
if (!!comment.parentCommentId) {
const 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) {
const parentCommentAuthor = Users.findOne(parentComment.userId);
// do not notify parent comment author if they have reply notifications turned off
if (Users.getSetting(parentCommentAuthor, "notifications_replies", false)) {
2017-08-24 13:16:50 +09:00
createNotification(parentComment.userId, 'newReply', {documentId: parentComment._id});
userIdsNotified.push(parentComment.userId);
}
}
2017-03-27 14:50:07 -04:00
}
}
}
2017-03-27 14:50:07 -04:00
if (!!Comments) {
2017-03-27 14:50:07 -04:00
addCallback("comments.new.async", CommentsNewNotifications);
}