metatext/MastodonAPI/Sources/MastodonAPI/Endpoints/PushSubscriptionEndpoint.swift

64 lines
1.9 KiB
Swift
Raw Normal View History

2020-08-12 07:24:39 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
2020-08-31 01:40:58 +00:00
import HTTP
import Mastodon
2020-08-12 07:24:39 +00:00
2020-08-30 23:33:11 +00:00
public enum PushSubscriptionEndpoint {
2020-08-12 07:24:39 +00:00
case create(
endpoint: URL,
publicKey: String,
auth: String,
2020-08-14 01:24:53 +00:00
alerts: PushSubscription.Alerts)
2020-08-12 07:24:39 +00:00
case read
2020-08-14 01:24:53 +00:00
case update(alerts: PushSubscription.Alerts)
2020-08-12 07:24:39 +00:00
case delete
}
2020-08-30 23:59:49 +00:00
extension PushSubscriptionEndpoint: Endpoint {
2020-08-30 23:33:11 +00:00
public typealias ResultType = PushSubscription
2020-08-12 07:24:39 +00:00
2020-08-30 23:33:11 +00:00
public var context: [String] {
2020-08-12 07:24:39 +00:00
defaultContext + ["push", "subscription"]
}
2020-08-30 23:33:11 +00:00
public var pathComponentsInContext: [String] { [] }
2020-08-12 07:24:39 +00:00
2020-08-30 23:33:11 +00:00
public var method: HTTPMethod {
2020-08-12 07:24:39 +00:00
switch self {
case .create: return .post
case .read: return .get
case .update: return .put
case .delete: return .delete
}
}
2020-09-23 07:04:37 +00:00
public var jsonBody: [String: Any]? {
2020-08-12 07:24:39 +00:00
switch self {
2020-08-14 01:24:53 +00:00
case let .create(endpoint, publicKey, auth, alerts):
2020-08-12 07:24:39 +00:00
return ["subscription":
["endpoint": endpoint.absoluteString,
"keys": [
"p256dh": publicKey,
"auth": auth]],
"data": [
"alerts": [
2020-08-14 01:24:53 +00:00
"follow": alerts.follow,
"favourite": alerts.favourite,
"reblog": alerts.reblog,
"mention": alerts.mention,
"poll": alerts.poll
2020-08-12 07:24:39 +00:00
]]]
2020-08-14 01:24:53 +00:00
case let .update(alerts):
2020-08-12 07:24:39 +00:00
return ["data":
["alerts":
2020-08-14 01:24:53 +00:00
["follow": alerts.follow,
"favourite": alerts.favourite,
"reblog": alerts.reblog,
"mention": alerts.mention,
"poll": alerts.poll]]]
2020-08-12 07:24:39 +00:00
default: return nil
}
}
}