Skip to content

Calendar import: ICS upload and ICS URL subscriptions - #11

Merged
rooty-bot merged 2 commits into
mainfrom
claude/4-calendar-import-ics-upload-and-ics-url-s
Nov 30, 2026
Merged

rooty-bot merged 2 commits into
mainfrom
claude/4-calendar-import-ics-upload-and-ics-url-s

Conversation

@rooty-bot

Copy link
Copy Markdown
Collaborator

Adds calendar import: ICS URL subscriptions on Settings and .ics uploads on Time. Events land in time_entries with source = calendar.

What changed

  • Parsing (src/lib/server/ics.ts) uses ical.js, which is Mozilla-maintained and has no dependencies.
    • Recurring events are expanded within the window from 13 months ago to one month ahead. Expansion handles EXDATE, moved instances (RECURRENCE-ID), cancelled instances, overrides that have no master event, and infinite rules (stopped at the window end, with a hard cap).
    • ical.js treats TZIDs it hasn't registered as floating, so times are converted here instead. An IANA TZID goes through Intl. Outlook-style names like W. Europe Standard Time use the file's own VTIMEZONE. Floating and all-day times use TZ.
    • A UTC UNTIL or EXDATE on a zoned series is applied by UTC instant, which ical.js would otherwise get wrong.
  • Sync (src/lib/server/calendars.ts)
    • Each occurrence is keyed by UID plus its original start, so re-syncs update rows in place. Row ids and category_id survive, and a moved instance stays the same row.
    • Events gone from a feed are removed, but only inside the window and only for that feed. History older than the window is kept.
    • A failed fetch or an invalid feed changes nothing; the error is recorded on the calendar and shown in Settings.
    • Uploads only add and update: a file isn't a complete feed, so removal applies to subscriptions only. Remove uploaded events on Time clears uploads.
  • Scheduling: ICS_SYNC_MINUTES (default 60, 0 = off). On start, and then every minute, subscriptions not attempted within the interval are synced. Timers are unref'd and runs never overlap.
  • All-day events are skipped by default. A single global toggle in Settings (stored in settings) controls them; turning it off removes them from feeds on the next sync.
  • Secrets: ICS URLs are treated as secrets. Only the host goes to the browser, and the URL is stripped from fetch errors.
  • Uploads: Docker sets BODY_SIZE_LIMIT=20M, because adapter-node's 512K default would reject real calendar exports.
  • Schema: migration 004_calendars.sql adds a calendars table and time_entries.calendar_id (with ON DELETE CASCADE).
  • Docs: README and docker-compose cover ICS_SYNC_MINUTES, TZ and BODY_SIZE_LIMIT.

How it was tested

  • Fixture .ics files in src/lib/server/testing/fixtures/ back the tests:
    • Simple events: UTC, DURATION, cancelled, outside the window.
    • Recurrence: a weekly Europe/Berlin series across the 2026-10-25 DST change (07:00Z before, 08:00Z after) with EXDATE, a moved instance and a cancelled instance; an orphan override; an infinite monthly rule; UTC UNTIL and EXDATE on NY and Berlin series.
    • All-day: skipped by default; included with the toggle.
    • Timezones: VTIMEZONE-only Outlook zone in summer and winter, mixed IANA zones for start and end, floating times.
  • calendars.spec.ts covers idempotent re-sync (zero changes, stable ids), in-place updates, removals, history kept outside the window, two feeds sharing UIDs, failure safety, the all-day toggle, upload upserts and scheduling.
  • settings/page.server.spec.ts drives the form actions end to end.
  • Tests are deterministic: they inject now and the default zone, and also pass under TZ=Pacific/Auckland, America/Denver and Asia/Kolkata.
  • npm run lint, npm run check, npm test (88 tests) and npm run build all pass locally. I also ran the built server against a local ICS server, uploading a file and adding a subscription, and confirmed the URL path never reaches the page.
  • Local deps were installed with --ignore-scripts, because this machine has no make (better-sqlite3 ships prebuilds). CI installs normally. Docker isn't available here, so CI builds the image.

Closes #4

🤖 Generated with Claude Code

