Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / apple / Hub / CalendarSink.swift

CalendarSink.swift76 lines · 4.0 KiBRaw
1// EverySync Hub — calendar -> `EKEvent` sink.
2//
3// `EKEventStore` + calendar access. Maps `RemoteEvent` summary / description /
4// location / start-end millis / allDay / timezone / RRULE / recurrenceId onto
5// `EKEvent` in a hub-managed calendar. Same driver-applies, Swift-renders
6// split as the other sinks: delete and conflict semantics follow
7// `CalendarKind` (syncId = UID [+ ␀RECURRENCE-ID for overridden instances];
8// canonical iCal keeps RECURRENCE-ID; soft-delete).
9//
10// Field mapping:
11// - `uid` (+ `recurrenceId` for overridden instances) -> identity via the
12// hub map (`SyncIdMap.swift`; the composite `uid␀recurrenceId` is the map
13// key — `EKEvent.eventIdentifier` is hub-local state, never an IPC column).
14// - `summary/description/location` -> `title/notes/location`.
15// - `startMillis/endMillis` -> `startDate/endDate` (UTC instant; `timeZone`
16// sets `event.timeZone` for display — the stored instants never shift).
17// - `allDay` -> `isAllDay`.
18// - `rrule` (raw RRULE line WITHOUT the `RRULE:` prefix) -> `recurrenceRules`
19// (`EKRecurrenceRule` parsed from the verbatim line; masters store duration
20// instead of end — same as `core.format.ICalendar`).
21// - `recurrenceId` -> detached occurrence (`event.recurrenceIdentifier`
22// lineage; each overridden instance merges independently).
23// - `deleted=true` envelope -> remove the `EKEvent`.
24// - `calendarId` selects the hub-managed calendar (NOT a system calendar id —
25// hub calendars are created per vault and recorded in the syncId map
26// namespace).
27//
28// MAC-CHECK (needs xcodebuild on a Mac):
29// - `EKRecurrenceRule` init from an RFC 5545 RRULE line: EventKit has no
30// string parser — the Mac must map the verbatim line through
31// `EKRecurrenceRule(recurrenceWith:interval:end:)` field-by-field
32// (FREQ/INTERVAL/COUNT/UNTIL/BYDAY/…). Keep that parser next to this file
33// (`RRuleParser.swift`); it is display-mapping, NOT merge logic.
34// - `requestFullAccessToEvents` vs legacy `requestAccess(to: .event)` per
35// deployment target.
36
37import EventKit
38import EverysyncCore
39
40/// Applies XCFramework driver output for one vault's events.
41/// Driver output in, `EKEvent` rows out — nothing else.
42final class CalendarSink {
43 private let eventStore: EKEventStore
44 private let syncIdMap: SyncIdMap
45
46 init(eventStore: EKEventStore, syncIdMap: SyncIdMap) {
47 self.eventStore = eventStore
48 self.syncIdMap = syncIdMap
49 }
50
51 // TODO(Mac): implement against the generated XCFramework `RemoteEvent` type:
52 //
53 // func apply(vaultId: String, envelopes: [RemoteEvent], targetCalendar: EKCalendar) throws:
54 // - map key = recurrenceId.isBlank ? uid : "uid␀recurrenceId"
55 // (matches `CalendarKind.syncId` — keep the separator ␀ U+0000).
56 // - deleted == true -> remove mapped EKEvent; drop the map entry.
57 // - else fetch-or-create EKEvent in targetCalendar; set title/notes/
58 // location/startDate/endDate/isAllDay/timeZone; recurrenceRules from
59 // the RRULE line via RRuleParser (nil line clears rules on masters;
60 // overridden instances keep the master's rules + their RECURRENCE-ID).
61 // - commit once; commit failure = unhealthy write (don't advance cursors).
62 //
63 // func detectLocalDeletes(knownKeys: Set<String>, targetCalendar: EKCalendar,
64 // window: (start: Date, end: Date)) throws -> Set<String>:
65 // - HEALTHY predicate fetch in the window first; throw on ANY failure.
66 // - known minus fetched = genuine local deletes -> tombstone envelopes.
67 // - Windowed-fetch caveat: only keys whose events fall inside the window
68 // are eligible for delete detection (older events need a wider fetch —
69 // document the window in E2E_CHECKLIST.md).
70
71 /// Composite sync key (matches `CalendarKind.syncId` exactly).
72 static func syncKey(uid: String, recurrenceId: String?) -> String {
73 guard let recurrenceId, !recurrenceId.isEmpty else { return uid }
74 return "\(uid)\u{0000}\(recurrenceId)"
75 }
76}