2021-10-26 14:09:02 -07:00
|
|
|
import { Router } from 'express';
|
2022-01-02 13:07:39 -08:00
|
|
|
import {handleErrorAsync, clearLinkedText} from '../../src/helpers';
|
2021-10-26 14:09:02 -07:00
|
|
|
import { createEvents } from 'ics';
|
|
|
|
import { calendar } from '../../src/calendar/calendar';
|
|
|
|
import {loadSuml} from "../loader";
|
2022-01-02 13:07:39 -08:00
|
|
|
import { Day } from '../../src/calendar/helpers';
|
|
|
|
|
2021-10-26 14:09:02 -07:00
|
|
|
// TODO caching? // import { caches } from "../../src/cache";
|
|
|
|
|
|
|
|
const translations = loadSuml('translations');
|
|
|
|
|
2021-10-27 12:00:45 -07:00
|
|
|
const renderEvents = (yearEvents, res) => {
|
|
|
|
const events = [];
|
|
|
|
let i = 1;
|
|
|
|
for (let year in yearEvents) {
|
|
|
|
if (!yearEvents.hasOwnProperty(year)) { continue; }
|
|
|
|
for (let event of yearEvents[year]) {
|
|
|
|
if (!event) { continue; }
|
|
|
|
const ics = event.toIcs(year, translations, clearLinkedText, i);
|
|
|
|
if (ics !== null) {
|
|
|
|
events.push(ics);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events.length === 0) {
|
|
|
|
return res.status(404).json({error: 'Not found'});
|
|
|
|
}
|
|
|
|
|
|
|
|
createEvents(
|
|
|
|
events,
|
|
|
|
(error, value) => {
|
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
|
|
|
return res.status(500).json({error: 'Unexpected server error'});
|
|
|
|
}
|
|
|
|
|
|
|
|
res.header('Content-type', 'text/calendar');
|
|
|
|
res.send(value);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-02 13:07:39 -08:00
|
|
|
const getEventName = (name) => {
|
|
|
|
name = translations.calendar.events[name] || name;
|
|
|
|
name = name.replace(/{.*?=(.*?)}/g, '$1')
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2022-02-07 06:43:58 -08:00
|
|
|
const buildMessage = (events, locale, day, link) => {
|
|
|
|
if (events.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let message = `[${locale.name}] ${day.toString()}\n\n${translations.calendar.banner}:\n`;
|
|
|
|
for (let event of events) {
|
|
|
|
message += ` - ${event}\n`;
|
|
|
|
}
|
|
|
|
message += `\n${link}`;
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2022-01-02 13:07:39 -08:00
|
|
|
const eventsSummary = (day, locale) => {
|
|
|
|
const eventsRaw = calendar.getCurrentYear().eventsByDate[day.toString()];
|
|
|
|
|
|
|
|
const link = `${locale.url}/${encodeURIComponent(config.calendar.route)}/${day}`;
|
|
|
|
const image = `${locale.url}/calendar/${day}.png`;
|
|
|
|
|
|
|
|
let message = null;
|
|
|
|
let events = [];
|
|
|
|
if (eventsRaw !== undefined && eventsRaw.length > 0) {
|
|
|
|
for (let event of eventsRaw) {
|
2022-02-07 06:43:58 -08:00
|
|
|
events.push(getEventName(event.name));
|
2022-01-02 13:07:39 -08:00
|
|
|
delete event.daysMemoise;
|
|
|
|
}
|
2022-02-07 06:43:58 -08:00
|
|
|
}
|
|
|
|
let eventsForMessage = [...events];
|
|
|
|
while (true) {
|
|
|
|
message = buildMessage(eventsForMessage, locale, day, link);
|
|
|
|
|
|
|
|
if (message === null || message.length <= 280) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
eventsForMessage = eventsForMessage.slice(0, eventsForMessage.length - 1);
|
|
|
|
}
|
2022-01-02 13:07:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
day: day.toString(),
|
|
|
|
link,
|
|
|
|
image,
|
|
|
|
message,
|
|
|
|
events,
|
|
|
|
eventsRaw: (eventsRaw || []),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 14:09:02 -07:00
|
|
|
const router = Router();
|
|
|
|
|
2022-01-02 13:07:39 -08:00
|
|
|
router.get('/calendar/today', handleErrorAsync(async (req, res) => {
|
|
|
|
return res.json(eventsSummary(
|
|
|
|
Day.today(),
|
|
|
|
req.locales[global.config.locale],
|
|
|
|
));
|
|
|
|
}));
|
|
|
|
|
|
|
|
router.get('/calendar/:year-:month-:day', handleErrorAsync(async (req, res) => {
|
|
|
|
return res.json(eventsSummary(
|
|
|
|
new Day(parseInt(req.params.year), parseInt(req.params.month), parseInt(req.params.day)),
|
|
|
|
req.locales[global.config.locale],
|
|
|
|
));
|
|
|
|
}));
|
|
|
|
|
2021-10-27 12:00:45 -07:00
|
|
|
const routeBase = `/queer-calendar-${global.config.locale}`;
|
|
|
|
|
|
|
|
router.get(routeBase + '.ics', handleErrorAsync(async (req, res) => {
|
|
|
|
let events = {};
|
|
|
|
for (let year of calendar.getAllYears()) {
|
|
|
|
events[year.year] = year.events;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderEvents(events, res);
|
|
|
|
}));
|
|
|
|
|
|
|
|
router.get(routeBase + '-:year-:uuid.ics', handleErrorAsync(async (req, res) => {
|
|
|
|
const year = calendar.getYear(req.params.year);
|
|
|
|
if (!year) {
|
|
|
|
return res.status(404).json({error: 'Not found'});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderEvents({[year.year]: [year.eventsByUuid[req.params.uuid]]}, res)
|
|
|
|
}));
|
|
|
|
|
|
|
|
router.get(routeBase + '-:year.ics', handleErrorAsync(async (req, res) => {
|
2021-10-26 14:09:02 -07:00
|
|
|
const year = calendar.getYear(req.params.year);
|
|
|
|
if (!year) {
|
|
|
|
return res.status(404).json({error: 'Not found'});
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:00:45 -07:00
|
|
|
renderEvents({[year.year]: year.events}, res)
|
2021-10-26 14:09:02 -07:00
|
|
|
}));
|
|
|
|
|
|
|
|
export default router;
|