Merge branch 'next' into fix-live-status-changes

This commit is contained in:
Wyatt Johnson
2018-01-08 10:56:54 -07:00
committed by GitHub
5 changed files with 28 additions and 36 deletions
+18 -26
View File
@@ -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: {
+3 -3
View File
@@ -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) {
@@ -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(() => {
@@ -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(() => {
+3 -3
View File
@@ -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()', () => {