2021-04-01 08:39:15 +02:00
|
|
|
//
|
|
|
|
// APIService+Relationship.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-1.
|
|
|
|
//
|
|
|
|
|
2021-04-02 12:13:45 +02:00
|
|
|
import UIKit
|
2021-04-01 08:39:15 +02:00
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import CommonOSLog
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension APIService {
|
|
|
|
|
|
|
|
func relationship(
|
2022-01-27 14:23:39 +01:00
|
|
|
records: [ManagedObjectRecord<MastodonUser>],
|
|
|
|
authenticationBox: MastodonAuthenticationBox
|
|
|
|
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Relationship]> {
|
|
|
|
let managedObjectContext = backgroundManagedObjectContext
|
|
|
|
|
|
|
|
let _query: Mastodon.API.Account.RelationshipQuery? = try? await managedObjectContext.perform {
|
|
|
|
var ids: [MastodonUser.ID] = []
|
|
|
|
for record in records {
|
|
|
|
guard let user = record.object(in: managedObjectContext) else { continue }
|
|
|
|
guard user.id != authenticationBox.userID else { continue }
|
|
|
|
ids.append(user.id)
|
|
|
|
}
|
|
|
|
guard !ids.isEmpty else { return nil }
|
|
|
|
return Mastodon.API.Account.RelationshipQuery(ids: ids)
|
|
|
|
}
|
|
|
|
guard let query = _query else {
|
|
|
|
throw APIError.implicit(.badRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = try await Mastodon.API.Account.relationships(
|
2021-04-02 12:13:45 +02:00
|
|
|
session: session,
|
2022-01-27 14:23:39 +01:00
|
|
|
domain: authenticationBox.domain,
|
2021-04-02 12:13:45 +02:00
|
|
|
query: query,
|
2022-01-27 14:23:39 +01:00
|
|
|
authorization: authenticationBox.userAuthorization
|
|
|
|
).singleOutput()
|
|
|
|
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
guard let me = authenticationBox.authenticationRecord.object(in: managedObjectContext)?.user else {
|
2022-02-11 12:27:14 +01:00
|
|
|
// assertionFailure()
|
2022-01-27 14:23:39 +01:00
|
|
|
return
|
|
|
|
}
|
2021-04-02 12:13:45 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let relationships = response.value
|
|
|
|
for record in records {
|
|
|
|
guard let user = record.object(in: managedObjectContext) else { continue }
|
|
|
|
guard let relationship = relationships.first(where: { $0.id == user.id }) else { continue }
|
2021-04-02 12:13:45 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
Persistence.MastodonUser.update(
|
|
|
|
mastodonUser: user,
|
|
|
|
context: Persistence.MastodonUser.RelationshipContext(
|
|
|
|
entity: relationship,
|
|
|
|
me: me,
|
|
|
|
networkDate: response.networkDate
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} // end for in
|
2021-04-02 12:13:45 +02:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
return response
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|