2021-11-01 12:54:07 +01:00
|
|
|
//
|
|
|
|
// APIService+Follower.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-11-1.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import CommonOSLog
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension APIService {
|
|
|
|
|
|
|
|
func followers(
|
|
|
|
userID: Mastodon.Entity.Account.ID,
|
|
|
|
maxID: String?,
|
2022-01-27 14:23:39 +01:00
|
|
|
authenticationBox: MastodonAuthenticationBox
|
|
|
|
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Account]> {
|
|
|
|
let domain = authenticationBox.domain
|
|
|
|
let authorization = authenticationBox.userAuthorization
|
2021-11-01 12:54:07 +01:00
|
|
|
|
2021-11-02 09:01:22 +01:00
|
|
|
let query = Mastodon.API.Account.FollowerQuery(
|
|
|
|
maxID: maxID,
|
|
|
|
limit: nil
|
|
|
|
)
|
2022-01-27 14:23:39 +01:00
|
|
|
let response = try await Mastodon.API.Account.followers(
|
2021-11-01 12:54:07 +01:00
|
|
|
session: session,
|
|
|
|
domain: domain,
|
|
|
|
userID: userID,
|
2021-11-02 09:01:22 +01:00
|
|
|
query: query,
|
2021-11-01 12:54:07 +01:00
|
|
|
authorization: authorization
|
2022-01-27 14:23:39 +01:00
|
|
|
).singleOutput()
|
|
|
|
|
|
|
|
let managedObjectContext = self.backgroundManagedObjectContext
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
let me = authenticationBox.authenticationRecord.object(in: managedObjectContext)?.user
|
|
|
|
|
|
|
|
for entity in response.value {
|
|
|
|
let result = Persistence.MastodonUser.createOrMerge(
|
|
|
|
in: managedObjectContext,
|
|
|
|
context: Persistence.MastodonUser.PersistContext(
|
|
|
|
domain: domain,
|
2021-11-01 12:54:07 +01:00
|
|
|
entity: entity,
|
2022-01-27 14:23:39 +01:00
|
|
|
cache: nil,
|
|
|
|
networkDate: response.networkDate
|
2021-11-01 12:54:07 +01:00
|
|
|
)
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
let user = result.user
|
|
|
|
me?.update(isFollowing: true, by: user)
|
2021-11-01 12:54:07 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
return response
|
2021-11-01 12:54:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|