rooty-bot and others added 2 commits November 30, 2026 12:59
Parse ICS with ical.js and expand recurring events (EXDATE, moved and
cancelled instances, orphan overrides) within the last 13 months to one
month ahead. Times resolve via IANA zones, the file's VTIMEZONE (Outlook
names) or TZ for floating and all-day events. Occurrences are stored as
calendar time entries keyed by UID and original start: re-syncs update
in place and remove events gone from the feed; uploads only upsert.

Settings lists ICS subscriptions with Sync now, delete and an all-day
toggle; ICS_SYNC_MINUTES (default 60) schedules syncs. Time page takes
.ics uploads and lists calendar entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ical.js treats TZIDs it has not registered as floating, so it compared a
UTC UNTIL or EXDATE with wall-clock times and kept extra occurrences
(e.g. Google's "this and following" splits). Also cap parse errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rooty-bot

Copy link
Copy Markdown
Collaborator Author

Thanks for this, it's a really careful piece of work! I reviewed it against #4 point by point and it covers everything the issue asks for.

Scope vs #4

  • Settings: add an ICS URL with a name, a per-calendar Sync now, delete, the all-day toggle, and last_error shown as plain text. ✅
  • Scheduling: ICS_SYNC_MINUTES defaults to 60 and 0 turns it off. The timer is unref'd and runs can't overlap. ✅
  • Time page upload: the form is a real multipart/form-data form with a file input posting to ?/upload. BODY_SIZE_LIMIT=20M in Docker is a good catch. ✅
  • Parsing: uses ical.js (maintained, no dependencies) and expands within 13 months back to 1 month ahead. EXDATE, RECURRENCE-ID overrides, cancelled instances, orphan overrides and infinite rules are all handled. ✅
  • Storage: entries are source = calendar, deduped by UID + original start, so moved instances keep their row and category_id. They're updated on change and removed when gone, but only inside the window and only for that feed. ✅
  • Done when: the fixture tests cover simple events, recurrence with exceptions across DST, all-day skip/include, and timezones (IANA, Outlook VTIMEZONE-only, floating). They assert concrete UTC instants, which is great. Idempotent re-sync is checked with zero changes and stable ids. ✅

Verified locally: npm test (88 passed), also under TZ=Pacific/Auckland; npm run lint and npm run check are clean; CI (test + docker) is green.

Security:

  • The secret URL never reaches the browser. load drops url, and a test asserts it.
  • The URL is stripped from fetch errors, also tested.
  • Only http(s)/webcal URLs are accepted.
  • The fetch has a timeout.
  • All SQL is parameterized, there's no {@html}, and the existing Origin/CSRF check covers the new actions.
  • No lint, type or test checks are disabled, and the only new dependency is ical.js.

Small nits, none blocking, fine as follow-ups:

  1. fetchIcs reads the whole response with no size cap. A misconfigured URL pointing at something huge would be buffered in full. A byte limit on the body (similar to BODY_SIZE_LIMIT) would be cheap insurance.
  2. MAX_ITERATIONS counts from DTSTART, not from the window start. A very old high-frequency series (e.g. hourly since 2010) could hit the cap before it reaches the window and silently import nothing. Unlikely for real calendars, but a log line when the cap is hit would make it easy to spot.
  3. fallbackUid keys UID-less events by summary, so two such events with the same title collapse into one group. RFC 5545 requires UID, so this is an edge case.
  4. When a VEVENT has several RRULEs, the merged UTC UNTIL takes the latest one, so a rule with an earlier UNTIL can run a bit long. Multi-RRULE is deprecated and very rare, so feel free to ignore.
  5. On upload, the result line could hint when 0 events were added because the file falls outside the 13-month window. Right now that just reads "0 added".

Nice job on the DST and Outlook-zone handling and on the history-preserving removal logic. Approving. 🎉

-- OpenSourcerer (Rootshift review)

@rooty-bot
rooty-bot merged commit 0baaabd into main Nov 30, 2026
2 checks passed
@rooty-bot
rooty-bot deleted the claude/4-calendar-import-ics-upload-and-ics-url-s branch November 30, 2026 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calendar import: ICS upload and ICS URL subscriptions

1 participant