2024-04-08 13:48:40 +00:00
|
|
|
//
|
|
|
|
// PushSubscription.swift
|
|
|
|
// PushNotifications
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 4/7/24.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2024-04-08 16:25:39 +00:00
|
|
|
import CryptoKit
|
2024-04-08 13:48:40 +00:00
|
|
|
|
|
|
|
public struct PushSubscription {
|
2024-04-09 16:35:00 +00:00
|
|
|
public let accountID: String
|
|
|
|
public internal(set) var endpoint: URL
|
2024-04-08 16:25:39 +00:00
|
|
|
public let secretKey: P256.KeyAgreement.PrivateKey
|
|
|
|
public let authSecret: Data
|
|
|
|
public var alerts: Alerts
|
|
|
|
public var policy: Policy
|
2024-04-08 13:48:40 +00:00
|
|
|
|
|
|
|
var defaultsDict: [String: Any] {
|
|
|
|
[
|
|
|
|
"accountID": accountID,
|
|
|
|
"endpoint": endpoint.absoluteString,
|
2024-04-08 16:25:39 +00:00
|
|
|
"secretKey": secretKey.rawRepresentation,
|
|
|
|
"authSecret": authSecret,
|
2024-04-08 13:48:40 +00:00
|
|
|
"alerts": alerts.rawValue,
|
|
|
|
"policy": policy.rawValue
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(defaultsDict: [String: Any]) {
|
|
|
|
guard let accountID = defaultsDict["accountID"] as? String,
|
|
|
|
let endpoint = (defaultsDict["endpoint"] as? String).flatMap(URL.init(string:)),
|
2024-04-08 16:25:39 +00:00
|
|
|
let secretKey = (defaultsDict["secretKey"] as? Data).flatMap({ try? P256.KeyAgreement.PrivateKey(rawRepresentation: $0) }),
|
|
|
|
let authSecret = defaultsDict["authSecret"] as? Data,
|
2024-04-08 13:48:40 +00:00
|
|
|
let alerts = defaultsDict["alerts"] as? Int,
|
|
|
|
let policy = (defaultsDict["policy"] as? String).flatMap(Policy.init(rawValue:)) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.accountID = accountID
|
|
|
|
self.endpoint = endpoint
|
2024-04-08 16:25:39 +00:00
|
|
|
self.secretKey = secretKey
|
|
|
|
self.authSecret = authSecret
|
2024-04-08 13:48:40 +00:00
|
|
|
self.alerts = Alerts(rawValue: alerts)
|
|
|
|
self.policy = policy
|
|
|
|
}
|
|
|
|
|
2024-04-08 16:25:39 +00:00
|
|
|
init(accountID: String, endpoint: URL, secretKey: P256.KeyAgreement.PrivateKey, authSecret: Data, alerts: Alerts, policy: Policy) {
|
|
|
|
self.accountID = accountID
|
|
|
|
self.endpoint = endpoint
|
|
|
|
self.secretKey = secretKey
|
|
|
|
self.authSecret = authSecret
|
|
|
|
self.alerts = alerts
|
|
|
|
self.policy = policy
|
|
|
|
}
|
|
|
|
|
2024-04-14 03:06:30 +00:00
|
|
|
public enum Policy: String, CaseIterable, Identifiable, Sendable {
|
2024-04-08 13:48:40 +00:00
|
|
|
case all, followed, followers
|
2024-04-10 23:13:47 +00:00
|
|
|
|
|
|
|
public var id: some Hashable {
|
|
|
|
self
|
|
|
|
}
|
2024-04-08 13:48:40 +00:00
|
|
|
}
|
|
|
|
|
2024-04-14 03:06:30 +00:00
|
|
|
public struct Alerts: OptionSet, Hashable, Sendable {
|
2024-04-08 16:25:39 +00:00
|
|
|
public static let mention = Alerts(rawValue: 1 << 0)
|
|
|
|
public static let status = Alerts(rawValue: 1 << 1)
|
|
|
|
public static let reblog = Alerts(rawValue: 1 << 2)
|
|
|
|
public static let follow = Alerts(rawValue: 1 << 3)
|
|
|
|
public static let followRequest = Alerts(rawValue: 1 << 4)
|
|
|
|
public static let favorite = Alerts(rawValue: 1 << 5)
|
|
|
|
public static let poll = Alerts(rawValue: 1 << 6)
|
|
|
|
public static let update = Alerts(rawValue: 1 << 7)
|
2024-04-18 17:17:55 +00:00
|
|
|
public static let emojiReaction = Alerts(rawValue: 1 << 8)
|
2024-04-08 13:48:40 +00:00
|
|
|
|
2024-04-08 16:25:39 +00:00
|
|
|
public let rawValue: Int
|
|
|
|
|
|
|
|
public init(rawValue: Int) {
|
|
|
|
self.rawValue = rawValue
|
|
|
|
}
|
2024-04-08 13:48:40 +00:00
|
|
|
}
|
|
|
|
}
|