mastodon-ios/Mastodon/Service/APIService/APIService+HomeTimeline.swift

67 lines
2.0 KiB
Swift
Raw Normal View History

2021-02-03 11:52:47 +01:00
//
// APIService+HomeTimeline.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021/2/3.
//
import Foundation
import Combine
import CoreData
import CoreDataStack
import CommonOSLog
2021-02-03 11:52:47 +01:00
import DateToolsSwift
import MastodonSDK
extension APIService {
func homeTimeline(
domain: String,
sinceID: Mastodon.Entity.Status.ID? = nil,
maxID: Mastodon.Entity.Status.ID? = nil,
limit: Int = onceRequestTootMaxCount,
local: Bool? = nil,
2021-02-03 11:52:47 +01:00
authorizationBox: AuthenticationService.MastodonAuthenticationBox
2021-02-05 10:53:00 +01:00
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, Error> {
2021-02-03 11:52:47 +01:00
let authorization = authorizationBox.userAuthorization
let requestMastodonUserID = authorizationBox.userID
2021-02-03 11:52:47 +01:00
let query = Mastodon.API.Timeline.HomeTimelineQuery(
maxID: maxID,
sinceID: sinceID,
minID: nil, // prefer sinceID
limit: limit,
local: local
2021-02-03 11:52:47 +01:00
)
return Mastodon.API.Timeline.home(
session: session,
domain: domain,
query: query,
authorization: authorization
)
2021-02-05 10:53:00 +01:00
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, Error> in
2021-02-03 11:52:47 +01:00
return APIService.Persist.persistTimeline(
managedObjectContext: self.backgroundManagedObjectContext,
domain: domain,
query: query,
2021-02-03 11:52:47 +01:00
response: response,
persistType: .home,
requestMastodonUserID: requestMastodonUserID,
log: OSLog.api
2021-02-03 11:52:47 +01:00
)
.setFailureType(to: Error.self)
2021-02-05 10:53:00 +01:00
.tryMap { result -> Mastodon.Response.Content<[Mastodon.Entity.Status]> in
2021-02-03 11:52:47 +01:00
switch result {
case .success:
return response
case .failure(let error):
throw error
}
}
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
}