2021-04-08 13:47:31 +02:00
|
|
|
//
|
|
|
|
// APIService+Settings.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by ihugo on 2021/4/9.
|
|
|
|
//
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
import os.log
|
2021-04-17 20:02:08 +02:00
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-04-08 13:47:31 +02:00
|
|
|
import Foundation
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension APIService {
|
|
|
|
|
2021-04-25 06:48:29 +02:00
|
|
|
func createSubscription(
|
2021-04-26 10:57:50 +02:00
|
|
|
subscriptionObjectID: NSManagedObjectID,
|
2021-04-13 11:37:13 +02:00
|
|
|
query: Mastodon.API.Subscriptions.CreateSubscriptionQuery,
|
2021-07-20 10:40:04 +02:00
|
|
|
mastodonAuthenticationBox: MastodonAuthenticationBox
|
2021-04-08 13:47:31 +02:00
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Subscription>, Error> {
|
|
|
|
let authorization = mastodonAuthenticationBox.userAuthorization
|
2021-04-25 06:48:29 +02:00
|
|
|
let domain = mastodonAuthenticationBox.domain
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2021-04-13 11:37:13 +02:00
|
|
|
return Mastodon.API.Subscriptions.createSubscription(
|
2021-04-08 13:47:31 +02:00
|
|
|
session: session,
|
|
|
|
domain: domain,
|
|
|
|
authorization: authorization,
|
|
|
|
query: query
|
|
|
|
)
|
|
|
|
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Subscription>, Error> in
|
2021-04-27 10:26:59 +02:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: create subscription successful %s", ((#file as NSString).lastPathComponent), #line, #function, response.value.endpoint)
|
2021-04-26 10:57:50 +02:00
|
|
|
|
|
|
|
let managedObjectContext = self.backgroundManagedObjectContext
|
|
|
|
return managedObjectContext.performChanges {
|
|
|
|
guard let subscription = managedObjectContext.object(with: subscriptionObjectID) as? NotificationSubscription else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
subscription.endpoint = response.value.endpoint
|
|
|
|
subscription.serverKey = response.value.serverKey
|
|
|
|
subscription.userToken = authorization.accessToken
|
|
|
|
subscription.didUpdate(at: response.networkDate)
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.map { _ in return response }
|
|
|
|
.eraseToAnyPublisher()
|
2021-04-27 10:26:59 +02:00
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
2021-04-12 15:42:43 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
func cancelSubscription(
|
2022-02-11 12:27:14 +01:00
|
|
|
domain: String,
|
|
|
|
authorization: Mastodon.API.OAuth.Authorization
|
|
|
|
) async throws -> Mastodon.Response.Content<Mastodon.Entity.EmptySubscription> {
|
|
|
|
let response = try await Mastodon.API.Subscriptions.removeSubscription(
|
2021-04-12 15:42:43 +02:00
|
|
|
session: session,
|
|
|
|
domain: domain,
|
2021-04-26 10:57:50 +02:00
|
|
|
authorization: authorization
|
2022-02-11 12:27:14 +01:00
|
|
|
).singleOutput()
|
|
|
|
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: cancel subscription successful", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
|
|
|
|
return response
|
2021-04-17 20:02:08 +02:00
|
|
|
}
|
2021-04-26 10:57:50 +02:00
|
|
|
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|