diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-04-01 17:27:18 +1100 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-04-01 17:27:18 +1100 |
| commit | ff7324e7bfc1e01c8502e24a38e0729d87e63564 (patch) | |
| tree | 3af12a838f191dad413c6ff7096dd83d9ee8daa5 /src/services/calendar.ts | |
| parent | launcher: hide todo action if tod not installed (diff) | |
| download | caelestia-shell-ff7324e7bfc1e01c8502e24a38e0729d87e63564.tar.gz caelestia-shell-ff7324e7bfc1e01c8502e24a38e0729d87e63564.tar.bz2 caelestia-shell-ff7324e7bfc1e01c8502e24a38e0729d87e63564.zip | |
calendar: cache calendars
Diffstat (limited to 'src/services/calendar.ts')
| -rw-r--r-- | src/services/calendar.ts | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/services/calendar.ts b/src/services/calendar.ts index 9743aad..9372066 100644 --- a/src/services/calendar.ts +++ b/src/services/calendar.ts @@ -1,5 +1,16 @@ +import { pathToFileName } from "@/utils/strings"; import { notify } from "@/utils/system"; -import { execAsync, GLib, GObject, property, register, timeout, type AstalIO } from "astal"; +import { + execAsync, + GLib, + GObject, + property, + readFileAsync, + register, + timeout, + writeFileAsync, + type AstalIO, +} from "astal"; import { calendar as config } from "config"; import ical from "ical.js"; @@ -17,6 +28,8 @@ export default class Calendar extends GObject.Object { return this.instance; } + readonly #cacheDir = `${CACHE}/calendars`; + #calCount: number = 1; #reminders: AstalIO.Time[] = []; #loading: boolean = false; @@ -55,12 +68,24 @@ export default class Calendar extends GObject.Object { this.#calCount = 1; const cals = await Promise.allSettled(config.webcals.get().map(c => execAsync(["curl", c]))); - for (const cal of cals) { + for (let i = 0; i < cals.length; i++) { + const cal = cals[i]; + const webcal = pathToFileName(config.webcals.get()[i]); + + let icalStr; if (cal.status === "fulfilled") { - const comp = new ical.Component(ical.parse(cal.value)); + icalStr = cal.value; + } else { + console.error(`Failed to get calendar from ${config.webcals.get()[i]}:\n${cal.reason}`); + icalStr = await readFileAsync(`${this.#cacheDir}/${webcal}`); + } + + if (icalStr) { + const comp = new ical.Component(ical.parse(icalStr)); const name = (comp.getFirstPropertyValue("x-wr-calname") ?? `Calendar ${this.#calCount++}`) as string; this.#calendars[name] = comp; - } else console.error(`Failed to get calendar: ${cal.reason}`); + writeFileAsync(`${this.#cacheDir}/${webcal}`, icalStr).catch(console.error); + } } this.notify("calendars"); @@ -160,6 +185,8 @@ export default class Calendar extends GObject.Object { constructor() { super(); + GLib.mkdir_with_parents(this.#cacheDir, 0o755); + this.updateCalendars().catch(console.error); config.webcals.subscribe(() => this.updateCalendars().catch(console.error)); config.upcomingDays.subscribe(() => this.updateUpcoming()); |