2021-04-29 09:51:52 +02:00
|
|
|
//
|
|
|
|
// APIService+DomainBlock.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/4/29.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
2021-04-30 06:53:25 +02:00
|
|
|
import CommonOSLog
|
2021-04-29 09:51:52 +02:00
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import DateToolsSwift
|
2021-04-30 06:53:25 +02:00
|
|
|
import Foundation
|
2021-04-29 09:51:52 +02:00
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension APIService {
|
|
|
|
func getDomainblocks(
|
|
|
|
domain: String,
|
|
|
|
limit: Int = onceRequestDomainBlocksMaxCount,
|
|
|
|
authorizationBox: AuthenticationService.MastodonAuthenticationBox
|
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<[String]>, Error> {
|
|
|
|
let authorization = authorizationBox.userAuthorization
|
|
|
|
|
|
|
|
let query = Mastodon.API.DomainBlock.Query(
|
|
|
|
maxID: nil, sinceID: nil, limit: limit
|
|
|
|
)
|
|
|
|
return Mastodon.API.DomainBlock.getDomainblocks(
|
|
|
|
domain: domain,
|
|
|
|
session: session,
|
|
|
|
authorization: authorization,
|
|
|
|
query: query
|
|
|
|
)
|
|
|
|
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[String]>, Error> in
|
2021-04-30 06:53:25 +02:00
|
|
|
self.backgroundManagedObjectContext.performChanges {
|
2021-04-30 08:55:02 +02:00
|
|
|
let blockedDomains: [DomainBlock] = {
|
|
|
|
let request = DomainBlock.sortedFetchRequest
|
|
|
|
request.predicate = DomainBlock.predicate(domain: authorizationBox.domain, userID: authorizationBox.userID)
|
|
|
|
request.returnsObjectsAsFaults = false
|
|
|
|
do {
|
|
|
|
return try self.backgroundManagedObjectContext.fetch(request)
|
|
|
|
} catch {
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
blockedDomains.forEach { self.backgroundManagedObjectContext.delete($0) }
|
|
|
|
|
2021-04-29 09:51:52 +02:00
|
|
|
response.value.forEach { domain in
|
|
|
|
// use constrain to avoid repeated save
|
2021-04-30 06:53:25 +02:00
|
|
|
_ = DomainBlock.insert(
|
2021-04-29 09:51:52 +02:00
|
|
|
into: self.backgroundManagedObjectContext,
|
|
|
|
blockedDomain: domain,
|
|
|
|
domain: authorizationBox.domain,
|
|
|
|
userID: authorizationBox.userID
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.tryMap { result -> Mastodon.Response.Content<[String]> in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
return response
|
|
|
|
case .failure(let error):
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func blockDomain(
|
2021-04-30 06:53:25 +02:00
|
|
|
user: MastodonUser,
|
2021-04-29 09:51:52 +02:00
|
|
|
authorizationBox: AuthenticationService.MastodonAuthenticationBox
|
2021-04-30 06:53:25 +02:00
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Empty>, Error> {
|
2021-04-29 09:51:52 +02:00
|
|
|
let authorization = authorizationBox.userAuthorization
|
|
|
|
|
|
|
|
return Mastodon.API.DomainBlock.blockDomain(
|
|
|
|
domain: authorizationBox.domain,
|
2021-04-30 06:53:25 +02:00
|
|
|
blockDomain: user.domainFromAcct,
|
2021-04-29 09:51:52 +02:00
|
|
|
session: session,
|
|
|
|
authorization: authorization
|
|
|
|
)
|
2021-04-30 06:53:25 +02:00
|
|
|
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Empty>, Error> in
|
|
|
|
self.backgroundManagedObjectContext.performChanges {
|
|
|
|
let requestMastodonUserRequest = MastodonUser.sortedFetchRequest
|
|
|
|
requestMastodonUserRequest.predicate = MastodonUser.predicate(domain: authorizationBox.domain, id: authorizationBox.userID)
|
|
|
|
requestMastodonUserRequest.fetchLimit = 1
|
|
|
|
guard let requestMastodonUser = self.backgroundManagedObjectContext.safeFetch(requestMastodonUserRequest).first else { return }
|
|
|
|
user.update(isDomainBlocking: true, by: requestMastodonUser)
|
2021-04-29 09:51:52 +02:00
|
|
|
}
|
|
|
|
.setFailureType(to: Error.self)
|
2021-04-30 06:53:25 +02:00
|
|
|
.tryMap { result -> Mastodon.Response.Content<Mastodon.Entity.Empty> in
|
2021-04-29 09:51:52 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
return response
|
|
|
|
case .failure(let error):
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func unblockDomain(
|
2021-04-30 06:53:25 +02:00
|
|
|
user: MastodonUser,
|
2021-04-29 09:51:52 +02:00
|
|
|
authorizationBox: AuthenticationService.MastodonAuthenticationBox
|
2021-04-30 06:53:25 +02:00
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Empty>, Error> {
|
2021-04-29 09:51:52 +02:00
|
|
|
let authorization = authorizationBox.userAuthorization
|
|
|
|
|
|
|
|
return Mastodon.API.DomainBlock.unblockDomain(
|
|
|
|
domain: authorizationBox.domain,
|
2021-04-30 06:53:25 +02:00
|
|
|
blockDomain: user.domainFromAcct,
|
2021-04-29 09:51:52 +02:00
|
|
|
session: session,
|
|
|
|
authorization: authorization
|
|
|
|
)
|
2021-04-30 06:53:25 +02:00
|
|
|
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Empty>, Error> in
|
|
|
|
self.backgroundManagedObjectContext.performChanges {
|
|
|
|
let requestMastodonUserRequest = MastodonUser.sortedFetchRequest
|
|
|
|
requestMastodonUserRequest.predicate = MastodonUser.predicate(domain: authorizationBox.domain, id: authorizationBox.userID)
|
|
|
|
requestMastodonUserRequest.fetchLimit = 1
|
|
|
|
guard let requestMastodonUser = self.backgroundManagedObjectContext.safeFetch(requestMastodonUserRequest).first else { return }
|
|
|
|
user.update(isDomainBlocking: false, by: requestMastodonUser)
|
2021-04-29 09:51:52 +02:00
|
|
|
}
|
|
|
|
.setFailureType(to: Error.self)
|
2021-04-30 06:53:25 +02:00
|
|
|
.tryMap { result -> Mastodon.Response.Content<Mastodon.Entity.Empty> in
|
2021-04-29 09:51:52 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
return response
|
|
|
|
case .failure(let error):
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|