import Foundation /// The serving Mac's backup, as its own device page reads it off `read.device` /// (`backup`) and `device.backupNow` answers it again: the relay's state in /// one of four words, when the copy last reached the bucket, why it is /// failing, and when the next whole snapshot is due. The core maps the relay's /// status (`cotton_ffi::methods::host_settings::backup_section`); nothing /// here decides a state. public struct DeviceBackup: Equatable, Sendable { public enum State: String, Sendable { /// No bucket, or half a configuration (its missing keys the error). case never /// The last sync reached the bucket. case ok /// The copy fell behind or failed; `lastError` says why. case failing /// The first sync is not in yet. case running } public var state: State /// When the copy last reached the bucket (ISO-8601). public var lastAt: String? /// Why it is failing, or what a half configuration is missing. public var lastError: String? /// When the next whole snapshot is due (ISO-8601). public var nextAt: String? /// Since when a failing copy has not got through (ISO-8601): the Mac's /// own judgement of when it fell behind, or the last sync it saw before a /// failure. `nil` when it is not failing, or the Mac does not know. public var since: String? /// Each served database's photos and files, in the Mac's serving order. public var media: [Media] /// One database's photos and files — the core's words /// (`host_settings::media_line`), nothing decided here. public struct Media: Equatable, Sendable { public enum State: String, Sendable { /// The bucket holds every file: `files` of them, as of `at`. case copied /// `pending` files still to copy `since`; the next pass is on its way. case copying /// The bucket refused, or the gate said no; `reason` says why. case failing /// The first pass has not finished. case starting /// Half a configuration; `reason` names what is missing. case never /// No bucket: this Mac holds the only copy. case off } public var databaseId: String public var name: String? public var state: State public var at: String? public var files: Int? public var pending: Int? public var since: String? public var reason: String? public init( databaseId: String, name: String? = nil, state: State, at: String? = nil, files: Int? = nil, pending: Int? = nil, since: String? = nil, reason: String? = nil ) { self.databaseId = databaseId self.name = name self.state = state self.at = at self.files = files self.pending = pending self.since = since self.reason = reason } /// One `databases[]` entry of the line, or `nil` for a state this /// build does not know. init?(database: JSONValue) { guard let id = database["databaseId"]?.string, let media = database["media"], let word = media["state"]?.string, let state = State(rawValue: word) else { return nil } let text = { (key: String) in media[key]?.string.flatMap { $0.isEmpty ? nil : $0 } } self.init( databaseId: id, name: database["name"]?.string.flatMap { $0.isEmpty ? nil : $0 }, state: state, at: text("at"), files: media["files"]?.int, pending: media["pending"]?.int, since: text("since"), reason: text("reason")) } } public init( state: State, lastAt: String? = nil, lastError: String? = nil, nextAt: String? = nil, since: String? = nil, media: [Media] = [] ) { self.state = state self.lastAt = lastAt self.lastError = lastError self.nextAt = nextAt self.since = since self.media = media } /// Half a configuration: the Mac's env file names a bucket but its /// settings do not add up, so nothing is copied off it. The core answers /// that — and only that — as `never` WITH what is missing as its error; /// no bucket named at all is `never` with none (a Mac holding the only /// copy by choice). public var isHalfSetUp: Bool { state == .never && lastError != nil } /// The line itself — `read.device`'s `backup`, or `device.backupNow`'s. /// `nil` for a null line or a state this build does not know, never a /// guess. public init?(line: JSONValue?) { guard let line, let word = line["state"]?.string, let state = State(rawValue: word) else { return nil } let text = { (key: String) in line[key]?.string.flatMap { $0.isEmpty ? nil : $0 } } self.init( state: state, lastAt: text("lastAt"), lastError: text("lastError"), nextAt: text("nextAt"), since: text("since"), media: (line["databases"]?.array ?? []).compactMap(Media.init(database:))) } /// Who said a page's backup (`read.device`'s `backupAnsweredBy`): the Mac /// itself on its own page, or the serving Mac for a phone or another Mac /// looking at that Mac's page. Every other answer carries no backup. public enum AnsweredBy: String, Sendable { case thisMac = "this_mac" case servingMac = "serving_mac" } /// The backup a `read.device` answer carries and who said it, or `nil` /// when the page is not the serving Mac's (`not_serving`, `no_serving_mac`, /// a phone's own `not_a_mac`): only that Mac answers one. public static func answered(_ answer: JSONValue) -> (backup: DeviceBackup, by: AnsweredBy)? { guard let word = answer["backupAnsweredBy"]?.string, let by = AnsweredBy(rawValue: word), let backup = DeviceBackup(line: answer["backup"]) else { return nil } return (backup, by) } }