gancio-upstream/server/api/controller/export.js

155 lines
4.6 KiB
JavaScript
Raw Normal View History

const { Event, Place, Tag } = require('../models/models')
2020-06-27 02:10:10 +02:00
const { htmlToText } = require('html-to-text')
const { Op, literal } = require('sequelize')
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) {
const format = req.params.format || 'json'
2019-04-03 00:25:12 +02:00
const tags = req.query.tags
const places = req.query.places
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
const opt = {
zone: res.locals.settings.instance_timezone,
locale: res.locals.settings.instance_locale
}
2019-06-26 14:44:21 +02:00
const where = {}
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] = {
placeId: places ? places.split(',').map(Number).filter(a => a) : [],
2023-10-30 09:36:18 +01:00
'$tags.tag$': tags.split(',')
}
}
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) {
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) {
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,
apUserApId: null,
2023-10-30 09:36:18 +01:00
start_datetime: { [Op.gte]: yesterday },
...where
},
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'] }]
})
}
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)
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
},
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
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
}
2019-04-03 00:25:12 +02:00
res.type('application/rss+xml; charset=UTF-8')
res.render('feed/rss.pug', { events, settings, unixFormat, title, link })
2019-04-03 00:25:12 +02: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-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 ]
const location = e.place.name !== 'online' ? `${e.place.name} - ${e.place.address}` : `${e.place.name} - ${e?.online_locations[0]}`
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',
title: e.title,
description: htmlToText(e.description),
2023-10-30 09:36:18 +01:00
htmlContent: e.description && e.description.replace(/\n/g,"<br>"),
location,
2022-02-26 21:27:40 +01:00
url: `${settings.baseurl}/event/${e.slug || e.id}`,
status: 'CONFIRMED',
2023-10-30 09:36:18 +01:00
categories: e.tags.map(t => t.tag || t),
alarms
2019-04-03 00:25:12 +02:00
}
if (e.place.latitude && e.place.longitude) {
ret.geo = { lat: e.place.latitude, lon: e.place.longitude }
}
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 ]
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