// 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. import CottonKit import CottonValues import SwiftUI /// A picture picked for a message: a file this app wrote, its type and size. public struct ChatPicture: Equatable, Sendable { public var path: String public var mime: String public var width: Int? public var height: Int? public init(path: String, mime: String, width: Int? = nil, height: Int? = nil) { self.path = path self.mime = mime self.width = width self.height = height } } /// A person's answer to a conversation's request, or a block. public enum ChatDecision: String, Sendable { case accept, refuse, block, unblock } @MainActor public protocol CottonChatting: 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]) async throws /// A message that failed, tried again. func retry(message: String) async throws /// 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) -> Bool func decide(thread: String, _ decision: ChatDecision) async throws /// The conversation with a number or a person, made when it is new: its thread. func start(number: String?, person: String?) async throws -> String /// A message's picture as a local file. func picture(message: String, index: Int) async throws -> URL /// Sends what `chat.act` queued (its answer's `flush`), now, off the screen's path. func flush() async var 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?) async throws /// 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]) async throws -> JSONValue } extension EnvironmentValues { /// The app's Cotton chat engine; nil in an app that carries none. @Entry public var cottonChat: (any CottonChatting)? = nil } /// What a row says about Cotton chat. public enum CottonChatRows { /// A Cotton conversation: its thread names its number as `cotton:+…`. public static func isCotton(thread: TableRecord?) -> Bool { thread?["external_key"].text?.hasPrefix("cotton:") == true } /// The thread waits on the person's answer to a stranger. public static func isRequest(thread: TableRecord?) -> Bool { thread?["request"].text == "pending" } public static func isCotton(message: TableRecord) -> Bool { message["channel"].text == "cotton" } } /// A Cotton message's send state, in the chat lines' words. public struct CottonDelivery: Equatable, Sendable { public var state: String public var reason: String? public init?(_ message: TableRecord) { guard CottonChatRows.isCotton(message: message), message["direction"].text == "out", let state = message["send_state"].text, ["queued", "sending", "sent", "delivered", "read", "failed"].contains(state) else { return nil } self.state = state let reason = message["send_error"].text?.trimmingCharacters(in: .whitespacesAndNewlines) self.reason = state == "failed" && reason?.isEmpty == false ? reason : nil } public var failed: Bool { state == "failed" } public var words: String { let said = ShellLines.line("chat", "state.\(state)") guard let 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. @MainActor public enum ChatSignOut { public static var forgetDevice: (@MainActor () async -> Void)? } extension Notification.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. public static let cottonPersonalDoor = Notification.Name("com.cottondb.personalDoor") /// A message, receipt or change landed on a chat (`userInfo["thread"]`). public static let 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`). public static let cottonChatRead = Notification.Name("com.cottondb.chat.read") }