Cotton code
134 lines · 6.3 KB · SwiftRaw
// The suite's link table: where a RECORD lives, by kind.//// Kept TOTAL over `RecordKind` — the switch has no `default`, so a kind added// to the schema without an address does not compile. A table with a hole in it is how a kind silently becomes// unopenable from a project page, a search hit or a push.//// The prefixes are pinned by more than this file: `NOTIFICATION_ROUTES` maps// twenty server types onto// them, and a comment thread addresses an entry by kind + id. They are wire// protocol (P6) and they do not move.import Foundation/// Every record a link can point at.////// `LinkKind` (`cotton_core::constants`, `crates/cotton-core/src/constants.rs`)/// covers what `cotton_links` stores; the eight extras are records the graph/// deliberately does not hold — a journal is a/// note with a date-identity, a thread is somebody else's words, a memory is a/// standing rule, a mood is the owner's own reading of their own moment — but/// each is addressed by kind + id somewhere: a search hit, a capture history, a/// comment thread, a session's source.public enum RecordKind: String, CaseIterable, Sendable {    // ── `LINK_KINDS` ──────────────────────────────────────────────────────    case todo    case event    case note    case person    case personNote = "person_note"    case thought    case location    case page    case transaction    case meeting    case document    case newsEdition = "news_edition"    case photo    case video    // ── Addressed by kind + id, but never a link ──────────────────────────    case journal    case messageThread = "message_thread"    case memory    case session    case goal    case mood    case sessionMessage = "session_message"    case call}extension Route {    /// The in-app route for a record. Total over ``RecordKind``.    public static func record(_ kind: RecordKind, _ id: String) -> Route {        switch kind {        case .todo: return .todosTodo(id: id)        case .event: return .calendarEvent(id: id, date: nil, at: nil)        // The journal is a note with a date-identity; both open the editor.        case .note, .journal: return .notesNote(id: id)        // A person note's id resolves to its parent PERSON first — the caller        // passes the person's id here, as `usePersonNoteParent` did.        case .person, .personNote: return .peoplePerson(id: id)        // The TABLE keeps its historical name (`cotton_inbox_items`, wire        // protocol); the route, the kind and every word anybody reads say        // "thought".        case .thought: return .thoughtsThought(id: id)        // The bus has no page per message and deliberately does not want one: a        // message is read in the conversation it belongs to. This builder is        // handed a MESSAGE id and a message does not know its session, so the        // honest landing is the list — one tap from every session's page, and        // never a route built from an id no screen reads.        case .sessionMessage: return .code        case .call: return .callsCall(id: id)        case .location: return .locationsLocation(id: id)        case .page: return .readerPage(id: id)        case .transaction: return .moneyTransaction(id: id)        case .meeting: return .meetingsMeeting(id: id)        case .document: return .documentsDocument(id: id)        case .newsEdition: return .newsEdition(id: id)        // The id is the THREAD's, never a message's: a push names the message        // that woke you but lands you in the conversation, which is the thing        // you can act on.        case .messageThread: return .messagesThread(thread: id)        // Top-level: an entry is a memory whatever list it sits in, so its        // address must not name one — a global sentence and a project's        // sentence open the same screen.        case .memory: return .memory(id: id)        // One agent RUN. The id is the `cotton_sessions` row's — never the        // tool's own session id, which is an upsert key, not an address.        case .session: return .sessionsId(id: id)        case .goal: return .goalsGoal(id: id)        // The STRIP, and the id is deliberately unused: a check-in has no page        // of its own — it is one dot among fourteen days, and the fortnight is        // what a person opens to read any of them.        case .mood: return .mood        case .photo: return .photosPhoto(id: id)        case .video: return .videosVideo(id: id)        }    }}extension Route {    /// Where a `tinmen-cotton://` (or Cotton Dev's `cottondev://`) link lands.    ///    /// The scheme is not read here: the SYSTEM routes a link to the app that    /// registered its scheme, and each flavor registers only its own    /// (`CottonURLScheme`).    ///    /// The three the scheme carries are not screens of their own — two are    /// REDIRECTS and `capture` is an instruction — and the strings on both    /// sides of each arrow are wire protocol: the Android keyboard writes one,    /// and `tinmen-cotton://capture` is documented.    ///    ///   * `://capture`       → `.capture`, which raises capture mode    ///   * `://keyboard-take` → `/take` (the Android input method's link)    ///   * `://mood?score=N`  → the timeline; the retired score is ignored    ///    /// Anything else is read as a path, so a link carrying    /// `people/person/<id>` lands on that person. Unknown paths answer `nil`    /// and the caller does nothing — never a guess at the nearest screen.    public static func deepLink(_ url: URL) -> Route? {        // `tinmen-cotton://capture` puts `capture` in the HOST, while        // `tinmen-cotton:///capture` puts it in the path. Both are written by        // things this app ships, so both are read.        var text = (url.host ?? "") + url.path        if !text.hasPrefix("/") { text = "/" + text }        if let query = url.query, !query.isEmpty { text += "?" + query }        guard let route = parse(text) else { return nil }        switch route {        // The input method's hand-off has one name in the scheme and another        // in the app, and both are shipped (the route table is Android's too).        case .keyboardTake: return .take        default: return route        }    }}