diff --git a/server/api/controller/event.js b/server/api/controller/event.js index 9f587fe2..11d090ad 100644 --- a/server/api/controller/event.js +++ b/server/api/controller/event.js @@ -315,13 +315,9 @@ const eventController = { author }) - const admins = await User.findAll({ where: { role: ['admin', 'editor'], is_active: true }, attributes: ['email'], raw: true }) - let emails = [res.locals.settings.admin_email] - emails = emails.concat(admins?.map(a => a.email)) - log.info('[EVENT] Report event to %s', emails) - // notify admins - mail.send(emails, 'report', { event, message: body.message, author }, undefined, true) + notifier.notifyAdmins('report', { event, message: body.message, author }) + log.info('[EVENT] Report event to admins') // notify author if (event['user.email'] && body.is_author_visible && !isMine) { diff --git a/server/api/controller/user.js b/server/api/controller/user.js index f7272236..f7851020 100644 --- a/server/api/controller/user.js +++ b/server/api/controller/user.js @@ -6,6 +6,7 @@ const { User } = require('../models/models') const settingsController = require('./settings') const log = require('../../log') const linkify = require('linkifyjs') +const notifier = require('../../notifier') const userController = { @@ -95,9 +96,9 @@ const userController = { log.info('Register user ', req.body.email) const user = await User.create(req.body) - log.info(`Sending registration email to ${user.email}`) + log.info(`Sending registration email to ${user.email} and registration confirmation to admins`) mail.send(user.email, 'register', { user, config }, res.locals.locale) - mail.send(settingsController.settings.admin_email, 'admin_register', { user, config }) + notifier.notifyAdmins('admin_register', { user, config }) res.sendStatus(200) } catch (e) { log.error('Registration error:', e) diff --git a/server/notifier.js b/server/notifier.js index 960cfa54..4d71c13b 100644 --- a/server/notifier.js +++ b/server/notifier.js @@ -1,5 +1,4 @@ const events = require('events') - const mail = require('./api/mail') const log = require('./log') const fediverseHelpers = require('./federation/helpers') @@ -22,11 +21,7 @@ const notifier = { // case 'mail': TODO: locale? // return mail.send(notification.email, 'event', { event, notification }) case 'admin_email': - const admins = await User.findAll({ where: { role: ['admin', 'editor'], is_active: true }, attributes: ['email'], raw: true }) - let emails = [settingsController.settings.admin_email] - emails = emails.concat(admins?.map(a => a.email)) - p = mail.send(emails, 'event', - { event, to_confirm: !event.is_visible, notification }, undefined, true) + p = notifier.notifyAdmins('event', { event, to_confirm: !event.is_visible, notification }) promises.push(p) break case 'ap': @@ -62,7 +57,19 @@ const notifier = { // get notification that matches with selected event return notifications.filter(notification => match(event, notification.filters)) - }, + }, + + /** + * Send admins an email notification + * @param {String} template The template to use to build the email (./server/emails/) + * @param {Object} locals Locals key/value object used in templates + */ + async notifyAdmins (template, locals) { + const admins = await User.findAll({ where: { role: ['admin', 'editor'], is_active: true }, attributes: ['email'], raw: true }) + let emails = [settingsController.settings.admin_email] + emails = emails.concat(admins?.map(a => a.email)) + return mail.send(emails, template, locals) + }, async notifyEvent (action, eventId) {