mastodon-ios/Mastodon/Service/APIService/APIService+PublicTimeline.s...

66 lines
1.9 KiB
Swift
Raw Normal View History

2021-01-28 09:10:30 +01:00
//
// APIService+PublicTimeline.swift
// Mastodon
//
// Created by sxiaojian on 2021/1/28.
//
import Foundation
import Combine
import CoreData
import CoreDataStack
import CommonOSLog
2021-01-28 09:10:30 +01:00
import DateToolsSwift
import MastodonSDK
extension APIService {
static let publicTimelineRequestWindowInSec: TimeInterval = 15 * 60
func publicTimeline(
2021-02-03 11:52:47 +01:00
domain: String,
sinceID: Mastodon.Entity.Status.ID? = nil,
maxID: Mastodon.Entity.Status.ID? = nil,
2021-04-01 08:39:15 +02:00
limit: Int = onceRequestStatusMaxCount
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 query = Mastodon.API.Timeline.PublicTimelineQuery(
local: nil,
remote: nil,
onlyMedia: nil,
maxID: maxID,
sinceID: sinceID,
minID: nil, // prefer sinceID
limit: limit
)
2021-01-28 09:10:30 +01:00
return Mastodon.API.Timeline.public(
session: session,
domain: domain,
2021-02-03 11:52:47 +01:00
query: query
2021-01-28 09:10:30 +01:00
)
2021-02-05 10:53:00 +01:00
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, Error> in
2021-03-17 04:33:25 +01:00
return APIService.Persist.persistStatus(
2021-01-28 09:10:30 +01:00
managedObjectContext: self.backgroundManagedObjectContext,
domain: domain,
query: query,
2021-01-28 09:10:30 +01:00
response: response,
persistType: .public,
requestMastodonUserID: nil,
log: OSLog.api
2021-01-28 09:10:30 +01:00
)
.setFailureType(to: Error.self)
2021-02-05 10:53:00 +01:00
.tryMap { result -> Mastodon.Response.Content<[Mastodon.Entity.Status]> in
2021-01-28 09:10:30 +01:00
switch result {
case .success:
return response
case .failure(let error):
throw error
}
}
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
2021-02-02 12:31:10 +01:00
2021-01-28 09:10:30 +01:00
}