// `/settings/join-request/[id]` — one device asking to join, answered from// anywhere.//// **The push is what this screen exists for.** `device_join_requested` arrives// on every device the person already has, carrying the pairing id and the six// digits; the tap lands here, on THAT request rather than on a settings page// with a list on it somewhere. Approving from the list still works and is the// same two verbs — this is the one that takes no navigating at the moment it// matters.//// **The row comes from `devices.pending`, and there is no read for it.** The// device registry is `registry.db` on the server and is deliberately never in// the person's database, so there is nothing in the file to answer from: the// screen asks the pending list and picks its row out by pairing id.//// **The digits are SHOWN, never typed, and the approve ECHOES the row's own.**// The same six reached this device and the one asking; seeing them match is the// whole of the second factor. Sending them back proves WHICH request the person// was looking at, so a queue that moved between the draw and the tap refuses// rather than admitting the wrong device.//// **Four states and each says which it is**: still asking, here it is, this// request is not waiting any more (approved elsewhere, declined, expired), or// the server refused the list. A screen that drew nothing for the third would// leave somebody staring at a spinner over a question already answered.importCottonKitimportFoundation@MainActor@ObservablepublicfinalclassJoinRequestModel {/// What the screen is drawing.publicenumState: Equatable, Sendable {case loading/// The row, as `devices.pending` answered it.case waiting(JSONValue)/// The pairing id is not on the pending list: somebody answered it, or/// it expired. Not an error, and it never becomes one.case settled/// The list itself could not be read — the sentence, never a spinner/// that never ends.case refused(String) }publicprivate(set) var state: State = .loading/// An approve or a decline is in flight; both verbs are inert while it is.publicprivate(set) var busy = false/// The verb landed and the screen is done — the host pops.publicprivate(set) var answered = falsepubliclet pairingId: Stringprivatelet core: any CottonCallingprivatelet toasts: ToastCenterpublicinit(core: any CottonCalling, toasts: ToastCenter, pairingId: String) {self.core = coreself.toasts = toastsself.pairingId = pairingId }/// The row this screen is about, if it is still waiting.publicvar pairing: JSONValue? {guardcase .waiting(let row) = state else { returnnil }return row }/// The six the server answered for THIS pairing, or `nil` on a server too/// old to send them — in which case there is no question to put and the/// approve is not offered.publicvar digits: String? {guardlet digits = pairing?.text("digits"), !digits.isEmpty else { returnnil }return digits }publicvar name: String { pairing?.text("name") ?? "" }/// Find the row. **One read, no poll**: a pairing that settles while this/// screen is up settles because somebody answered it, and the answer lands/// through the verb below or through the push's own dedupe — a five-second/// timer here would be a second opinion about a question with one answer.publicfunc load() async {do {let answer = tryawait core.callJSON(CottonMethod.devicesPending)let rows = answer.list("pending")guardlet row = rows.first(where: { $0.text("pairingId") == pairingId }) else { state = .settledreturn } state = .waiting(row) } catch {// A 4xx is the server's settled answer and a 5xx is not, but the// screen's move is the same either way: say what happened. There is// no list to leave stale behind it. state = .refused(DeviceRefusalWords.toastSentence(for: error)) } }/// Yes — with the digits the ROW showed, never a number from anywhere else.publicfunc approve() async {guardlet digits, !busy else { return } busy = truedefer { busy = false }do {tryawait core.approvePairing(pairingId, digits: digits) toasts.show(ShellLines.line("devices", "is_approved_2", ["name": "\(name)"]), tone: .success) answered = true } catch { toasts.show(DeviceRefusalWords.toastSentence(for: error), tone: .error) } }/// No. It says nothing on success — the request simply goes.publicfunc decline() async {guard !busy else { return } busy = truedefer { busy = false }do {tryawait core.declinePairing(pairingId) answered = true } catch { toasts.show(DeviceRefusalWords.toastSentence(for: error), tone: .error) } }}