WIP: Query for events

This commit is contained in:
Marek S. Lukasiewicz
2025-01-03 23:55:53 +01:00
parent 23f9208e9c
commit c028032787
3 changed files with 50 additions and 7 deletions

View File

@@ -3,6 +3,6 @@ requiredPermissions:
- fetch
functions:
queryIcs:
path: ./caldav.ts:queryIcs
path: ./caldav.ts:queryEvents
events:
- query:cal-ics
- query:cal-events

View File

@@ -7,27 +7,70 @@ import { parseIcsCalendar, type VCalendar } from "ts-ics";
interface Event {
summary: string | undefined;
description: string | undefined;
created: string | undefined;
lastModified: string | undefined;
start: string | undefined;
end: string | undefined;
}
export async function queryIcs({ query }: QueryProviderEvent): Promise<any[]> {
export async function queryEvents(
{ query }: QueryProviderEvent,
): Promise<any[]> {
const secrets = await readYamlPage("SECRETS");
const icsUrl = secrets.icsUrl;
// TODO: Loop over multiple sources (like CalDAV, .ics URLs) and assign calendar name based on X-WR-CALNAME
const result = await fetch(icsUrl);
const icsData = await result.text();
const calendarParsed: VCalendar = parseIcsCalendar(icsData);
// TODO: Loop over multiple .ics URLs and assign calendar name based on X-WR-CALNAME
if (calendarParsed.events === undefined) {
editor.flashNotification("Did not parse events from iCalendar");
return [];
}
const events: Event[] = calendarParsed.events.map((ics) => {
let events: Event[] = calendarParsed.events.map((ics) => {
return {
summary: ics.summary,
description: ics.description,
// Mimic properties of pages
created: ics.created ? localDateString(ics.created.date) : undefined,
lastModified: ics.lastModified
? localDateString(ics.lastModified.date)
: undefined,
// Consistent with formats above
start: localDateString(ics.start.date),
end: ics.end ? localDateString(ics.end.date) : undefined,
};
});
return applyQuery(query, events);
// FIXME: Fails whenever any filters are applied, but same code works for github plug
events = applyQuery(query, events);
return events;
}
// Copied from @silverbulletmd/silverbullet/lib/dates.ts which is not exported in the package
export function localDateString(d: Date): string {
return d.getFullYear() +
"-" + String(d.getMonth() + 1).padStart(2, "0") +
"-" + String(d.getDate()).padStart(2, "0") +
"T" + String(d.getHours()).padStart(2, "0") +
":" + String(d.getMinutes()).padStart(2, "0") +
":" + String(d.getSeconds()).padStart(2, "0") +
"." + String(d.getMilliseconds()).padStart(3, "0");
}
// didn't help
function flattenObject(obj: any, prefix = ""): any {
let result: any = {};
for (let [key, value] of Object.entries(obj)) {
if (prefix) {
key = prefix + "_" + key;
}
if (value && typeof value === "object") {
result = { ...result, ...flattenObject(value, key) };
} else {
result[key] = value;
}
}
return result;
}

View File

@@ -17,7 +17,7 @@
]
},
"imports": {
"@silverbulletmd/silverbullet": "jsr:@silverbulletmd/silverbullet@^0.9.0",
"@silverbulletmd/silverbullet": "jsr:@silverbulletmd/silverbullet@^0.10.1",
"tsdav": "npm:tsdav@2.1.3",
"ts-ics": "npm:ts-ics@1.6.5",
"@js-temporal/polyfill": "https://esm.sh/@js-temporal/polyfill@0.4.4"