| 1 | import Foundation |
| 2 | |
| 3 | // What Base's server answers the apps, decoded tolerantly: a field a newer |
| 4 | // Base adds is ignored, and one an older Base leaves out takes its default, |
| 5 | // so neither ends a session or empties a screen. The shapes are the plan's |
| 6 | // Task 5 (`/api/v1/me`, `/api/v1/home`) and Ask's existing JSON |
| 7 | // (internal/web/ask.go askStateJSON, ask_relay.go, internal/equipo/chat.go), |
| 8 | // the same contract ask.js reads (docs/api/base-native-v1.md, Task 6). |
| 9 | |
| 10 | extension KeyedDecodingContainer { |
| 11 | /// The value at `key`, or `fallback` when it is missing, null or of |
| 12 | /// another type. |
| 13 | func value<T: Decodable>(_ key: Key, _ fallback: T) -> T { |
| 14 | ((try? decodeIfPresent(T.self, forKey: key)) ?? nil) ?? fallback |
| 15 | } |
| 16 | func optional<T: Decodable>(_ key: Key) -> T? { (try? decodeIfPresent(T.self, forKey: key)) ?? nil } |
| 17 | /// An RFC 3339 time (Go's, with or without fractional seconds); nil when |
| 18 | /// missing, empty or unreadable. |
| 19 | func date(_ key: Key) -> Date? { BaseJSON.date(optional(key)) } |
| 20 | /// An http(s) URL; nil when missing, empty or anything else. |
| 21 | func webURL(_ key: Key) -> URL? { BaseJSON.webURL(optional(key)) } |
| 22 | } |
| 23 | |
| 24 | public enum BaseJSON { |
| 25 | public static func date(_ text: String?) -> Date? { |
| 26 | guard let text, !text.isEmpty else { return nil } |
| 27 | if let d = try? Date(text, strategy: .iso8601.year().month().day().time(includingFractionalSeconds: true).timeZone(separator: .omitted)) { return d } |
| 28 | if let d = try? Date(text, strategy: Date.ISO8601FormatStyle(includingFractionalSeconds: true)) { return d } |
| 29 | return try? Date(text, strategy: .iso8601) |
| 30 | } |
| 31 | |
| 32 | /// `equipo.WebURL`: an absolute http or https URL with a host and no |
| 33 | /// user info, else nil. Links the server passes are already clean; this |
| 34 | /// holds the apps to the same rule. |
| 35 | public static func webURL(_ text: String?) -> URL? { |
| 36 | guard let text, let c = URLComponents(string: text), let scheme = c.scheme?.lowercased(), scheme == "http" || scheme == "https", |
| 37 | let host = c.host, !host.isEmpty, c.user == nil, c.password == nil, let url = c.url else { return nil } |
| 38 | return url |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // MARK: /api/v1/me |
| 43 | |
| 44 | /// `GET /api/v1/me`: who is signed in, to which Base, and their Appearance. |
| 45 | public struct Me: Decodable, Sendable, Equatable { |
| 46 | public struct Member: Decodable, Sendable, Equatable { |
| 47 | public var id = "", name = "", email = "", initials = "" |
| 48 | public var owner = false |
| 49 | public init(id: String = "", name: String = "", email: String = "", initials: String = "", owner: Bool = false) { |
| 50 | self.id = id; self.name = name; self.email = email; self.initials = initials; self.owner = owner |
| 51 | } |
| 52 | enum CodingKeys: String, CodingKey { case id, name, email, initials, owner } |
| 53 | public init(from decoder: Decoder) throws { |
| 54 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 55 | id = c.value(.id, ""); name = c.value(.name, ""); email = c.value(.email, "") |
| 56 | initials = c.value(.initials, ""); owner = c.value(.owner, false) |
| 57 | } |
| 58 | } |
| 59 | public struct Base: Decodable, Sendable, Equatable { |
| 60 | public var name = "", slug = "", timezone = "" |
| 61 | public var origin: URL? |
| 62 | public init(name: String = "", slug: String = "", origin: URL? = nil, timezone: String = "") { |
| 63 | self.name = name; self.slug = slug; self.origin = origin; self.timezone = timezone |
| 64 | } |
| 65 | enum CodingKeys: String, CodingKey { case name, slug, origin, timezone } |
| 66 | public init(from decoder: Decoder) throws { |
| 67 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 68 | name = c.value(.name, ""); slug = c.value(.slug, ""); origin = c.webURL(.origin); timezone = c.value(.timezone, "") |
| 69 | } |
| 70 | /// Base's time zone, for when a job started; nil if unknown. |
| 71 | public var timeZone: TimeZone? { TimeZone(identifier: timezone) } |
| 72 | } |
| 73 | public var member: Member |
| 74 | public var base: Base |
| 75 | public var appearance: BaseAppearance |
| 76 | public var equipoOrigin: URL? |
| 77 | |
| 78 | public init(member: Member, base: Base, appearance: BaseAppearance = .system, equipoOrigin: URL? = nil) { |
| 79 | self.member = member; self.base = base; self.appearance = appearance; self.equipoOrigin = equipoOrigin |
| 80 | } |
| 81 | enum CodingKeys: String, CodingKey { case member, base, appearance, equipoOrigin = "equipo_origin" } |
| 82 | public init(from decoder: Decoder) throws { |
| 83 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 84 | member = c.value(.member, Member()); base = c.value(.base, Base()) |
| 85 | appearance = BaseAppearance(server: c.optional(.appearance)); equipoOrigin = c.webURL(.equipoOrigin) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // MARK: /api/v1/home |
| 90 | |
| 91 | /// `GET /api/v1/home`: Home's greeting, its first steps and the apps, in |
| 92 | /// Home's order (P6). |
| 93 | public struct HomeData: Decodable, Sendable, Equatable { |
| 94 | public struct Step: Decodable, Sendable, Equatable, Hashable { |
| 95 | public var label: String |
| 96 | public var done: Bool |
| 97 | public init(label: String, done: Bool) { self.label = label; self.done = done } |
| 98 | enum CodingKeys: String, CodingKey { case label, done } |
| 99 | public init(from decoder: Decoder) throws { |
| 100 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 101 | label = c.value(.label, ""); done = c.value(.done, false) |
| 102 | } |
| 103 | } |
| 104 | public struct App: Decodable, Sendable, Equatable, Identifiable { |
| 105 | public enum Kind: String, Sendable, Equatable { case oficina, installed, own, other } |
| 106 | public var id: String |
| 107 | public var kind: Kind |
| 108 | public var name: String |
| 109 | public var url: URL? |
| 110 | public var iconURL: String? |
| 111 | public var tint: String? |
| 112 | public var iconSVG: String? |
| 113 | public var bundleID: String? |
| 114 | public var scheme: String? |
| 115 | public init(id: String, kind: Kind, name: String, url: URL?, iconURL: String? = nil, tint: String? = nil, |
| 116 | iconSVG: String? = nil, bundleID: String? = nil, scheme: String? = nil) { |
| 117 | self.id = id; self.kind = kind; self.name = name; self.url = url; self.iconURL = iconURL |
| 118 | self.tint = tint; self.iconSVG = iconSVG; self.bundleID = bundleID; self.scheme = scheme |
| 119 | } |
| 120 | enum CodingKeys: String, CodingKey { |
| 121 | case id, kind, name, url, tint, scheme |
| 122 | case iconURL = "icon_url", iconSVG = "icon_svg", bundleID = "bundle_id" |
| 123 | } |
| 124 | public init(from decoder: Decoder) throws { |
| 125 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 126 | id = c.value(.id, ""); kind = Kind(rawValue: c.value(.kind, "")) ?? .other; name = c.value(.name, "") |
| 127 | url = c.webURL(.url); iconURL = c.optional(.iconURL); tint = c.optional(.tint); iconSVG = c.optional(.iconSVG) |
| 128 | bundleID = c.optional(.bundleID); scheme = c.optional(.scheme) |
| 129 | } |
| 130 | } |
| 131 | public var greeting: String |
| 132 | public var lede: String |
| 133 | public var steps: [Step] |
| 134 | public var apps: [App] |
| 135 | enum CodingKeys: String, CodingKey { case greeting, lede, steps, apps } |
| 136 | public init(from decoder: Decoder) throws { |
| 137 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 138 | greeting = c.value(.greeting, ""); lede = c.value(.lede, "") |
| 139 | steps = c.value(.steps, []); apps = c.value(.apps, []).filter { !$0.id.isEmpty } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // MARK: Ask |
| 144 | |
| 145 | /// Ask's states (ask.go): `ready` shows the conversation; `norunner` and |
| 146 | /// `oldrunner` show it with a note; the rest stand in its place. |
| 147 | /// `not_linked` is the app's own (Task 7: Equipo doesn't know this person); |
| 148 | /// anything a newer Base says that this app doesn't know is `unavailable`. |
| 149 | public enum AskPhase: String, Sendable, Equatable, CaseIterable { |
| 150 | case loading, connect, pending, reconnect, unavailable, ready, norunner, oldrunner |
| 151 | case notLinked = "not_linked" |
| 152 | |
| 153 | public init(server value: String?) { self = value.flatMap(AskPhase.init(rawValue:)).flatMap { $0 == .loading ? nil : $0 } ?? .unavailable } |
| 154 | |
| 155 | /// The states that show the conversation (ask.js `chatting`). |
| 156 | public var chatting: Bool { self == .ready || self == .norunner || self == .oldrunner } |
| 157 | } |
| 158 | |
| 159 | /// One line of the conversation (equipo.Message). Role is `person` or the |
| 160 | /// Assistant's. |
| 161 | public struct AskMessage: Decodable, Sendable, Equatable, Identifiable { |
| 162 | public var id: String |
| 163 | public var role: String |
| 164 | public var text: String |
| 165 | public var turnID: String |
| 166 | public var createdAt: Date? |
| 167 | /// The files the person sent with it, in order (none for most). |
| 168 | public var attachments: [AskAttachment] |
| 169 | /// What the Assistant looked up on the web for its reply, in the order |
| 170 | /// it did, drawn under its words (none on a person's message). |
| 171 | public var lookups: [AskLookup] |
| 172 | public var isPerson: Bool { role == "person" } |
| 173 | public init(id: String, role: String, text: String, turnID: String = "", createdAt: Date? = nil, attachments: [AskAttachment] = [], |
| 174 | lookups: [AskLookup] = []) { |
| 175 | self.id = id; self.role = role; self.text = text; self.turnID = turnID; self.createdAt = createdAt; self.attachments = attachments |
| 176 | self.lookups = lookups |
| 177 | } |
| 178 | enum CodingKeys: String, CodingKey { case id, role, text, turnID = "turn_id", createdAt = "created_at", attachments, lookups } |
| 179 | public init(from decoder: Decoder) throws { |
| 180 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 181 | id = c.value(.id, ""); role = c.value(.role, ""); text = c.value(.text, "") |
| 182 | turnID = c.value(.turnID, ""); createdAt = c.date(.createdAt) |
| 183 | attachments = c.value(.attachments, [AskAttachment]()).filter { !$0.id.isEmpty } |
| 184 | lookups = role == "person" ? [] : c.value(.lookups, [AskLookup]()).filter { !$0.answer.isEmpty && !$0.sources.isEmpty } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /// One thing the Assistant looked up for a reply (docs/api/base-native-v1.md, |
| 189 | /// Lookup): a short answer, drawn with LookupMarkdown (never a link), and |
| 190 | /// the pages it came from, each drawn as its title (a link) and its host. |
| 191 | public struct AskLookup: Decodable, Sendable, Equatable { |
| 192 | public var answer: String |
| 193 | public var sources: [AskSource] |
| 194 | public init(answer: String, sources: [AskSource]) { self.answer = answer; self.sources = sources } |
| 195 | enum CodingKeys: String, CodingKey { case answer, sources } |
| 196 | public init(from decoder: Decoder) throws { |
| 197 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 198 | answer = c.value(.answer, "") |
| 199 | sources = c.value(.sources, [AskSource]()).filter { $0.url != nil } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /// A page a lookup's answer came from: https only (anything else has no |
| 204 | /// url, and is dropped). |
| 205 | public struct AskSource: Decodable, Sendable, Equatable { |
| 206 | public var url: URL? |
| 207 | public var title: String |
| 208 | public init(url: URL?, title: String) { self.url = url; self.title = title } |
| 209 | enum CodingKeys: String, CodingKey { case url, title } |
| 210 | public init(from decoder: Decoder) throws { |
| 211 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 212 | let u = c.webURL(.url) |
| 213 | url = u?.scheme == "https" ? u : nil |
| 214 | title = c.value(.title, "") |
| 215 | } |
| 216 | /// The host as the Sources line shows it, without "www." (sourceHost |
| 217 | /// in ask_cards.go). |
| 218 | public var host: String { |
| 219 | let h = (url?.host ?? "").lowercased() |
| 220 | return h.hasPrefix("www.") ? String(h.dropFirst(4)) : h |
| 221 | } |
| 222 | /// What the link says: the title, or the host without one. |
| 223 | public var label: String { title.isEmpty ? host : title } |
| 224 | } |
| 225 | |
| 226 | /// One of a message's files (docs/api/base-native-v1.md, Attachment). |
| 227 | /// Until the app draws files (slice 2), only its name shows. A kind the |
| 228 | /// app doesn't know is a file. |
| 229 | public struct AskAttachment: Decodable, Sendable, Equatable { |
| 230 | public var id: String |
| 231 | public var kind: String |
| 232 | public var name: String |
| 233 | public var mediaType: String |
| 234 | public var size: Int64 |
| 235 | public var width: Int |
| 236 | public var height: Int |
| 237 | public var isImage: Bool { kind == "image" } |
| 238 | public init(id: String, kind: String = "file", name: String, mediaType: String = "", size: Int64 = 0, width: Int = 0, height: Int = 0) { |
| 239 | self.id = id; self.kind = kind; self.name = name; self.mediaType = mediaType; self.size = size; self.width = width; self.height = height |
| 240 | } |
| 241 | enum CodingKeys: String, CodingKey { case id, kind, name, mediaType = "media_type", size, width, height } |
| 242 | public init(from decoder: Decoder) throws { |
| 243 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 244 | id = c.value(.id, ""); name = c.value(.name, ""); mediaType = c.value(.mediaType, "") |
| 245 | let k: String = c.value(.kind, "file") |
| 246 | kind = k == "image" ? "image" : "file" |
| 247 | size = c.value(.size, Int64(0)); width = c.value(.width, 0); height = c.value(.height, 0) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /// The Assistant's answer to the person's messages (equipo.Turn). |
| 252 | public struct AskTurn: Decodable, Sendable, Equatable { |
| 253 | public var id: String |
| 254 | public var kind: String |
| 255 | public var state: String |
| 256 | public var reason: String |
| 257 | /// The reply's message id, when the server says it (ask.js reads it). |
| 258 | public var replyID: String |
| 259 | public init(id: String, kind: String = "", state: String, reason: String = "", replyID: String = "") { |
| 260 | self.id = id; self.kind = kind; self.state = state; self.reason = reason; self.replyID = replyID |
| 261 | } |
| 262 | enum CodingKeys: String, CodingKey { case id, kind, state, reason, replyID = "reply_id" } |
| 263 | public init(from decoder: Decoder) throws { |
| 264 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 265 | id = c.value(.id, ""); kind = c.value(.kind, ""); state = c.value(.state, "") |
| 266 | reason = c.value(.reason, ""); replyID = c.value(.replyID, "") |
| 267 | } |
| 268 | /// replied, failed and cancelled end a turn. |
| 269 | public var ended: Bool { state == "replied" || state == "failed" || state == "cancelled" } |
| 270 | /// A team_update turn is the Assistant taking a job's news by itself: |
| 271 | /// quiet, with no status, activity or Stop. |
| 272 | public var quiet: Bool { kind == "team_update" } |
| 273 | } |
| 274 | |
| 275 | /// Something in the conversation to act on (equipo.Card): a question, |
| 276 | /// confirm, outcome or connect card, or a job (`kind` "job"). |
| 277 | public struct AskCard: Decodable, Sendable, Equatable, Identifiable { |
| 278 | public struct Option: Decodable, Sendable, Equatable, Hashable { |
| 279 | public var value: String |
| 280 | public var label: String |
| 281 | public init(value: String, label: String) { self.value = value; self.label = label } |
| 282 | enum CodingKeys: String, CodingKey { case value, label } |
| 283 | public init(from decoder: Decoder) throws { |
| 284 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 285 | value = c.value(.value, ""); label = c.value(.label, "") |
| 286 | } |
| 287 | } |
| 288 | public struct Link: Decodable, Sendable, Equatable, Hashable { |
| 289 | public var label: String |
| 290 | public var url: URL? |
| 291 | public var rel: String |
| 292 | public init(label: String, url: URL?, rel: String = "") { self.label = label; self.url = url; self.rel = rel } |
| 293 | enum CodingKeys: String, CodingKey { case label, url, rel } |
| 294 | public init(from decoder: Decoder) throws { |
| 295 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 296 | label = c.value(.label, ""); url = c.webURL(.url); rel = c.value(.rel, "") |
| 297 | } |
| 298 | } |
| 299 | public var id: String |
| 300 | public var revision: Int64 |
| 301 | public var kind: String |
| 302 | public var state: String |
| 303 | public var question: String |
| 304 | public var options: [Option] |
| 305 | public var links: [Link] |
| 306 | public var result: String |
| 307 | public var job: String |
| 308 | public var detail: String |
| 309 | public var startedAt: Date? |
| 310 | public var turnID: String |
| 311 | /// A paused job offers "Carry on now" (`carry_on`; Base sends it on a |
| 312 | /// paused or stopped job card only). |
| 313 | public var carryOn: Bool |
| 314 | |
| 315 | public init(id: String, revision: Int64 = 1, kind: String, state: String, question: String = "", options: [Option] = [], |
| 316 | links: [Link] = [], result: String = "", job: String = "", detail: String = "", startedAt: Date? = nil, turnID: String = "", |
| 317 | carryOn: Bool = false) { |
| 318 | self.id = id; self.revision = revision; self.kind = kind; self.state = state; self.question = question |
| 319 | self.options = options; self.links = links; self.result = result; self.job = job; self.detail = detail |
| 320 | self.startedAt = startedAt; self.turnID = turnID; self.carryOn = carryOn |
| 321 | } |
| 322 | enum CodingKeys: String, CodingKey { |
| 323 | case id, revision, kind, state, question, options, links, result, job, detail |
| 324 | case startedAt = "started_at", turnID = "turn_id", carryOn = "carry_on" |
| 325 | } |
| 326 | public init(from decoder: Decoder) throws { |
| 327 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 328 | id = c.value(.id, ""); revision = c.value(.revision, 0); kind = c.value(.kind, ""); state = c.value(.state, "") |
| 329 | question = c.value(.question, ""); options = c.value(.options, []).filter { !$0.value.isEmpty } |
| 330 | links = c.value(.links, []).filter { $0.url != nil }; result = c.value(.result, "") |
| 331 | job = c.value(.job, ""); detail = c.value(.detail, ""); startedAt = c.date(.startedAt); turnID = c.value(.turnID, "") |
| 332 | carryOn = c.value(.carryOn, false) |
| 333 | } |
| 334 | |
| 335 | public var isOpen: Bool { state == "open" } |
| 336 | public var isJob: Bool { kind == "job" } |
| 337 | public var jobState: JobState? { isJob ? JobState(rawValue: state) : nil } |
| 338 | /// A paused job that offers "Carry on now", or a stopped one held |
| 339 | /// while building was off, once it is on again. |
| 340 | public var offersCarryOn: Bool { (jobState == .paused || jobState == .stopped) && carryOn } |
| 341 | /// The kinds drawn as cards (ask.js `kinds`). |
| 342 | public static let drawnKinds: Set<String> = ["question", "confirm", "outcome", "connect"] |
| 343 | /// Where to see a job's work once there is something to see (ready, |
| 344 | /// done): its first link with no rel. |
| 345 | public var go: Link? { |
| 346 | guard isJob, state == "ready" || state == "done" else { return nil } |
| 347 | return links.first { $0.rel.isEmpty } |
| 348 | } |
| 349 | /// The job's own page in Equipo (rel "equipo"): the job view's title. |
| 350 | public var equipoLink: Link? { isJob ? links.first { $0.rel == "equipo" } : nil } |
| 351 | /// An open connect card's link to Base's own connect page. |
| 352 | public var connectLink: Link? { kind == "connect" ? links.first { $0.rel == "oficina_connect" } : nil } |
| 353 | } |
| 354 | |
| 355 | /// A job as `/ask/state` lists it: its card, with the cards that belong |
| 356 | /// to it (jobView in ask.go). |
| 357 | public struct AskJob: Decodable, Sendable, Equatable { |
| 358 | public var card: AskCard |
| 359 | public var cards: [AskCard] |
| 360 | public init(card: AskCard, cards: [AskCard] = []) { self.card = card; self.cards = cards } |
| 361 | enum CodingKeys: String, CodingKey { case cards } |
| 362 | public init(from decoder: Decoder) throws { |
| 363 | card = try AskCard(from: decoder) |
| 364 | cards = try decoder.container(keyedBy: CodingKeys.self).value(.cards, []) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /// `GET /ask/state`: what the conversation draws, and the cursor its event |
| 369 | /// stream starts after. `renew_after` and `equipo_origin` are Task 7's. |
| 370 | public struct AskSnapshot: Decodable, Sendable, Equatable { |
| 371 | public var state: AskPhase |
| 372 | public var messages: [AskMessage] |
| 373 | public var jobs: [AskJob] |
| 374 | public var cards: [AskCard] |
| 375 | public var turn: AskTurn? |
| 376 | public var status: String |
| 377 | public var cursor: Int64 |
| 378 | public var renewAfter: Date? |
| 379 | public var equipoOrigin: URL? |
| 380 | /// The member disconnected Ask (on any client) and hasn't set it up |
| 381 | /// again: no set-up by itself. Nil from a Base that doesn't say. |
| 382 | public var disconnected: Bool? |
| 383 | enum CodingKeys: String, CodingKey { |
| 384 | case state, messages, jobs, cards, turn, status, cursor, disconnected |
| 385 | case renewAfter = "renew_after", equipoOrigin = "equipo_origin" |
| 386 | } |
| 387 | public init(from decoder: Decoder) throws { |
| 388 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 389 | state = AskPhase(server: c.optional(.state)); messages = c.value(.messages, []).filter { !$0.id.isEmpty } |
| 390 | jobs = c.value(.jobs, []); cards = c.value(.cards, []).filter { !$0.id.isEmpty } |
| 391 | turn = c.optional(.turn).flatMap { (t: AskTurn) in t.id.isEmpty ? nil : t } |
| 392 | status = c.value(.status, ""); cursor = c.value(.cursor, 0) |
| 393 | renewAfter = c.date(.renewAfter); equipoOrigin = c.webURL(.equipoOrigin) |
| 394 | disconnected = c.optional(.disconnected) |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /// `POST /ask/messages`'s answer: the person's message and the turn it |
| 399 | /// started (equipo.Sent). |
| 400 | public struct AskSent: Decodable, Sendable, Equatable { |
| 401 | public var message: AskMessage? |
| 402 | public var turn: AskTurn? |
| 403 | enum CodingKeys: String, CodingKey { case message, turn } |
| 404 | public init(from decoder: Decoder) throws { |
| 405 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 406 | message = c.optional(.message).flatMap { (m: AskMessage) in m.id.isEmpty ? nil : m } |
| 407 | turn = c.optional(.turn).flatMap { (t: AskTurn) in t.id.isEmpty ? nil : t } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /// `POST /ask/cards/{id}/answer`'s answer: the card as it is now, and the |
| 412 | /// message and turn the answer started (both null for an outcome card). |
| 413 | public struct AskAnswered: Decodable, Sendable, Equatable { |
| 414 | public var card: AskCard? |
| 415 | public var message: AskMessage? |
| 416 | public var turn: AskTurn? |
| 417 | enum CodingKeys: String, CodingKey { case card, message, turn } |
| 418 | public init(from decoder: Decoder) throws { |
| 419 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 420 | card = c.optional(.card).flatMap { (k: AskCard) in k.id.isEmpty ? nil : k } |
| 421 | message = c.optional(.message).flatMap { (m: AskMessage) in m.id.isEmpty ? nil : m } |
| 422 | turn = c.optional(.turn).flatMap { (t: AskTurn) in t.id.isEmpty ? nil : t } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /// `POST /ask/cards/{id}/carry_on`'s answer: the job card as it is now. |
| 427 | public struct AskCarriedOn: Decodable, Sendable, Equatable { |
| 428 | public var card: AskCard? |
| 429 | enum CodingKeys: String, CodingKey { case card } |
| 430 | public init(from decoder: Decoder) throws { |
| 431 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 432 | card = c.optional(.card).flatMap { (k: AskCard) in k.id.isEmpty ? nil : k } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /// `POST /api/v1/ask/oficina`'s answer (Task 7): `ready` with when to renew, |
| 437 | /// or `not_linked`. |
| 438 | public struct AskSetupAnswer: Decodable, Sendable, Equatable { |
| 439 | public var state: AskPhase |
| 440 | public var renewAfter: Date? |
| 441 | enum CodingKeys: String, CodingKey { case state, renewAfter = "renew_after" } |
| 442 | public init(from decoder: Decoder) throws { |
| 443 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 444 | state = AskPhase(server: c.optional(.state)); renewAfter = c.date(.renewAfter) |
| 445 | } |
| 446 | } |
| 447 | |