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')
|
2019-10-02 21:04:03 +02:00
|
|
|
const fediverseHelpers = require('./federation/helpers')
|
2019-04-29 00:27:29 +02:00
|
|
|
|
2022-12-23 01:08:14 +01:00
|
|
|
|
|
|
|
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-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
|
|
|
|
2021-12-02 12:39:55 +01:00
|
|
|
emitter: new events.EventEmitter(),
|
|
|
|
|
2024-03-28 13:23:40 +01:00
|
|
|
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}`)
|
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':
|
2024-03-28 13:23:40 +01:00
|
|
|
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',
|
2024-04-10 11:43:04 +02:00
|
|
|
{ event, to_confirm: !event.is_visible, notification }, undefined, true)
|
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
|
|
|
|
2022-12-23 01:08:14 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 13:53:48 +02:00
|
|
|
const notifications = await Notification.findAll({ where: { action }})
|
2022-12-23 01:08:14 +01:00
|
|
|
|
|
|
|
// get notification that matches with selected event
|
|
|
|
return notifications.filter(notification => match(event, notification.filters))
|
|
|
|
},
|
|
|
|
|
2019-10-02 21:04:03 +02:00
|
|
|
async notifyEvent (action, eventId) {
|
2022-12-23 01:08:14 +01:00
|
|
|
|
2024-06-26 15:35:18 +02:00
|
|
|
const event = await Event.findByPk(eventId, { include: [ { model: Tag, attributes: ['tag'], through: { attributes: [] }}, Place],
|
|
|
|
attributes: { exclude: ['userId', 'placeId', 'image_path', 'ap_object', 'ap_id', 'apUserApId', 'likes', 'boost'] } })
|
2021-12-02 12:39:55 +01:00
|
|
|
|
2024-06-12 13:53:48 +02:00
|
|
|
// emit this notification to plugins
|
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
|
|
|
|
2024-06-12 13:53:48 +02:00
|
|
|
// select valid notification for this event
|
2022-12-23 01:08:14 +01:00
|
|
|
const notifications = await notifier.getNotifications(event, action)
|
2019-06-25 01:05:38 +02:00
|
|
|
|
2024-06-12 13:53:48 +02:00
|
|
|
const promises = notifications.map(async notification => {
|
|
|
|
const event_notification = await EventNotification.create({ eventId: event.id, notificationId: notification.id, status: 'sending' })
|
2019-06-22 18:54:35 +02:00
|
|
|
try {
|
2019-06-25 01:05:38 +02:00
|
|
|
await notifier.sendNotification(notification, event)
|
2024-06-12 13:53:48 +02:00
|
|
|
await event_notification.update({ status: 'sent' })
|
|
|
|
} catch (e) {
|
|
|
|
log.error('[NOTIFY EVENT]', e)
|
|
|
|
await event_notification.update({ status: 'error', error: String(e) })
|
2019-04-29 00:27:29 +02:00
|
|
|
}
|
2019-06-22 18:54:35 +02:00
|
|
|
})
|
2024-06-12 13:53:48 +02:00
|
|
|
|
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
|