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(
|
|
|
|
domain: String,
|
2021-04-20 09:40:10 +02:00
|
|
|
query: Mastodon.API.V2.Search.Query,
|
2021-07-20 10:40:04 +02:00
|
|
|
mastodonAuthenticationBox: MastodonAuthenticationBox
|
2021-03-31 13:29:54 +02:00
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.SearchResult>, Error> {
|
|
|
|
let authorization = mastodonAuthenticationBox.userAuthorization
|
2021-07-14 14:28:41 +02:00
|
|
|
let requestMastodonUserID = mastodonAuthenticationBox.userID
|
2021-03-31 13:29:54 +02:00
|
|
|
|
2021-04-20 09:40:10 +02:00
|
|
|
return Mastodon.API.V2.Search.search(session: session, domain: domain, query: query, authorization: authorization)
|
2021-07-14 14:28:41 +02:00
|
|
|
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.SearchResult>, Error> in
|
|
|
|
// persist status
|
|
|
|
let statusResponse = response.map { $0.statuses }
|
|
|
|
return APIService.Persist.persistStatus(
|
|
|
|
managedObjectContext: self.backgroundManagedObjectContext,
|
|
|
|
domain: domain,
|
|
|
|
query: nil,
|
|
|
|
response: statusResponse,
|
|
|
|
persistType: .lookUp,
|
|
|
|
requestMastodonUserID: requestMastodonUserID,
|
|
|
|
log: OSLog.api
|
|
|
|
)
|
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.tryMap { result -> Mastodon.Response.Content<Mastodon.Entity.SearchResult> in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
return response
|
|
|
|
case .failure(let error):
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2021-03-31 13:29:54 +02:00
|
|
|
}
|
|
|
|
}
|