Skip to content

Commit

Permalink
Generate stable UUID v5 from topic URL (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarprause committed Apr 16, 2023
1 parent 35604e5 commit 3da78e0
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Handler, serve } from "https://deno.land/std@0.177.0/http/server.ts";
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@3.6.1";
import {
Expand All @@ -8,6 +9,9 @@ import {

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

// https://www.rfc-editor.org/rfc/rfc4122.html#appendix-C
const NAMESPACE_URL_UUID = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";

const handle: Handler = async (request) => {
const eventsUrl = new URL("/discourse-post-event/events.json", discourseUrl);

Expand Down Expand Up @@ -51,14 +55,14 @@ const handle: Handler = async (request) => {

const calendar = ical({ name: discourseUrl });

const calendarEvents = events
const calendarEventPromises = events
// TODO: filter by ends_at
.filter(
(event) =>
event.starts_at &&
new Date(event.starts_at) > new Date(Date.now() - 24 * hour)
)
.map((event) => {
.map(async (event) => {
const start = new Date(event.starts_at);

const url =
Expand All @@ -68,6 +72,10 @@ const handle: Handler = async (request) => {
: undefined);

const eventConfig: ICalEventData = {
id: await v5.generate(
NAMESPACE_URL_UUID,
new TextEncoder().encode(url)
),
summary: event.name || event.post?.topic?.title || "Unnamed event",
start,
...(event.ends_at
Expand All @@ -78,14 +86,10 @@ const handle: Handler = async (request) => {
repeating: event.recurrence
? repeatingFromRecurrence(event.recurrence, start)
: undefined,
// TODO:
// alarm: {
// desc: 'Write Rust NOW',
// advance: 30,
// },
};
return eventConfig;
});
const calendarEvents = await Promise.all(calendarEventPromises);

for (const calendarEvent of calendarEvents) {
calendar.createEvent(calendarEvent);
Expand Down

0 comments on commit 3da78e0

Please sign in to comment.