update all emails to use GraphQL queries; various fixes

This commit is contained in:
SachaG 2017-08-20 17:02:30 +09:00
parent 44687974ac
commit 0a6034aed1
6 changed files with 90 additions and 115 deletions

View file

@ -4,5 +4,6 @@ addRoute([
// {name: "cheatsheet", path: "/cheatsheet", component: import('./components/Cheatsheet.jsx')},
{name: "groups", path: "/groups", component: () => getDynamicComponent(import('./components/Groups.jsx'))},
{name: "settings", path: "/settings", component: () => getDynamicComponent(import('./components/Settings.jsx'))},
{name: "emails", path: "/emails", component: () => getDynamicComponent(import('./components/Emails.jsx'))},
// {name: "emails", path: "/emails", component: () => getDynamicComponent(import('./components/Emails.jsx'))},
{name: "emails", path: "/emails", componentName: 'Emails'},
]);

View file

@ -19,7 +19,7 @@ Meteor.startup(function () {
} else {
// else get test object (sample post, comment, user, etc.)
const result = await runQuery(email.query, email.testVariables)
const result = await runQuery(email.query, email.testVariables || {})
const emailTestData = result.data;
const subject = typeof email.subject === 'function' ? email.subject(emailTestData) : email.subject;
@ -28,12 +28,8 @@ Meteor.startup(function () {
html += `
<h4 style="margin: 20px;"><code>Subject: ${subject}</code></h4>
<div style="border: 1px solid #999; padding: 20px; margin: 20px;">
<pre>
<code>
${JSON.stringify(emailTestData, null, '\t')}
</code>
</pre>
<div style="border: 1px solid #999; padding: 10px 20px; margin: 20px;">
<pre><code>${JSON.stringify(emailTestData, null, 2)}</code></pre>
</div>
`
}

View file

@ -1,70 +1,99 @@
import Users from 'meteor/vulcan:users';
import VulcanEmail from 'meteor/vulcan:email';
// note: leverage weak dependencies on packages
const Comments = Package['vulcan:comments'] ? Package['vulcan:comments'].default : null;
const Posts = Package['vulcan:posts'] ? Package['vulcan:posts'].default : null;
const getTestUser = userId => typeof Users.findOne(userId) === "undefined" ? Users.findOne() : Users.findOne(userId);
VulcanEmail.addEmails({
newUser: {
template: "newUser",
path: "/email/new-user/:_id?",
getProperties: Users.getNotificationProperties,
subject() {
return "A new user has been created";
},
getTestObject: getTestUser,
query: `
query UsersSingleQuery($documentId: String){
UsersSingle(documentId: $documentId){
displayName
profileUrl
}
}
`
},
accountApproved: {
template: "accountApproved",
path: "/email/account-approved/:_id?",
getProperties: Users.getNotificationProperties,
subject() {
return "Your account has been approved.";
},
getTestObject: getTestUser,
query: `
query UsersSingleQuery($documentId: String){
UsersSingle(documentId: $documentId){
displayName
}
SiteData{
title
url
}
}
`
}
});
if (!!Posts) {
const getTestPost = postId => typeof Posts.findOne(postId) === "undefined" ? {post: Posts.findOne()} : {post: Posts.findOne(postId)};
const postsQuery = `
query PostsSingleQuery($documentId: String){
PostsSingle(documentId: $documentId){
title
url
pageUrl
linkUrl
htmlBody
thumbnailUrl
user{
profileUrl
displayName
}
}
}
`
const dummyPost = {title: '[title]', user: {displayName: '[user]'}};
VulcanEmail.addEmails({
newPost: {
template: "newPost",
path: "/email/new-post/:_id?",
getProperties: Posts.getNotificationProperties,
subject({postAuthorName="[postAuthorName]", postTitle="[postTitle]"}) {
return postAuthorName+' has created a new post: '+postTitle;
subject(data) {
const post = _.isEmpty(data) ? dummyPost : data.PostsSingle;
return post.user.displayName+' has created a new post: '+post.title;
},
getTestObject: getTestPost,
query: postsQuery
},
newPendingPost: {
template: "newPendingPost",
path: "/email/new-pending-post/:_id?",
getProperties: Posts.getNotificationProperties,
subject({postAuthorName="[postAuthorName]", postTitle="[postTitle]"}) {
return postAuthorName+' has a new post pending approval: '+postTitle;
subject(data) {
const post = _.isEmpty(data) ? dummyPost : data.PostsSingle;
return post.user.displayName+' has a new post pending approval: '+post.title;
},
getTestObject: getTestPost,
query: postsQuery
},
postApproved: {
template: "postApproved",
path: "/email/post-approved/:_id?",
getProperties: Posts.getNotificationProperties,
subject({postTitle="[postTitle]"}) {
return 'Your post “'+postTitle+'” has been approved';
subject(data) {
const post = _.isEmpty(data) ? dummyPost : data.PostsSingle;
return 'Your post “'+post.title+'” has been approved';
},
getTestObject: getTestPost,
query: postsQuery
}
});
@ -74,38 +103,55 @@ if (!!Posts) {
if (!!Comments) {
const getTestComment = commentId => typeof Comments.findOne(commentId) === "undefined" ? {comment: Comments.findOne()} : {comment: Comments.findOne(commentId)};
const commentsQuery = `
query CommentsSingleQuery($documentId: String){
CommentsSingle(documentId: $documentId){
pageUrl
htmlBody
post{
pageUrl
title
}
user{
profileUrl
displayName
}
}
}
`
const dummyComment = {post: {title: '[title]'}, user: {displayName: '[user]'}};
VulcanEmail.addEmails({
newComment: {
template: "newComment",
path: "/email/new-comment/:_id?",
getProperties: Comments.getNotificationProperties,
subject({authorName = "[authorName]", postTitle = "[postTitle]"}) {
return authorName+' left a new comment on your post "' + postTitle + '"';
subject(data) {
const comment = _.isEmpty(data) ? dummyComment : data.CommentsSingle;
return comment.user.displayName+' left a new comment on your post "' + comment.post.title + '"';
},
getTestObject: getTestComment,
query: commentsQuery
},
newReply: {
template: "newReply",
path: "/email/new-reply/:_id?",
getProperties: Comments.getNotificationProperties,
subject({authorName = "[authorName]", postTitle = "[postTitle]"}) {
return authorName+' replied to your comment on "'+postTitle+'"';
subject(data) {
const comment = _.isEmpty(data) ? dummyComment : data.CommentsSingle;
return comment.user.displayName+' replied to your comment on "'+comment.post.title+'"';
},
getTestObject: getTestComment,
query: commentsQuery
},
newCommentSubscribed: {
template: "newComment",
path: "/email/new-comment-subscribed/:_id?",
getProperties: Comments.getNotificationProperties,
subject({authorName = "[authorName]", postTitle = "[postTitle]"}) {
return authorName+' left a new comment on "' + postTitle + '"';
subject(data) {
const comment = _.isEmpty(data) ? dummyComment : data.CommentsSingle;
return comment.user.displayName+' left a new comment on "' + comment.post.title + '"';
},
getTestObject: getTestComment,
query: commentsQuery
}
});

View file

@ -1,62 +0,0 @@
import Users from 'meteor/vulcan:users';
import { Utils, getSetting } from 'meteor/vulcan:core';
// note: leverage weak dependencies on packages
const Comments = Package['vulcan:comments'] ? Package['vulcan:comments'].default : null;
const Posts = Package['vulcan:posts'] ? Package['vulcan:posts'].default : null;
Users.getNotificationProperties = user => {
const properties = {
profileUrl: Users.getProfileUrl(user),
displayName: Users.getDisplayName(user),
siteTitle: getSetting('title'),
siteUrl: Utils.getSiteUrl(),
};
return properties;
};
if (!!Posts) {
Posts.getNotificationProperties = data => {
const post = data.post;
const postAuthor = Users.findOne(post.userId);
const properties = {
postAuthorName : Posts.getAuthorName(post),
postTitle : Utils.cleanUp(post.title),
profileUrl: Users.getProfileUrl(postAuthor, true),
postUrl: Posts.getPageUrl(post, true),
thumbnailUrl: post.thumbnailUrl,
linkUrl: !!post.url ? Utils.getOutgoingUrl(post.url) : Posts.getPageUrl(post, true),
};
if(post.url)
properties.url = post.url;
if(post.htmlBody)
properties.htmlBody = post.htmlBody;
return properties;
};
}
if (!!Comments) {
Comments.getNotificationProperties = data => {
const comment = data.comment;
const commentAuthor = Users.findOne(comment.userId);
const post = Posts.findOne(comment.postId);
const properties = {
profileUrl: commentAuthor && Users.getProfileUrl(commentAuthor, true),
postUrl: Posts.getPageUrl(post, true),
authorName : Comments.getAuthorName(comment),
postTitle: post.title,
htmlBody: comment.htmlBody,
commentUrl: Comments.getPageUrl(comment, true),
};
return properties;
};
}

View file

@ -1,4 +1,3 @@
import './helpers.js';
import './emails.js';
import './callbacks.js';
import './custom_fields.js';

View file

@ -8,17 +8,12 @@ export const createNotification = (userIds, notificationName, data) => {
// if userIds is not an array, wrap it in one
if (!Array.isArray(userIds)) userIds = [userIds];
const email = VulcanEmail.emails[notificationName];
userIds.forEach(userId => {
const user = Users.findOne(userId);
const email = VulcanEmail.emails[notificationName];
const properties = email.getProperties(data);
const subject = typeof email.subject === 'function' ? email.subject(properties) : email.subject;
const html = VulcanEmail.getTemplate(email.template)(properties);
const userEmail = Users.getEmail(user);
if (!!userEmail) {
VulcanEmail.buildAndSendHTML(Users.getEmail(user), subject, html);
const to = Users.getEmail(Users.findOne(userId));
if (to) {
VulcanEmail.buildAndSend({ to, email, data });
} else {
console.log(`// Couldn't send notification: admin user ${user._id} doesn't have an email`); // eslint-disable-line
}