Cotton code
61 lines · 3.1 KB · SwiftRaw
// NO LONGER ON COTTON (CIRCLE 2, cfc1e46c6f + 4ad9aab410; SHELL DRAW 3, 28 Nov 2026): `circle.list`'s section `gone`// holds the people whose account was erased (`state: "left"`, "Left Cotton") and those whose number now leads to// another account (`state: "changed"`, "This number belongs to someone else on Cotton now"), each row with its `number`// and `detail`, the core's words. Comms says that `detail` where it shows such a person: in place of their chat row's// last line, and under the name in their chat's header. Read when the list is read and when a chat opens; a person// chat's number is asked (`chat.contact`) only while someone is gone. Android: messages ChatModel.readGone.import CottonKitimport SwiftUI@MainActor@Observablefinal class ChatCircleGone {    static let shared = ChatCircleGone()    /// The core's words by number (E.164), for the people no longer on Cotton.    private(set) var gone: [String: String] = [:]    /// Each person chat's number, asked only while someone is gone.    private(set) var numbers: [String: String] = [:]    @ObservationIgnored private var readAt: Date?    /// The core's words for `thread` when its person is no longer on Cotton, else nil.    func words(_ thread: String) -> String? {        guard !gone.isEmpty, let number = numbers[thread] else { return nil }        return gone[Self.key(number)]    }    /// Who is no longer on Cotton; while anyone is, the numbers of the person chats in `threads` not yet known.    func read(threads: [String]) async {        guard let core = (ChatBooks.shared as? CoreChatBook)?.core else { return }        // The list asks the object, so once a minute at most however often the chat list is read again.        if readAt.map({ Date().timeIntervalSince($0) > 60 }) ?? true {            readAt = Date()            guard let list = try? await core.callJSON("circle.list") else { return }            gone = Self.of(list)        }        guard !gone.isEmpty else { return }        for thread in threads where numbers[thread] == nil {            if let contact = try? await core.callJSON("chat.contact", ["thread": thread]), let number = contact["number"]?.string {                numbers[thread] = number            }        }    }    /// The `gone` rows of a `circle.list` answer, by number; empty when signed out, unreachable or none.    nonisolated static func of(_ list: JSONValue) -> [String: String] {        guard case .array(let sections)? = list["sections"] else { return [:] }        var out: [String: String] = [:]        for section in sections where section["id"]?.string == "gone" {            guard case .array(let rows)? = section["rows"] else { continue }            for row in rows {                guard let number = row["number"]?.string, !number.isEmpty, let words = row["detail"]?.string, !words.isEmpty else { continue }                out[key(number)] = words            }        }        return out    }    /// A number as the core writes it in `circle.list` (`+` and digits), for a lookup.    nonisolated static func key(_ number: String) -> String { number.filter { $0.isNumber || $0 == "+" } }}