Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Service/BlockDomainService.swift

123 lines
4.8 KiB
Swift
Raw Normal View History

2021-04-29 09:51:52 +02:00
//
// BlockDomainService.swift
// Mastodon
//
// Created by sxiaojian on 2021/4/29.
//
2021-04-30 08:55:02 +02:00
import Combine
2021-04-29 09:51:52 +02:00
import CoreData
import CoreDataStack
import Foundation
import MastodonSDK
import OSLog
import UIKit
2021-04-29 09:51:52 +02:00
final class BlockDomainService {
2021-04-30 08:55:02 +02:00
// input
weak var backgroundManagedObjectContext: NSManagedObjectContext?
weak var authenticationService: AuthenticationService?
// output
let blockedDomains = CurrentValueSubject<[String], Never>([])
init(
backgroundManagedObjectContext: NSManagedObjectContext,
authenticationService: AuthenticationService
) {
2021-04-30 08:55:02 +02:00
self.backgroundManagedObjectContext = backgroundManagedObjectContext
self.authenticationService = authenticationService
guard let authorizationBox = authenticationService.activeMastodonAuthenticationBox.value else { return }
backgroundManagedObjectContext.perform {
let _blockedDomains: [DomainBlock] = {
let request = DomainBlock.sortedFetchRequest
request.predicate = DomainBlock.predicate(domain: authorizationBox.domain, userID: authorizationBox.userID)
request.returnsObjectsAsFaults = false
do {
return try backgroundManagedObjectContext.fetch(request)
} catch {
assertionFailure(error.localizedDescription)
return []
}
}()
self.blockedDomains.value = _blockedDomains.map(\.blockedDomain)
}
2021-04-29 09:51:52 +02:00
}
2021-04-30 08:55:02 +02:00
func blockDomain(
userProvider: UserProvider,
2021-05-06 12:03:58 +02:00
cell: UITableViewCell?
2021-04-30 08:55:02 +02:00
) {
guard let activeMastodonAuthenticationBox = userProvider.context.authenticationService.activeMastodonAuthenticationBox.value else { return }
guard let context = userProvider.context else {
return
}
var mastodonUser: AnyPublisher<MastodonUser?, Never>
2021-05-06 12:03:58 +02:00
if let cell = cell {
mastodonUser = userProvider.mastodonUser(for: cell).eraseToAnyPublisher()
} else {
mastodonUser = userProvider.mastodonUser().eraseToAnyPublisher()
}
mastodonUser
.compactMap { mastodonUser -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Empty>, Error>? in
guard let mastodonUser = mastodonUser else {
return nil
}
return context.apiService.blockDomain(user: mastodonUser, authorizationBox: activeMastodonAuthenticationBox)
}
.switchToLatest()
2021-04-30 08:55:02 +02:00
.flatMap { _ -> AnyPublisher<Mastodon.Response.Content<[String]>, Error> in
context.apiService.getDomainblocks(domain: activeMastodonAuthenticationBox.domain, authorizationBox: activeMastodonAuthenticationBox)
}
.sink { completion in
switch completion {
case .finished:
break
case .failure(let error):
print(error)
}
2021-04-30 08:55:02 +02:00
} receiveValue: { [weak self] response in
self?.blockedDomains.value = response.value
}
.store(in: &userProvider.disposeBag)
}
2021-04-30 08:55:02 +02:00
func unblockDomain(
userProvider: UserProvider,
2021-05-06 12:03:58 +02:00
cell: UITableViewCell?
2021-04-30 08:55:02 +02:00
) {
guard let activeMastodonAuthenticationBox = userProvider.context.authenticationService.activeMastodonAuthenticationBox.value else { return }
guard let context = userProvider.context else {
return
}
var mastodonUser: AnyPublisher<MastodonUser?, Never>
2021-05-06 12:03:58 +02:00
if let cell = cell {
mastodonUser = userProvider.mastodonUser(for: cell).eraseToAnyPublisher()
} else {
mastodonUser = userProvider.mastodonUser().eraseToAnyPublisher()
}
mastodonUser
.compactMap { mastodonUser -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Empty>, Error>? in
guard let mastodonUser = mastodonUser else {
return nil
}
return context.apiService.unblockDomain(user: mastodonUser, authorizationBox: activeMastodonAuthenticationBox)
}
.switchToLatest()
2021-04-30 08:55:02 +02:00
.flatMap { _ -> AnyPublisher<Mastodon.Response.Content<[String]>, Error> in
context.apiService.getDomainblocks(domain: activeMastodonAuthenticationBox.domain, authorizationBox: activeMastodonAuthenticationBox)
}
.sink { completion in
switch completion {
case .finished:
break
case .failure(let error):
print(error)
}
2021-04-30 08:55:02 +02:00
} receiveValue: { [weak self] response in
self?.blockedDomains.value = response.value
}
.store(in: &userProvider.disposeBag)
}
2021-04-29 09:51:52 +02:00
}