2021-03-31 13:29:54 +02:00
|
|
|
//
|
|
|
|
// APIService+Search.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/3/31.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
2021-07-14 14:28:41 +02:00
|
|
|
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,
|
2022-01-27 14:23:39 +01:00
|
|
|
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
|
|
|
|
2022-01-27 14:23:39 +01: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
|
|
|
|
)
|
2021-07-14 14:28:41 +02:00
|
|
|
)
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|