gitbay/Push/PushPayload.swift
28 lines · 1098 bytes
1import Foundation
2
3/// A push notification as the app reads it.
4///
5/// `instance` and `user` name the account the notice belongs to. One
6/// device token is one install, and an install registers against every
7/// account signed in on it, so `path` alone is ambiguous — two
8/// instances can hold the same `owner/name`.
9nonisolated struct PushPayload: Equatable, Sendable {
10 let instance: URL
11 let user: String
12 let path: String
13 /// The recipient's unread count, for the app icon badge. Absent on
14 /// a notification from a server that predates it.
15 let badge: Int
16
17 init?(userInfo: [AnyHashable: Any]) {
18 guard let raw = userInfo["instance"] as? String,
19 let instance = URL(string: raw), instance.scheme != nil,
20 let user = userInfo["user"] as? String, !user.isEmpty,
21 let path = userInfo["path"] as? String, !path.isEmpty
22 else { return nil }
23 self.instance = instance
24 self.user = user
25 self.path = path
26 self.badge = (userInfo["aps"] as? [AnyHashable: Any])?["badge"] as? Int ?? 0
27 }
28}