Simplify email.js

This commit is contained in:
Tankred Hase 2016-05-31 16:48:18 +02:00
parent 5c675474d7
commit c7ce99a6cb

View File

@ -19,7 +19,6 @@
const log = require('npmlog'); const log = require('npmlog');
const util = require('../service/util'); const util = require('../service/util');
const message = require('./message.json');
/** /**
* A simple wrapper around Nodemailer to send verification emails * A simple wrapper around Nodemailer to send verification emails
@ -55,39 +54,20 @@ class Email {
} }
/** /**
* Send the verification email to the user to verify email address * Send the verification email to the user using a template.
* ownership. If the primary email address is provided, only one email * @param {Object} template the email template to use
* will be sent out. Otherwise all of the PGP key's user IDs will be
* verified, resulting in an email sent per user ID.
* @param {Array} userIds user id documents containing the nonces
* @param {Array} primaryEmail (optional) user's primary email address
* @param {Object} origin Required for links to the keyserver: { protocol:'https', host:'openpgpkeys@example.com' }
* @yield {undefined}
*/
*sendVerifyKey(options) {
let primaryEmail = options.primaryEmail, userIds = options.userIds, origin = options.origin;
let primaryUserId = userIds.find(uid => uid.email === primaryEmail);
if (primaryUserId) { // send only one email to the primary user id
return yield this._sendVerifyKeyHelper(primaryUserId, origin);
}
for (let uid of userIds) {
yield this._sendVerifyKeyHelper(uid, origin);
}
}
/**
* Helper function to send a verification message
* @param {Object} userId user id document * @param {Object} userId user id document
* @param {Object} origin origin of the server * @param {Object} origin origin of the server
* @yield {Object} send response from the SMTP server * @yield {Object} send response from the SMTP server
*/ */
*_sendVerifyKeyHelper(userId, origin) { *send(options) {
let msg = { let template = options.template, userId = options.userId, origin = options.origin;
let message = {
from: this._sender, from: this._sender,
to: userId, to: userId,
subject: message.verifyKey.subject, subject: template.subject,
text: message.verifyKey.text, text: template.text,
html: message.verifyKey.html, html: template.html,
params: { params: {
name: userId.name, name: userId.name,
baseUrl: origin.protocol + '://' + origin.host, baseUrl: origin.protocol + '://' + origin.host,
@ -95,33 +75,7 @@ class Email {
nonce: encodeURIComponent(userId.nonce) nonce: encodeURIComponent(userId.nonce)
} }
}; };
return yield this.send(msg); return yield this._sendHelper(message);
}
/**
* Send the verification email to the user to verify key removal. Only one email
* needs to sent to a single user id to authenticate removal of all user ids
* that belong the a certain key id.
* @param {Object} userId user id document
* @param {Object} origin origin of the server
* @yield {Object} send response from the SMTP server
*/
*sendVerifyRemove(options) {
let userId = options.userId, origin = options.origin;
let msg = {
from: this._sender,
to: userId,
subject: message.verifyRemove.subject,
text: message.verifyRemove.text,
html: message.verifyRemove.html,
params: {
name: userId.name,
baseUrl: origin.protocol + '://' + origin.host,
keyid: encodeURIComponent(userId.keyid),
nonce: encodeURIComponent(userId.nonce)
}
};
return yield this.send(msg);
} }
/** /**
@ -134,7 +88,7 @@ class Email {
* @param {Object} params (optional) nodermailer template parameters * @param {Object} params (optional) nodermailer template parameters
* @yield {Object} reponse object containing SMTP info * @yield {Object} reponse object containing SMTP info
*/ */
*send(options) { *_sendHelper(options) {
let template = { let template = {
subject: options.subject, subject: options.subject,
text: options.text, text: options.text,