Cotton code
140 lines · 4.6 KB · SwiftRaw
// What the Chat screens read about people (assets/design/chat/CONTRACT.md,// "Finding people and inviting them"): someone's page// (`chat.contact`), your own name and picture (`chat.profile`), who you// blocked (`chat.blocked`), who you know on Cotton (`chat.contacts`) and an// invite sent from your own phone (`chat.invite`). Nobody is found by their// number (DISCOVERY OFF 1, 27 Nov 2026). Every word a// screen shows comes in these answers or from the kit's chat lines.#if os(iOS)import CottonKitimport Foundationenum ChatPeopleMethod {    static let contact = "chat.contact"    static let profilePicture = "chat.profilePicture"    static let profile = "chat.profile"    static let profileSet = "chat.profile.set"    static let blocked = "chat.blocked"    static let unblock = "chat.unblock"    static let block = "chat.block"    static let contacts = "chat.contacts"    static let invite = "chat.invite"    static let start = "chat.start"}/// One person Cotton knows (`Person` in the contract).struct ChatKnown: Codable, Equatable, Sendable, Identifiable, Hashable {    var number: String    var name: String?    var theirName: String?    var contact: String?    var person: String?    var thread: String?    var hasPicture: Bool?    var id: String { number }    /// The name to draw: how this phone knows them, else their own, else the number.    var shown: String { name ?? theirName ?? number }}/// `chat.contact { thread }`.struct ChatContactAnswer: Codable, Equatable, Sendable {    struct Mute: Codable, Equatable, Sendable, Hashable {        var label: String        /// When the mute ends; nil until it is turned back on.        var until: String?    }    struct Between: Codable, Equatable, Sendable {        var photos: Int        var videos: Int        var voice: Int        var files: Int        var links: Int    }    var thread: String    var number: String    var name: String?    var theirName: String?    var hasPicture: Bool?    var person: String?    /// `on` or `notYet`.    var reach: String?    var blocked: Bool    var mutedUntil: String?    var mutes: [Mute]    var between: Between    /// The person verified their safety number, and nothing changed since (CONTRACT.md "Safety number").    var verified: Bool?}/// `chat.profile {}` and `chat.profile.set`.struct ChatProfileAnswer: Codable, Equatable, Sendable {    struct Picture: Codable, Equatable, Sendable { var path: String? }    var name: String?    var picture: Picture?    var number: String?}/// `chat.blocked {}`.struct ChatBlockedAnswer: Codable, Equatable, Sendable {    struct Person: Codable, Equatable, Sendable, Identifiable {        var number: String        var name: String?        var at: String?        var id: String { number }    }    var people: [Person]}/// `chat.contacts { search? }`.struct ChatContactsAnswer: Codable, Equatable, Sendable {    var people: [ChatKnown]}/// `chat.invite { thread | number }`: the words and the `sms:` link, sent from the person's own phone.struct ChatInviteAnswer: Codable, Equatable, Sendable {    var number: String    var name: String?    var text: String    var link: String    var sms: String    var waiting: Bool}/// `chat.start { number | person }`.struct ChatStartAnswer: Codable, Equatable, Sendable {    var thread: String    var reach: String?    /// Where its words go, on a copy linked to WhatsApp through the Mac ("via WhatsApp", "on Cotton"); the chat it    /// opens says the same in its header and by its field.    var place: ChatGoes?}/// `chat.profilePicture { number, dir }` (and `chat.group.photo`).struct ChatPicturePath: Codable, Equatable, Sendable {    var path: String?    var mime: String?}/// Where fetched pictures of people are kept (`dir` of `chat.profilePicture`).enum ChatPeopleFiles {    static var pictures: String {        let dir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("chat-people", isDirectory: true)        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)        return dir.path(percentEncoded: false)    }}/// A date the core wrote (ISO 8601), said short in the phone's own way.enum ChatWhen {    static func said(_ iso: String?) -> String? {        guard let iso else { return nil }        let parse = ISO8601DateFormatter()        parse.formatOptions = [.withInternetDateTime, .withFractionalSeconds]        guard let date = parse.date(from: iso) ?? ISO8601DateFormatter().date(from: iso) else { return nil }        return date.formatted(date: .abbreviated, time: .shortened)    }}#endif