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
|
2021-02-04 07:45:44 +01:00
|
|
|
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 = 100,
|
2021-02-04 07:45:44 +01:00
|
|
|
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
|
2021-02-04 07:45:44 +01:00
|
|
|
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,
|
2021-02-04 07:45:44 +01:00
|
|
|
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,
|
2021-02-04 07:45:44 +01:00
|
|
|
domain: domain,
|
|
|
|
query: query,
|
2021-02-03 11:52:47 +01:00
|
|
|
response: response,
|
2021-02-04 07:45:44 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|