gancio-upstream/server/notifier.js

77 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-06-18 14:45:04 +02:00
const mail = require('./api/mail')
2019-08-25 14:34:26 +02:00
// const bot = require('./api/controller/fediverse')
2019-06-22 18:54:35 +02:00
const config = require('config')
const debug = require('debug')('notifier')
const fediverseHelpers = require('./federation/helpers')
2019-09-11 19:12:24 +02:00
const { event: Event, notification: Notification, event_notification: EventNotification,
user: User, place: Place, tag: Tag, fed_users: FedUsers } = require('./api/models')
const eventController = require('./api/controller/event')
2019-06-22 18:54:35 +02:00
const notifier = {
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) {
case 'mail':
2019-09-11 19:12:24 +02:00
return mail.send(notification.email, 'event', { event, config, notification })
case 'admin_email':
p = mail.send([config.smtp.auth.user, config.admin_email], 'event', { event, to_confirm: !event.is_visible, config, notification })
promises.push(p)
2019-10-28 17:33:20 +01:00
break
case 'ap':
p = fediverseHelpers.sendEvent(event, event.user, notification.action)
promises.push(p)
2019-06-22 18:54:35 +02:00
}
return Promise.all(promises)
},
async notifyEvent (action, eventId) {
2019-10-28 17:33:20 +01:00
const event = await Event.findByPk(eventId, {
include: [ Tag, Place, Notification, { model: User, include: { model: FedUsers, as: 'followers' } } ]
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)
},
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