94 lines
3.1 KiB
Swift
94 lines
3.1 KiB
Swift
//
|
|
// PushSubscription.swift
|
|
// Pachyderm
|
|
//
|
|
// Created by Shadowfacts on 9/9/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public struct PushSubscription: Decodable, Sendable {
|
|
public let id: String
|
|
public let endpoint: URL
|
|
public let serverKey: String
|
|
public let alerts: Alerts
|
|
public let policy: Policy
|
|
|
|
public static func create(endpoint: URL, publicKey: Data, authSecret: Data, alerts: Alerts, policy: Policy) -> Request<PushSubscription> {
|
|
return Request(method: .post, path: "/api/v1/push/subscription", body: ParametersBody([
|
|
"subscription[endpoint]" => endpoint.absoluteString,
|
|
"subscription[keys][p256dh]" => publicKey.base64EncodedString(),
|
|
"subscription[keys][auth]" => authSecret.base64EncodedString(),
|
|
"data[alerts][mention]" => alerts.mention,
|
|
"data[alerts][status]" => alerts.status,
|
|
"data[alerts][reblog]" => alerts.reblog,
|
|
"data[alerts][follow]" => alerts.follow,
|
|
"data[alerts][follow_request]" => alerts.followRequest,
|
|
"data[alerts][favourite]" => alerts.favourite,
|
|
"data[alerts][poll]" => alerts.poll,
|
|
"data[alerts][update]" => alerts.update,
|
|
"data[policy]" => policy.rawValue,
|
|
]))
|
|
}
|
|
|
|
public static func update(alerts: Alerts, policy: Policy) -> Request<PushSubscription> {
|
|
return Request(method: .put, path: "/api/v1/push/subscription", body: ParametersBody([
|
|
"data[alerts][mention]" => alerts.mention,
|
|
"data[alerts][status]" => alerts.status,
|
|
"data[alerts][reblog]" => alerts.reblog,
|
|
"data[alerts][follow]" => alerts.follow,
|
|
"data[alerts][follow_request]" => alerts.followRequest,
|
|
"data[alerts][favourite]" => alerts.favourite,
|
|
"data[alerts][poll]" => alerts.poll,
|
|
"data[alerts][update]" => alerts.update,
|
|
"data[policy]" => policy.rawValue,
|
|
]))
|
|
}
|
|
|
|
public static func delete() -> Request<Empty> {
|
|
return Request(method: .delete, path: "/api/v1/push/subscription")
|
|
}
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case id
|
|
case endpoint
|
|
case serverKey = "server_key"
|
|
case alerts
|
|
case policy
|
|
}
|
|
}
|
|
|
|
extension PushSubscription {
|
|
public struct Alerts: Decodable, Sendable {
|
|
public let mention: Bool
|
|
public let status: Bool
|
|
public let reblog: Bool
|
|
public let follow: Bool
|
|
public let followRequest: Bool
|
|
public let favourite: Bool
|
|
public let poll: Bool
|
|
public let update: Bool
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case mention
|
|
case status
|
|
case reblog
|
|
case follow
|
|
case followRequest = "follow_request"
|
|
case favourite
|
|
case poll
|
|
case update
|
|
}
|
|
}
|
|
}
|
|
|
|
extension PushSubscription {
|
|
public enum Policy: String, Decodable, Sendable {
|
|
case all
|
|
case followed
|
|
case followers
|
|
case none
|
|
}
|
|
}
|