From 615be40b454a78c07985ad6583ebfe7eef25a1a7 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 8 Jan 2018 10:43:10 -0700 Subject: [PATCH] refactored mailer service with lodash! --- services/mailer.js | 44 ++++++++----------- services/users.js | 6 +-- .../graph/mutations/setUserBanStatus.js | 4 +- .../mutations/setUserSuspensionStatus.js | 4 +- test/server/services/users.js | 6 +-- 5 files changed, 28 insertions(+), 36 deletions(-) diff --git a/services/mailer.js b/services/mailer.js index ecb65de8c..08f1d6c29 100644 --- a/services/mailer.js +++ b/services/mailer.js @@ -1,13 +1,12 @@ const debug = require('debug')('talk:services:mailer'); const nodemailer = require('nodemailer'); const kue = require('./kue'); +const i18n = require('./i18n'); const path = require('path'); -const fs = require('fs'); +const fs = require('fs-extra'); const _ = require('lodash'); const {TEMPLATE_LOCALS} = require('../middleware/staticTemplate'); -const i18n = require('./i18n'); - const { SMTP_HOST, SMTP_USERNAME, @@ -23,38 +22,29 @@ const templates = { }; // load the templates per request during development -templates.render = (name, format = 'txt', context) => new Promise((resolve, reject) => { +templates.render = async (name, format = 'txt', context) => { - // If we are in production mode, check the view cache. if (process.env.NODE_ENV === 'production') { - if (name in templates.data && format in templates.data[name]) { - let view = templates.data[name][format]; - return resolve(view(context)); + // If we are in production mode, check the view cache. + const view = _.get(templates.data, [name, format], null); + if (view !== null) { + return view(context); } } const filename = path.join(__dirname, 'email', [name, format, 'ejs'].join('.')); + const file = await fs.readFile(filename, 'utf8'); + const view = _.template(file); - fs.readFile(filename, (err, file) => { - if (err) { - return reject(err); - } - - let view = _.template(file); + if (process.env.NODE_ENV === 'production') { // If we are in production mode, fill the view cache. - if (process.env.NODE_ENV === 'production') { - if (!(name in templates.data)) { - templates.data[name] = {}; - } + _.set(templates.data, [name, format], view); + } - templates.data[name][format] = view; - } - - return resolve(view(context)); - }); -}); + return view(context); +}; const mailer = {}; @@ -103,7 +93,9 @@ mailer.task = new kue.Task({ */ mailer.send = async (options) => { if (!mailer.enabled) { - throw new Error('email is not enabled because required configuration is not available'); + const err = new Error('sending email is not enabled because required configuration is not available'); + console.warn(err); + return; } // Create the new locals object and attach the static locals and the i18n @@ -118,7 +110,7 @@ mailer.send = async (options) => { return templates.render(options.template, fmt, locals); })); - // Create the job. + // Create the job to send the email later. return mailer.task.create({ title: 'Mail', message: { diff --git a/services/users.js b/services/users.js index c9b0a73fd..33c6047fa 100644 --- a/services/users.js +++ b/services/users.js @@ -24,7 +24,7 @@ const RECAPTCHA_WINDOW = '10m'; // 10 minutes. const RECAPTCHA_INCORRECT_TRIGGER = 5; // after 3 incorrect attempts, recaptcha will be required. const ActionsService = require('./actions'); -const MailerService = require('./mailer'); +const mailer = require('./mailer'); const i18n = require('./i18n'); const Wordlist = require('./wordlist'); const DomainList = require('./domain_list'); @@ -423,7 +423,7 @@ class UsersService { redirectURI ); - return MailerService.send({ + return mailer.send({ template: 'email-confirm', locals: { token, @@ -449,7 +449,7 @@ class UsersService { to, }); - return MailerService.send(options); + return mailer.send(options); } static async changePassword(id, password) { diff --git a/test/server/graph/mutations/setUserBanStatus.js b/test/server/graph/mutations/setUserBanStatus.js index 37a6bac62..3b62ba327 100644 --- a/test/server/graph/mutations/setUserBanStatus.js +++ b/test/server/graph/mutations/setUserBanStatus.js @@ -5,7 +5,7 @@ const Context = require('../../../../graph/context'); const SettingsService = require('../../../../services/settings'); const UserModel = require('../../../../models/user'); const UsersService = require('../../../../services/users'); -const MailerService = require('../../../../services/mailer'); +const mailer = require('../../../../services/mailer'); const sinon = require('sinon'); const chai = require('chai'); @@ -22,7 +22,7 @@ describe('graph.mutations.banUser', () => { let spy; before(() => { - spy = sinon.spy(MailerService, 'send'); + spy = sinon.spy(mailer, 'send'); }); afterEach(() => { diff --git a/test/server/graph/mutations/setUserSuspensionStatus.js b/test/server/graph/mutations/setUserSuspensionStatus.js index 5ac46fe84..3a18ad00a 100644 --- a/test/server/graph/mutations/setUserSuspensionStatus.js +++ b/test/server/graph/mutations/setUserSuspensionStatus.js @@ -6,7 +6,7 @@ const Context = require('../../../../graph/context'); const SettingsService = require('../../../../services/settings'); const UserModel = require('../../../../models/user'); const UsersService = require('../../../../services/users'); -const MailerService = require('../../../../services/mailer'); +const mailer = require('../../../../services/mailer'); const sinon = require('sinon'); const chai = require('chai'); @@ -24,7 +24,7 @@ describe('graph.mutations.suspendUser', () => { let spy; before(() => { - spy = sinon.spy(MailerService, 'send'); + spy = sinon.spy(mailer, 'send'); }); afterEach(() => { diff --git a/test/server/services/users.js b/test/server/services/users.js index 343b73bdf..837ce9fa2 100644 --- a/test/server/services/users.js +++ b/test/server/services/users.js @@ -1,6 +1,6 @@ const UsersService = require('../../../services/users'); const SettingsService = require('../../../services/settings'); -const MailerService = require('../../../services/mailer'); +const mailer = require('../../../services/mailer'); const chai = require('chai'); chai.use(require('chai-as-promised')); @@ -29,11 +29,11 @@ describe('services.UsersService', () => { password: '3Coral!3' }]); - sinon.spy(MailerService, 'send'); + sinon.spy(mailer, 'send'); }); afterEach(() => { - MailerService.send.restore(); + mailer.send.restore(); }); describe('#findById()', () => {