import Foundation import UserNotifications /// Routing metadata is never authority. All IDs remain bound to the active /// registration; revisions are checked again by the server in its transaction. public struct NotificationBinding: Codable, Equatable, Sendable { public let version: Int public let bindingId: String public let notificationId: String public let cardKind: String? public let cardRevision: Int64? public let taskRevision: Int64? public let stepId: String? public let stepRevision: Int64? public let attempt: Int64? static func uuid(_ value: String) -> Bool { UUID(uuidString: value)?.uuidString.lowercased() == value } static func id(_ value: String?) -> Bool { guard let value, !value.isEmpty, value.utf8.count <= 128 else { return false } return value.utf8.allSatisfy { (48...57).contains($0) || (65...90).contains($0) || (97...122).contains($0) || $0 == 45 || $0 == 95 } } } public struct NotificationResponse: Codable, Equatable, Sendable { public enum Intent: Codable, Equatable, Sendable { case answer(String), question, retry, merge, open } public let binding: NotificationBinding public let cardID: String? public let taskID: String? public let intent: Intent public var route: PushRoute { cardID.map(PushRoute.card) ?? taskID.map(PushRoute.task) ?? .conversation } public init?(userInfo: [AnyHashable: Any], category: String, action: String, text: String?) { guard let data = userInfo["equipo"] as? [String: Any], let raw = data["actions"] as? [String: Any], JSONSerialization.isValidJSONObject(raw), let encoded = try? JSONSerialization.data(withJSONObject: raw), let binding = try? EquipoJSON.decoder.decode(NotificationBinding.self, from: encoded), binding.version == 1, NotificationBinding.uuid(binding.bindingId), NotificationBinding.uuid(binding.notificationId) else { return nil } let card = data["card_id"] as? String, task = data["task_id"] as? String if let card, !NotificationBinding.id(card) { return nil } if let task, !NotificationBinding.id(task) { return nil } let intent: Intent switch category { case "equipo.question.text.v1", "equipo.question.choices.v1": guard data["kind"] as? String == "card", NotificationBinding.id(card), binding.cardKind == "question", (binding.cardRevision ?? 0) > 0 else { return nil } if action == "open", task != nil { intent = .open } else if action == "answer", category == "equipo.question.choices.v1" { intent = .question } else if action == "answer", let text, !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, text.utf8.count <= 16 * 1024, !text.contains("\0") { intent = .answer(text) } else { return nil } case "equipo.merge.v1": guard data["kind"] as? String == "card", NotificationBinding.id(card), NotificationBinding.id(task), binding.cardKind == "merge", (binding.cardRevision ?? 0) > 0 else { return nil } if action == "merge" { intent = .merge } else if action == "open" { intent = .open } else { return nil } case "equipo.stopped.v1": guard data["kind"] as? String == "task", NotificationBinding.id(task), NotificationBinding.id(binding.stepId), (binding.taskRevision ?? 0) > 0, (binding.stepRevision ?? 0) > 0, (binding.attempt ?? 0) > 0 else { return nil } if action == "retry" { intent = .retry } else if action == "open" { intent = .open } else { return nil } default: return nil } self.binding = binding; cardID = card; taskID = task; self.intent = intent } func pendingWrite() throws -> PendingWrite { let key = binding.notificationId var body: [String: AnyEncodable] = ["idempotency_key": .init(key)] let revision: Int64 switch intent { case .answer(let text): guard let value = binding.cardRevision else { throw EquipoAPIError.undecodable } revision = value; body["choice"] = .init(text) case .retry: guard let value = binding.taskRevision, let step = binding.stepId, let stepRevision = binding.stepRevision, let attempt = binding.attempt else { throw EquipoAPIError.undecodable } revision = value; body["step_id"] = .init(step); body["step_revision"] = .init(stepRevision); body["attempt"] = .init(attempt) default: throw EquipoAPIError.undecodable } body["revision"] = .init(revision) return PendingWrite(body: try EquipoJSON.encoder.encode(body), ifMatch: revision, key: key) } } @MainActor enum NotificationCategories { static var all: Set { let flags: UNNotificationActionOptions = [.authenticationRequired, .foreground] func action(_ id: String, _ key: String.LocalizationValue) -> UNNotificationAction { UNNotificationAction(identifier: id, title: EquipoStrings.text(key), options: flags) } let open = action("open", "task.open") let text = UNTextInputNotificationAction(identifier: "answer", title: EquipoStrings.text("chat.card.answer"), options: flags, textInputButtonTitle: EquipoStrings.text("composer.send"), textInputPlaceholder: EquipoStrings.text("conversation.placeholder")) return [ UNNotificationCategory(identifier: "needs_you", actions: [], intentIdentifiers: []), UNNotificationCategory(identifier: "equipo.question.text.v1", actions: [text, open], intentIdentifiers: []), UNNotificationCategory(identifier: "equipo.question.choices.v1", actions: [action("answer", "chat.card.answer")], intentIdentifiers: []), UNNotificationCategory(identifier: "equipo.merge.v1", actions: [action("merge", "chat.card.merge"), open], intentIdentifiers: []), UNNotificationCategory(identifier: "equipo.stopped.v1", actions: [action("retry", "work.pause.try_again"), open], intentIdentifiers: []) ] } }