Fix Camper News email notification logic

This commit is contained in:
Maxim Orlov
2015-04-25 16:05:36 +02:00
parent a6f6d7e1cf
commit 1539708613
4 changed files with 41 additions and 36 deletions

View File

@ -155,7 +155,7 @@ exports.returnIndividualStory = function(req, res, next) {
title: story.headline, title: story.headline,
link: story.link, link: story.link,
originalStoryLink: dashedName, originalStoryLink: dashedName,
originalStoryAuthorEmail: story.author.email || "", originalStoryAuthorEmail: story.author.email || '',
author: story.author, author: story.author,
description: story.description, description: story.description,
rank: story.upVotes.length, rank: story.upVotes.length,
@ -398,7 +398,7 @@ exports.storySubmission = function(req, res, next) {
var comment = new Comment({ var comment = new Comment({
associatedPost: data.associatedPost, associatedPost: data.associatedPost,
originalStoryLink: data.originalStoryLink, originalStoryLink: data.originalStoryLink,
originalStoryAuthorEmail: req.user.email, originalStoryAuthorEmail: data.originalStoryAuthorEmail,
body: sanitizedBody, body: sanitizedBody,
rank: 0, rank: 0,
upvotes: 0, upvotes: 0,
@ -496,53 +496,54 @@ exports.storySubmission = function(req, res, next) {
return next(err); return next(err);
} }
try { try {
Context.find({'_id': comment.associatedPost}, function (err, associatedStory) { // Based on the context retrieve the parent object of the comment (Story/Comment)
Context.find({'_id': data.associatedPost}, function (err, associatedContext) {
if (err) { if (err) {
return next(err); return next(err);
} }
associatedStory = associatedStory.pop(); associatedContext = associatedContext.pop();
if (associatedStory) { if (associatedContext) {
associatedStory.comments.push(data._id); associatedContext.comments.push(data._id);
associatedStory.save(function (err) { associatedContext.save(function (err) {
if (err) { if (err) {
return next(err); return next(err);
} }
res.send(true); res.send(true);
}); });
} }
User.findOne({'profile.username': associatedStory.author.username}, function(err, recipient) { // Find the author of the parent object
User.findOne({'profile.username': associatedContext.author.username}, function(err, recipient) {
if (err) { if (err) {
return next(err); return next(err);
} }
var recipients = ''; // If the emails of both authors differ, only then proceed with email notification
if (data.originalStoryAuthorEmail && (data.originalStoryAuthorEmail !== recipient.email)) { if (data.author.email && (data.author.email !== recipient.email)) {
recipients = data.originalStoryAuthorEmail + ',' + recipient.email; var transporter = nodemailer.createTransport({
} else { service: 'Mandrill',
recipients = recipient.email; auth: {
user: secrets.mandrill.user,
pass: secrets.mandrill.password
}
});
var mailOptions = {
to: recipient.email,
from: 'Team@freecodecamp.com',
subject: data.author.username + ' replied to your post on Camper News',
text: [
'Just a quick heads-up: ' + data.author.username + ' replied to you on Camper News.',
'You can keep this conversation going.',
'Just head back to the discussion here: http://freecodecamp.com/stories/' + data.originalStoryLink,
'- the Free Code Camp Volunteer Team'
].join('\n')
};
transporter.sendMail(mailOptions, function (err) {
if (err) {
return err;
}
});
} }
var transporter = nodemailer.createTransport({
service: 'Mandrill',
auth: {
user: secrets.mandrill.user,
pass: secrets.mandrill.password
}
});
var mailOptions = {
to: recipients,
from: 'Team@freecodecamp.com',
subject: associatedStory.author.username + " replied to your post on Camper News",
text: [
"Just a quick heads-up: " + associatedStory.author.username + " replied to you on Camper News.",
"You can keep this conversation going.",
"Just head back to the discussion here: http://freecodecamp.com/stories/" + comment.originalStoryLink,
'- the Free Code Camp Volunteer Team'
].join('\n')
};
transporter.sendMail(mailOptions, function (err) {
if (err) {
return err;
}
});
}); });
}); });
} catch (e) { } catch (e) {

View File

@ -287,6 +287,8 @@ $(document).ready(function() {
{ {
data: { data: {
associatedPost: storyId, associatedPost: storyId,
originalStoryLink: originalStoryLink,
originalStoryAuthorEmail: originalStoryAuthorEmail,
body: data body: data
} }
}) })

View File

@ -108,6 +108,7 @@
data: { data: {
associatedPost: commentId, associatedPost: commentId,
originalStoryLink: originalStoryLink, originalStoryLink: originalStoryLink,
originalStoryAuthorEmail: originalStoryAuthorEmail,
body: $('#comment-to-comment-textinput').val(), body: $('#comment-to-comment-textinput').val(),
} }
}) })

View File

@ -2,6 +2,7 @@
script. script.
var storyId = !{JSON.stringify(id)}; var storyId = !{JSON.stringify(id)};
var originalStoryLink = !{JSON.stringify(originalStoryLink)}; var originalStoryLink = !{JSON.stringify(originalStoryLink)};
var originalStoryAuthorEmail = !{JSON.stringify(originalStoryAuthorEmail)};
var comments = !{JSON.stringify(comments)}; var comments = !{JSON.stringify(comments)};
var upVotes = !{JSON.stringify(upVotes)}; var upVotes = !{JSON.stringify(upVotes)};
var image = !{JSON.stringify(image)}; var image = !{JSON.stringify(image)};