// `/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. import CottonKit import Foundation @MainActor @Observable public final class JoinRequestModel { /// What the screen is drawing. public enum State: 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) } public private(set) var state: State = .loading /// An approve or a decline is in flight; both verbs are inert while it is. public private(set) var busy = false /// The verb landed and the screen is done — the host pops. public private(set) var answered = false public let pairingId: String private let core: any CottonCalling private let toasts: ToastCenter public init(core: any CottonCalling, toasts: ToastCenter, pairingId: String) { self.core = core self.toasts = toasts self.pairingId = pairingId } /// The row this screen is about, if it is still waiting. public var pairing: JSONValue? { guard case .waiting(let row) = state else { return nil } 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. public var digits: String? { guard let digits = pairing?.text("digits"), !digits.isEmpty else { return nil } return digits } public var 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. public func load() async { do { let answer = try await core.callJSON(CottonMethod.devicesPending) let rows = answer.list("pending") guard let row = rows.first(where: { $0.text("pairingId") == pairingId }) else { state = .settled return } 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. public func approve() async { guard let digits, !busy else { return } busy = true defer { busy = false } do { try await 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. public func decline() async { guard !busy else { return } busy = true defer { busy = false } do { try await core.declinePairing(pairingId) answered = true } catch { toasts.show(DeviceRefusalWords.toastSentence(for: error), tone: .error) } } }