// ONE tap, and the two parsers behind it.//// iOS gives an app ONE `UNUserNotificationCenterDelegate` callback for every// notification anybody taps, so the two tables — the push table and the// local-reminder table — become two parsers behind one entry point here.// Each rejects the other's payload shape (see `ReminderTaps`' header), which is// what makes the merge safe and is the test this file owes.//// **The payload lives under `userInfo["body"]`, and reading that wrong is// silent.** The server's `apns.rs` places its custom data object at a top-level// `body` key rather than merging it into the root, because the previous app's// notification library mapped `content.data` to `userInfo["body"]`. That is a// fact about a runtime this app replaces — but the server still writes it, and// changing the server from a phone lane would break any build still on the old// runtime. So this side reads `body` and FALLS BACK to the root: a phone// that reads both is a phone that survives either, and the day the nesting goes// (it is in the after-the-cutover ledger) nothing here changes.//// A local reminder has no `body` key at all — its `userInfo` IS `{kind, id}` —// so the same rule serves both tables with one line.//// **No notification carries an ACTION.** The reply box on a message banner was// the only one, and it went with the multi-person sharing data plane (the code// is on the branch `archive/sharing-v1`) — there is nobody to reply to. This// app registers no notification category, so every response iOS hands over is// an ordinary tap. Cotton Chat's notifications are the exception, and never// reach these tables: their Reply, Mark as read and Call back are answered by// the chat engine before the bridge (`ChatNotificationHooks`).importFoundationimportUserNotifications/// One response, in the shape both parsers need.////// A value type with no `UNNotificationResponse` in it, so a test can name a/// tap without a notification centre — and so the delegate's job shrinks to/// building one of these.publicstructNotificationTap: Equatable, Sendable {/// The notification's `userInfo`, whole and un-unwrapped.publiclet userInfo: JSONValue/// `request.identifier`. Absent on a push composed without one.publiclet identifier: String?/// iOS's identifier for this response. Always the DEFAULT one today: this/// app registers no category and so offers no action.publiclet actionIdentifier: Stringpublicinit( userInfo: JSONValue, identifier: String? = nil, actionIdentifier: String = NotificationTaps.defaultActionIdentifier ) {self.userInfo = userInfoself.identifier = identifierself.actionIdentifier = actionIdentifier }}/// What a tap should DO.publicenumNotificationTapOutcome: Equatable, Sendable {/// Navigate here.caseopen(Route)/// Nothing: an unmapped type, an unreadable payload, or a kind that leads/// nowhere. Opening the app is the whole response.case nothing}publicenumNotificationTaps {/// iOS's own identifier for "the person tapped the notification itself".publicstaticlet defaultActionIdentifier = UNNotificationDefaultActionIdentifier/// The data object, wherever it is. See the header.publicstaticfunc payload(of userInfo: JSONValue) -> JSONValue {guardcase .object = userInfo else { return userInfo }iflet nested = userInfo["body"], case .object = nested { return nested }return userInfo }/// The key this response is deduped on.////// Prefers the notification's own identifier; falls back to the/// `notificationId` the send worker injects into every payload, so two/// distinct identifier-less notifications never collide on a shared/// sentinel; and finally to the literal `"unknown"`, which is the/// TypeScript's last resort and is kept because dropping a second/// nameless tap is better than double-pushing the first one's screen.publicstaticfunc dedupeKey(_ tap: NotificationTap) -> String {iflet identifier = tap.identifier, !identifier.isEmpty { return identifier }let data = payload(of: tap.userInfo)iflet injected = NotificationRoutes.stringParam(data, "notificationId") {return injected }return"unknown" }/// What this tap should do.////// The reminder table is asked first and the push table second, and the/// order is not load-bearing: each answers `nil` for the other's shape./// `NotificationTapTests` proves that by resolving both payloads through/// both tables.publicstaticfunc resolve(_ tap: NotificationTap) -> NotificationTapOutcome {// A response that is not the plain tap is ignored: this app registers// no category, so an action identifier it did not offer is not its own.guard tap.actionIdentifier == defaultActionIdentifier else { return .nothing }let data = payload(of: tap.userInfo)iflet route = ReminderTaps.route(for: data) { return .open(route) }iflet route = NotificationRoutes.route(for: data) { return .open(route) }return .nothing }/// What this tap opens, and in which database: the route `resolve` finds,/// and the `databaseId` the payload names — the stamp a row read across/// databases carries. No server notification carries one yet, so today the/// database is nil and the tap opens where a link with none does.publicstaticfunc link(_ tap: NotificationTap) -> DatabaseLink? {guardcase .open(let route) = resolve(tap) else { returnnil }let database = NotificationRoutes.stringParam(payload(of: tap.userInfo), "databaseId")returnDatabaseLink(database: database, route: route) }// ── The bridge from UIKit's own bag ───────────────────────────────────/// `userInfo` as a `JSONValue`.////// The one place this app meets `[AnyHashable: Any]`, because that is the/// type iOS hands a delegate. It is converted through `JSONSerialization`/// rather than by casting member by member: a cast chain is a guess the/// compiler cannot check, and an APNs payload is JSON by construction —/// so the honest reading is "re-parse it", and anything that is not JSON/// (which no notification this app can receive is) answers `.null` and/// routes nowhere.publicstaticfunc value(of userInfo: [AnyHashable: Any]) -> JSONValue {guard JSONSerialization.isValidJSONObject(userInfo),let data = try? JSONSerialization.data(withJSONObject: userInfo),let value = try? JSONDecoder().decode(JSONValue.self, from: data)else { return .null }return value }}