// The message engine owns its queue's identity and transport fields; the // definition names the columns a queued row is written with (`queue`); conversation surfaces use one model and input // for every channel, without drawing a separate WhatsApp page. import CottonValues import Foundation extension ConversationLayout { /// Whether words said here are queued for the engine to deliver: the /// definition names the row's columns (`queue`). public var usesLocalQueue: Bool { queue != nil } func localQueueInput(words: String, thread: String) -> [String: RecordValue]? { guard let queue else { return nil } return [queue.link: .text(thread), queue.body: .text(words)] } } /// Cotton's outbound WhatsApp and Instagram rows carry the Mac's delivery state. /// The worker reserves an id before transmission: an id alone is no receipt. public enum MessageDelivery: Equatable, Sendable { case queued case sending case sent case delivered case seen case failed(String) public init?(_ message: TableRecord) { guard message["direction"].text == "out", ["whatsapp", "instagram"].contains(message["channel"].text ?? ""), let state = message["send_state"].text else { return nil } switch state { case "queued": self = .queued case "sending": self = .sending case "sent", "delivered", "read": guard message["external_id"].text?.isEmpty == false else { self = .sending; return } switch state { case "delivered": self = .delivered case "read": self = .seen default: self = .sent } case "failed": let reason = message["send_error"].text?.trimmingCharacters(in: .whitespacesAndNewlines) self = .failed(reason.flatMap { $0.isEmpty ? nil : $0 } ?? ShellLines.line("definitions", "sending.no_reason")) default: return nil } } public var isConfirmed: Bool { switch self { case .sent, .delivered, .seen: true case .queued, .sending, .failed: false } } public var words: String { switch self { case .queued: "Queued" case .sending: "Sending" case .sent: "Sent" case .delivered: "Delivered" case .seen: "Seen" case .failed(let reason): "Failed ยท \(reason)" } } }