Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Handle calendar link and ics creation server side (#2171)

Luke Vellaea39d4a68bb1
diff --git a/apps/web/src/app/api/event/[...route]/route.ts b/apps/web/src/app/api/event/[...route]/route.ts
new file mode 100644
index 000000000..2ddf3a78e
--- /dev/null
+++ b/apps/web/src/app/api/event/[...route]/route.ts
@@ -0,0 +1,111 @@
+import { prisma } from "@rallly/database";
+import type { CalendarEvent } from "calendar-link";
+import { google, office365, outlook, yahoo } from "calendar-link";
+import { Hono } from "hono";
+import { handle } from "hono/vercel";
+
+import { createIcsEvent } from "@/utils/ics";
+
+const app = new Hono().basePath("/api/event");
+
+async function getScheduledEvent(eventId: string) {
+ return prisma.scheduledEvent.findUnique({
+ where: { id: eventId },
+ select: {
+ uid: true,
+ title: true,
+ description: true,
+ location: true,
+ start: true,
+ end: true,
+ allDay: true,
+ timeZone: true,
+ user: {
+ select: {
+ name: true,
+ email: true,
+ },
+ },
+ },
+ });
+}
+
+function toCalendarEvent(
+ event: NonNullable<Awaited<ReturnType<typeof getScheduledEvent>>>,
+): CalendarEvent {
+ return {
+ title: event.title,
+ description: event.description ?? undefined,
+ start: event.start,
+ end: event.end,
+ allDay: event.allDay,
+ location: event.location ?? undefined,
+ organizer: { name: event.user.name, email: event.user.email },
+ busy: true,
+ };
+}
+
+app.get("/:eventId/google-calendar", async (c) => {
+ const event = await getScheduledEvent(c.req.param("eventId"));
+ if (!event) {
+ return c.json({ error: "Event not found" }, 404);
+ }
+ return c.redirect(google(toCalendarEvent(event)));
+});
+
+app.get("/:eventId/outlook", async (c) => {
+ const event = await getScheduledEvent(c.req.param("eventId"));
+ if (!event) {
+ return c.json({ error: "Event not found" }, 404);
+ }
+ return c.redirect(outlook(toCalendarEvent(event)));
+});
+
+app.get("/:eventId/office365", async (c) => {
+ const event = await getScheduledEvent(c.req.param("eventId"));
+ if (!event) {
+ return c.json({ error: "Event not found" }, 404);
+ }
+ return c.redirect(office365(toCalendarEvent(event)));
+});
+
+app.get("/:eventId/yahoo", async (c) => {
+ const event = await getScheduledEvent(c.req.param("eventId"));
+ if (!event) {
+ return c.json({ error: "Event not found" }, 404);
+ }
+ return c.redirect(yahoo(toCalendarEvent(event)));
+});
+
+app.get("/:eventId/ics", async (c) => {
+ const event = await getScheduledEvent(c.req.param("eventId"));
+ if (!event) {
+ return c.json({ error: "Event not found" }, 404);
+ }
+
+ const { error, value } = createIcsEvent({
+ uid: event.uid,
+ title: event.title,
+ description: event.description ?? undefined,
+ location: event.location ?? undefined,
+ start: event.start,
+ end: event.end,
+ allDay: event.allDay,
+ timeZone: event.timeZone ?? undefined,
+ organizer: { name: event.user.name, email: event.user.email },
+ method: "publish",
+ });
+
+ if (error || !value) {
+ return c.json({ error: "Failed to generate ICS file" }, 500);
+ }
+
+ return new Response(value, {
+ headers: {
+ "Content-Type": "text/calendar; charset=utf-8",
+ "Content-Disposition": `attachment; filename="event.ics"`,
+ },
+ });
+});
+
+export const GET = handle(app);
diff --git a/apps/web/src/components/add-to-calendar-button.tsx b/apps/web/src/components/add-to-calendar-button.tsx
index 1cae26ce6..cc4fde973 100644
--- a/apps/web/src/components/add-to-calendar-button.tsx
+++ b/apps/web/src/components/add-to-calendar-button.tsx
@@ -9,45 +9,12 @@ import {
DropdownMenuTrigger,
} from "@rallly/ui/dropdown-menu";
import { Icon } from "@rallly/ui/icon";
-import type { CalendarEvent } from "calendar-link";
-import { google, ics, office365, outlook, yahoo } from "calendar-link";
import { DownloadIcon, PlusIcon } from "lucide-react";
import Image from "next/image";
import { Trans } from "@/components/trans";
-export function AddToCalendarButton({
- title,
- description,
- location,
- start,
- duration,
- organizer,
- guests,
-}: {
- title: string;
- description?: string;
- location?: string;
- start: Date;
- duration: number;
- organizer?: {
- name: string;
- email: string;
- };
- guests?: string[];
-}) {
- const calendarEvent: CalendarEvent = {
- title,
- description,
- start,
- allDay: duration === 0,
- duration: duration > 0 ? [duration, "minutes"] : undefined,
- location,
- organizer,
- guests,
- busy: true,
- };
-
+export function AddToCalendarButton({ eventId }: { eventId: string }) {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
@@ -59,78 +26,69 @@ export function AddToCalendarButton({
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent forceMount={true} align="start">
- <DropdownMenuItem
- onClick={() => {
- const res = google(calendarEvent);
- window.open(res, "_blank");
- }}
- >
- <Image
- src="/static/google-calendar.svg"
- width={16}
- height={16}
- alt="Google Calendar"
- />
- Google Calendar
+ <DropdownMenuItem asChild>
+ <a
+ href={`/api/event/${eventId}/google-calendar`}
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ <Image
+ src="/static/google-calendar.svg"
+ width={16}
+ height={16}
+ alt="Google Calendar"
+ />
+ Google Calendar
+ </a>
</DropdownMenuItem>
- <DropdownMenuItem
- onClick={() => {
- const res = office365(calendarEvent);
- window.open(res, "_blank");
- }}
- >
- <Image
- src="/static/microsoft-365.svg"
- width={16}
- height={16}
- alt="Microsoft 365"
- />
- <Trans i18nKey="microsoft365" defaults="Microsoft 365" />
+ <DropdownMenuItem asChild>
+ <a
+ href={`/api/event/${eventId}/office365`}
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ <Image
+ src="/static/microsoft-365.svg"
+ width={16}
+ height={16}
+ alt="Microsoft 365"
+ />
+ <Trans i18nKey="microsoft365" defaults="Microsoft 365" />
+ </a>
</DropdownMenuItem>
- <DropdownMenuItem
- onClick={() => {
- const res = outlook(calendarEvent);
- window.open(res, "_blank");
- }}
- >
- <Image
- src="/static/outlook.svg"
- width={16}
- height={16}
- alt="Outlook"
- />
- <Trans i18nKey="outlook" defaults="Outlook" />
+ <DropdownMenuItem asChild>
+ <a
+ href={`/api/event/${eventId}/outlook`}
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ <Image
+ src="/static/outlook.svg"
+ width={16}
+ height={16}
+ alt="Outlook"
+ />
+ <Trans i18nKey="outlook" defaults="Outlook" />
+ </a>
</DropdownMenuItem>
- <DropdownMenuItem
- onClick={() => {
- const res = yahoo(calendarEvent);
- window.open(res, "_blank");
- }}
- >
- <Image src="/static/yahoo.svg" width={16} height={16} alt="Yahoo" />
- <Trans i18nKey="yahoo" defaults="Yahoo" />
+ <DropdownMenuItem asChild>
+ <a
+ href={`/api/event/${eventId}/yahoo`}
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ <Image src="/static/yahoo.svg" width={16} height={16} alt="Yahoo" />
+ <Trans i18nKey="yahoo" defaults="Yahoo" />
+ </a>
</DropdownMenuItem>
<DropdownMenuSeparator />
- <DropdownMenuItem
- onClick={() => {
- const data = ics(calendarEvent);
-
- // download the file
- const link = document.createElement("a");
- link.setAttribute("href", data);
- link.setAttribute(
- "download",
- `${title.toLocaleLowerCase().replace(/\s/g, "-")}.ics`,
- );
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- }}
- >
- <Icon>
- <DownloadIcon />
- </Icon>
- <Trans i18nKey="downloadICSFile" defaults="Download ICS File" />
+ <DropdownMenuItem asChild>
+ <a href={`/api/event/${eventId}/ics`} download>
+ <Icon>
+ <DownloadIcon />
+ </Icon>
+ <Trans i18nKey="downloadICSFile" defaults="Download ICS File" />
+ </a>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
diff --git a/apps/web/src/components/poll/scheduled-event.tsx b/apps/web/src/components/poll/scheduled-event.tsx
index 90e9e933a..659c1a74a 100644
--- a/apps/web/src/components/poll/scheduled-event.tsx
+++ b/apps/web/src/components/poll/scheduled-event.tsx
@@ -61,8 +61,6 @@ export function ScheduledEvent() {
const poll = usePoll();
const { event } = poll;
- const attendees = useAttendees();
-
if (!event) {
return null;
}
@@ -101,21 +99,7 @@ export function ScheduledEvent() {
<IfParticipantsVisible>
<Attendees />
</IfParticipantsVisible>
- <AddToCalendarButton
- title={poll.title}
- description={poll.description ?? undefined}
- start={event.start}
- duration={event.duration}
- location={poll.location ?? undefined}
- organizer={
- poll.user
- ? { name: poll.user.name, email: poll.user.email }
- : undefined
- }
- guests={attendees
- .filter((attendee) => !!attendee.email)
- .map((attendee) => attendee.email as string)}
- />
+ <AddToCalendarButton eventId={event.id} />
</div>
</div>
</div>
diff --git a/apps/web/src/trpc/client/types.ts b/apps/web/src/trpc/client/types.ts
index 7d0349e0a..8fb8c3158 100644
--- a/apps/web/src/trpc/client/types.ts
+++ b/apps/web/src/trpc/client/types.ts
@@ -9,7 +9,6 @@ export type GetPollApiResponse = {
user: {
id: string;
name: string;
- email: string | null;
image: string | null;
banned: boolean;
} | null;
diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.ts
index 3cd6a618a..b1a66cb8f 100644
--- a/apps/web/src/trpc/routers/polls.ts
+++ b/apps/web/src/trpc/routers/polls.ts
@@ -710,7 +710,6 @@ export const polls = router({
select: {
id: true,
name: true,
- email: true,
image: true,
banned: true,
},
@@ -726,6 +725,7 @@ export const polls = router({
},
scheduledEvent: {
select: {
+ id: true,
start: true,
end: true,
allDay: true,
@@ -758,6 +758,7 @@ export const polls = router({
const event = res.scheduledEvent
? {
+ id: res.scheduledEvent.id,
start: res.scheduledEvent.start,
duration: res.scheduledEvent.allDay
? 0
@@ -906,13 +907,6 @@ export const polls = router({
p.votes.some((v) => v.optionId === input.optionId && v.type !== "no"),
);
- const icsAttendees = attendees
- .filter((a) => !!a.email) // remove participants without email
- .map((a) => ({
- name: a.name,
- email: a.email as string,
- }));
-
const event = createIcsEvent({
uid,
sequence: 0,
@@ -930,7 +924,6 @@ export const polls = router({
name: poll.user.name,
email: poll.user.email,
},
- attendees: icsAttendees,
});
const scheduledEvent = await prisma.$transaction(async (tx) => {