| 1 | import Foundation |
| 2 | |
| 3 | // Push registration and set aside (native slice 2): wire.RegisterPush, |
| 4 | // wire.PushRegistration, wire.RegisterActivity and wire.SetCardVisibility |
| 5 | // (internal/wire/push.go), served by the conversation engine. |
| 6 | |
| 7 | /// The APNs gateway a device token belongs to. |
| 8 | public enum APNsEnvironment: String, Codable, Sendable { case production, development } |
| 9 | |
| 10 | /// The device's platform, as the server names it. Only iOS starts Live Activities. |
| 11 | public enum PushPlatform: String, Codable, Sendable { |
| 12 | case ios, macos |
| 13 | public static var current: PushPlatform { |
| 14 | #if os(iOS) |
| 15 | return .ios |
| 16 | #else |
| 17 | return .macos |
| 18 | #endif |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | /// The body of `POST /push-registrations`: this device's APNs token, its |
| 23 | /// gateway and platform, and on iPhone the Live Activity push-to-start |
| 24 | /// token. Tokens are lowercase hex. They are secrets: never logged. |
| 25 | public struct PushRegistrationBody: Codable, Sendable, Equatable { |
| 26 | public var token: String |
| 27 | public var environment: APNsEnvironment |
| 28 | public var platform: PushPlatform |
| 29 | public var startToken: String? |
| 30 | public var actionsVersion: Int? |
| 31 | public var bindingId: String? |
| 32 | public init(token: String, environment: APNsEnvironment, platform: PushPlatform, startToken: String? = nil) { |
| 33 | self.token = token; self.environment = environment; self.platform = platform |
| 34 | // Only an iPhone has a push-to-start token; the server refuses one from a Mac. |
| 35 | self.startToken = platform == .ios ? startToken : nil |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// What the server keeps, without its tokens. |
| 40 | public struct PushRegistration: Codable, Sendable, Equatable { |
| 41 | public var environment: String |
| 42 | public var platform: String |
| 43 | public var liveActivities: Bool |
| 44 | public var updatedAt: Date |
| 45 | } |
| 46 | |
| 47 | /// The body of `POST /push-registrations/activities`: a running Live |
| 48 | /// Activity's own update token, for its task. |
| 49 | public struct ActivityTokenBody: Codable, Sendable, Equatable { |
| 50 | public var taskId: String |
| 51 | public var token: String |
| 52 | } |
| 53 | |
| 54 | public enum PushToken { |
| 55 | /// An APNs token as the server takes it: lowercase hex. |
| 56 | public static func hex(_ data: Data) -> String { data.map { String(format: "%02x", $0) }.joined() } |
| 57 | } |
| 58 | |
| 59 | extension EquipoClient { |
| 60 | /// Registers this device for pushes, replacing its earlier registration. |
| 61 | public func registerPush(_ write: PendingWrite) async throws -> PushRegistration { |
| 62 | try await send(APIRequest("POST", "/push-registrations", body: write.body, bearer: token, idempotencyKey: write.key)) |
| 63 | } |
| 64 | /// A running Live Activity's token. 404 for a task that is not the |
| 65 | /// person's; 409 `task_ended` once it ended. |
| 66 | public func registerActivity(_ write: PendingWrite) async throws { |
| 67 | let _: EmptyResponse = try await send(APIRequest("POST", "/push-registrations/activities", body: write.body, bearer: token, idempotencyKey: write.key)) |
| 68 | } |
| 69 | |
| 70 | /// Set aside (or Undo), bound to the card revision the person saw. It |
| 71 | /// never answers, cancels or starts anything. |
| 72 | public static func visibility(aside: Bool, of card: ThinCard) throws -> PendingWrite { |
| 73 | let body: [String: AnyEncodable] = ["aside": .init(aside), "revision": .init(card.revision)] |
| 74 | return PendingWrite(body: try EquipoJSON.encoder.encode(body), ifMatch: card.revision) |
| 75 | } |
| 76 | public func setVisibility(card id: String, _ write: PendingWrite) async throws -> ThinCard { |
| 77 | guard write.ifMatch != nil else { throw EquipoAPIError.status(428) } |
| 78 | return try await send(APIRequest("POST", "/cards/\(escapePath(id))/visibility", body: write.body, bearer: token, |
| 79 | idempotencyKey: write.key, ifMatch: write.ifMatch)) |
| 80 | } |
| 81 | |
| 82 | func escapePath(_ id: String) -> String { |
| 83 | id.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed.subtracting(CharacterSet(charactersIn: "/"))) ?? id |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | struct NotificationCapabilities: Decodable { let notificationActionsV1: Bool? } |
| 88 | extension EquipoClient { |
| 89 | func notificationCard(_ id: String) async throws -> ThinCard { |
| 90 | try await get("/cards/\(escape(id))") |
| 91 | } |
| 92 | func notificationActionsEnabled() async throws -> Bool { |
| 93 | let value: NotificationCapabilities = try await get("/capabilities") |
| 94 | return value.notificationActionsV1 == true |
| 95 | } |
| 96 | func retryNotification(task: String, write: PendingWrite) async throws { |
| 97 | let _: TaskAccepted = try await post("/tasks/\(escape(task))/retry", write) |
| 98 | } |
| 99 | } |
| 100 | |