// The one place a refused phone's writes are turned away. // // A server that has REFUSED this device — `device_not_approved`, // `device_revoked`, `device_bad_signature` — used to replace the whole app // with a refusal screen, and the person's full copy sat unreadable behind it. // The copy is theirs and it is whole, so the app now opens on it READ-ONLY: // every `read.*` is local and answers as it always did; what cannot happen is // a write, because a row filed on a phone the server no longer admits is a // row that reaches no other device and may never. // // **Seventy-five callers ask `write` directly**, so the refusal is not a flag // each screen checks — it is a wrapper over the core every model is handed, // and `write` is the one method it turns away. Everything else passes: // `join.*` (the way back in), `sync.*`, `read.*`, the media path, the // terminal frames. A refused write answers the same envelope the crate would, // so `ScreenWriter` shows the sentence in the place it shows every refusal // and nothing anywhere has to know the gate exists. // // The gate is a fact `BootModel` owns (`readOnly`) and `RootView` copies // down on every gate transition; the wrapper reads it on every call, from // whichever thread the call arrives on. import Foundation /// Why writes are refused right now, or `nil` when they are not. /// /// A class with a lock rather than an actor: `callRaw` is synchronous on the /// real core and the wrapper must answer on the same thread it was asked on. public final class WriteGate: @unchecked Sendable { private let lock = NSLock() private var sentence: String? public init() {} /// Hold writes with this sentence, or let them through with `nil`. public func hold(_ sentence: String?) { lock.lock() defer { lock.unlock() } self.sentence = sentence } /// The sentence a write is refused with right now, or `nil`. public var refusal: String? { lock.lock() defer { lock.unlock() } return sentence } /// The methods the gate turns away. ONE: the tool path every screen /// writes through. The phone's own acts — leaving, erasing, joining /// again — must still work on a refused phone; they are the way out. public static let refusedMethods: Set = [CottonMethod.write] /// The refusal code a held write answers with. A screen keys on /// `message`, which is the sentence; the code is for a log. public static let code = "read_only" } /// A `CottonCalling` over another, refusing writes while the gate is held. public struct GatedCore: CottonCalling { private let core: any CottonCalling private let gate: WriteGate public init(over core: any CottonCalling, gate: WriteGate) { self.core = core self.gate = gate } public func callRaw(_ method: String, requestJSON: String) async throws -> String { if WriteGate.refusedMethods.contains(method), let sentence = gate.refusal { return GatedCore.refusalEnvelope(sentence) } return try await core.callRaw(method, requestJSON: requestJSON) } public func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async { await core.subscribe(handler) } public func close() async { await core.close() } /// The crate's own envelope shape, so `CottonCalling.call` throws the /// same `CottonCallError` a refusal from below the boundary would. /// The sentence is the gate's own (`gate.write_refused`, a line row); /// strings always encode, and an answer that somehow did not is the /// malformed answer `call` already says, not a second sentence here. static func refusalEnvelope(_ sentence: String) -> String { struct Envelope: Encodable { var ok = false let error: [String: String] } let envelope = Envelope(error: ["code": WriteGate.code, "message": sentence]) let encoder = JSONEncoder() encoder.outputFormatting = .sortedKeys return String(decoding: (try? encoder.encode(envelope)) ?? Data(), as: UTF8.self) } }