2022-12-23 01:08:14 +01:00
|
|
|
const { Event, Place, Tag } = require('../models/models')
|
2020-06-27 02:10:10 +02:00
|
|
|
|
2022-05-03 18:02:20 +02:00
|
|
|
const { htmlToText } = require('html-to-text')
|
2022-01-14 10:59:27 +01:00
|
|
|
const { Op, literal } = require('sequelize')
|
2023-03-28 19:02:08 +02:00
|
|
|
const { DateTime } = require('luxon')
|
2019-04-03 00:25:12 +02:00
|
|
|
const ics = require('ics')
|
2023-10-30 09:36:18 +01:00
|
|
|
const collectionController = require('./collection')
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
const exportController = {
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async export (req, res) {
|
2024-01-08 00:01:19 +01:00
|
|
|
const format = req.params.format || 'json'
|
2019-04-03 00:25:12 +02:00
|
|
|
const tags = req.query.tags
|
|
|
|
const places = req.query.places
|
2024-05-24 22:13:17 +02:00
|
|
|
const collection = req.query?.collection ?? res.locals.settings.collection_in_home
|
2021-07-27 22:01:34 +02:00
|
|
|
const show_recurrent = !!req.query.show_recurrent
|
2019-07-03 16:33:03 +02:00
|
|
|
|
2023-03-28 19:02:08 +02:00
|
|
|
const opt = {
|
|
|
|
zone: res.locals.settings.instance_timezone,
|
|
|
|
locale: res.locals.settings.instance_locale
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:44:21 +02:00
|
|
|
const where = {}
|
2023-03-28 19:02:08 +02:00
|
|
|
const yesterday = DateTime.local(opt).minus({day: 1}).toUnixInteger()
|
2019-09-17 18:16:59 +02:00
|
|
|
|
2023-10-30 09:36:18 +01:00
|
|
|
if (!collection) {
|
2022-01-25 01:30:47 +01:00
|
|
|
|
2023-10-30 09:36:18 +01:00
|
|
|
if (tags && places) {
|
|
|
|
where[Op.or] = {
|
2024-05-21 12:31:48 +02:00
|
|
|
placeId: places ? places.split(',').map(Number).filter(a => a) : [],
|
2023-10-30 09:36:18 +01:00
|
|
|
'$tags.tag$': tags.split(',')
|
|
|
|
}
|
2022-01-14 10:59:27 +01:00
|
|
|
}
|
2022-01-25 01:30:47 +01:00
|
|
|
|
2023-10-30 09:36:18 +01:00
|
|
|
if (tags) {
|
|
|
|
where['$tags.tag$'] = tags.split(',')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (places) {
|
2024-05-21 12:31:48 +02:00
|
|
|
where.placeId = places.split(',').map(Number).filter(a => a)
|
2023-10-30 09:36:18 +01:00
|
|
|
}
|
2022-01-25 01:30:47 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-27 22:01:34 +02:00
|
|
|
if (!show_recurrent) {
|
|
|
|
where.parentId = null
|
|
|
|
}
|
|
|
|
|
2023-10-30 09:36:18 +01:00
|
|
|
let events = []
|
|
|
|
|
|
|
|
if (collection) {
|
2023-12-15 16:39:56 +01:00
|
|
|
events = await collectionController._getEvents({ name: collection })
|
2023-10-30 09:36:18 +01:00
|
|
|
} else {
|
|
|
|
events = await Event.findAll({
|
|
|
|
order: ['start_datetime'],
|
|
|
|
attributes: { exclude: ['is_visible', 'recurrent', 'createdAt', 'likes', 'boost', 'userId', 'placeId'] },
|
|
|
|
where: {
|
|
|
|
is_visible: true,
|
|
|
|
recurrent: null,
|
2024-01-23 09:51:45 +01:00
|
|
|
apUserApId: null,
|
2023-10-30 09:36:18 +01:00
|
|
|
start_datetime: { [Op.gte]: yesterday },
|
|
|
|
...where
|
2022-01-14 10:59:27 +01:00
|
|
|
},
|
2023-10-30 09:36:18 +01:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Tag,
|
|
|
|
order: [literal('(SELECT COUNT("tagTag") FROM event_tags WHERE tagTag = tag) DESC')],
|
|
|
|
attributes: ['tag'],
|
|
|
|
required: !!tags,
|
|
|
|
through: { attributes: [] }
|
|
|
|
},
|
|
|
|
{ model: Place, attributes: ['name', 'id', 'address'] }]
|
|
|
|
})
|
|
|
|
}
|
2019-10-02 21:04:03 +02:00
|
|
|
|
2022-06-18 01:14:26 +02:00
|
|
|
switch (format) {
|
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
|
|
|
}
|
2023-05-16 17:17:45 +02:00
|
|
|
return res.send('Please specify a valid format: rss, feed, ics or json').status(404)
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2022-06-18 01:14:26 +02:00
|
|
|
feed (_req, res, events, title = res.locals.settings.title, link = `${res.locals.settings.baseurl}/feed/rss`) {
|
2022-03-07 17:47:31 +01:00
|
|
|
const settings = res.locals.settings
|
2023-03-28 19:02:08 +02:00
|
|
|
|
|
|
|
const opt = {
|
|
|
|
zone: settings.instance_timezone,
|
|
|
|
locale: settings.instance_locale
|
|
|
|
}
|
|
|
|
|
|
|
|
function unixFormat (timestamp, format='EEEE d MMMM HH:mm') {
|
|
|
|
return DateTime.fromSeconds(timestamp, opt).toFormat(format)
|
2023-06-08 22:30:09 +02:00
|
|
|
}
|
2023-03-28 19:02:08 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
res.type('application/rss+xml; charset=UTF-8')
|
2023-03-28 19:02:08 +02:00
|
|
|
res.render('feed/rss.pug', { events, settings, unixFormat, title, link })
|
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)
|
|
|
|
*/
|
2022-05-05 11:12:19 +02:00
|
|
|
ics (_req, res, events, alarms = []) {
|
2022-03-07 17:47:31 +01:00
|
|
|
const settings = res.locals.settings
|
2019-04-03 00:25:12 +02:00
|
|
|
const eventsMap = events.map(e => {
|
2023-02-04 10:42:06 +01:00
|
|
|
|
2023-04-20 15:12:17 +02:00
|
|
|
const tmpStart = DateTime.fromSeconds(e.start_datetime, { zone: 'UTC' })
|
2023-03-28 21:48:26 +02:00
|
|
|
const start = [ tmpStart.year, tmpStart.month, tmpStart.day, tmpStart.hour, tmpStart.minute ]
|
2023-02-04 10:42:06 +01:00
|
|
|
|
2023-04-25 21:41:53 +02:00
|
|
|
const location = e.place.name !== 'online' ? `${e.place.name} - ${e.place.address}` : `${e.place.name} - ${e?.online_locations[0]}`
|
2023-02-04 10:42:06 +01:00
|
|
|
const ret = {
|
2022-11-29 23:00:42 +01:00
|
|
|
uid: `${e.id}@${settings.hostname}`,
|
2019-04-03 00:25:12 +02:00
|
|
|
start,
|
2023-04-20 15:12:17 +02:00
|
|
|
startInputType: 'utc',
|
|
|
|
endInputType: 'utc',
|
2024-01-29 12:27:39 +01:00
|
|
|
title: e.title,
|
2022-05-03 18:02:20 +02:00
|
|
|
description: htmlToText(e.description),
|
2023-10-30 09:36:18 +01:00
|
|
|
htmlContent: e.description && e.description.replace(/\n/g,"<br>"),
|
2023-04-25 21:41:53 +02:00
|
|
|
location,
|
2022-02-26 21:27:40 +01:00
|
|
|
url: `${settings.baseurl}/event/${e.slug || e.id}`,
|
2022-05-03 18:02:20 +02:00
|
|
|
status: 'CONFIRMED',
|
2023-10-30 09:36:18 +01:00
|
|
|
categories: e.tags.map(t => t.tag || t),
|
2019-11-06 11:21:00 +01:00
|
|
|
alarms
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
2023-02-04 10:42:06 +01:00
|
|
|
|
2023-04-25 21:41:53 +02:00
|
|
|
if (e.place.latitude && e.place.longitude) {
|
|
|
|
ret.geo = { lat: e.place.latitude, lon: e.place.longitude }
|
|
|
|
}
|
|
|
|
|
2023-02-04 10:42:06 +01:00
|
|
|
if (e.end_datetime) {
|
2023-04-20 15:12:17 +02:00
|
|
|
const tmpEnd = DateTime.fromSeconds(e.end_datetime, { zone: 'UTC' })
|
2023-03-28 21:48:26 +02:00
|
|
|
const end = [ tmpEnd.year, tmpEnd.month, tmpEnd.day, tmpEnd.hour, tmpEnd.minute ]
|
2023-02-04 10:42:06 +01:00
|
|
|
ret.end = end
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
2019-04-03 00:25:12 +02:00
|
|
|
})
|
|
|
|
res.type('text/calendar; charset=UTF-8')
|
2021-07-27 16:30:35 +02:00
|
|
|
ics.createEvents(eventsMap, (err, value) => {
|
|
|
|
if (err) {
|
|
|
|
return res.status(401).send(err)
|
|
|
|
}
|
|
|
|
return res.send(value)
|
|
|
|
})
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exportController
|