feat: no more sending emails to banned users, + feature flag

isekai-main
Julian Lam 3 years ago
parent f0e32ff182
commit ea27eaf166

@ -141,6 +141,7 @@
"sendValidationEmail": 1, "sendValidationEmail": 1,
"includeUnverifiedEmails": 0, "includeUnverifiedEmails": 0,
"emailPrompt": 1, "emailPrompt": 1,
"sendEmailToBanned": 0,
"requireEmailAddress": 0, "requireEmailAddress": 0,
"inviteExpiration": 7, "inviteExpiration": 7,
"dailyDigestFreq": "off", "dailyDigestFreq": "off",

@ -43,5 +43,6 @@
"include-unverified-emails": "Send emails to recipients who have not explicitly confirmed their emails", "include-unverified-emails": "Send emails to recipients who have not explicitly confirmed their emails",
"include-unverified-warning": "By default, users with emails associated with their account have already been verified, but there are situations where this is not the case (e.g. SSO logins, grandfathered users, etc). <strong>Enable this setting at your own risk</strong> &ndash; sending emails to unverified addresses may be a violation of regional anti-spam laws.", "include-unverified-warning": "By default, users with emails associated with their account have already been verified, but there are situations where this is not the case (e.g. SSO logins, grandfathered users, etc). <strong>Enable this setting at your own risk</strong> &ndash; sending emails to unverified addresses may be a violation of regional anti-spam laws.",
"prompt": "Prompt users to enter or confirm their emails", "prompt": "Prompt users to enter or confirm their emails",
"prompt-help": "If a user does not have an email set, or their email is not confirmed, a warning will be shown on screen." "prompt-help": "If a user does not have an email set, or their email is not confirmed, a warning will be shown on screen.",
"sendEmailToBanned": "Send emails to users even if they have been banned"
} }

@ -218,7 +218,8 @@ Emailer.send = async (template, uid, params) => {
throw Error('[emailer] App not ready!'); throw Error('[emailer] App not ready!');
} }
let userData = await User.getUserFields(uid, ['email', 'username', 'email:confirmed']); let userData = await User.getUserFields(uid, ['email', 'username', 'email:confirmed', 'banned']);
userData.banned = true;
// 'welcome' and 'verify-email' explicitly used passed-in email address // 'welcome' and 'verify-email' explicitly used passed-in email address
if (['welcome', 'verify-email'].includes(template)) { if (['welcome', 'verify-email'].includes(template)) {
@ -226,6 +227,14 @@ Emailer.send = async (template, uid, params) => {
} }
({ template, userData, params } = await Plugins.hooks.fire('filter:email.prepare', { template, uid, userData, params })); ({ template, userData, params } = await Plugins.hooks.fire('filter:email.prepare', { template, uid, userData, params }));
if (!meta.config.sendEmailToBanned && template !== 'banned') {
if (userData.banned) {
winston.warn(`[emailer/send] User ${userData.username} (uid: ${uid}) is banned; not sending email due to system config.`);
return false;
}
}
if (!userData || !userData.email) { if (!userData || !userData.email) {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
winston.warn(`uid : ${uid} has no email, not sending "${template}" email.`); winston.warn(`uid : ${uid} has no email, not sending "${template}" email.`);

@ -51,6 +51,13 @@
</div> </div>
<p class="help-block">[[admin/settings/email:prompt-help]]</p> <p class="help-block">[[admin/settings/email:prompt-help]]</p>
<div class="checkbox">
<label for="sendEmailToBanned" class="mdl-switch mdl-js-switch mdl-js-ripple-effect">
<input class="mdl-switch__input" type="checkbox" id="sendEmailToBanned" data-field="sendEmailToBanned" name="sendEmailToBanned" />
<span class="mdl-switch__label">[[admin/settings/email:sendEmailToBanned]]</span>
</label>
</div>
<div class="checkbox"> <div class="checkbox">
<label for="removeEmailNotificationImages" class="mdl-switch mdl-js-switch mdl-js-ripple-effect"> <label for="removeEmailNotificationImages" class="mdl-switch mdl-js-switch mdl-js-ripple-effect">
<input class="mdl-switch__input" type="checkbox" id="removeEmailNotificationImages" data-field="removeEmailNotificationImages" name="removeEmailNotificationImages" /> <input class="mdl-switch__input" type="checkbox" id="removeEmailNotificationImages" data-field="removeEmailNotificationImages" name="removeEmailNotificationImages" />

@ -8,6 +8,8 @@ const path = require('path');
const db = require('./mocks/databasemock'); const db = require('./mocks/databasemock');
const Plugins = require('../src/plugins'); const Plugins = require('../src/plugins');
const Emailer = require('../src/emailer'); const Emailer = require('../src/emailer');
const user = require('../src/user');
const meta = require('../src/meta');
const Meta = require('../src/meta'); const Meta = require('../src/meta');
describe('emailer', () => { describe('emailer', () => {
@ -138,4 +140,42 @@ describe('emailer', () => {
'email:custom:test': '', 'email:custom:test': '',
}, done); }, done);
}); });
describe('emailer.send()', () => {
let senderUid;
let recipientUid;
before(async () => {
Plugins.hooks.register('emailer-test', {
hook: 'filter:email.send',
method: async () => {}, // noop
});
senderUid = await user.create({ username: 'sender' });
recipientUid = await user.create({ username: 'recipient', email: 'test@example.org' });
await user.email.confirmByUid(recipientUid);
});
it('should return false on attempted email send to a banned user', async () => {
await user.bans.ban(recipientUid);
const success = await Emailer.send('test', recipientUid, {});
assert.strictEqual(success, false);
});
it('should return true if the template is "banned"', async () => {
const success = await Emailer.send('banned', recipientUid, {});
assert.strictEqual(success, true);
});
it('should return true if system settings allow sending to banned users', async () => {
meta.config.sendEmailToBanned = 1;
const success = await Emailer.send('test', recipientUid, {});
assert.strictEqual(success, true);
meta.config.sendEmailToBanned = 0;
});
after(() => {
Plugins.hooks.unregister('emailer-test', 'filter:email.send');
});
});
}); });

Loading…
Cancel
Save