// Who is in a database — the membership facts (keys-sharing KS3) as the app// reads them, and the rules a screen applies before it offers anything.//// A member is a PERSON key admitted to a database with `member` or `admin`// rights. The owner is not a rights value: the database's maker, named by the// registry, who holds every right and can never be removed. The core reads// the database's own admission/removal facts and this device's live statement.importFoundation/// One person in a database, as the People screen draws them.publicstructMember: Codable, Equatable, Hashable, Identifiable, Sendable {/// What a person may do. Stored values only — the owner is a fact about/// the database, not a right.publicenumRights: String, Codable, Sendable {/// Reads and writes records.case member/// Also admits and removes members.case admin }/// The person's public key, base64url — the row id of `cotton_person_keys`.publiclet personKey: Stringpublicvar name: Stringpublicvar rights: Rights/// The database's maker. Holds every right; never removable.publicvar isOwner: Boolpublicvar id: String { personKey }publicinit(personKey: String, name: String, rights: Rights, isOwner: Bool = false) {self.personKey = personKeyself.name = nameself.rights = rightsself.isOwner = isOwner }/// The key as a person reads it aloud: its first eight characters in two/// groups of four. A key too short to have one is shown whole.publicvar fingerprint: String { Fingerprint.of(personKey) ?? personKey }/// The one word under the name.publicvar rightsWord: String { isOwner ? "owner" : rights.rawValue }}/// The first eight characters of a key's base64url, in two groups of four —/// the same reading the Join screens give a device key.publicenumFingerprint {publicstaticlet length = 8publicstaticlet group = 4publicstaticfunc of(_ key: String?) -> String? {let key = key?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""guard key.count >= length else { returnnil }let head = Array(key.prefix(length))return stride(from: 0, to: head.count, by: group) .map { String(head[$0..<min($0 + group, head.count)]) } .joined(separator: " ") }}/// What one read of a database's membership answers.publicstructMembersRead: Decodable, Equatable, Sendable {publicvar members: [Member]/// The person key THIS device signs as, when it is one of them — what the/// screen's Remove is decided against.publicvar me: String?/// The core evaluates the signed membership and database kind.publicvar canAdmit: Boolpublicvar admitReason: String?publicinit(members: [Member], me: String? = nil, canAdmit: Bool, admitReason: String? = nil) {self.members = membersself.me = meself.canAdmit = canAdmitself.admitReason = admitReason }privateenumCodingKeys: String, CodingKey { case members, me, canAdmit, admitReason }publicinit(from decoder: Decoder) throws {let fields = try decoder.container(keyedBy: CodingKeys.self) members = try fields.decode([Member].self, forKey: .members) me = try fields.decodeIfPresent(String.self, forKey: .me) canAdmit = try fields.decode(Bool.self, forKey: .canAdmit) admitReason = try fields.decodeIfPresent(String.self, forKey: .admitReason)guard canAdmit || admitReason?.isEmpty == falseelse {throwDecodingError.dataCorruptedError(forKey: .admitReason, in: fields, debugDescription: "A denied admission requires its native reason.") } }}/// The rules of the People list. Pure, so a test can hold them.publicenumMembers {/// The owner first, then everyone else by name; a tie by fingerprint, so/// two people called the same thing keep one order on every device.publicstaticfunc ordered(_ members: [Member]) -> [Member] { members.sorted { a, b inif a.isOwner != b.isOwner { return a.isOwner }let byName = a.name.localizedCaseInsensitiveCompare(b.name)if byName != .orderedSame { return byName == .orderedAscending }return a.personKey < b.personKey } }/// Who may remove whom: the owner or an admin removes a member, and nobody/// removes the owner. Nobody at all when this device's person is unknown.publicstaticfunc mayRemove(_ target: Member, by actor: Member?) -> Bool {guardlet actor, !target.isOwner else { returnfalse }return actor.isOwner || actor.rights == .admin }/// The refusal KS4 gives when the owner is named — kept here so the screen/// never has to ask the server a question whose answer it knows.publicstaticlet ownerCannotBeRemoved = "the owner of a database cannot be removed from it"}/// The membership calls a screen makes.publicprotocolMembersCalling: Sendable {func members(of databaseId: String) asyncthrows -> MembersRead/// KS4's removal act — `databases.remove(person)`./// Answers the act's receipt ("Removed Rhyn from this database"). @discardableResultfunc remove(_ personKey: String, from databaseId: String) asyncthrows -> ActAnswer}/// Live facts from the window's database. The caller's identity is the core's/// answer; Swift never substitutes the database's owner for this device.publicstructCoreMembers: MembersCalling {privatelet core: any CottonCallingpublicinit(core: any CottonCalling) { self.core = core }publicfunc members(of databaseId: String) asyncthrows -> MembersRead {tryawait core.call(CottonMethod.readMembers, Scope(database: databaseId), as: MembersRead.self) } @discardableResultpublicfunc remove(_ personKey: String, from databaseId: String) asyncthrows -> ActAnswer {tryawait core.pageAct("databases.remove", Removal(database: databaseId, personKey: personKey)) }privatestructScope: Encodable { let database: String }privatestructRemoval: Encodable {let database: Stringlet personKey: String }}