gancio-upstream/server/notifier.js

118 lines
4 KiB
JavaScript
Raw Normal View History

2021-12-02 12:39:55 +01:00
const events = require('events')
2019-06-18 14:45:04 +02:00
const mail = require('./api/mail')
2021-03-05 14:19:52 +01:00
const log = require('./log')
const fediverseHelpers = require('./federation/helpers')
const { Event, Notification, EventNotification, User, Place, Tag } = require('./api/models/models')
2021-10-21 12:23:47 +02:00
const settingsController = require('./api/controller/settings')
2019-06-22 18:54:35 +02:00
const notifier = {
2020-01-30 17:48:09 +01:00
2021-12-02 12:39:55 +01:00
emitter: new events.EventEmitter(),
async sendNotification (notification, event) {
2019-06-22 18:54:35 +02:00
const promises = []
2021-04-28 12:44:26 +02:00
log.info(`Send ${notification.type} notification ${notification.action}`)
let p
2019-06-22 18:54:35 +02:00
switch (notification.type) {
2020-02-20 18:37:10 +01:00
// case 'mail': TODO: locale?
// return mail.send(notification.email, 'event', { event, notification })
2019-09-11 19:12:24 +02:00
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',
2020-02-20 18:37:10 +01:00
{ event, to_confirm: !event.is_visible, notification })
promises.push(p)
2019-10-28 17:33:20 +01:00
break
case 'ap':
p = fediverseHelpers.sendEvent(event, notification.action)
promises.push(p)
2019-06-22 18:54:35 +02:00
}
return Promise.all(promises)
},
2020-01-30 17:48:09 +01:00
async getNotifications(event, action) {
log.debug(`getNotifications ${event.title} ${action}`)
function match(event, filters) {
// matches if no filter specified
if (!filters) { return true }
// check for visibility
if (typeof filters.is_visible !== 'undefined' && filters.is_visible !== event.is_visible) { return false }
if (!filters.tags && !filters.places) { return true }
if (!filters.tags.length && !filters.places.length) { return true }
if (filters.tags.length) {
const m = intersection(event.tags.map(t => t.tag), filters.tags)
if (m.length > 0) { return true }
}
if (filters.places.length) {
if (filters.places.find(p => p === event.place.name)) {
return true
}
}
}
const notifications = await Notification.findAll({ where: { action }, include: [Event] })
// get notification that matches with selected event
return notifications.filter(notification => match(event, notification.filters))
},
async notifyEvent (action, eventId) {
2019-10-28 17:33:20 +01:00
const event = await Event.findByPk(eventId, {
2020-07-08 00:57:28 +02:00
include: [Tag, Place, Notification, User]
2019-06-25 01:05:38 +02:00
})
2021-12-02 12:39:55 +01:00
notifier.emitter.emit(action, event.get({ plain: true, raw: true }))
2024-01-08 22:55:59 +01:00
log.debug('[NOTIFY] %s, %s [%d]', action, event.title, event.id)
2019-10-28 17:33:20 +01:00
2019-06-22 18:54:35 +02:00
// insert notifications
const notifications = await notifier.getNotifications(event, action)
await event.addNotifications(notifications)
2021-07-19 12:05:56 +02:00
const event_notifications = await event.getNotifications({ through: { where: { status: 'new' } } })
2019-06-25 01:05:38 +02:00
const promises = event_notifications.map(async notification => {
2019-06-22 18:54:35 +02:00
try {
2020-07-08 00:57:28 +02:00
await notification.event_notification.update({ status: 'sending' })
2019-06-25 01:05:38 +02:00
await notifier.sendNotification(notification, event)
notification.event_notification.status = 'sent'
2019-06-22 18:54:35 +02:00
} catch (err) {
2021-07-08 20:41:56 +02:00
log.error('[NOTIFY EVENT]', err)
notification.event_notification.status = 'error'
}
return notification.event_notification.save()
2019-06-22 18:54:35 +02:00
})
return Promise.all(promises)
},
2020-01-30 17:48:09 +01:00
2019-09-11 19:12:24 +02:00
async notify () {
2019-06-22 18:54:35 +02:00
// get all event notification in queue
const eventNotifications = await EventNotification.findAll({ where: { status: 'new' } })
const promises = eventNotifications.map(async e => {
const event = await Event.findByPk(e.eventId, { include: [User, Place, Tag] })
2019-09-11 19:12:24 +02:00
if (!event.place) { return }
2019-06-22 18:54:35 +02:00
const notification = await Notification.findByPk(e.notificationId)
try {
2019-10-28 17:33:20 +01:00
await notifier.sendNotification(notification, event)
2019-06-22 18:54:35 +02:00
e.status = 'sent'
return e.save()
} catch (err) {
2021-07-08 20:41:56 +02:00
log.error('[NOTIFY]', err)
2019-06-22 18:54:35 +02:00
e.status = 'error'
2020-07-08 00:57:28 +02:00
e.error = err
2019-06-22 18:54:35 +02:00
return e.save()
}
})
return Promise.all(promises)
}
}
2019-09-11 19:12:24 +02:00
module.exports = notifier