57
app/cron.js
57
app/cron.js
@@ -1,6 +1,7 @@
|
||||
const mail = require('./mail')
|
||||
const bot = require('./controller/bot')
|
||||
const settingsController = require('./controller/settings')
|
||||
const config = require('./config.js')
|
||||
|
||||
const { Event, Notification, EventNotification,
|
||||
User, Place, Tag } = require('./model')
|
||||
@@ -8,36 +9,28 @@ let settings
|
||||
|
||||
async function sendNotification (notification, event, eventNotification) {
|
||||
const promises = []
|
||||
try {
|
||||
switch (notification.type) {
|
||||
case 'mail':
|
||||
const p = mail.send(notification.email, 'event', { event })
|
||||
promises.push(p)
|
||||
break
|
||||
case 'admin_email':
|
||||
const admins = await User.findAll({ where: { is_admin: true } })
|
||||
promises.push(admins.map(admin =>
|
||||
mail.send(admin.email, 'event', { event, to_confirm: true, notification })))
|
||||
break
|
||||
case 'mastodon':
|
||||
// instance publish
|
||||
if (settings.mastodon_auth.instance && settings.mastodon_auth.access_token) {
|
||||
const b = bot.post(settings.mastodon_auth, event)
|
||||
promises.push(b)
|
||||
}
|
||||
// user publish
|
||||
if (event.user && event.user.mastodon_auth && event.user.mastodon_auth.access_token) {
|
||||
const b = bot.post(event.user.mastodon_auth, event).then(ret => {
|
||||
event.activitypub_id = ret.id
|
||||
return event.save()
|
||||
})
|
||||
promises.push(b)
|
||||
}
|
||||
break
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('CATCH!', e)
|
||||
return false
|
||||
switch (notification.type) {
|
||||
case 'mail':
|
||||
return mail.send(notification.email, 'event', { event, config, notification })
|
||||
case 'admin_email':
|
||||
const admins = await User.findAll({ where: { is_admin: true } })
|
||||
const admin_emails = admins.map(admin => admin.email)
|
||||
return mail.send(admin_emails, 'event', { event, to_confirm: true, notification })
|
||||
case 'mastodon':
|
||||
// instance publish
|
||||
if (settings.mastodon_auth.instance && settings.mastodon_auth.access_token) {
|
||||
const b = bot.post(settings.mastodon_auth, event)
|
||||
promises.push(b)
|
||||
}
|
||||
// user publish
|
||||
if (event.user && event.user.mastodon_auth && event.user.mastodon_auth.access_token) {
|
||||
const b = bot.post(event.user.mastodon_auth, event).then(ret => {
|
||||
event.activitypub_id = ret.id
|
||||
return event.save()
|
||||
})
|
||||
promises.push(b)
|
||||
}
|
||||
break
|
||||
}
|
||||
return Promise.all(promises)
|
||||
}
|
||||
@@ -54,8 +47,8 @@ async function loop () {
|
||||
await sendNotification(notification, event, e)
|
||||
e.status = 'sent'
|
||||
return e.save()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
e.status = 'error'
|
||||
return e.save()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user