feat: Implement deletion of records for blocked users

This commit is contained in:
Marcus Kida 2022-11-24 07:53:04 +01:00
parent ac5e68b74b
commit 65ed6650e8
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
4 changed files with 84 additions and 0 deletions

View File

@ -86,6 +86,8 @@ extension HomeTimelineViewModel.LoadLatestState {
await enter(state: Idle.self)
viewModel.homeTimelineNavigationBarTitleViewModel.receiveLoadingStateCompletion(.finished)
viewModel.context.instanceService.updateMutesAndBlocks()
// stop refresher if no new statuses
let statuses = response.value
let newStatuses = statuses.filter { !latestStatusIDs.contains($0.id) }

View File

@ -22,6 +22,36 @@ extension APIService {
let isFollowing: Bool
}
@discardableResult
public func getBlocked(
authenticationBox: MastodonAuthenticationBox
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Account]> {
let managedObjectContext = backgroundManagedObjectContext
let response = try await Mastodon.API.Account.blocks(
session: session,
domain: authenticationBox.domain,
authorization: authenticationBox.userAuthorization
).singleOutput()
let userIDs = response.value.map { $0.id }
let predicate = NSPredicate(format: "%K IN %@", #keyPath(MastodonUser.id), userIDs)
let fetchRequest = MastodonUser.fetchRequest()
fetchRequest.predicate = predicate
fetchRequest.includesPropertyValues = false
try await managedObjectContext.performChanges {
let accounts = try managedObjectContext.fetch(fetchRequest) as! [MastodonUser]
for account in accounts {
managedObjectContext.delete(account)
}
}
return response
}
public func toggleBlock(
user: ManagedObjectRecord<MastodonUser>,
authenticationBox: MastodonAuthenticationBox

View File

@ -110,6 +110,11 @@ public extension InstanceService {
try await apiService?.getMutes(
authenticationBox: authBox
)
try await apiService?.getBlocked(
authenticationBox: authBox
)
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [Instance] update mutes and blocks succeeded")
} catch {
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [Instance] update mutes and blocks failure: \(error.localizedDescription)")

View File

@ -215,6 +215,53 @@ extension Mastodon.API.Account {
}
public extension Mastodon.API.Account {
static func blocksEndpointURL(domain: String) -> URL {
return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("blocks")
}
/// Block
///
/// Block the given account. Clients should filter statuses from this account if received (e.g. due to a boost in the Home timeline).
///
/// - Since: 0.0.0
/// - Version: 3.3.0
/// # Last Update
/// 2021/4/1
/// # Reference
/// [Document](https://docs.joinmastodon.org/methods/blocks/)
/// - Parameters:
/// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com"
/// - authorization: User token.
/// - Returns: `AnyPublisher` contains `Relationship` nested in the response
static func blocks(
session: URLSession,
domain: String,
authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Account]>, Error> {
let request = Mastodon.API.get(
url: blocksEndpointURL(domain: domain),
query: BlocksQuery(),
authorization: authorization
)
return session.dataTaskPublisher(for: request)
.tryMap { data, response in
let value = try Mastodon.API.decode(type: [Mastodon.Entity.Account].self, from: data, response: response)
return Mastodon.Response.Content(value: value, response: response)
}
.eraseToAnyPublisher()
}
private struct BlocksQuery: GetQuery {
var queryItems: [URLQueryItem]? {
[URLQueryItem(name: "limit", value: "-1")]
}
}
}
extension Mastodon.API.Account {
static func blockEndpointURL(domain: String, accountID: Mastodon.Entity.Account.ID) -> URL {