Skip to content
Draft
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
70 changes: 1 addition & 69 deletions src/discourse-calendar.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import { z } from "https://deno.land/x/zod@v3.20.5/mod.ts";
import {
ICalEventData,
ICalEventRepeatingFreq,
ICalWeekday,
} from "https://esm.sh/v108/ical-generator@6.0.1";
import { RRule } from "https://esm.sh/v108/rrule@2.7.2";

const DiscourseEventRecurrence = z.enum([
"every_day",
"every_weekday",
"every_week",
"every_two_weeks",
"every_month",
"every_year",
]);
type DiscourseEventRecurrence = z.infer<typeof DiscourseEventRecurrence>;

export const DiscourseEvent = z.object({
id: z.number(),
name: z.string().nullable(),
starts_at: z.string().datetime({ offset: true }),
ends_at: z.string().datetime({ offset: true }).nullable(),
recurrence: DiscourseEventRecurrence.nullable(),
rrule: z.string().optional(),
post: z
.object({
url: z.string().nullable(),
Expand All @@ -33,55 +17,3 @@ export const DiscourseEvent = z.object({
})
.nullable(),
});

export const repeatingFromRecurrence = (
recurrence: DiscourseEventRecurrence,
start: Date
): ICalEventData["repeating"] => {
if (recurrence === "every_day") {
return { freq: ICalEventRepeatingFreq.DAILY };
}
if (recurrence === "every_weekday") {
return {
freq: ICalEventRepeatingFreq.DAILY,
byDay: [
ICalWeekday.MO,
ICalWeekday.TU,
ICalWeekday.WE,
ICalWeekday.TH,
ICalWeekday.FR,
],
};
}
if (recurrence === "every_week") {
return { freq: ICalEventRepeatingFreq.WEEKLY };
}
if (recurrence === "every_two_weeks") {
return { freq: ICalEventRepeatingFreq.WEEKLY, interval: 2 };
}
if (recurrence === "every_month") {
const weekdayIndex = start.getDay();
const weekdays = [
RRule.SU,
RRule.MO,
RRule.TU,
RRule.WE,
RRule.TH,
RRule.FR,
RRule.SA,
];
const weekday = weekdays[weekdayIndex];

const weekNumber = Math.floor(start.getDate() / 7) + 1;

const rrule = new RRule({
freq: RRule.MONTHLY,
byweekday: weekday.nth(weekNumber),
});

return rrule.toString();
}
if (recurrence === "every_year") {
return { freq: ICalEventRepeatingFreq.YEARLY };
}
};
9 changes: 2 additions & 7 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { Handler, serve } from "https://deno.land/std@0.182.0/http/server.ts";
import * as v5 from "https://deno.land/std@0.182.0/uuid/v5.ts";
import { z } from "https://deno.land/x/zod@v3.20.5/index.ts";
import ical, { ICalEventData } from "https://esm.sh/v108/ical-generator@6.0.1";
import {
DiscourseEvent,
repeatingFromRecurrence,
} from "./discourse-calendar.ts";
import { DiscourseEvent } from "./discourse-calendar.ts";

const discourseUrl = Deno.env.get("DISCOURSE_URL");

Expand Down Expand Up @@ -76,9 +73,7 @@ const handle: Handler = async (request) => {
: { duration: 3600 }),
url,
description: url,
repeating: event.recurrence
? repeatingFromRecurrence(event.recurrence, start)
: undefined,
repeating: event.rrule,
};
return eventConfig;
});
Expand Down
Loading