55 lines
1.6 KiB
Swift
55 lines
1.6 KiB
Swift
//
|
|
// PushSubscription.swift
|
|
// PushNotifications
|
|
//
|
|
// Created by Shadowfacts on 4/7/24.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public struct PushSubscription {
|
|
let accountID: String
|
|
let endpoint: URL
|
|
let alerts: Alerts
|
|
let policy: Policy
|
|
|
|
var defaultsDict: [String: Any] {
|
|
[
|
|
"accountID": accountID,
|
|
"endpoint": endpoint.absoluteString,
|
|
"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:)),
|
|
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
|
|
self.alerts = Alerts(rawValue: alerts)
|
|
self.policy = policy
|
|
}
|
|
|
|
enum Policy: String {
|
|
case all, followed, followers
|
|
}
|
|
|
|
struct Alerts: OptionSet {
|
|
static let mention = Alerts(rawValue: 1 << 0)
|
|
static let status = Alerts(rawValue: 1 << 1)
|
|
static let reblog = Alerts(rawValue: 1 << 2)
|
|
static let follow = Alerts(rawValue: 1 << 3)
|
|
static let followRequest = Alerts(rawValue: 1 << 4)
|
|
static let favorite = Alerts(rawValue: 1 << 5)
|
|
static let poll = Alerts(rawValue: 1 << 6)
|
|
static let update = Alerts(rawValue: 1 << 7)
|
|
|
|
let rawValue: Int
|
|
}
|
|
}
|