Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Service/APIService/APIService+Subscriptions.swift

69 lines
2.5 KiB
Swift
Raw Normal View History

2021-04-08 13:47:31 +02:00
//
// APIService+Settings.swift
// Mastodon
//
// Created by ihugo on 2021/4/9.
//
import os.log
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(
subscriptionObjectID: NSManagedObjectID,
query: Mastodon.API.Subscriptions.CreateSubscriptionQuery,
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
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
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: create subscription successful %s", ((#file as NSString).lastPathComponent), #line, #function, response.value.endpoint)
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()
}
.eraseToAnyPublisher()
2021-04-08 13:47:31 +02:00
}
2021-04-12 15:42:43 +02:00
func cancelSubscription(
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,
authorization: authorization
).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-08 13:47:31 +02:00
}