oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2import UserNotifications
3
4/// Routing metadata is never authority. All IDs remain bound to the active
5/// registration; revisions are checked again by the server in its transaction.
6public struct NotificationBinding: Codable, Equatable, Sendable {
7 public let version: Int
8 public let bindingId: String
9 public let notificationId: String
10 public let cardKind: String?
11 public let cardRevision: Int64?
12 public let taskRevision: Int64?
13 public let stepId: String?
14 public let stepRevision: Int64?
15 public let attempt: Int64?
16 static func uuid(_ value: String) -> Bool { UUID(uuidString: value)?.uuidString.lowercased() == value }
17 static func id(_ value: String?) -> Bool {
18 guard let value, !value.isEmpty, value.utf8.count <= 128 else { return false }
19 return value.utf8.allSatisfy { (48...57).contains($0) || (65...90).contains($0) || (97...122).contains($0) || $0 == 45 || $0 == 95 }
20 }
21}
22
23public struct NotificationResponse: Codable, Equatable, Sendable {
24 public enum Intent: Codable, Equatable, Sendable { case answer(String), question, retry, merge, open }
25 public let binding: NotificationBinding
26 public let cardID: String?
27 public let taskID: String?
28 public let intent: Intent
29 public var route: PushRoute { cardID.map(PushRoute.card) ?? taskID.map(PushRoute.task) ?? .conversation }
30 public init?(userInfo: [AnyHashable: Any], category: String, action: String, text: String?) {
31 guard let data = userInfo["equipo"] as? [String: Any], let raw = data["actions"] as? [String: Any],
32 JSONSerialization.isValidJSONObject(raw), let encoded = try? JSONSerialization.data(withJSONObject: raw),
33 let binding = try? EquipoJSON.decoder.decode(NotificationBinding.self, from: encoded),
34 binding.version == 1, NotificationBinding.uuid(binding.bindingId), NotificationBinding.uuid(binding.notificationId) else { return nil }
35 let card = data["card_id"] as? String, task = data["task_id"] as? String
36 if let card, !NotificationBinding.id(card) { return nil }
37 if let task, !NotificationBinding.id(task) { return nil }
38 let intent: Intent
39 switch category {
40 case "equipo.question.text.v1", "equipo.question.choices.v1":
41 guard data["kind"] as? String == "card", NotificationBinding.id(card), binding.cardKind == "question", (binding.cardRevision ?? 0) > 0 else { return nil }
42 if action == "open", task != nil { intent = .open }
43 else if action == "answer", category == "equipo.question.choices.v1" { intent = .question }
44 else if action == "answer", let text, !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
45 text.utf8.count <= 16 * 1024, !text.contains("\0") { intent = .answer(text) }
46 else { return nil }
47 case "equipo.merge.v1":
48 guard data["kind"] as? String == "card", NotificationBinding.id(card), NotificationBinding.id(task),
49 binding.cardKind == "merge", (binding.cardRevision ?? 0) > 0 else { return nil }
50 if action == "merge" { intent = .merge }
51 else if action == "open" { intent = .open }
52 else { return nil }
53 case "equipo.stopped.v1":
54 guard data["kind"] as? String == "task", NotificationBinding.id(task), NotificationBinding.id(binding.stepId),
55 (binding.taskRevision ?? 0) > 0, (binding.stepRevision ?? 0) > 0, (binding.attempt ?? 0) > 0 else { return nil }
56 if action == "retry" { intent = .retry }
57 else if action == "open" { intent = .open }
58 else { return nil }
59 default: return nil
60 }
61 self.binding = binding; cardID = card; taskID = task; self.intent = intent
62 }
63 func pendingWrite() throws -> PendingWrite {
64 let key = binding.notificationId
65 var body: [String: AnyEncodable] = ["idempotency_key": .init(key)]
66 let revision: Int64
67 switch intent {
68 case .answer(let text):
69 guard let value = binding.cardRevision else { throw EquipoAPIError.undecodable }
70 revision = value; body["choice"] = .init(text)
71 case .retry:
72 guard let value = binding.taskRevision, let step = binding.stepId, let stepRevision = binding.stepRevision, let attempt = binding.attempt else { throw EquipoAPIError.undecodable }
73 revision = value; body["step_id"] = .init(step); body["step_revision"] = .init(stepRevision); body["attempt"] = .init(attempt)
74 default: throw EquipoAPIError.undecodable
75 }
76 body["revision"] = .init(revision)
77 return PendingWrite(body: try EquipoJSON.encoder.encode(body), ifMatch: revision, key: key)
78 }
79}
80
81@MainActor enum NotificationCategories {
82 static var all: Set<UNNotificationCategory> {
83 let flags: UNNotificationActionOptions = [.authenticationRequired, .foreground]
84 func action(_ id: String, _ key: String.LocalizationValue) -> UNNotificationAction { UNNotificationAction(identifier: id, title: EquipoStrings.text(key), options: flags) }
85 let open = action("open", "task.open")
86 let text = UNTextInputNotificationAction(identifier: "answer", title: EquipoStrings.text("chat.card.answer"), options: flags,
87 textInputButtonTitle: EquipoStrings.text("composer.send"), textInputPlaceholder: EquipoStrings.text("conversation.placeholder"))
88 return [
89 UNNotificationCategory(identifier: "needs_you", actions: [], intentIdentifiers: []),
90 UNNotificationCategory(identifier: "equipo.question.text.v1", actions: [text, open], intentIdentifiers: []),
91 UNNotificationCategory(identifier: "equipo.question.choices.v1", actions: [action("answer", "chat.card.answer")], intentIdentifiers: []),
92 UNNotificationCategory(identifier: "equipo.merge.v1", actions: [action("merge", "chat.card.merge"), open], intentIdentifiers: []),
93 UNNotificationCategory(identifier: "equipo.stopped.v1", actions: [action("retry", "work.pause.try_again"), open], intentIdentifiers: [])
94 ]
95 }
96}
97