// Cotton to Cotton chat, as the screens reach it: the engine// (`CottonChat/ChatCenter`) is handed down through the environment by the// app that carries it (Messages), and a screen with none draws a Cotton// thread as any other. Every rule is the core's (`chat.*`): the screen hands// over what the person did and draws what the rows say.importCottonKitimportCottonValuesimportSwiftUI/// A picture picked for a message: a file this app wrote, its type and size.publicstructChatPicture: Equatable, Sendable {publicvar path: Stringpublicvar mime: Stringpublicvar width: Int?publicvar height: Int?publicinit(path: String, mime: String, width: Int? = nil, height: Int? = nil) {self.path = pathself.mime = mimeself.width = widthself.height = height }}/// A person's answer to a conversation's request, or a block.publicenumChatDecision: String, Sendable {case accept, refuse, block, unblock}@MainActorpublicprotocolCottonChatting: AnyObject {/// A message written here; its row is queued at once and moves by itself.func send(thread: String, body: String?, replyTo: String?, pictures: [ChatPicture]) asyncthrows/// A message that failed, tried again.func retry(message: String) asyncthrows/// A key pressed in the conversation's composer.func typed(thread: String)/// The conversation on screen, or none: its messages are read while it shows.func showing(thread: String?)/// Whether the other person is typing in this conversation now.func isTyping(thread: String) -> Boolfunc decide(thread: String, _ decision: ChatDecision) asyncthrows/// The conversation with a number or a person, made when it is new: its thread.func start(number: String?, person: String?) asyncthrows -> String/// A message's picture as a local file.func picture(message: String, index: Int) asyncthrows -> URL/// Sends what `chat.act` queued (its answer's `flush`), now, off the screen's path.func flush() asyncvar readReceipts: Bool { get }var sendsTyping: Bool { get }/// Whether this app's chats send and draw read receipts and typing at/// all (the kit's `signals`): false, the two switches are not offered.var signals: Bool { get }func set(readReceipts: Bool?, typing: Bool?) asyncthrows/// A call that seals or opens, with this phone's chat key (`privateKey`), which only the engine holds:/// the safety number's (`chat.safety`, `chat.safety.scan`).func keyed(_ method: String, _ request: [String: JSONValue]) asyncthrows -> JSONValue}extensionEnvironmentValues {/// The app's Cotton chat engine; nil in an app that carries none. @Entry publicvar cottonChat: (any CottonChatting)? = nil}/// What a row says about Cotton chat.publicenumCottonChatRows {/// A Cotton conversation: its thread names its number as `cotton:+…`.publicstaticfunc isCotton(thread: TableRecord?) -> Bool { thread?["external_key"].text?.hasPrefix("cotton:") == true }/// The thread waits on the person's answer to a stranger.publicstaticfunc isRequest(thread: TableRecord?) -> Bool { thread?["request"].text == "pending" }publicstaticfunc isCotton(message: TableRecord) -> Bool { message["channel"].text == "cotton" }}/// A Cotton message's send state, in the chat lines' words.publicstructCottonDelivery: Equatable, Sendable {publicvar state: Stringpublicvar reason: String?publicinit?(_ message: TableRecord) {guardCottonChatRows.isCotton(message: message), message["direction"].text == "out",let state = message["send_state"].text, ["queued", "sending", "sent", "delivered", "read", "failed"].contains(state)else { returnnil }self.state = statelet reason = message["send_error"].text?.trimmingCharacters(in: .whitespacesAndNewlines)self.reason = state == "failed" && reason?.isEmpty == false ? reason : nil }publicvar failed: Bool { state == "failed" }publicvar words: String {let said = ShellLines.line("chat", "state.\(state)")guardlet reason else { return said }return"\(said) \u{00B7} \(reason)" }}/// The step a phone signing out takes while its door is still open: the chat/// engine asks the object, through the core, to forget this phone's chat/// device. Set by the engine where one runs (Messages); nil elsewhere.@MainActorpublicenumChatSignOut {publicstaticvar forgetDevice: (@MainActor () async -> Void)?}extensionNotification.Name {/// The personal object's door opened (`userInfo["open"] == true`: a/// sign-in, a connect) or closed (a sign-out, a removal, the object/// refusing this phone): what an engine holding a door of its own hears.publicstaticlet cottonPersonalDoor = Notification.Name("com.cottondb.personalDoor")/// A message, receipt or change landed on a chat (`userInfo["thread"]`).publicstaticlet cottonChatLanded = Notification.Name("com.cottondb.chat.landed")/// A chat was read (`userInfo["thread"]`, and `["row"]`, its list row, when/// read here): its notifications are asked again (`chat.notices`).publicstaticlet cottonChatRead = Notification.Name("com.cottondb.chat.read")}