Node.js based forum software built for the modern web
refactor: one owner for the email cancel gate (#14873)
palmoni558e1d449fb1f
diff --git a/src/emailer.js b/src/emailer.jsindex a7baac6570..2d8f9b6b2e 100644--- a/src/emailer.js+++ b/src/emailer.js@@ -269,15 +269,6 @@ Emailer.send = async (template, uid, params) => {params.displayname = userData.displayname;params.rtl = translator.languageDirection(userSettings.userLang) === 'rtl';- const result = await Plugins.hooks.fire('filter:email.cancel', {- cancel: false, // set to true in plugin to cancel sending email- template: template,- params: params,- });-- if (result.cancel) {- return;- }await Emailer.sendToEmail(template, userData.email, userSettings.userLang, params);};@@ -314,6 +305,17 @@ Emailer.sendToEmail = async (template, email, language, params) => {params.unsubUrl = unsubUrl;}+ const { cancel } = await Plugins.hooks.fire('filter:email.cancel', {+ cancel: false, // set to true in plugin to cancel sending email+ template: template,+ params: params,+ });++ if (cancel) {+ winston.info(`[emailer] "${template}" email to ${email} was cancelled by a plugin.`);+ return;+ }+const result = await Plugins.hooks.fire('filter:email.params', {template: template,email: email,diff --git a/test/emailer.js b/test/emailer.jsindex 36aa96c89a..7231db18aa 100644--- a/test/emailer.js+++ b/test/emailer.js@@ -70,6 +70,29 @@ describe('emailer', () => {});});+ it('should let a plugin cancel a direct sendToEmail call', async () => {+ let sent = false;+ const sendMethod = async () => {+ sent = true;+ };+ const cancelMethod = async (data) => {+ data.cancel = data.template === template;+ return data;+ };++ Plugins.hooks.register('emailer-test', { hook: 'static:email.send', method: sendMethod });+ Plugins.hooks.register('emailer-test', { hook: 'filter:email.cancel', method: cancelMethod });++ try {+ await Emailer.sendToEmail(template, email, language, params);+ } finally {+ Plugins.hooks.unregister('emailer-test', 'static:email.send', sendMethod);+ Plugins.hooks.unregister('emailer-test', 'filter:email.cancel', cancelMethod);+ }++ assert.strictEqual(sent, false);+ });+it('should build custom template on config change', (done) => {const text = 'a random string of text';