// 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`). import Foundation import UserNotifications /// 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. public struct NotificationTap: Equatable, Sendable { /// The notification's `userInfo`, whole and un-unwrapped. public let userInfo: JSONValue /// `request.identifier`. Absent on a push composed without one. public let identifier: String? /// iOS's identifier for this response. Always the DEFAULT one today: this /// app registers no category and so offers no action. public let actionIdentifier: String public init( userInfo: JSONValue, identifier: String? = nil, actionIdentifier: String = NotificationTaps.defaultActionIdentifier ) { self.userInfo = userInfo self.identifier = identifier self.actionIdentifier = actionIdentifier } } /// What a tap should DO. public enum NotificationTapOutcome: Equatable, Sendable { /// Navigate here. case open(Route) /// Nothing: an unmapped type, an unreadable payload, or a kind that leads /// nowhere. Opening the app is the whole response. case nothing } public enum NotificationTaps { /// iOS's own identifier for "the person tapped the notification itself". public static let defaultActionIdentifier = UNNotificationDefaultActionIdentifier /// The data object, wherever it is. See the header. public static func payload(of userInfo: JSONValue) -> JSONValue { guard case .object = userInfo else { return userInfo } if let 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. public static func dedupeKey(_ tap: NotificationTap) -> String { if let identifier = tap.identifier, !identifier.isEmpty { return identifier } let data = payload(of: tap.userInfo) if let 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. public static func 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) if let route = ReminderTaps.route(for: data) { return .open(route) } if let 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. public static func link(_ tap: NotificationTap) -> DatabaseLink? { guard case .open(let route) = resolve(tap) else { return nil } let database = NotificationRoutes.stringParam(payload(of: tap.userInfo), "databaseId") return DatabaseLink(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. public static func 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 } }