2021-03-31 13:29:54 +02:00
|
|
|
//
|
|
|
|
// APIService+Recommend.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/3/31.
|
|
|
|
//
|
|
|
|
|
2021-04-09 07:09:30 +02:00
|
|
|
import Combine
|
2021-03-31 13:29:54 +02:00
|
|
|
import Foundation
|
|
|
|
import MastodonSDK
|
2021-04-09 07:09:30 +02:00
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import OSLog
|
2021-03-31 13:29:54 +02:00
|
|
|
|
|
|
|
extension APIService {
|
2021-04-21 08:46:31 +02:00
|
|
|
func suggestionAccount(
|
|
|
|
query: Mastodon.API.Suggestions.Query?,
|
2022-02-10 12:30:41 +01:00
|
|
|
authenticationBox: MastodonAuthenticationBox
|
|
|
|
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Account]> {
|
|
|
|
|
|
|
|
let response = try await Mastodon.API.Suggestions.get(
|
|
|
|
session: session,
|
|
|
|
domain: authenticationBox.domain,
|
|
|
|
query: query,
|
|
|
|
authorization: authenticationBox.userAuthorization
|
|
|
|
).singleOutput()
|
|
|
|
|
|
|
|
let managedObjectContext = backgroundManagedObjectContext
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
for entity in response.value {
|
|
|
|
_ = Persistence.MastodonUser.createOrMerge(
|
|
|
|
in: managedObjectContext,
|
|
|
|
context: Persistence.MastodonUser.PersistContext(
|
|
|
|
domain: authenticationBox.domain,
|
|
|
|
entity: entity,
|
|
|
|
cache: nil,
|
|
|
|
networkDate: response.networkDate
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} // end for … in
|
|
|
|
}
|
|
|
|
|
|
|
|
return response
|
2021-04-21 08:46:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func suggestionAccountV2(
|
2021-03-31 14:56:11 +02:00
|
|
|
query: Mastodon.API.Suggestions.Query?,
|
2022-02-10 12:30:41 +01:00
|
|
|
authenticationBox: MastodonAuthenticationBox
|
|
|
|
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.V2.SuggestionAccount]> {
|
|
|
|
let response = try await Mastodon.API.V2.Suggestions.get(
|
|
|
|
session: session,
|
|
|
|
domain: authenticationBox.domain,
|
|
|
|
query: query,
|
|
|
|
authorization: authenticationBox.userAuthorization
|
|
|
|
).singleOutput()
|
|
|
|
|
|
|
|
let managedObjectContext = backgroundManagedObjectContext
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
for entity in response.value {
|
|
|
|
_ = Persistence.MastodonUser.createOrMerge(
|
|
|
|
in: managedObjectContext,
|
|
|
|
context: Persistence.MastodonUser.PersistContext(
|
|
|
|
domain: authenticationBox.domain,
|
|
|
|
entity: entity.account,
|
|
|
|
cache: nil,
|
|
|
|
networkDate: response.networkDate
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} // end for … in
|
|
|
|
}
|
|
|
|
|
|
|
|
return response
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|