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

65 lines
2.1 KiB
Swift
Raw Normal View History

2021-03-31 13:29:54 +02:00
//
// APIService+Search.swift
// Mastodon
//
// Created by sxiaojian on 2021/3/31.
//
import Foundation
import Combine
import MastodonSDK
import CommonOSLog
2021-03-31 13:29:54 +02:00
extension APIService {
func search(
2021-04-20 09:40:10 +02:00
query: Mastodon.API.V2.Search.Query,
authenticationBox: MastodonAuthenticationBox
) async throws -> Mastodon.Response.Content<Mastodon.Entity.SearchResult> {
let domain = authenticationBox.domain
let authorization = authenticationBox.userAuthorization
2021-03-31 13:29:54 +02:00
let response = try await Mastodon.API.V2.Search.search(
session: session,
domain: domain,
query: query,
authorization: authorization
).singleOutput()
let managedObjectContext = self.backgroundManagedObjectContext
try await managedObjectContext.performChanges {
let me = authenticationBox.authenticationRecord.object(in: managedObjectContext)?.user
// user
for entity in response.value.accounts {
_ = Persistence.MastodonUser.createOrMerge(
in: managedObjectContext,
context: Persistence.MastodonUser.PersistContext(
domain: domain,
entity: entity,
cache: nil,
networkDate: response.networkDate
)
)
}
// statuses
for entity in response.value.statuses {
_ = Persistence.Status.createOrMerge(
in: managedObjectContext,
context: Persistence.Status.PersistContext(
domain: domain,
entity: entity,
me: me,
statusCache: nil,
userCache: nil,
networkDate: response.networkDate
)
)
}
} // ent try await managedObjectContext.performChanges { }
return response
2021-03-31 13:29:54 +02:00
}
}