Cotton code
214 lines · 10.5 KB · SwiftRaw
// The push types the server composes, each mapped to a `Route`.//// Row for row: every fallback, every parameter that rides the payload unread,// and the two retired Calls types kept ABSENT with the sentence saying why.//// **THE SENDER IS THE SERVER, and this table is exactly the list of what it// sends.** A type the server does not send has no business here — which is why// the two Calls pushes (`cotton_call_missed` and the ring's alert fallback)// left with the cell in 2026-08-23 and are named below rather than merely// missing: *"a route for a push nobody sends is a promise nothing keeps"*. When// Calls is ported its types come back WITH their sender and their tests, in the// same change.//// **An unmapped type answers `nil` and the caller does NOTHING** — never a// guess at the nearest screen. That is the same sentence `Route.parse` already// carries. The table is held TOTAL against `cotton_core::rules::notifications`'// `PUSH_TYPES` through `routes.list`, and both of that guard's known holes are// EMPTY today: every type the server sends is mapped, and every type this table// maps is sent.//// **The five chat and share types went with the multi-person sharing data// plane** (the code is on the branch `archive/sharing-v1`): `cotton_message`,// `share_invitation`, `share_accepted`, `share_ended` and `share_key_changed`.// `grant_created` is NOT one of them — it is OAuth, and it stays.//// Where the payload LIVES: the crate's `apns.rs` places the custom data object// at a top-level `body` key, because `expo-notifications` mapped// `content.data = userInfo["body"]`. A native `UNUserNotificationCenter` reads// `userInfo` whole, so the reading of that envelope is ``NotificationTap``'s —// this file is handed the data object itself.import Foundation/// The push tap table.public enum NotificationRoutes {    /// A non-empty `String` field, or `nil`. The ONE extractor, used everywhere    /// — `stringParam`, ported.    public static func stringParam(_ data: JSONValue, _ key: String) -> String? {        guard let value = data[key]?.string, !value.isEmpty else { return nil }        return value    }    /// One type and where it lands.    ///    /// The lookup dictionary is derived from this declaration. Membership    /// matches the native vocabulary; declaration order does not affect routing.    public struct Entry: Sendable {        public let type: String        public let route: @Sendable (JSONValue) -> Route    }    /// The push routes, with the types    /// that arrived after it — the Code tool's three beside the session    /// question they belong with, and `device_join_requested` at the end of    /// the device block.    public static let entries: [Entry] = [        // A statement finished being read. The notification's whole point is        // the paper Cotton just read; the account is one tap back from there.        Entry(type: "cotton_money_statement_read") { data in            guard let statementId = stringParam(data, "statementId") else { return .money }            return .moneyStatement(id: statementId)        },        Entry(type: "cotton_meeting_ready") { data in            guard let meetingId = stringParam(data, "meetingId") else { return .meetings(importing: false) }            return .record(.meeting, meetingId)        },        // `errorCode` rides and is unread: the failure's own page says more        // than a code in a banner could.        Entry(type: "cotton_meeting_failed") { data in            guard let meetingId = stringParam(data, "meetingId") else { return .meetings(importing: false) }            return .record(.meeting, meetingId)        },        Entry(type: "cotton_document_ready") { data in            guard let documentId = stringParam(data, "documentId") else {                return .documents(status: nil)            }            return .record(.document, documentId)        },        // An assistant's `notify_owner`: its document, else the documents        // list (the decide queue left with TABLE DROP 1).        Entry(type: "cotton_agent_note") { data in            guard let documentId = stringParam(data, "documentId") else {                return .documents(status: nil)            }            return .record(.document, documentId)        },        Entry(type: "cotton_session_question") { data in            guard let sessionId = stringParam(data, "sessionId") else {                return .code            }            return .record(.session, sessionId)        },        // The Code tool's three (Cotton note "The Code tool — why a Mac holds the sessions"). A session        // blocked on the person opens ON that session, because the answer is a        // button on its own page and a list would be one tap further from the        // only thing the push is about. `waitingKind` and `requestId` ride the        // payload and are UNREAD: the page asks `read.session` for the row, so        // what it draws is what the Mac says now rather than what it said when        // the push was composed — a permission answered on another device in        // the meantime reads as settled rather than as a live question.        Entry(type: "cotton_session_waiting") { data in            guard let sessionId = stringParam(data, "sessionId") else {                return .code            }            return .record(.session, sessionId)        },        // `exitCode` rides and is unread — the session's own page carries the        // exit, the last line and the report, which is more than a banner        // could say.        Entry(type: "cotton_session_ended") { data in            guard let sessionId = stringParam(data, "sessionId") else {                return .code            }            return .record(.session, sessionId)        },        // `storyCount` rides and is unread.        Entry(type: "cotton_news_edition_ready") { data in            guard let editionId = stringParam(data, "editionId") else { return .news }            return .record(.newsEdition, editionId)        },        Entry(type: "cotton_news_edition_failed") { data in            guard let editionId = stringParam(data, "editionId") else { return .news }            return .record(.newsEdition, editionId)        },        // Always the reports list. A report filed in the app is read        // there, and `feedbackId` rides the payload unread: the list carries        // every report this person has given, newest first.        Entry(type: "cotton_feedback_capture") { _ in .tableList(table: "project_feedback", filter: nil, title: nil, query: nil) },        // `runId` rides and is unread.        Entry(type: "cotton_assistant_result") { data in            guard let noteId = stringParam(data, "noteId") else { return .notes }            return .record(.note, noteId)        },        // A Mac that holds the household's copy and does not serve it: its        // copy has fallen behind, or the Mac that serves has gone quiet — and        // each one's clearing. Devices is where the Macs and who serves are.        // The serving Mac's own sender rang the owner (its first push, or        // Test push): Devices is where that Mac's row and its line are.        Entry(type: "cotton_first_push") { _ in .devices },        Entry(type: "cotton_standby_behind") { _ in .devices },        Entry(type: "cotton_standby_silent") { _ in .devices },        Entry(type: "device_approved") { _ in .devices },        // A device ASKED to join, and the tap is the person going to answer —        // on THAT request, not on a settings page with a list somewhere on it.        //        // `name`, `platform` and `digits` ride the payload and are UNREAD: the        // screen asks `devices.pending` for the row, so what it draws is what        // the server says now rather than what it said when the push was        // composed — a request answered on another device in the meantime reads        // as settled rather than as a live question.        //        // The id-less fallback is the Devices tool, where the waiting list is: a push        // with no `pairingId` is a push this build cannot open a page for, and        // the list is the same two verbs one tap further on.        Entry(type: "device_join_requested") { data in            guard let pairingId = stringParam(data, "pairingId") else { return .devices }            return .settingsJoinRequest(id: pairingId)        },        Entry(type: "device_revoked") { _ in .devices },        // The recovery key is a fact of this device's backups (Spec 10.4).        Entry(type: "recovery_key_shown") { _ in .device },    ]    /// Every type this table maps, in declaration order.    public static let mappedTypes: [String] = entries.map(\.type)    /// Every push type that LEFT, named rather than merely absent, so a build    /// that quietly re-added one would not look like a fix.    ///    /// The two Calls pushes went 2026-08-23 with the cell that sent them. The    /// seven below went with the multi-person sharing data plane: nothing on    /// this device receives a message any more — the archive it reads is    /// IMPORTED — and there is nobody to share with. The account-usage,    /// outage and repository pushes went with TABLE DROP 1 (27 Nov 2026). The crate's    /// `RETIRED_PUSH_TYPES` is the twin of this list.    public static let retiredTypes: [String] = [        "cotton_account_usage_high",        "cotton_call_missed",        "cotton_call_ring",        "cotton_message",        "cotton_outage",        "cotton_repo_checks_failed",        "cotton_repo_comment",        "cotton_repo_pull_opened",        "cotton_repo_pull_ready",        "cotton_repo_review_requested",        "cotton_repo_run_failed",        "grant_created",        "share_accepted",        "share_ended",        "share_invitation",        "share_key_changed",        "share_revoked",        "share_stale",    ]    private static let byType: [String: @Sendable (JSONValue) -> Route] = Dictionary(        uniqueKeysWithValues: entries.map { ($0.type, $0.route) }    )    /// Where this push's tap lands, or `nil` to ignore it.    ///    /// `nil` for anything that is not an object, carries no string `type`, or    /// names a type this table does not map — the three shapes of "not mine",    /// and the reason mounting this table beside the reminder one is safe.    public static func route(for data: JSONValue) -> Route? {        guard            case .object = data,            let type = data["type"]?.string,            let resolve = byType[type]        else { return nil }        return resolve(data)    }}