2019-06-18 14:45:04 +02:00
|
|
|
const mail = require('./api/mail')
|
2021-09-27 10:42:17 +02:00
|
|
|
const config = require('./config')
|
2021-03-05 14:19:52 +01:00
|
|
|
const log = require('./log')
|
2019-10-02 21:04:03 +02:00
|
|
|
const fediverseHelpers = require('./federation/helpers')
|
2019-04-29 00:27:29 +02:00
|
|
|
|
2020-06-27 02:10:10 +02:00
|
|
|
const Event = require('./api/models/event')
|
2020-07-08 00:57:28 +02:00
|
|
|
const Notification = require('./api/models/notification')
|
|
|
|
const EventNotification = require('./api/models/eventnotification')
|
|
|
|
const User = require('./api/models/user')
|
|
|
|
const Place = require('./api/models/place')
|
|
|
|
const Tag = require('./api/models/tag')
|
2020-06-27 02:10:10 +02:00
|
|
|
|
2019-10-02 21:04:03 +02:00
|
|
|
const eventController = require('./api/controller/event')
|
2021-10-21 12:23:47 +02:00
|
|
|
const settingsController = require('./api/controller/settings')
|
2019-04-29 00:27:29 +02:00
|
|
|
|
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 = []
|
2021-04-28 12:44:26 +02:00
|
|
|
log.info(`Send ${notification.type} notification ${notification.action}`)
|
2019-10-02 21:04:03 +02:00
|
|
|
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':
|
2021-10-21 12:23:47 +02:00
|
|
|
p = mail.send(settingsController.settings.admin_email, 'event',
|
2020-02-20 18:37:10 +01:00
|
|
|
{ event, to_confirm: !event.is_visible, notification })
|
2019-10-02 21:04:03 +02:00
|
|
|
promises.push(p)
|
2019-10-28 17:33:20 +01:00
|
|
|
break
|
2019-10-02 21:04:03 +02:00
|
|
|
case 'ap':
|
2019-12-04 00:50:15 +01:00
|
|
|
p = fediverseHelpers.sendEvent(event, notification.action)
|
2019-10-02 21:04:03 +02:00
|
|
|
promises.push(p)
|
2019-06-22 18:54:35 +02:00
|
|
|
}
|
|
|
|
return Promise.all(promises)
|
|
|
|
},
|
2020-01-30 17:48:09 +01:00
|
|
|
|
2019-10-02 21:04:03 +02:00
|
|
|
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-03-05 14:19:52 +01:00
|
|
|
log.debug(action, event.title)
|
2019-10-28 17:33:20 +01:00
|
|
|
|
2019-06-22 18:54:35 +02:00
|
|
|
// insert notifications
|
2019-10-02 21:04:03 +02:00
|
|
|
const notifications = await eventController.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
|
|
|
|
2019-10-02 21:04:03 +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)
|
2019-10-02 21:04:03 +02:00
|
|
|
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)
|
2019-10-02 21:04:03 +02:00
|
|
|
notification.event_notification.status = 'error'
|
2019-04-29 00:27:29 +02:00
|
|
|
}
|
2019-10-02 21:04:03 +02:00
|
|
|
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-04-29 00:27:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
module.exports = notifier
|