Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
feat(emails): Mailer accept multiple emails Part 1 (#1767), r=@philbooth
Browse files Browse the repository at this point in the history
  • Loading branch information
vbudhram authored Mar 28, 2017
1 parent 005eeca commit b06b0da
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 75 deletions.
21 changes: 14 additions & 7 deletions lib/routes/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ module.exports = function (
if (! account.emailVerified) {
return getGeoData(ip)
.then(function (geoData) {
mailer.sendVerifyCode(account, account.emailCode, {
mailer.sendVerifyCode([], account, {
code: account.emailCode,
service: form.service || query.service,
redirectTo: form.redirectTo,
resume: form.resume,
Expand Down Expand Up @@ -852,7 +853,8 @@ module.exports = function (
return getGeoData(ip)
.then(
function (geoData) {
return mailer.sendVerifyCode(emailRecord, emailCode, {
return mailer.sendVerifyCode([], emailRecord, {
code: emailCode,
service: service,
redirectTo: redirectTo,
resume: resume,
Expand Down Expand Up @@ -888,7 +890,8 @@ module.exports = function (
.then(
function (geoData) {
mailer.sendNewDeviceLoginNotification(
emailRecord.email,
[],
emailRecord,
{
acceptLanguage: request.app.acceptLanguage,
flowId: flowId,
Expand Down Expand Up @@ -928,10 +931,11 @@ module.exports = function (
.then(
function (geoData) {
return mailer.sendVerifyLoginEmail(
[],
emailRecord,
tokenVerificationId,
{
acceptLanguage: request.app.acceptLanguage,
code: tokenVerificationId,
flowId: flowId,
flowBeginTime: flowBeginTime,
ip: ip,
Expand Down Expand Up @@ -1690,9 +1694,10 @@ module.exports = function (
'recoveryEmailResendCode')
.then(func.bind(
mailer,
[],
sessionToken,
code,
{
code: code,
service: service,
timestamp: Date.now(),
redirectTo: request.payload.redirectTo,
Expand Down Expand Up @@ -1876,7 +1881,8 @@ module.exports = function (
// so only send it if we're sure this is for sync.
if (service === 'sync') {
return mailer.sendPostVerifyEmail(
account.email,
[],
account,
{
acceptLanguage: request.app.acceptLanguage
}
Expand Down Expand Up @@ -1972,8 +1978,9 @@ module.exports = function (
function mailUnblockCode(code) {
return getGeoData(ip)
.then((geoData) => {
return mailer.sendUnblockCode(emailRecord, code, userAgent.call({
return mailer.sendUnblockCode([], emailRecord, userAgent.call({
acceptLanguage: request.app.acceptLanguage,
unblockCode: code,
flowId: flowId,
flowBeginTime: flowBeginTime,
ip: ip,
Expand Down
14 changes: 10 additions & 4 deletions lib/routes/password.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ module.exports = function (
.then(
function (geoData) {
return mailer.sendPasswordChangedNotification(
account.email,
[],
account,
userAgent.call({
acceptLanguage: request.app.acceptLanguage,
ip: ip,
Expand Down Expand Up @@ -411,9 +412,11 @@ module.exports = function (
.then(
function (geoData) {
return mailer.sendRecoveryCode(
[],
passwordForgotToken,
passwordForgotToken.passCode,
userAgent.call({
code: passwordForgotToken.passCode,
token: passwordForgotToken,
service: service,
redirectTo: request.payload.redirectTo,
resume: request.payload.resume,
Expand Down Expand Up @@ -511,9 +514,11 @@ module.exports = function (
.then(
function (geoData) {
return mailer.sendRecoveryCode(
[],
passwordForgotToken,
passwordForgotToken.passCode,
userAgent.call({
code: passwordForgotToken.passCode,
token: passwordForgotToken,
service: service,
redirectTo: request.payload.redirectTo,
resume: request.payload.resume,
Expand Down Expand Up @@ -598,7 +603,8 @@ module.exports = function (
.then(
function (accountResetToken) {
return mailer.sendPasswordResetNotification(
passwordForgotToken.email,
[],
passwordForgotToken,
{
acceptLanguage: request.app.acceptLanguage,
flowId: flowId,
Expand Down
105 changes: 75 additions & 30 deletions lib/senders/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,35 @@ module.exports = function (log, config, error, db, translator, sender) {

var getSafeMailer = BOUNCES_ENABLED ? bounceGatedMailer : noopMailer

function getVerifiedSecondaryEmails(emais) {
return emais.reduce(function(list, email) {
if (! email.isPrimary && email.isVerified) {
list.push(email.email)
}
return list
}, [])
}

function getSecondaryEmails(emails) {
return emails.reduce(function(list, email) {
if (! email.isPrimary) {
list.push(email.email)
}
return list
}, [])
}

senders.email = {
sendVerifyCode: function (account, code, opts) {
return getSafeMailer(account.email)
sendVerifyCode: function (emails, account, opts) {
var primaryEmail = account.email
return getSafeMailer(primaryEmail)
.then(function (mailer) {
return mailer.verifyEmail({
email: account.email,
email: primaryEmail,
flowId: opts.flowId,
flowBeginTime: opts.flowBeginTime,
uid: account.uid.toString('hex'),
code: code.toString('hex'),
code: opts.code.toString('hex'),
service: opts.service,
redirectTo: opts.redirectTo,
resume: opts.resume,
Expand All @@ -119,13 +138,17 @@ module.exports = function (log, config, error, db, translator, sender) {
})
})
},
sendVerifyLoginEmail: function (account, code, opts) {
return getSafeMailer(account.email)
sendVerifyLoginEmail: function (emails, account, opts) {
var primaryEmail = account.email
var ccEmails = getVerifiedSecondaryEmails(emails)

return getSafeMailer(primaryEmail)
.then(function (mailer) {
return mailer.verifyLoginEmail({
acceptLanguage: opts.acceptLanguage || defaultLanguage,
code: code.toString('hex'),
email: account.email,
code: opts.code.toString('hex'),
ccEmails: ccEmails,
email: primaryEmail,
ip: opts.ip,
flowId: opts.flowId,
flowBeginTime: opts.flowBeginTime,
Expand All @@ -142,15 +165,19 @@ module.exports = function (log, config, error, db, translator, sender) {
})
})
},
sendRecoveryCode: function (token, code, opts) {
return getSafeMailer(token.email)
sendRecoveryCode: function (emails, account, opts) {
var primaryEmail = account.email
var ccEmails = getVerifiedSecondaryEmails(emails)

return getSafeMailer(primaryEmail)
.then(function (mailer) {
return mailer.recoveryEmail({
email: token.email,
ccEmails: ccEmails,
email: primaryEmail,
flowId: opts.flowId,
flowBeginTime: opts.flowBeginTime,
token: token.data.toString('hex'),
code: code.toString('hex'),
token: opts.token.data.toString('hex'),
code: opts.code.toString('hex'),
service: opts.service,
redirectTo: opts.redirectTo,
resume: opts.resume,
Expand All @@ -165,11 +192,15 @@ module.exports = function (log, config, error, db, translator, sender) {
})
})
},
sendPasswordChangedNotification: function (email, opts) {
return getSafeMailer(email)
sendPasswordChangedNotification: function (emails, account, opts) {
var primaryEmail = account.email
var ccEmails = getSecondaryEmails(emails)

return getSafeMailer(primaryEmail)
.then(function (mailer) {
return mailer.passwordChangedEmail({
email: email,
email: primaryEmail,
ccEmails: ccEmails,
acceptLanguage: opts.acceptLanguage || defaultLanguage,
ip: opts.ip,
location: opts.location,
Expand All @@ -180,25 +211,33 @@ module.exports = function (log, config, error, db, translator, sender) {
})
})
},
sendPasswordResetNotification: function (email, opts) {
return getSafeMailer(email)
sendPasswordResetNotification: function (emails, account, opts) {
var primaryEmail = account.email
var ccEmails = getSecondaryEmails(emails)

return getSafeMailer(primaryEmail)
.then(function (mailer) {
return mailer.passwordResetEmail({
email: email,
ccEmails: ccEmails,
email: primaryEmail,
acceptLanguage: opts.acceptLanguage || defaultLanguage,
flowId: opts.flowId,
flowBeginTime: opts.flowBeginTime,
flowBeginTime: opts.flowBeginTime
})
})
},
sendNewDeviceLoginNotification: function (email, opts) {
return getSafeMailer(email)
sendNewDeviceLoginNotification: function (emails, account, opts) {
var primaryEmail = account.email
var ccEmails = getSecondaryEmails(emails)

return getSafeMailer(primaryEmail)
.then(function (mailer) {
return mailer.newDeviceLoginEmail({
acceptLanguage: opts.acceptLanguage || defaultLanguage,
flowId: opts.flowId,
flowBeginTime: opts.flowBeginTime,
email: email,
ccEmails: ccEmails,
email: primaryEmail,
ip: opts.ip,
location: opts.location,
timeZone: opts.timeZone,
Expand All @@ -209,23 +248,29 @@ module.exports = function (log, config, error, db, translator, sender) {
})
})
},
sendPostVerifyEmail: function (email, opts) {
return getSafeMailer(email)
sendPostVerifyEmail: function (emails, account, opts) {
var primaryEmail = account.email

return getSafeMailer(primaryEmail)
.then(function (mailer) {
return mailer.postVerifyEmail({
email: email,
email: primaryEmail,
acceptLanguage: opts.acceptLanguage || defaultLanguage
})
})
},
sendUnblockCode: function (account, unblockCode, opts) {
return getSafeMailer(account.email)
sendUnblockCode: function (emails, account, opts) {
var primaryEmail = account.email
var ccEmails = getVerifiedSecondaryEmails(emails)

return getSafeMailer(primaryEmail)
.then(function (mailer) {
return mailer.unblockCodeEmail({
acceptLanguage: opts.acceptLanguage || defaultLanguage,
flowId: opts.flowId,
flowBeginTime: opts.flowBeginTime,
email: account.email,
ccEmails: ccEmails,
email: primaryEmail,
ip: opts.ip,
location: opts.location,
timeZone: opts.timeZone,
Expand All @@ -234,7 +279,7 @@ module.exports = function (log, config, error, db, translator, sender) {
uaOS: opts.uaOS,
uaOSVersion: opts.uaOSVersion,
uid: account.uid.toString('hex'),
unblockCode: unblockCode
unblockCode: opts.unblockCode
})
})
},
Expand Down
Loading

0 comments on commit b06b0da

Please sign in to comment.