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

50 lines
1.7 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(
domain: String,
2021-04-20 09:40:10 +02:00
query: Mastodon.API.V2.Search.Query,
mastodonAuthenticationBox: MastodonAuthenticationBox
2021-03-31 13:29:54 +02:00
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.SearchResult>, Error> {
let authorization = mastodonAuthenticationBox.userAuthorization
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)
.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
}
}