2020-06-27 02:10:10 +02:00
|
|
|
const Event = require('../models/event')
|
|
|
|
const Place = require('../models/place')
|
|
|
|
const Tag = require('../models/tag')
|
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
const { Op } = require('sequelize')
|
2020-11-17 00:34:56 +01:00
|
|
|
const moment = require('dayjs')
|
2019-04-03 00:25:12 +02:00
|
|
|
const ics = require('ics')
|
|
|
|
|
|
|
|
const exportController = {
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async export (req, res) {
|
2019-04-03 00:25:12 +02:00
|
|
|
const type = req.params.type
|
|
|
|
const tags = req.query.tags
|
|
|
|
const places = req.query.places
|
2019-07-03 16:33:03 +02:00
|
|
|
|
2019-06-26 14:44:21 +02:00
|
|
|
const where = {}
|
|
|
|
const yesterday = moment().subtract('1', 'day').unix()
|
2019-07-03 16:33:03 +02:00
|
|
|
let where_tags = {}
|
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
if (tags) {
|
2019-07-03 16:33:03 +02:00
|
|
|
where_tags = { where: { tag: tags.split(',') } }
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
2019-07-03 16:33:03 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
if (places) {
|
2019-06-26 14:44:21 +02:00
|
|
|
where.placeId = places.split(',')
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
2019-09-17 18:16:59 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
const events = await Event.findAll({
|
2019-05-30 12:12:51 +02:00
|
|
|
order: ['start_datetime'],
|
2020-01-30 15:11:54 +01:00
|
|
|
attributes: { exclude: ['is_visible', 'recurrent', 'createdAt', 'updatedAt', 'likes', 'boost', 'slug', 'userId', 'placeId'] },
|
2019-06-07 17:02:33 +02:00
|
|
|
where: {
|
|
|
|
is_visible: true,
|
2020-01-30 15:11:54 +01:00
|
|
|
recurrent: { [Op.eq]: null },
|
2019-05-30 12:12:51 +02:00
|
|
|
start_datetime: { [Op.gte]: yesterday },
|
2019-06-26 14:44:21 +02:00
|
|
|
...where
|
2019-05-30 12:12:51 +02:00
|
|
|
},
|
2020-06-14 20:58:23 +02:00
|
|
|
include: [{ model: Tag, required: false, ...where_tags }, { model: Place, attributes: ['name', 'id', 'address'] }]
|
2019-04-03 00:25:12 +02:00
|
|
|
})
|
2019-10-02 21:04:03 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
switch (type) {
|
2019-09-17 18:16:59 +02:00
|
|
|
case 'rss':
|
2019-04-03 00:25:12 +02:00
|
|
|
case 'feed':
|
2019-10-20 14:22:55 +02:00
|
|
|
return exportController.feed(req, res, events.slice(0, 20))
|
2019-04-03 00:25:12 +02:00
|
|
|
case 'ics':
|
2019-10-20 14:22:55 +02:00
|
|
|
return exportController.ics(req, res, events)
|
2019-04-29 00:27:29 +02:00
|
|
|
case 'json':
|
|
|
|
return res.json(events)
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-10-20 14:22:55 +02:00
|
|
|
feed (req, res, events) {
|
2019-04-03 00:25:12 +02:00
|
|
|
res.type('application/rss+xml; charset=UTF-8')
|
2019-10-20 14:22:55 +02:00
|
|
|
res.render('feed/rss.pug', { events, settings: req.settings, moment })
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2019-11-06 11:21:00 +01:00
|
|
|
/**
|
|
|
|
* send an ics of specified events (optionally with reminders)
|
|
|
|
* @param {*} events array of events from sequelize
|
|
|
|
* @param {*} alarms https://github.com/adamgibbons/ics#attributes (alarms)
|
|
|
|
*/
|
|
|
|
ics (req, res, events, alarms = []) {
|
2019-04-03 00:25:12 +02:00
|
|
|
const eventsMap = events.map(e => {
|
2019-10-20 14:22:55 +02:00
|
|
|
const tmpStart = moment.unix(e.start_datetime)
|
|
|
|
const tmpEnd = moment.unix(e.end_datetime)
|
|
|
|
const start = tmpStart.utc(true).format('YYYY-M-D-H-m').split('-')
|
|
|
|
const end = tmpEnd.utc(true).format('YYYY-M-D-H-m').split('-')
|
2019-04-03 00:25:12 +02:00
|
|
|
return {
|
|
|
|
start,
|
2019-10-20 14:22:55 +02:00
|
|
|
// startOutputType: 'utc',
|
2019-04-03 00:25:12 +02:00
|
|
|
end,
|
2019-10-20 14:22:55 +02:00
|
|
|
// endOutputType: 'utc',
|
2019-10-23 00:25:18 +02:00
|
|
|
title: `[${req.settings.title}] ${e.title}`,
|
2019-04-03 00:25:12 +02:00
|
|
|
description: e.description,
|
2019-10-20 14:22:55 +02:00
|
|
|
location: `${e.place.name} - ${e.place.address}`,
|
2021-04-14 01:30:51 +02:00
|
|
|
url: `${req.settings.baseurl}/event/${e.slug || e.id}`,
|
2019-11-06 11:21:00 +01:00
|
|
|
alarms
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
res.type('text/calendar; charset=UTF-8')
|
2019-11-06 11:21:00 +01:00
|
|
|
const ret = ics.createEvents(eventsMap)
|
|
|
|
res.send(ret.value)
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exportController
|