Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@repo/auth": "workspace:*",
"@repo/db": "workspace:*",
"@repo/env": "workspace:*",
"@analog/ical": "workspace:*",
"@repo/google-calendar": "workspace:*",
"@repo/google-tasks": "workspace:*",
"@repo/temporal": "workspace:*",
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { conferencingRouter } from "./routers/conferencing";
import { earlyAccessRouter } from "./routers/early-access";
import { eventsRouter } from "./routers/events";
import { freeBusyRouter } from "./routers/free-busy";
import { icsRouter } from "./routers/ics";
import { placesRouter } from "./routers/places";
import { tasksRouter } from "./routers/tasks";
import { userRouter } from "./routers/user";
Expand All @@ -27,6 +28,7 @@ export const appRouter = createTRPCRouter({
conferencing: conferencingRouter,
earlyAccess: earlyAccessRouter,
places: placesRouter,
ics: icsRouter,
});

export type AppRouter = typeof appRouter;
Expand Down
70 changes: 70 additions & 0 deletions packages/api/src/routers/ics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { importCalendar, importEvent, isCalendar, isEvent } from "@analog/ical";
import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { createTRPCRouter, publicProcedure } from "../trpc";

const FETCH_TIMEOUT = 10000;

export const icsRouter = createTRPCRouter({
parseFromUrl: publicProcedure
.input(
z.object({
url: z.url(),
}),
)
.query(async ({ input }) => {
const response = await fetch(input.url, {
signal: AbortSignal.timeout(FETCH_TIMEOUT),
});

if (!response.ok) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Failed to fetch ICS file: ${response.status} ${response.statusText}`,
});
}

const content = await response.text();

if (!content.trim()) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "ICS file is empty",
});
}

try {
if (isCalendar(content)) {
const calendar = importCalendar(content);
return {
type: "calendar",
events: calendar.events,
};
}

if (isEvent(content)) {
const event = importEvent(content);
return {
type: "event",
events: [event],
};
}

throw new TRPCError({
code: "BAD_REQUEST",
message: "Invalid ICS content: not a VCALENDAR or VEVENT",
});
} catch (error) {
if (error instanceof TRPCError) {
throw error;
}

throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to process ICS file",
cause: error instanceof Error ? error.message : "Unknown error",
});
}
}),
});
4 changes: 4 additions & 0 deletions packages/ical/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { config } from "@repo/eslint-config/base";

/** @type {import("eslint").Linter.Config} */
export default config;
25 changes: 25 additions & 0 deletions packages/ical/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@analog/ical",
"version": "0.1.0",
"license": "Apache-2.0",
"exports": {
".": "./src/index.ts"
},
"scripts": {
"lint": "eslint .",
"type-check": "tsc --noEmit"
},
"dependencies": {
"ts-ics": "^2.2.0"
},
"peerDependencies": {
"temporal-polyfill": "^0.3.0"
},
"devDependencies": {
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/node": "^22.9.0",
"eslint": "^9.30.1",
"typescript": "^5.8.3"
}
}
127 changes: 127 additions & 0 deletions packages/ical/src/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { Temporal } from "temporal-polyfill";
import {
generateIcsCalendar,
generateIcsEvent,
generateIcsTimezone,
type IcsAttendee,
type IcsCalendar,
type IcsDateObject,
type IcsEvent,
type IcsTimezone,
} from "ts-ics";

import type { Attendee } from "@repo/api/interfaces";

import type { iCalendarEvent } from "./interfaces";

function formatOffset(offset: string): string {
return offset.replace(":", "");
}

function toDate(value: iCalendarEvent["start"]): Date {
if (value instanceof Temporal.PlainDate) {
return new Date(value.toString());
}

if (value instanceof Temporal.Instant) {
return new Date(value.epochMilliseconds);
}

return new Date(value.toInstant().epochMilliseconds);
}

function toAttendee(attendee: Attendee): IcsAttendee {
const result: IcsAttendee = {
email: attendee.email ?? "",
};

if (attendee.name) {
result.name = attendee.name;
}

if (attendee.status) {
if (attendee.status === "accepted") {
result.partstat = "ACCEPTED";
} else if (attendee.status === "declined") {
result.partstat = "DECLINED";
} else if (attendee.status === "tentative") {
result.partstat = "TENTATIVE";
} else {
result.partstat = "NEEDS-ACTION";
}
}

if (attendee.type) {
if (attendee.type === "optional") {
result.role = "OPT-PARTICIPANT";
} else if (attendee.type === "resource") {
result.role = "NON-PARTICIPANT";
} else {
result.role = "REQ-PARTICIPANT";
}
}

return result;
}

function toIcsEvent(event: iCalendarEvent): IcsEvent {
const start: IcsDateObject = {
date: toDate(event.start),
type: event.allDay ? "DATE" : "DATE-TIME",
};

const end: IcsDateObject = {
date: toDate(event.end),
type: event.allDay ? "DATE" : "DATE-TIME",
};

if (!event.allDay) {
if (event.start instanceof Temporal.ZonedDateTime) {
start.local = {
date: start.date,
timezone: event.start.timeZoneId,
tzoffset: formatOffset(event.start.offset),
};
}

if (event.end instanceof Temporal.ZonedDateTime) {
end.local = {
date: end.date,
timezone: event.end.timeZoneId,
tzoffset: formatOffset(event.end.offset),
};
}
}

return {
uid: event.id,
summary: event.title ?? "",
stamp: { date: new Date() },
start,
end,
...(event.description && { description: event.description }),
...(event.location && { location: event.location }),
...(event.url && { url: event.url }),
...(event.attendees &&
event.attendees.length > 0 && {
attendees: event.attendees.map(toAttendee),
}),
};
}

export function exportEvent(event: iCalendarEvent): string {
return generateIcsEvent(toIcsEvent(event));
}

export function exportEvents(events: iCalendarEvent[]): string {
const calendar: IcsCalendar = {
prodId: "@analog/ical",
version: "2.0",
events: events.map(toIcsEvent),
};
return generateIcsCalendar(calendar);
}

export function exportTimezone(timezone: IcsTimezone): string {
return generateIcsTimezone(timezone);
}
Loading