mirror of
https://github.com/block/buzz.git
synced 2026-11-29 04:32:08 +00:00
fix(mobile/ios): absorb sequential duplicate wakes
Co-authored-by: npub1tquskdu6yc4h8l7xxtceculxw600grekeq0xg2ukqfrwl7vrzg3quz3gmp <58390b379a262b73ffc632f19c73e6769ef40f36c81e642b960246eff9831222@buzz.block.builderlab.xyz> Signed-off-by: npub1tquskdu6yc4h8l7xxtceculxw600grekeq0xg2ukqfrwl7vrzg3quz3gmp <58390b379a262b73ffc632f19c73e6769ef40f36c81e642b960246eff9831222@buzz.block.builderlab.xyz>
This commit is contained in:
parent
c02ed4f436
commit
2da2da5ab8
@@ -0,0 +1,78 @@
|
||||
import Foundation
|
||||
|
||||
public enum PushNotificationIdentityError: Error, Equatable {
|
||||
case missingIdentity
|
||||
case malformedIdentity
|
||||
}
|
||||
|
||||
public struct PushNotificationIdentity: Codable, Equatable, Sendable {
|
||||
public static let userInfoKey = "buzz_push_identity"
|
||||
|
||||
public let eventID: String
|
||||
public let origin: String
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case eventID = "event_id"
|
||||
case origin
|
||||
}
|
||||
|
||||
public init(eventID: String, origin: String) {
|
||||
self.eventID = eventID.lowercased()
|
||||
self.origin = origin
|
||||
}
|
||||
|
||||
public var userInfoValue: [String: String] {
|
||||
["event_id": eventID, "origin": origin]
|
||||
}
|
||||
|
||||
public static func decodeIfPresent(
|
||||
from userInfo: [AnyHashable: Any]
|
||||
) throws -> PushNotificationIdentity? {
|
||||
guard let raw = userInfo[userInfoKey] else { return nil }
|
||||
guard let fields = raw as? [String: String],
|
||||
let eventID = fields["event_id"],
|
||||
let origin = fields["origin"],
|
||||
fields.count == 2,
|
||||
!eventID.isEmpty,
|
||||
!origin.isEmpty
|
||||
else {
|
||||
throw PushNotificationIdentityError.malformedIdentity
|
||||
}
|
||||
return PushNotificationIdentity(eventID: eventID, origin: origin)
|
||||
}
|
||||
|
||||
public static func require(
|
||||
from userInfo: [AnyHashable: Any]
|
||||
) throws -> PushNotificationIdentity {
|
||||
guard let identity = try decodeIfPresent(from: userInfo) else {
|
||||
throw PushNotificationIdentityError.missingIdentity
|
||||
}
|
||||
return identity
|
||||
}
|
||||
}
|
||||
|
||||
public struct PushDeliveredNotificationRecord {
|
||||
public let requestIdentifier: String
|
||||
public let userInfo: [AnyHashable: Any]
|
||||
|
||||
public init(requestIdentifier: String, userInfo: [AnyHashable: Any]) {
|
||||
self.requestIdentifier = requestIdentifier
|
||||
self.userInfo = userInfo
|
||||
}
|
||||
}
|
||||
|
||||
public enum PushDuplicateAbsorption {
|
||||
public static func requestIdentifiersToRemove(
|
||||
matching identity: PushNotificationIdentity,
|
||||
from delivered: [PushDeliveredNotificationRecord]
|
||||
) throws -> [String] {
|
||||
try delivered.compactMap { notification in
|
||||
guard let deliveredIdentity = try PushNotificationIdentity.decodeIfPresent(
|
||||
from: notification.userInfo
|
||||
) else {
|
||||
return nil
|
||||
}
|
||||
return deliveredIdentity == identity ? notification.requestIdentifier : nil
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user