This commit is contained in:
lesion
2019-06-22 18:54:35 +02:00
parent cf81a73f2f
commit 40bf6e4d1f
4 changed files with 76 additions and 54 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "gancio", "name": "gancio",
"version": "0.9.4", "version": "0.9.5",
"description": "A shared agenda for local communities", "description": "A shared agenda for local communities",
"author": "lesion", "author": "lesion",
"scripts": { "scripts": {

View File

@@ -180,8 +180,6 @@ const eventController = {
[Op.and]: [ [Op.and]: [
Sequelize.literal(`start_datetime >= ${start}`), Sequelize.literal(`start_datetime >= ${start}`),
Sequelize.literal(`start_datetime <= ${end}`) Sequelize.literal(`start_datetime <= ${end}`)
// { start_datetime: { [Op.gte]: start } },
// { start_datetime: { [Op.lte]: end } }
] ]
}, },
order: [ order: [

View File

@@ -66,6 +66,7 @@ async function setupQuestionnaire() {
name: 'db.storage', name: 'db.storage',
message: 'sqlite db path', message: 'sqlite db path',
default: '/var/gancio/db.sqlite', default: '/var/gancio/db.sqlite',
filter: p => path.resolve(p),
when: answers => answers.db.dialect === 'sqlite', when: answers => answers.db.dialect === 'sqlite',
validate: db_path => db_path.length>0 && fs.existsSync(path.dirname(db_path)) validate: db_path => db_path.length>0 && fs.existsSync(path.dirname(db_path))
}) })
@@ -102,7 +103,8 @@ async function setupQuestionnaire() {
name: 'upload_path', name: 'upload_path',
message: 'Where gancio has to store media?', message: 'Where gancio has to store media?',
default: '/var/gancio/', default: '/var/gancio/',
validate: (p) => { filter: p => path.resolve(p),
validate: p => {
const exists = fs.existsSync(p) const exists = fs.existsSync(p)
if (!exists) consola.warn(`"${p}" does not exists, please create it`) if (!exists) consola.warn(`"${p}" does not exists, please create it`)
return exists return exists

View File

@@ -1,60 +1,82 @@
const mail = require('./api/mail') const mail = require('./api/mail')
const bot = require('./api/controller/bot') const bot = require('./api/controller/bot')
const settingsController = require('./api/controller/settings') const settingsController = require('./api/controller/settings')
const config = require('./config.js') const eventController = require()
const config = require('config')
const { Event, Notification, EventNotification, const { event: Event, notification: Notification, eventNotification: EventNotification,
User, Place, Tag } = require('./api/models') user: User, place: Place, tag: Tag } = require('./api/models')
let settings
async function sendNotification(notification, event, eventNotification) { const notifier = {
const promises = [] async sendNotification(notification, event, eventNotification) {
switch (notification.type) { const promises = []
// case 'mail': switch (notification.type) {
// return mail.send(notification.email, 'event', { event, config, notification }) case 'mail':
case 'admin_email': return mail.send(notification.email, 'event', { event, config, notification })
// const admins = await User.findAll({ where: { is_admin: true } }) case 'admin_email':
// const admin_emails = admins.map(admin => admin.email) const admins = await User.findAll({ where: { is_admin: true } })
return mail.send(admin_emails, 'event', { event, to_confirm: true, notification }) const admin_emails = admins.map(admin => admin.email)
case 'mastodon': return mail.send(admin_emails, 'event', { event, to_confirm: true, notification })
// instance publish case 'mastodon':
if (settings.mastodon_auth.instance && settings.mastodon_auth.access_token) { // instance publish
const b = bot.post(settings.mastodon_auth, event).then(b => { if (settings.mastodon_auth.instance && settings.mastodon_auth.access_token) {
event.activitypub_id = b.data.id const b = bot.post(settings.mastodon_auth, event).then(b => {
// event.activitypub_ids.push(b.data.id) event.activitypub_id = b.data.id
return event.save() event.activitypub_ids.push(b.data.id)
}) return event.save()
promises.push(b) })
} promises.push(b)
} }
return Promise.all(promises)
}
async function notify() {
settings = await settingsController.settings()
// 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] })
if (!event.place) return
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'
return e.save()
} }
}) return Promise.all(promises)
},
async notifyEvent(event) {
// insert notifications
const notifications = await eventController.getNotifications(event)
await event.setNotifications(notifications)
return Promise.all(promises) const promises = notifications.map(async e => {
const notification = await Notification.findByPk(e.notificationId)
try {
await sendNotification(notification, event, e)
e.status = 'sent'
} catch (err) {
console.error(err)
e.status = 'error'
// e.error = err
}
return e.save()
})
return Promise.all(promises)
},
async notify() {
// 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] })
if (!event.place) return
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)
}
} }
let interval // let interval
function startLoop(seconds) { // function startLoop(seconds) {
interval = setInterval(notify, seconds * 1000) // interval = setInterval(notify, seconds * 1000)
} // }
startLoop(26000) // startLoop(26000)
module.exports = notifier