mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 01:51:40 -05:00
clean up notifications/email code
This commit is contained in:
parent
bac853d438
commit
69dc464902
5 changed files with 22 additions and 29 deletions
|
@ -96,13 +96,14 @@ VulcanEmail.send = (to, subject, html, text) => {
|
|||
|
||||
};
|
||||
|
||||
VulcanEmail.buildAndSend = async ({ to, email, variables }) => {
|
||||
VulcanEmail.buildAndSend = async ({ to, emailName, variables }) => {
|
||||
|
||||
// execute email's GraphQL query
|
||||
const email = VulcanEmail.emails[emailName];
|
||||
const result = email.query ? await runQuery(email.query, variables) : {data: {}};
|
||||
|
||||
// if email has a data() function, merge its return value with results from the query
|
||||
const emailData = email.data ? {...result.data, ...email.data()} : result.data;
|
||||
const emailData = email.data ? {...result.data, ...email.data(variables)} : result.data;
|
||||
|
||||
const subject = typeof email.subject === 'function' ? email.subject(emailData) : email.subject;
|
||||
const html = VulcanEmail.buildTemplate(VulcanEmail.getTemplate(email.template)(emailData));
|
||||
|
|
|
@ -4,6 +4,8 @@ import VulcanEmail from '../namespace.js';
|
|||
|
||||
Meteor.startup(function () {
|
||||
|
||||
console.log(VulcanEmail.emails)
|
||||
|
||||
_.forEach(VulcanEmail.emails, (email, key) => {
|
||||
|
||||
// template live preview routes
|
||||
|
@ -19,10 +21,11 @@ Meteor.startup(function () {
|
|||
} else {
|
||||
|
||||
// else get test object (sample post, comment, user, etc.)
|
||||
const result = email.query ? await runQuery(email.query, email.testVariables || {}) : {data: {}};
|
||||
const testVariables = email.testVariables || {};
|
||||
const result = email.query ? await runQuery(email.query, testVariables) : {data: {}};
|
||||
|
||||
// if email has a data() function, merge it with results of query
|
||||
const emailTestData = email.data ? {...result.data, ...email.data()} : result.data;
|
||||
const emailTestData = email.data ? {...result.data, ...email.data(testVariables)} : result.data;
|
||||
const subject = typeof email.subject === 'function' ? email.subject(emailTestData) : email.subject;
|
||||
|
||||
// then apply email template to properties, and wrap it with buildTemplate
|
||||
|
|
|
@ -139,14 +139,17 @@ export const GraphQLSchema = {
|
|||
mainSchema.push(field.resolveAs);
|
||||
} else {
|
||||
|
||||
// get resolver name from resolveAs object, or else default to field name
|
||||
const resolverName = field.resolveAs.fieldName || fieldName;
|
||||
|
||||
// if resolveAs is an object, first push its type definition
|
||||
// include arguments if there are any
|
||||
mainSchema.push(`${field.resolveAs.fieldName}${field.resolveAs.arguments ? `(${field.resolveAs.arguments})` : ''}: ${field.resolveAs.type}`);
|
||||
mainSchema.push(`${resolverName}${field.resolveAs.arguments ? `(${field.resolveAs.arguments})` : ''}: ${field.resolveAs.type}`);
|
||||
|
||||
// then build actual resolver object and pass it to addGraphQLResolvers
|
||||
const resolver = {
|
||||
[mainTypeName]: {
|
||||
[field.resolveAs.fieldName]: field.resolveAs.resolver
|
||||
[resolverName]: field.resolveAs.resolver
|
||||
}
|
||||
};
|
||||
addGraphQLResolvers(resolver);
|
||||
|
|
|
@ -10,10 +10,7 @@ const Posts = Package['vulcan:posts'] ? Package['vulcan:posts'].default : null;
|
|||
* @summary Add notification callback when a post is approved
|
||||
*/
|
||||
function PostsApprovedNotification (post) {
|
||||
const notificationData = {
|
||||
post: _.pick(post, '_id', 'userId', 'title', 'url')
|
||||
};
|
||||
createNotification(post.userId, 'postApproved', notificationData);
|
||||
createNotification(post.userId, 'postApproved', {documentId: post._id});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,20 +21,16 @@ 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');
|
||||
|
||||
const notificationData = {
|
||||
post: _.pick(post, '_id', 'userId', 'title', 'url', 'slug')
|
||||
};
|
||||
|
||||
// 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
|
||||
createNotification(adminIds, 'newPendingPost', notificationData);
|
||||
createNotification(adminIds, 'newPendingPost', {documentId: post._id});
|
||||
} else if (!!notifiedUserIds.length) {
|
||||
// if post is approved, notify everybody
|
||||
createNotification(notifiedUserIds, 'newPost', notificationData);
|
||||
createNotification(notifiedUserIds, 'newPost', {documentId: post._id});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -55,17 +48,14 @@ function CommentsNewNotifications (comment) {
|
|||
|
||||
const post = Posts.findOne(comment.postId);
|
||||
const postAuthor = Users.findOne(post.userId);
|
||||
const notificationData = {
|
||||
comment: _.pick(comment, '_id', 'userId', 'author', 'htmlBody', 'postId'),
|
||||
post: _.pick(post, '_id', 'userId', 'title', 'url')
|
||||
};
|
||||
|
||||
|
||||
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) {
|
||||
createNotification(post.userId, 'newComment', notificationData);
|
||||
createNotification(post.userId, 'newComment', {documentId: comment._id});
|
||||
userIdsNotified.push(post.userId);
|
||||
}
|
||||
|
||||
|
@ -82,11 +72,7 @@ function CommentsNewNotifications (comment) {
|
|||
|
||||
// do not notify parent comment author if they have reply notifications turned off
|
||||
if (Users.getSetting(parentCommentAuthor, "notifications_replies", false)) {
|
||||
|
||||
// add parent comment to notification data
|
||||
notificationData.parentComment = _.pick(parentComment, '_id', 'userId', 'author', 'htmlBody');
|
||||
|
||||
createNotification(parentComment.userId, 'newReply', notificationData);
|
||||
createNotification(parentComment.userId, 'newReply', {documentId: parentComment._id});
|
||||
userIdsNotified.push(parentComment.userId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,18 +2,18 @@ import Users from 'meteor/vulcan:users';
|
|||
import VulcanEmail from 'meteor/vulcan:email';
|
||||
import { getSetting } from 'meteor/vulcan:core';
|
||||
|
||||
export const createNotification = (userIds, notificationName, data) => {
|
||||
export const createNotification = (userIds, notificationName, variables) => {
|
||||
|
||||
if (getSetting('emailNotifications', true)) {
|
||||
// if userIds is not an array, wrap it in one
|
||||
if (!Array.isArray(userIds)) userIds = [userIds];
|
||||
|
||||
const email = VulcanEmail.emails[notificationName];
|
||||
const emailName = notificationName;
|
||||
|
||||
userIds.forEach(userId => {
|
||||
const to = Users.getEmail(Users.findOne(userId));
|
||||
if (to) {
|
||||
VulcanEmail.buildAndSend({ to, email, data });
|
||||
VulcanEmail.buildAndSend({ to, emailName, variables });
|
||||
} else {
|
||||
console.log(`// Couldn't send notification: admin user ${user._id} doesn't have an email`); // eslint-disable-line
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue