// What this phone knows about itself, and the one call that tells the // server to forget it. // // **`deviceId` is `identifierForVendor`**, the value the previous build // registered under, so a forget names the same row. import Foundation #if canImport(UIKit) import UIKit #endif /// What this phone knows about itself, at registration time. public struct DeviceFacts: Equatable, Sendable { /// `identifierForVendor`. See the file header. public let deviceId: String /// The `apns-topic` every push to this device carries. public let bundleId: String public let appVersion: String? public let build: String? public let locale: String? public let timezone: String? public init( deviceId: String, bundleId: String, appVersion: String? = nil, build: String? = nil, locale: String? = nil, timezone: String? = nil ) { self.deviceId = deviceId self.bundleId = bundleId self.appVersion = appVersion self.build = build self.locale = locale self.timezone = timezone } } /// Where the facts come from — a seam, so a test names them. /// /// **`@MainActor`, because `UIDevice.current` is.** The vendor identifier is /// UIKit's and UIKit is main-actor-isolated; the only caller is /// `NotificationsModel`, which is `@MainActor` already, so this is a /// compile-time fact rather than a `MainActor.assumeIsolated` that would trap /// at runtime to say the same thing. @MainActor public protocol DeviceFactsSource: Sendable { /// `nil` when the vendor identifier is unavailable, which iOS allows while /// the device is locked and just after a restore. There is nothing to /// register without it, and the next foreground asks again. func read() -> DeviceFacts? } /// The real one. public struct SystemDeviceFacts: DeviceFactsSource { public init() {} public func read() -> DeviceFacts? { #if canImport(UIKit) guard let vendor = UIDevice.current.identifierForVendor?.uuidString else { return nil } let info = Bundle.main.infoDictionary return DeviceFacts( deviceId: vendor, bundleId: Bundle.main.bundleIdentifier ?? "", // The label with its build, "1.0.0 (256)" — the marketing // version alone does not say which build a phone runs. appVersion: CottonConfig.bundleVersionLabel, build: info?["CFBundleVersion"] as? String, locale: Locale.current.identifier, timezone: TimeZone.current.identifier ) #else // No vendor identifier on the Mac, and no push registration either — // the Mac is a server, not a device that is pushed to. return nil #endif } } /// The one call. public enum PushRegistrar { /// Tell the server to forget this device. /// /// **Answers either way and never throws**, on every path — the crate's own /// contract, and this side does not add one. Called before a leave and /// before sign-out: a person handing back a phone must never be trapped by /// a deregistration that could not reach the server. public static func forget( deviceId: String, through core: any CottonCalling ) async -> PushForgotten { do { return try await core.call( CottonMethod.pushForget, PushForget(deviceId: deviceId), as: PushForgotten.self ) } catch let refusal as CottonCallError { // The crate answers rather than refusing, so reaching here means // the BOUNDARY refused — an `unknown_method` on a build without the // crate half, say. Same shape either way: the caller says nothing // and moves on. return PushForgotten(forgotten: false, reason: refusal.code) } catch { return PushForgotten(forgotten: false, reason: "unreadable") } } }