Cotton code
174 lines · 8.9 KB · SwiftRaw
// Which shares KNOCK on the app's door, and where they knock.//// The share extension stages the bytes, writes the mailbox entry and — for// some kinds — opens Cotton. This is the rule about WHICH kinds, declared once// so that adding one is an entry in a list rather than an edit inside a// `guard`.//// ── Why a share opens the app at all ─────────────────────────────────────────//// An extension cannot import anything: it has no core and no database// (design P9). What it can do is stage the bytes, write the mailbox entry and// knock. The app drains, files and lands.//// The owner's ruling (15 Sep 2026) was about a WhatsApp export: "it// automatically should open the app and process it there and take me into the// chat". The walk that afternoon found the second half of the same wish — a// shared voice note staged quietly, did nothing visible, and was still sitting// in the app group when the person went looking for their meeting. So a// recording knocks too.//// ── Every kind knocks ────────────────────────────────────────────────────//// It used to be a chat export and a recording only — a picture and a link// were filed quietly on the next foreground, on the reasoning that yanking// somebody out of Safari to watch that happen was worse. The owner ruled the// other way (23 Oct 2026): when the share sheet closes the app opens, for// every kind of shared item, the way the chat-export branch does. Somebody who// shared a thing into Cotton wants to see where it went. So every kind the// drain has a branch for knocks, and each knocks at the screen that branch// would land on had it found nothing to file.// ── The knock is not navigation ──────────────────────────────────────────────//// The URL says "a new entry is in the mailbox"; the DRAIN decides where the app// goes, because the drain is the only thing holding the staged file// (`Boot/ShareArrivals.swift`). The route in the knock matters for exactly one// case — a build whose drain finds nothing, because something else drained// first — and there it has to land somewhere honest for the kind that was// shared. That is why a recording knocks at `/meetings` and not at// `/messages/import`.import Foundation/// A share that opens Cotton, and what it is./// One per branch of the drain (`ShareIntake.swift`), in its precedence.public enum ShareKnockKind: String, Sendable, Equatable, CaseIterable {    /// A `.cottonpkg`.    case package    /// A zip, or a WhatsApp `_chat.txt`.    case chatExport    /// A Goodreads library export, about to be handed to the object.    case goodreads    /// An EPUB, or a PDF in Cotton Books, about to become a book.    case book    /// A photo or a clip, about to be filed in Photos or Videos.    case picture    /// An audio file that is about to become a meeting.    case recording    /// An audio file or a folder of them, about to become songs, or (in    /// Cotton Music) an album ZIP, about to become its album.    case song    /// A video or a folder of them, about to become films (Cotton Cinemas).    case film    /// Any other file, about to become a document.    case document    /// No file: an address, a page, or prose that may carry addresses.    case link}/// Which shares knock, where, and how the app recognises one coming back.public enum ShareKnock {    /// What this share is, or `nil` when it carries nothing the drain files.    ///    /// **The ONE list**, and it reads the SAME classification the drain does —    /// `isPackageShare`, `isChatExportShare`, `isPictureShare` and    /// `isAudioShare`, out of `ShareIntake`, in the drain's order — so a share    /// knocks for the branch that will take it. Two lists that must agree is    /// how a fix lands on only one of them.    ///    /// `head` is the first bytes of the file, for the two rules that have to    /// look inside one: a `.txt` whose name does not say whether it is an    /// export, and a `.csv` that may be a Goodreads library. It is a closure    /// rather than a string so nothing is read for a share that is decided by    /// its name. `shelf` says whether a PDF is a book in this app.    public static func kind(_ entry: ShareEntry, shelf: ShareShelf, head: (String) -> String?) -> ShareKnockKind? {        guard let file = SharedFile(entry) else {            return entry.url != nil || entry.text != nil ? .link : nil        }        return kind(file, shelf: shelf, head: { head(file.path) })    }    /// What this shared FILE is: always a kind, since the drain's last file    /// branch takes every file the narrow ones leave.    public static func kind(_ file: SharedFile, shelf: ShareShelf, head: () -> String?) -> ShareKnockKind {        if isPackageShare(file) { return .package }        // In Cotton Music a ZIP is an album first (a Bandcamp download); the        // drain sends one holding no song on to the chat export.        if shelf.audioIsMusic, isZipShare(file) { return .song }        if isChatExportShare(file, head: head) { return .chatExport }        if isGoodreadsShare(file, head: head) { return .goodreads }        if shareBookFormat(file, shelf: shelf) != nil { return .book }        if shelf.videoIsFilm, isFilmShare(file) || isFolderShare(file) { return .film }        // In Cotton an MKV or an AVI is a film whatever its length: no camera        // writes one. A clip's container knocks as a picture; the drain asks.        if shelf.videoMayBeFilm, isFilmOnlyShare(file) { return .film }        if isPictureShare(file) { return .picture }        if isFolderShare(file) { return .song }        if isAudioShare(file) { return shelf.audioIsMusic || isMusicOnlyShare(file) ? .song : .recording }        return .document    }    /// Where this kind knocks — the route a build whose drain found nothing    /// should still land on.    ///    /// The route strings are the crate's (design P6), reached through `Route`    /// rather than spelled here.    public static func route(_ kind: ShareKnockKind) -> Route {        switch kind {        case .package: return .bringIn        case .chatExport: return .messagesImport        case .goodreads, .book: return bookList        case .picture: return .photos(show: nil)        case .recording: return .meetings(importing: false)        case .song: return songList        case .film: return filmList        case .document: return .documents(status: nil)        case .link: return .reader        }    }    /// Where a book or a Goodreads library lands when the drain found nothing    /// to file: the books' own list.    public static let bookList = Route.tableList(table: "book", filter: nil, title: nil, query: nil)    /// Where songs land when the drain found nothing to file: the songs' list.    public static let songList = Route.tableList(table: "song", filter: nil, title: nil, query: nil)    /// Where films land when the drain found nothing to file: the films' list.    public static let filmList = Route.tableList(table: "film", filter: nil, title: nil, query: nil)    /// Where a subscription list lands when it brought no new show in: the    /// podcasts' list.    public static let podcastList = Route.tableList(table: "podcast", filter: nil, title: nil, query: nil)    /// The URL to open Cotton at, carrying the mailbox entry's id.    ///    /// The id is a fact worth having in the log; the app never looks the entry    /// up by it, because **the MAILBOX is the truth and the URL is only the    /// knock**.    public static func url(_ kind: ShareKnockKind, shareId: String) -> URL? {        var components = URLComponents()        components.scheme = CottonURLScheme.name        components.host = ""        components.path = route(kind).path        components.queryItems = [URLQueryItem(name: ShareOpen.marker, value: shareId)]        return components.url    }    /// Whether this URL is the extension's knock rather than an ordinary deep    /// link into one of those screens (a notification's, one typed by hand). Only a knock triggers a drain.    ///    /// The marker decides, and the route has to be one a knock uses — a    /// `?share=` on some other screen is somebody else's query parameter.    public static func isArrival(_ url: URL) -> Bool {        guard CottonURLScheme.isOurs(url) else { return false }        guard let landing = Route.deepLink(url) else { return false }        guard ShareKnockKind.allCases.contains(where: { route($0) == landing }) else {            return false        }        guard let items = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems        else { return false }        return items.contains { $0.name == ShareOpen.marker }    }}