Cotton code
1028 lines · 39.9 KB · SwiftRaw
// Cotton Chat (chat-new-ios, 19 Nov 2026): the shapes the core answers// Chat's five methods in, mirrored from `cotton-core::messages::app::shapes`// (camelCase on the wire). The phone decides nothing: order, names, last// lines, times, counts, grouping, dividers, ticks, the composer's button,// every word and every colour arrive here.////   chat.list    {filter?, search?, after?}                      → ChatListAnswer//   chat.thread  {thread, before?, after?, around?}              → ChatThreadAnswer//   chat.compose {thread, text?, replyTo?, edit?, cancel?}       → ChatComposeAnswer//   chat.act     {act, thread, message?, messages?, text?, emoji?, to?, until?, replyTo?, seconds?} → ChatActAnswer//   chat.themes  {}                                              → ChatThemesAnswerimport Foundationenum ChatMethod {    static let list = "chat.list"    static let thread = "chat.thread"    static let compose = "chat.compose"    static let act = "chat.act"    static let themes = "chat.themes"    static let picture = "chat.picture"    static let ahead = "chat.ahead"    static let warm = "chat.warm"    static let previews = "chat.previews"    static let preview = "chat.preview"    /// A link's page this phone fetched, read by the core as its card (`chat.linkCard {thread, url, page}`, CONTRACT.md "Link previews").    static let linkCard = "chat.linkCard"    /// The voice player: the speed (`chat.settings` voiceSpeed) and waveforms for old voice messages.    static let settings = "chat.settings"    static let waveforms = "chat.waveforms"    static let waveform = "chat.waveform"    static let file = "chat.file"    static let flush = "chat.flush"    static let call = "chat.call"    static let calls = "chat.calls"    static let callScreen = "chat.callScreen"    static let groupCall = "chat.groupCall"}/// A message's tick: `sending`, `sent`, `delivered`, `read`, `failed`.enum ChatTick: String, Codable, Sendable {    case sending, sent, delivered, read, failed}/// The small mark before a last line or inside a bubble.enum ChatMark: String, Codable, Sendable {    case photo, album, video, voice, audio, file, contact, location, poll, deleted}/// A round picture: the photo's media key, else typed initials on a tint.struct ChatAvatar: Codable, Equatable, Sendable, Hashable {    var photo: String?    var initials: String    var tint: Int    /// Cotton's own mark in place of a face (the Downloads chat's `picture.mark`).    var mark: String? = nil}struct ChatLastLine: Codable, Equatable, Sendable, Hashable {    var text: String    var sender: String?    var mark: ChatMark?    var mine: Bool    var tick: ChatTick?    /// Where his own last line stands while it is on its way to WhatsApp through the Mac, in its bubble's words    /// ("Waiting to send", "Sending…", "Not sent: <why>"); absent once it has gone (CONTRACT.md "Writing to a number").    var state: String? = nil}struct ChatSwipe: Codable, Equatable, Sendable, Hashable {    var act: String    var label: String    /// The question to ask before the act (Delete), in the core's words.    var confirm: String?}struct ChatListRow: Codable, Equatable, Sendable, Hashable, Identifiable {    /// Cotton's own Downloads chat's thread (the core's `chat_downloads::THREAD`): in every list, never the person's.    static let downloads = "downloads"    var thread: String    /// A message can be written into it from here.    var sendable: Bool?    var name: String    var group: Bool    var source: String    var picture: ChatAvatar    var lastLine: ChatLastLine?    var when: String?    var unread: Int    var markedUnread: Bool    var pinned: Bool    var muted: Bool    var archived: Bool    var typing: String?    var draft: String?    var leading: [ChatSwipe]    var trailing: [ChatSwipe]    /// A hold's menu, in the core's order and words: Mark as read or Mark as unread, Pin, Mute, Archive, Delete.    var hold: [ChatSwipe] = []    var id: String { thread }    enum CodingKeys: String, CodingKey {        case thread, sendable, name, group, source, picture, lastLine, when, unread, markedUnread, pinned, muted, archived, typing, draft, leading, trailing, hold    }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        thread = try c.decode(String.self, forKey: .thread)        sendable = try c.decodeIfPresent(Bool.self, forKey: .sendable)        name = try c.decode(String.self, forKey: .name)        group = try c.decode(Bool.self, forKey: .group)        source = try c.decode(String.self, forKey: .source)        picture = try c.decode(ChatAvatar.self, forKey: .picture)        lastLine = try c.decodeIfPresent(ChatLastLine.self, forKey: .lastLine)        when = try c.decodeIfPresent(String.self, forKey: .when)        unread = try c.decode(Int.self, forKey: .unread)        markedUnread = try c.decode(Bool.self, forKey: .markedUnread)        pinned = try c.decode(Bool.self, forKey: .pinned)        muted = try c.decode(Bool.self, forKey: .muted)        archived = try c.decode(Bool.self, forKey: .archived)        typing = try c.decodeIfPresent(String.self, forKey: .typing)        draft = try c.decodeIfPresent(String.self, forKey: .draft)        leading = try c.decode([ChatSwipe].self, forKey: .leading)        trailing = try c.decode([ChatSwipe].self, forKey: .trailing)        hold = try c.decodeIfPresent([ChatSwipe].self, forKey: .hold) ?? []    }}/// A fan tile, in the fan's order: a filter id, or `account`.struct ChatFanTile: Codable, Equatable, Sendable, Hashable, Identifiable {    var id: String    var label: String    var count: Int?    var on: Bool}struct ChatListCounts: Codable, Equatable, Sendable {    var waiting: Int?    var all: Int    var people: Int    var groups: Int    var unread: Int    var archived: Int    var sources: [String: Int]}struct ChatListWords: Codable, Equatable, Sendable {    var title: String    var search: String    var empty: String}struct ChatListAnswer: Codable, Equatable, Sendable {    var filter: String    var search: String?    var rows: [ChatListRow]    var next: String?    var counts: ChatListCounts    var fan: [ChatFanTile]    /// The filters above the list: All, Unread, Groups.    var filters: [ChatFanTile]    var words: ChatListWords    /// The wait in ms until any chat's next message goes by its timer (absent: none set to go): the list is asked again then.    var erasesIn: Double? = nil    enum CodingKeys: String, CodingKey { case filter, search, rows, next, counts, fan, filters, words, erasesIn }    init(filter: String, search: String?, rows: [ChatListRow], next: String?, counts: ChatListCounts, fan: [ChatFanTile],         filters: [ChatFanTile] = [], words: ChatListWords) {        self.filter = filter; self.search = search; self.rows = rows; self.next = next; self.counts = counts; self.fan = fan        self.filters = filters; self.words = words    }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        filter = try c.decode(String.self, forKey: .filter)        search = try c.decodeIfPresent(String.self, forKey: .search)        rows = try c.decode([ChatListRow].self, forKey: .rows)        next = try c.decodeIfPresent(String.self, forKey: .next)        counts = try c.decode(ChatListCounts.self, forKey: .counts)        fan = try c.decodeIfPresent([ChatFanTile].self, forKey: .fan) ?? []        filters = try c.decodeIfPresent([ChatFanTile].self, forKey: .filters) ?? []        words = try c.decode(ChatListWords.self, forKey: .words)        erasesIn = try c.decodeIfPresent(Double.self, forKey: .erasesIn)    }}// MARK: chat.threadenum ChatBubbleKind: String, Codable, Sendable {    case text, photo, album, video, voice, audio, file, contact, location, poll, deleted}struct ChatMediaPart: Codable, Equatable, Sendable, Hashable {    var index: Int    var kind: ChatMark    var name: String?    var width: Double?    var height: Double?    var length: String?    var size: String?    var waveform: [Double]?    var preview: String?    var contact: String?    var number: String?    /// The core says the store does not hold the file (it answered 404 when last asked) and asks again only later.    var gone: Bool? = nil    /// A voice message's words, heard on its sender's phone after it went (`chat.act transcript`): drawn under the player.    var transcript: String? = nil    /// Voice, audio and video: the length as a number, for the player's countdown and position.    var durationMs: Int? = nil    /// Voice: the core's 36 bar heights, 0…1 (none: the player draws its thin track).    var bars: [Double]? = nil    /// Voice: false on a note of theirs not played yet on any of the person's phones; absent is played.    var played: Bool? = nil    /// The message this part opens through when it is not its bubble's (FEEDBACK-4 B3): a grid of one tap's pictures    /// to WhatsApp names each tile's own, `outbox:<row>` while it waits (index 0), the landed message after.    var message: String? = nil    /// The message this part's picture and file are asked of, kept and opened by (`chat.picture`, `chat.file`).    func opens(in bubble: ChatBubble) -> String { message ?? bubble.message }}struct ChatLinkCard: Codable, Equatable, Sendable, Hashable {    var url: String    var site: String?    var title: String?    var description: String?    var thumb: String?}struct ChatQuote: Codable, Equatable, Sendable, Hashable {    var message: String?    var sender: String    var line: String    var mark: ChatMark?}struct ChatReaction: Codable, Equatable, Sendable, Hashable {    var emoji: String    var count: Int    var mine: Bool}struct ChatSender: Codable, Equatable, Sendable, Hashable {    var name: String    var picture: ChatAvatar    var tint: Int}/// An agent's message: whose agent it is (`agent: { id, name, line }` on the bubble). Plain data, so it stands outside/// ChatAgent.swift's iOS-only drawing: the macOS stub build compiles these shapes too.struct ChatAgentTag: Codable, Equatable, Sendable, Hashable {    var id: String    var name: String    var line: String}struct ChatBubble: Codable, Equatable, Sendable, Hashable, Identifiable {    var message: String    var mine: Bool    var kind: ChatBubbleKind    var text: String?    var sender: ChatSender?    var head: Bool    var tail: Bool    /// First of one sender's unbroken words in a day: their picture, and in a group their name, stand here once.    var face: Bool    var time: String    var tick: ChatTick?    /// The person's own message still going: until this moment (ms since the epoch) nothing is drawn under    /// it, so a send that goes at once never flashes "Sending" (the core's `quietUntil`).    var quietUntil: Double?    /// Where the person's own text stands, in the core's words, drawn under it whenever present: a WhatsApp text    /// on its way through the Mac ("Waiting to send", "Sending…", "Not sent: <why>"). It wins over the marks.    var state: String? = nil    var edited: String?    var forwarded: String?    var pinned: Bool    var reply: ChatQuote?    var reactions: [ChatReaction]    var media: [ChatMediaPart]    var link: ChatLinkCard?    var actions: [String]    /// A poll, drawn as one (CONTRACT.md "Polls shown"): its words stand here, never as `text`.    var poll: ChatPoll? = nil    /// An agent's words (CONTRACT.md "An agent in a chat"): whose agent wrote them; drawn on the left, never as his.    var agent: ChatAgentTag? = nil    var id: String { message }    enum CodingKeys: String, CodingKey {        case message, mine, kind, text, sender, head, tail, face, time, tick, quietUntil, state, edited, forwarded, pinned, reply, reactions, media, link, actions, poll, agent    }    init(message: String, mine: Bool, kind: ChatBubbleKind, text: String?, sender: ChatSender?, head: Bool, tail: Bool,         time: String, tick: ChatTick?, edited: String?, forwarded: String?, pinned: Bool, reply: ChatQuote?,         reactions: [ChatReaction], media: [ChatMediaPart], link: ChatLinkCard?, actions: [String], face: Bool? = nil) {        self.message = message; self.mine = mine; self.kind = kind; self.text = text; self.sender = sender        self.head = head; self.tail = tail; self.face = face ?? head; self.time = time; self.tick = tick; self.edited = edited        self.forwarded = forwarded; self.pinned = pinned; self.reply = reply; self.reactions = reactions        self.media = media; self.link = link; self.actions = actions    }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        message = try c.decode(String.self, forKey: .message)        mine = try c.decode(Bool.self, forKey: .mine)        kind = try c.decode(ChatBubbleKind.self, forKey: .kind)        text = try c.decodeIfPresent(String.self, forKey: .text)        sender = try c.decodeIfPresent(ChatSender.self, forKey: .sender)        head = try c.decode(Bool.self, forKey: .head)        tail = try c.decode(Bool.self, forKey: .tail)        face = try c.decodeIfPresent(Bool.self, forKey: .face) ?? head        time = try c.decode(String.self, forKey: .time)        tick = try c.decodeIfPresent(ChatTick.self, forKey: .tick)        quietUntil = try c.decodeIfPresent(Double.self, forKey: .quietUntil)        state = try c.decodeIfPresent(String.self, forKey: .state)        edited = try c.decodeIfPresent(String.self, forKey: .edited)        forwarded = try c.decodeIfPresent(String.self, forKey: .forwarded)        pinned = try c.decodeIfPresent(Bool.self, forKey: .pinned) ?? false        reply = try c.decodeIfPresent(ChatQuote.self, forKey: .reply)        reactions = try c.decodeIfPresent([ChatReaction].self, forKey: .reactions) ?? []        media = try c.decodeIfPresent([ChatMediaPart].self, forKey: .media) ?? []        link = try c.decodeIfPresent(ChatLinkCard.self, forKey: .link)        actions = try c.decodeIfPresent([String].self, forKey: .actions) ?? []        poll = try c.decodeIfPresent(ChatPoll.self, forKey: .poll)        agent = try c.decodeIfPresent(ChatAgentTag.self, forKey: .agent)    }}/// A poll as its bubble draws it (the core's `poll::card`): the question, how many may be chosen, each option with/// its count, its part of the people who voted and who chose it, and the total.struct ChatPoll: Codable, Equatable, Sendable, Hashable {    var question: String    /// "Select one" or "Select one or more"; absent on an exported poll.    var choose: String?    var options: [ChatPollOption]    /// Everyone who voted, once each ("4 votes", "No votes yet").    var said: String    /// The whole poll as one line, for VoiceOver.    var spoken: String    /// The Votes panel's title.    var votesTitle: String    /// He can vote here (a WhatsApp poll his Mac's link sends to): a tap on an option is `chat.act vote`.    var votable: Bool? = nil}struct ChatPollOption: Codable, Equatable, Sendable, Hashable {    var text: String    var votes: Int    /// "2 votes", "1 vote", "No votes".    var said: String    /// Its part of the people who voted, 0 to 100: the bar's length.    var percent: Int    /// Who chose it, as the chat names them, "You" first.    var voters: [String]    /// The person's own choice.    var mine: Bool    enum CodingKeys: String, CodingKey { case text, votes, said, percent, voters, mine }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        text = try c.decode(String.self, forKey: .text)        votes = try c.decodeIfPresent(Int.self, forKey: .votes) ?? 0        said = try c.decode(String.self, forKey: .said)        percent = try c.decodeIfPresent(Int.self, forKey: .percent) ?? 0        voters = try c.decodeIfPresent([String].self, forKey: .voters) ?? []        mine = try c.decodeIfPresent(Bool.self, forKey: .mine) ?? false    }}/// One item of a thread page, oldest first (`item` names which).enum ChatThreadItem: Codable, Equatable, Sendable, Hashable, Identifiable {    /// A day: its label, and the numeral and words it is drawn as ("20", "Friday · November").    case day(label: String, numeral: String? = nil, words: String? = nil)    case unread(label: String)    /// `opens`: what a tap on the line opens, where it opens something (`safety`: the safety number's screen).    /// `groupCall`: a group's call still going on, its line offering Join (chat/CONTRACT.md "Group calls").    case event(message: String, text: String, call: ChatThreadCall? = nil, opens: String? = nil, groupCall: ChatGroupCallLine? = nil)    case bubble(ChatBubble)    private enum Tag: String, CodingKey { case item, label, message, text, call, opens, numeral, words, groupCall }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: Tag.self)        switch try c.decode(String.self, forKey: .item) {        case "day":            self = .day(label: try c.decode(String.self, forKey: .label), numeral: try c.decodeIfPresent(String.self, forKey: .numeral),                        words: try c.decodeIfPresent(String.self, forKey: .words))        case "unread": self = .unread(label: try c.decode(String.self, forKey: .label))        case "event":            self = .event(message: try c.decode(String.self, forKey: .message), text: try c.decode(String.self, forKey: .text),                          call: try c.decodeIfPresent(ChatThreadCall.self, forKey: .call), opens: try c.decodeIfPresent(String.self, forKey: .opens),                          groupCall: try c.decodeIfPresent(ChatGroupCallLine.self, forKey: .groupCall))        default: self = .bubble(try ChatBubble(from: decoder))        }    }    func encode(to encoder: Encoder) throws {        var c = encoder.container(keyedBy: Tag.self)        switch self {        case .day(let label, let numeral, let words):            try c.encode("day", forKey: .item); try c.encode(label, forKey: .label)            try c.encodeIfPresent(numeral, forKey: .numeral); try c.encodeIfPresent(words, forKey: .words)        case .unread(let label): try c.encode("unread", forKey: .item); try c.encode(label, forKey: .label)        case .event(let message, let text, let call, let opens, let groupCall):            try c.encode("event", forKey: .item); try c.encode(message, forKey: .message); try c.encode(text, forKey: .text)            try c.encodeIfPresent(call, forKey: .call); try c.encodeIfPresent(opens, forKey: .opens)            try c.encodeIfPresent(groupCall, forKey: .groupCall)        case .bubble(let bubble):            try c.encode("bubble", forKey: .item)            try bubble.encode(to: encoder)        }    }    /// A stable identity: a bubble's or event's message; a divider by its place.    var id: String {        switch self {        case .day(let label, _, _): "day:\(label)"        case .unread: "unread"        case .event(let message, _, _, _, _): "event:\(message)"        case .bubble(let bubble): bubble.message        }    }    var bubble: ChatBubble? { if case .bubble(let b) = self { b } else { nil } }}struct ChatHeader: Codable, Equatable, Sendable {    var name: String    var picture: ChatAvatar    var group: Bool    var source: String    var status: String?    /// Call and Video call, only on a Cotton conversation with someone to ring.    var calls: [ChatCallButton]?    /// Where a message written here goes, on a copy linked to WhatsApp through the Mac: drawn under the name.    var place: ChatGoes? = nil}/// Where a message goes, said before it is sent (CONTRACT.md "Writing to a number"): `cotton` ("on Cotton") or/// `whatsapp` ("via WhatsApp"), in the core's words. Absent on a copy with no WhatsApp link.struct ChatGoes: Codable, Equatable, Sendable, Hashable {    var id: String    var mark: String}// MARK: calls (`messages::app::calls`)/// The header's Call / Video call: tap → `chat.call {thread, video}`.struct ChatCallButton: Codable, Equatable, Sendable, Hashable {    var video: Bool    var label: String}/// A call's centred line in its thread: tap → `chat.call {call}`.struct ChatThreadCall: Codable, Equatable, Sendable, Hashable {    var call: String    var video: Bool    var missed: Bool    var incoming: Bool    var time: String    /// The tap's accessible name ("Call back").    var back: String}/// A group's call going on, on its line in the group: Join beside it (`chat.call { thread, join: true }`).struct ChatGroupCallLine: Codable, Equatable, Sendable, Hashable {    var room: String    var video: Bool    var join: String}/// `chat.groupCall { thread }`: whether the group's call goes on, for the strip under the header. `unknown`: offline,/// never said as over.struct ChatGroupCallState: Codable, Equatable, Sendable {    var live: Bool    var room: String?    var unknown: Bool?    var video: Bool?    var here: Bool?    var banner: String?    /// "Asha, Ravi in the call".    var `in`: String?    var people: [String]?    var count: Int?    var join: String?    var hereText: String?}/// `chat.call`: from a conversation, or calling back a call.struct ChatCallAsk: Codable, Equatable, Sendable {    var thread: String?    var call: String?    var video: Bool?}/// `chat.calls`: the Calls tile, newest first, 80 a page.struct ChatCallsAnswer: Codable, Equatable, Sendable {    var rows: [ChatCallRow]    var next: String?    var words: ChatCallsWords}struct ChatCallsWords: Codable, Equatable, Sendable {    var title: String    var empty: String    var back: String    /// Record, right of the title (CONTRACT.md "Recordings"): starts a recording.    var record: String?    /// Bring in a recording: the Files picker, audio only.    var `import`: String?    /// What Record says when the microphone is not allowed (iPhone's ask of the contract).    var no_microphone: String?}/// A row of Calls: a call, or (CONTRACT.md "Recordings") a recording listed with the calls by day.struct ChatCallRow: Codable, Equatable, Sendable, Hashable, Identifiable {    var call: String    var name: String    var picture: ChatAvatar    var thread: String?    var incoming: Bool    var video: Bool    var missed: Bool    var group: Bool    var line: String    var when: String    /// `call` or `recording`; nil is a call.    var kind: String? = nil    /// The heading of the day the row is under: drawn above a row whose day differs from the one before.    var day: String? = nil    /// A recording row's recording, or the notes this person took on a call.    var recording: String? = nil    /// A recording row's mark: `recording`, `memo` or `import`.    var mark: String? = nil    var isRecording: Bool { kind == "recording" }    var id: String { isRecording ? "recording-\(recording ?? "")" : call }}extension ChatCallRow {    private enum Keys: String, CodingKey {        case call, name, picture, thread, incoming, video, missed, group, line, when, kind, day, recording, mark    }    /// A recording row carries only its own fields: the call's stand empty.    init(from decoder: any Decoder) throws {        let c = try decoder.container(keyedBy: Keys.self)        call = try c.decodeIfPresent(String.self, forKey: .call) ?? ""        name = try c.decode(String.self, forKey: .name)        picture = try c.decodeIfPresent(ChatAvatar.self, forKey: .picture) ?? ChatAvatar(photo: nil, initials: "", tint: 0)        thread = try c.decodeIfPresent(String.self, forKey: .thread)        incoming = try c.decodeIfPresent(Bool.self, forKey: .incoming) ?? false        video = try c.decodeIfPresent(Bool.self, forKey: .video) ?? false        missed = try c.decodeIfPresent(Bool.self, forKey: .missed) ?? false        group = try c.decodeIfPresent(Bool.self, forKey: .group) ?? false        line = try c.decodeIfPresent(String.self, forKey: .line) ?? ""        when = try c.decodeIfPresent(String.self, forKey: .when) ?? ""        kind = try c.decodeIfPresent(String.self, forKey: .kind)        day = try c.decodeIfPresent(String.self, forKey: .day)        recording = try c.decodeIfPresent(String.self, forKey: .recording)        mark = try c.decodeIfPresent(String.self, forKey: .mark)    }}/// `chat.callScreen {speaker?}`: the call screen and the returning bar.struct ChatCallScreen: Codable, Equatable, Sendable {    var shown: Bool    var phase: String    var name: String    var picture: ChatAvatar    var status: String?    /// Ms since the epoch the call went live; the clock is drawn from it.    var liveAt: Double?    var video: Bool    var camera: Bool    var controls: [ChatCallControl]    var ring: ChatCallRing?    var bar: String?    var endedFor: Double    var thread: String?    /// Someone on the call is taking notes (CONTRACT.md "Take notes"): drawn on the call screen and its bar while it stands.    var notes: ChatCallNotes? = nil    /// A group call (CONTRACT.md "Group calls"): a tile per person in `grid`, and the privacy line.    var group: Bool? = nil    var tiles: [ChatCallTile]? = nil    var grid: ChatCallGrid? = nil    /// "Protected in transit", and what a tap on it says.    var privacy: String? = nil    var privacyDetail: String? = nil    /// Take notes refused or stopped because someone cannot show the mark, in the core's words.    var notice: String? = nil    /// The controls this phone offers: all the core's but Take notes, until the iPhone records the notes it takes    /// (CONTRACT.md "Take notes", the iPhone's tracks). The mark of anyone else's notes is drawn all the same.    var offered: [ChatCallControl] { controls.filter { $0.id != "notes" } }    static let hidden = ChatCallScreen(shown: false, phase: "", name: "", picture: ChatAvatar(photo: nil, initials: "", tint: 0),                                       status: nil, liveAt: nil, video: false, camera: false, controls: [], ring: nil, bar: nil,                                       endedFor: 0, thread: nil)}/// One person on a group call: the others in the room's order, you last.struct ChatCallTile: Codable, Equatable, Sendable, Identifiable {    var member: String    var name: String    var picture: ChatAvatar    var you: Bool    var joined: Bool    /// "Ringing…" for someone still being rung.    var state: String?    var speaking: Bool    var muted: Bool    var camera: Bool    var notes: Bool    var id: String { member }}/// The tiles' columns and rows (2: 1×2; 3–4: 2×2; 5–6: 2×3; 7–8: 2×4; 9: 3×3).struct ChatCallGrid: Codable, Equatable, Sendable {    var columns: Int    var rows: Int}/// `chat.callScreen`'s `notes`: who is taking notes, the line that says so, and the mark.struct ChatCallNotes: Codable, Equatable, Sendable {    struct Taker: Codable, Equatable, Sendable {        var member: String        var name: String    }    var by: [Taker]    var line: String    var mark: Bool}struct ChatCallControl: Codable, Equatable, Sendable, Hashable, Identifiable {    var id: String    var label: String    var on: Bool}struct ChatCallRing: Codable, Equatable, Sendable {    var title: String    var answer: String    var decline: String}struct ChatPinnedBar: Codable, Equatable, Sendable {    var message: String    var title: String    var line: String    var count: Int}/// A stranger's first message waits on an answer: Accept, Block and/// Delete go to `chat.accept` / `chat.block` / `chat.refuse`.struct ChatAskCard: Codable, Equatable, Sendable {    var title: String    var line: String    var accept: String    var block: String    var refuse: String}struct ChatJump: Codable, Equatable, Sendable {    var count: Int    var label: String}struct ChatActionWords: Codable, Equatable, Sendable {    var reply: String    var react: String    var copy: String    var forward: String    var edit: String    var delete: String    var pin: String    var unpin: String    var select: String    /// Message info, in a Cotton group (older cores answer none).    var info: String?    /// Delete for me, in a WhatsApp chat that sends: his copy only (CONTRACT.md "Delete for me"; older cores answer none).    var deleteMine: String?    /// Star and Unstar (older cores answer none).    var star: String?    var unstar: String?    var copied: String    var emoji: [String]    /// An action's word, by the act a bubble offers.    func word(_ act: String) -> String {        switch act {        case "reply": reply        case "react": react        case "copy": copy        case "forward": forward        case "edit": edit        case "delete": delete        case "pin": pin        case "unpin": unpin        case "select": select        case "info": info ?? ShellLines.line("chat", "app.group.info")        case "deleteMine": deleteMine.flatMap { $0.isEmpty ? nil : $0 } ?? ShellLines.line("chat", "app.actions.delete_mine")        case "star": star ?? ShellLines.line("chat", "app.actions.star")        case "unstar": unstar ?? ShellLines.line("chat", "app.actions.unstar")        case "retry": ShellLines.line("chat", "app.actions.retry")        default: act        }    }}struct ChatThreadAnswer: Codable, Equatable, Sendable {    var thread: String    var header: ChatHeader    var pinned: ChatPinnedBar?    var items: [ChatThreadItem]    var older: String?    var newer: String?    var focus: String?    var unread: Int    var jump: ChatJump?    var composer: ChatComposeAnswer?    var readOnly: String?    var request: ChatAskCard?    var actions: ChatActionWords    /// Whether the person can be written to yet.    var reach: ChatReach? = nil    /// "Loading this chat…" while the first pull runs and nothing of it has come.    var loading: String? = nil    /// The chat's disappearing timer (CONTRACT.md "Disappearing messages"): none where the core answers none.    var timer: ChatTimerCard? = nil    /// The wait in ms until this chat's next message goes (its timer's time come): the chat is asked again then, so it    /// leaves while he looks (absent: none set to go).    var erasesIn: Double? = nil}/// One length a chat's timer can take (`timer.choices`).struct ChatTimerChoice: Codable, Equatable, Sendable, Hashable, Identifiable {    var seconds: Int    var label: String    var on: Bool    var id: Int { seconds }}/// A chat's disappearing timer (`chat.thread`'s `timer`): `chat.act { act: "timer", thread, seconds }` sets it.struct ChatTimerCard: Codable, Equatable, Sendable {    /// 0 when off.    var seconds: Int    /// "Disappearing messages".    var title: String    /// "Disappearing messages: 7 days" while on (the header says it); none while off.    var label: String?    /// What it does, and what it cannot stop.    var note: String    /// Off, 24 hours, 7 days, 90 days; empty where the person cannot set it (they cannot write in the chat).    var choices: [ChatTimerChoice]}/// `reach` on `chat.thread`: whether the person can be written to yet./// Here, not in ChatPeople.swift (iOS only), because ChatThreadAnswer builds on the Mac too.struct ChatReach: Codable, Equatable, Sendable {    /// `on` or `notYet`.    var state: String    var waiting: Int?    var number: String?}// MARK: chat.composeenum ChatButton: String, Codable, Sendable {    case mic, send, idle}enum ChatStripKind: String, Codable, Sendable {    case reply, edit}struct ChatStrip: Codable, Equatable, Sendable {    var kind: ChatStripKind    var message: String    var title: String    var line: String    var cancel: String}struct ChatComposeAnswer: Codable, Equatable, Sendable {    var thread: String    var text: String    var placeholder: String    var button: ChatButton    var buttonLabel: String    var attach: Bool    var attachLabel: String    var strip: ChatStrip?    /// The send rule: Send is live only with something to send.    var sendEnabled: Bool    /// The composer's microphone dictates into the text (Cotton's voice path).    var dictate: Bool    var dictateLabel: String    /// The on-screen keyboard's Return sends (false: a new line).    var returnSends: Bool    var hardwareReturnSends: Bool    /// Where this message goes (the header's `place`), drawn by the field before Send.    var place: ChatGoes? = nil    /// The link in the words this phone fetches a card for (`composer.preview {url}`, FEEDBACK-4 B4); absent when none goes.    var preview: ChatPreviewAsk? = nil    /// The words of the sheet a poll is made on, where he can make one (a WhatsApp chat whose outbox takes it).    var poll: ChatPollMaker? = nil    init(thread: String, text: String, placeholder: String, button: ChatButton, buttonLabel: String, attach: Bool, attachLabel: String,         strip: ChatStrip?, sendEnabled: Bool, dictate: Bool, dictateLabel: String, returnSends: Bool, hardwareReturnSends: Bool) {        self.thread = thread; self.text = text; self.placeholder = placeholder; self.button = button; self.buttonLabel = buttonLabel        self.attach = attach; self.attachLabel = attachLabel; self.strip = strip; self.sendEnabled = sendEnabled        self.dictate = dictate; self.dictateLabel = dictateLabel; self.returnSends = returnSends; self.hardwareReturnSends = hardwareReturnSends    }    enum CodingKeys: String, CodingKey {        case thread, text, placeholder, button, buttonLabel, attach, attachLabel, strip, sendEnabled, dictate, dictateLabel, returnSends,             hardwareReturnSends, place, preview, poll    }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        thread = try c.decode(String.self, forKey: .thread)        text = try c.decode(String.self, forKey: .text)        placeholder = try c.decode(String.self, forKey: .placeholder)        button = try c.decode(ChatButton.self, forKey: .button)        buttonLabel = try c.decode(String.self, forKey: .buttonLabel)        attach = try c.decode(Bool.self, forKey: .attach)        attachLabel = try c.decode(String.self, forKey: .attachLabel)        strip = try c.decodeIfPresent(ChatStrip.self, forKey: .strip)        // The first shapes had none of these: read as the core then meant them.        sendEnabled = try c.decodeIfPresent(Bool.self, forKey: .sendEnabled) ?? (button == .send)        dictate = try c.decodeIfPresent(Bool.self, forKey: .dictate) ?? false        dictateLabel = try c.decodeIfPresent(String.self, forKey: .dictateLabel) ?? ""        returnSends = try c.decodeIfPresent(Bool.self, forKey: .returnSends) ?? false        hardwareReturnSends = try c.decodeIfPresent(Bool.self, forKey: .hardwareReturnSends) ?? true        place = try c.decodeIfPresent(ChatGoes.self, forKey: .place)        preview = try c.decodeIfPresent(ChatPreviewAsk.self, forKey: .preview)        poll = try c.decodeIfPresent(ChatPollMaker.self, forKey: .poll)    }}/// `composer.preview`: the link whose card this phone may fetch.struct ChatPreviewAsk: Codable, Equatable, Sendable {    var url: String}/// `chat.linkCard`'s answer: the fetched page read as its card; `image` the picture this phone fetches and makes the/// card's `thumb`. Empty (`{}`, no title) when no card goes.struct ChatLinkCardAnswer: Codable, Equatable, Sendable {    var url: String?    var site: String?    var title: String?    var description: String?    var image: String?}/// The sheet a poll is made on (CONTRACT.md "Making a poll"): its words only; the core checks what is sent.struct ChatPollMaker: Codable, Equatable, Sendable {    /// The attach sheet's row, and the sheet's title.    var label: String    var question: String    var option: String    var add: String    /// The switch: more than one may be chosen.    var many: String    var send: String    /// At most this many options.    var most: Int}// MARK: chat.actstruct ChatActAnswer: Codable, Equatable, Sendable {    var ok: Bool    var act: String    var thread: String    var message: String?    var bubble: ChatBubble?    var row: ChatListRow?    var sentTo: [String]?    var flush: Bool    var said: String?}// MARK: chat.themes/// One theme: colours by token (`ground`, `bubble_out`, `ink_in`,/// `tint.<n>` …), for light and for dark.struct ChatThemeAnswer: Codable, Equatable, Sendable, Identifiable {    var id: String    var name: String    var on: Bool    /// `plain` or a quiet pattern's id (`weave`), drawn in `pattern`.    var ground: String    var light: [String: String]    var dark: [String: String]}/// One choice of a Settings row: a text size (with its scale) or a ground.struct ChatChoice: Codable, Equatable, Sendable, Identifiable {    var id: String    var label: String    var scale: Double?}/// A Settings row: its title, what is chosen, the choices in order.struct ChatChoosing: Codable, Equatable, Sendable {    var title: String    var chosen: String    var options: [ChatChoice]}struct ChatThemeWords: Codable, Equatable, Sendable {    var title: String    var light: String    var dark: String    var settings: String}/// `chat.themes` (and what `chat.theme`, `chat.text`, `chat.ground` answer):/// every theme, the one standing for each appearance, the text size, the/// thread's ground and Settings' words.struct ChatThemesAnswer: Codable, Equatable, Sendable {    var themes: [ChatThemeAnswer]    var light: String    var dark: String    var text: ChatChoosing    var ground: ChatChoosing    var words: ChatThemeWords    func theme(dark: Bool) -> ChatThemeAnswer? {        let chosen = dark ? self.dark : light        return themes.first { $0.id == chosen } ?? themes.first    }}enum ChatSettingsMethod {    static let theme = "chat.theme"    static let text = "chat.text"    static let ground = "chat.ground"}// MARK: requestsstruct ChatListAsk: Codable, Equatable, Sendable {    var filter: String?    var search: String?    var after: String?    /// Only the rows a message can be written into (Forward's picker).    var sendable: Bool?}struct ChatThreadAsk: Codable, Equatable, Sendable {    var thread: String    var before: String?    var after: String?    var around: String?}struct ChatComposeAsk: Codable, Equatable, Sendable {    var thread: String    var text: String?    var replyTo: String?    var edit: String?    var cancel: Bool?    /// Pictures waiting in the writing box, to go with what is written as their caption: with any, Send is live.    var attached: Int?}/// `chat.file`'s answer: any attachment written under its own extension/// (a PDF a `.pdf`, a video an `.mp4`), to open with the system.struct ChatFileAnswer: Codable, Equatable, Sendable {    var path: String    var mime: String    var name: String?    /// `video`, `file`, `photo`, `voice` …, when the message said one.    var kind: String?    /// Whether it is kept in the person's own media past the chat store's 30 days.    var kept: Bool?    var url: URL { URL(fileURLWithPath: path) }}struct ChatActAsk: Codable, Equatable, Sendable {    var act: String    var thread: String    var message: String?    var messages: [String]?    var text: String?    var emoji: String?    var to: [String]?    var until: String?    var attachments: [ChatAttachment]?    /// `send`: the message this answers (the composer's reply strip), so its bubble quotes it.    var replyTo: String?    /// `timer`: the length, one of the card's choices (0 is Off).    var seconds: Int?    /// `send`: the link card fetched here for a link in the words (`url, site, title, description, thumb`); the core keeps it.    var preview: ChatLinkCard?    /// `vote`: the option tapped, 0 first.    var option: Int?    /// `poll`: the question, the options in order, and whether more than one may be chosen.    var question: String?    var options: [String]?    var many: Bool?}/// A file sent with a message: a photo, a video, a file or a voice message,/// lying on the phone at `path` until `chat.flush` seals and uploads it.struct ChatAttachment: Codable, Equatable, Sendable {    var path: String    var mime: String    var name: String?    var width: Double?    var height: Double?    /// `photo`, `video`, `file` or `voice`.    var kind: String?    var durationMs: Double?    /// A voice message's levels, 0…1.    var waveform: [Double]?    /// A picture's preview made here (`ChatPreviewMaker`): sealed in the message, so it arrives with the words.    var preview: String?}