gancio-upstream/server/notifier.js

82 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-06-18 14:45:04 +02:00
const mail = require('./api/mail')
2019-06-22 18:54:35 +02:00
const config = require('config')
const debug = require('debug')('notifier')
const fediverseHelpers = require('./federation/helpers')
2020-01-30 17:48:09 +01:00
const {
event: Event, notification: Notification, event_notification: EventNotification,
2020-02-20 20:58:49 +01:00
user: User, place: Place, tag: Tag
} = require('./api/models')
const eventController = require('./api/controller/event')
2019-06-22 18:54:35 +02:00
const notifier = {
2020-01-30 17:48:09 +01:00
2019-10-28 17:33:20 +01:00
sendNotification (notification, event) {
2019-06-22 18:54:35 +02:00
const promises = []
debug('Send %s notification %s', notification.type, 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':
2020-02-20 18:37:10 +01:00
p = mail.send(config.admin_email, 'event',
{ 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 notifyEvent (action, eventId) {
2019-10-28 17:33:20 +01:00
const event = await Event.findByPk(eventId, {
2020-01-27 00:47:03 +01:00
include: [Tag, Place, Notification, { model: User }]
2019-06-25 01:05:38 +02:00
})
debug('%s -> %s', action, event.title)
2019-10-28 17:33:20 +01:00
2019-06-22 18:54:35 +02:00
// insert notifications
const notifications = await eventController.getNotifications(event, action)
await event.addNotifications(notifications)
const event_notifications = await event.getNotifications()
2019-06-25 01:05:38 +02:00
const promises = event_notifications.map(async notification => {
2019-06-22 18:54:35 +02:00
try {
// 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) {
debug(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) {
console.error(err)
e.status = 'error'
// e.error = err
return e.save()
}
})
return Promise.all(promises)
}
}
2019-09-11 19:12:24 +02:00
module.exports = notifier