gancio-upstream/server/notifier.js

86 lines
2.7 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')
const settingsController = require('./api/controller/settings')
2019-06-22 18:54:35 +02:00
const config = require('config')
2019-06-25 01:05:38 +02:00
const eventController = require('./api/controller/event')
const get = require('lodash/get')
2019-09-11 19:12:24 +02:00
const { event: Event, notification: Notification, event_notification: EventNotification,
2019-06-22 18:54:35 +02:00
user: User, place: Place, tag: Tag } = require('./api/models')
2019-06-22 18:54:35 +02:00
const notifier = {
2019-09-11 19:12:24 +02:00
async sendNotification (notification, event) {
2019-07-28 20:49:39 +02:00
return
2019-06-22 18:54:35 +02:00
const promises = []
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':
return mail.send([config.smtp.auth.user, config.admin_email], 'event', { event, to_confirm: !event.is_visible, config, notification })
2019-08-25 14:34:26 +02:00
// case 'mastodon':
// // instance publish
// if (bot.bot) {
// const b = bot.post(event).then(b => {
// event.activitypub_id = String(b.data.id)
// return event.save()
// }).catch(e => {
// console.error("ERROR !! ", e)
// })
// promises.push(b)
// }
2019-06-22 18:54:35 +02:00
}
return Promise.all(promises)
},
2019-09-11 19:12:24 +02:00
async notifyEvent (eventId) {
2019-06-25 01:05:38 +02:00
const event = await Event.findByPk(eventId, {
include: [ Tag, Place, User ]
})
2019-06-22 18:54:35 +02:00
// insert notifications
const notifications = await eventController.getNotifications(event)
2019-06-25 01:05:38 +02:00
const a = await event.setNotifications(notifications)
2019-06-22 18:54:35 +02:00
2019-06-25 01:05:38 +02:00
const eventNotifications = await EventNotification.findAll({
where: {
2019-09-11 19:12:24 +02:00
notificationId: notifications.map(n => n.id),
2019-06-25 01:05:38 +02:00
status: 'new'
}
})
const promises = eventNotifications.map(async e => {
2019-06-22 18:54:35 +02:00
const notification = await Notification.findByPk(e.notificationId)
try {
2019-06-25 01:05:38 +02:00
await notifier.sendNotification(notification, event)
2019-06-22 18:54:35 +02:00
e.status = 'sent'
} catch (err) {
e.status = 'error'
}
2019-06-22 18:54:35 +02:00
return e.save()
})
2019-09-11 19:12:24 +02:00
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 {
await sendNotification(notification, event, e)
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