Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Service/APIService/APIService+Recommend.swift

77 lines
2.4 KiB
Swift
Raw Normal View History

2021-03-31 13:29:54 +02:00
//
// APIService+Recommend.swift
// Mastodon
//
// Created by sxiaojian on 2021/3/31.
//
import Combine
2021-03-31 13:29:54 +02:00
import Foundation
import MastodonSDK
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?,
authenticationBox: MastodonAuthenticationBox
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Account]> {
let response = try await Mastodon.API.Suggestions.accounts(
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(
query: Mastodon.API.Suggestions.Query?,
authenticationBox: MastodonAuthenticationBox
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.V2.SuggestionAccount]> {
let response = try await Mastodon.API.V2.Suggestions.accounts(
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
}
2021-03-31 13:29:54 +02:00
}