|
| 1 | +import {RequestHandler} from 'express'; |
| 2 | +import {StatusCodes} from 'http-status-codes'; |
| 3 | +import {Dependencies} from '../dependencies'; |
| 4 | +import * as E from 'fp-ts/Either'; |
| 5 | +import {MeetupEventRow} from '../sync-worker/db/get-meetup-events'; |
| 6 | + |
| 7 | +const formatICalDate = (timestamp: number): string => { |
| 8 | + const date = new Date(timestamp); |
| 9 | + return date |
| 10 | + .toISOString() |
| 11 | + .replace(/[-:]/g, '') |
| 12 | + .replace(/\.\d{3}/, ''); |
| 13 | +}; |
| 14 | + |
| 15 | +const escapeICalText = (text: string): string => |
| 16 | + text |
| 17 | + .replace(/\\/g, '\\\\') |
| 18 | + .replace(/;/g, '\\;') |
| 19 | + .replace(/,/g, '\\,') |
| 20 | + .replace(/\n/g, '\\n'); |
| 21 | + |
| 22 | +const foldLine = (line: string): string => { |
| 23 | + const maxLength = 75; |
| 24 | + if (line.length <= maxLength) { |
| 25 | + return line; |
| 26 | + } |
| 27 | + const parts: string[] = []; |
| 28 | + let remaining = line; |
| 29 | + while (remaining.length > 0) { |
| 30 | + if (parts.length === 0) { |
| 31 | + parts.push(remaining.slice(0, maxLength)); |
| 32 | + remaining = remaining.slice(maxLength); |
| 33 | + } else { |
| 34 | + // Continuation lines start with a space |
| 35 | + parts.push(' ' + remaining.slice(0, maxLength - 1)); |
| 36 | + remaining = remaining.slice(maxLength - 1); |
| 37 | + } |
| 38 | + } |
| 39 | + return parts.join('\r\n'); |
| 40 | +}; |
| 41 | + |
| 42 | +const generateICalendar = ( |
| 43 | + events: ReadonlyArray<MeetupEventRow> |
| 44 | +): string => { |
| 45 | + const lines: string[] = [ |
| 46 | + 'BEGIN:VCALENDAR', |
| 47 | + 'VERSION:2.0', |
| 48 | + 'PRODID:-//MakeSpace//Members App//EN', |
| 49 | + 'CALSCALE:GREGORIAN', |
| 50 | + 'METHOD:PUBLISH', |
| 51 | + 'X-WR-CALNAME:MakeSpace Events', |
| 52 | + ]; |
| 53 | + |
| 54 | + for (const event of events) { |
| 55 | + lines.push('BEGIN:VEVENT'); |
| 56 | + lines.push(foldLine(`UID:${event.uid}`)); |
| 57 | + lines.push(`DTSTART:${formatICalDate(event.dtstart)}`); |
| 58 | + lines.push(`DTEND:${formatICalDate(event.dtend)}`); |
| 59 | + lines.push(foldLine(`SUMMARY:${escapeICalText(event.summary)}`)); |
| 60 | + if (event.description) { |
| 61 | + lines.push(foldLine(`DESCRIPTION:${escapeICalText(event.description)}`)); |
| 62 | + } |
| 63 | + if (event.location) { |
| 64 | + lines.push(foldLine(`LOCATION:${escapeICalText(event.location)}`)); |
| 65 | + } |
| 66 | + if (event.url) { |
| 67 | + lines.push(foldLine(`URL:${event.url}`)); |
| 68 | + } |
| 69 | + lines.push('END:VEVENT'); |
| 70 | + } |
| 71 | + |
| 72 | + lines.push('END:VCALENDAR'); |
| 73 | + return lines.join('\r\n'); |
| 74 | +}; |
| 75 | + |
| 76 | +export const calendarIcsHandler = |
| 77 | + (deps: Dependencies): RequestHandler => |
| 78 | + async (req, res) => { |
| 79 | + const events = await deps.getMeetupEvents()(); |
| 80 | + |
| 81 | + if (E.isLeft(events)) { |
| 82 | + deps.logger.error('Failed to get Meetup events: %s', events.left); |
| 83 | + return res |
| 84 | + .status(StatusCodes.INTERNAL_SERVER_ERROR) |
| 85 | + .send('Failed to load calendar'); |
| 86 | + } |
| 87 | + |
| 88 | + const icalContent = generateICalendar(events.right); |
| 89 | + |
| 90 | + res.setHeader('Content-Type', 'text/calendar; charset=utf-8'); |
| 91 | + res.setHeader( |
| 92 | + 'Content-Disposition', |
| 93 | + 'attachment; filename="makespace-events.ics"' |
| 94 | + ); |
| 95 | + return res.status(StatusCodes.OK).send(icalContent); |
| 96 | + }; |
0 commit comments