import Foundation // Push registration and set aside (native slice 2): wire.RegisterPush, // wire.PushRegistration, wire.RegisterActivity and wire.SetCardVisibility // (internal/wire/push.go), served by the conversation engine. /// The APNs gateway a device token belongs to. public enum APNsEnvironment: String, Codable, Sendable { case production, development } /// The device's platform, as the server names it. Only iOS starts Live Activities. public enum PushPlatform: String, Codable, Sendable { case ios, macos public static var current: PushPlatform { #if os(iOS) return .ios #else return .macos #endif } } /// The body of `POST /push-registrations`: this device's APNs token, its /// gateway and platform, and on iPhone the Live Activity push-to-start /// token. Tokens are lowercase hex. They are secrets: never logged. public struct PushRegistrationBody: Codable, Sendable, Equatable { public var token: String public var environment: APNsEnvironment public var platform: PushPlatform public var startToken: String? public var actionsVersion: Int? public var bindingId: String? public init(token: String, environment: APNsEnvironment, platform: PushPlatform, startToken: String? = nil) { self.token = token; self.environment = environment; self.platform = platform // Only an iPhone has a push-to-start token; the server refuses one from a Mac. self.startToken = platform == .ios ? startToken : nil } } /// What the server keeps, without its tokens. public struct PushRegistration: Codable, Sendable, Equatable { public var environment: String public var platform: String public var liveActivities: Bool public var updatedAt: Date } /// The body of `POST /push-registrations/activities`: a running Live /// Activity's own update token, for its task. public struct ActivityTokenBody: Codable, Sendable, Equatable { public var taskId: String public var token: String } public enum PushToken { /// An APNs token as the server takes it: lowercase hex. public static func hex(_ data: Data) -> String { data.map { String(format: "%02x", $0) }.joined() } } extension EquipoClient { /// Registers this device for pushes, replacing its earlier registration. public func registerPush(_ write: PendingWrite) async throws -> PushRegistration { try await send(APIRequest("POST", "/push-registrations", body: write.body, bearer: token, idempotencyKey: write.key)) } /// A running Live Activity's token. 404 for a task that is not the /// person's; 409 `task_ended` once it ended. public func registerActivity(_ write: PendingWrite) async throws { let _: EmptyResponse = try await send(APIRequest("POST", "/push-registrations/activities", body: write.body, bearer: token, idempotencyKey: write.key)) } /// Set aside (or Undo), bound to the card revision the person saw. It /// never answers, cancels or starts anything. public static func visibility(aside: Bool, of card: ThinCard) throws -> PendingWrite { let body: [String: AnyEncodable] = ["aside": .init(aside), "revision": .init(card.revision)] return PendingWrite(body: try EquipoJSON.encoder.encode(body), ifMatch: card.revision) } public func setVisibility(card id: String, _ write: PendingWrite) async throws -> ThinCard { guard write.ifMatch != nil else { throw EquipoAPIError.status(428) } return try await send(APIRequest("POST", "/cards/\(escapePath(id))/visibility", body: write.body, bearer: token, idempotencyKey: write.key, ifMatch: write.ifMatch)) } func escapePath(_ id: String) -> String { id.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed.subtracting(CharacterSet(charactersIn: "/"))) ?? id } } struct NotificationCapabilities: Decodable { let notificationActionsV1: Bool? } extension EquipoClient { func notificationCard(_ id: String) async throws -> ThinCard { try await get("/cards/\(escape(id))") } func notificationActionsEnabled() async throws -> Bool { let value: NotificationCapabilities = try await get("/capabilities") return value.notificationActionsV1 == true } func retryNotification(task: String, write: PendingWrite) async throws { let _: TaskAccepted = try await post("/tasks/\(escape(task))/retry", write) } }