Fetch timeline 40 per page + 200 per batch

This commit is contained in:
Thomas Ricouard 2024-09-17 14:26:57 +02:00
parent 9ec6a4ef66
commit d2eca1d646
4 changed files with 32 additions and 17 deletions

View file

@ -16,7 +16,8 @@ func loadStatuses(for timeline: TimelineFilter,
var statuses: [Status] = try await client.get(endpoint: timeline.endpoint(sinceId: nil, var statuses: [Status] = try await client.get(endpoint: timeline.endpoint(sinceId: nil,
maxId: nil, maxId: nil,
minId: nil, minId: nil,
offset: nil)) offset: nil,
limit: 6))
statuses = statuses.filter { $0.reblog == nil && !$0.content.asRawText.isEmpty } statuses = statuses.filter { $0.reblog == nil && !$0.content.asRawText.isEmpty }
switch widgetFamily { switch widgetFamily {
case .systemSmall, .systemMedium: case .systemSmall, .systemMedium:

View file

@ -1,8 +1,8 @@
import Foundation import Foundation
public enum Timelines: Endpoint { public enum Timelines: Endpoint {
case pub(sinceId: String?, maxId: String?, minId: String?, local: Bool) case pub(sinceId: String?, maxId: String?, minId: String?, local: Bool, limit: Int?)
case home(sinceId: String?, maxId: String?, minId: String?) case home(sinceId: String?, maxId: String?, minId: String?, limit: Int?)
case list(listId: String, sinceId: String?, maxId: String?, minId: String?) case list(listId: String, sinceId: String?, maxId: String?, minId: String?)
case hashtag(tag: String, additional: [String]?, maxId: String?, minId: String?) case hashtag(tag: String, additional: [String]?, maxId: String?, minId: String?)
case link(url: URL, sinceId: String?, maxId: String?, minId: String?) case link(url: URL, sinceId: String?, maxId: String?, minId: String?)
@ -24,12 +24,19 @@ public enum Timelines: Endpoint {
public func queryItems() -> [URLQueryItem]? { public func queryItems() -> [URLQueryItem]? {
switch self { switch self {
case let .pub(sinceId, maxId, minId, local): case let .pub(sinceId, maxId, minId, local, limit):
var params = makePaginationParam(sinceId: sinceId, maxId: maxId, mindId: minId) ?? [] var params = makePaginationParam(sinceId: sinceId, maxId: maxId, mindId: minId) ?? []
params.append(.init(name: "local", value: local ? "true" : "false")) params.append(.init(name: "local", value: local ? "true" : "false"))
if let limit {
params.append(.init(name: "limit", value: String(limit)))
}
return params
case let .home(sinceId, maxId, mindId, limit):
var params = makePaginationParam(sinceId: sinceId, maxId: maxId, mindId: mindId) ?? []
if let limit {
params.append(.init(name: "limit", value: String(limit)))
}
return params return params
case let .home(sinceId, maxId, mindId):
return makePaginationParam(sinceId: sinceId, maxId: maxId, mindId: mindId)
case let .list(_, sinceId, maxId, mindId): case let .list(_, sinceId, maxId, mindId):
return makePaginationParam(sinceId: sinceId, maxId: maxId, mindId: mindId) return makePaginationParam(sinceId: sinceId, maxId: maxId, mindId: mindId)
case let .hashtag(_, additional, maxId, minId): case let .hashtag(_, additional, maxId, minId):

View file

@ -157,22 +157,26 @@ public enum TimelineFilter: Hashable, Equatable, Identifiable, Sendable {
} }
} }
public func endpoint(sinceId: String?, maxId: String?, minId: String?, offset: Int?) -> Endpoint { public func endpoint(sinceId: String?,
maxId: String?,
minId: String?,
offset: Int?,
limit: Int?) -> Endpoint {
switch self { switch self {
case .federated: return Timelines.pub(sinceId: sinceId, maxId: maxId, minId: minId, local: false) case .federated: return Timelines.pub(sinceId: sinceId, maxId: maxId, minId: minId, local: false, limit: limit)
case .local: return Timelines.pub(sinceId: sinceId, maxId: maxId, minId: minId, local: true) case .local: return Timelines.pub(sinceId: sinceId, maxId: maxId, minId: minId, local: true, limit: limit)
case let .remoteLocal(_, filter): case let .remoteLocal(_, filter):
switch filter { switch filter {
case .local: case .local:
return Timelines.pub(sinceId: sinceId, maxId: maxId, minId: minId, local: true) return Timelines.pub(sinceId: sinceId, maxId: maxId, minId: minId, local: true, limit: limit)
case .federated: case .federated:
return Timelines.pub(sinceId: sinceId, maxId: maxId, minId: minId, local: false) return Timelines.pub(sinceId: sinceId, maxId: maxId, minId: minId, local: false, limit: limit)
case .trending: case .trending:
return Trends.statuses(offset: offset) return Trends.statuses(offset: offset)
} }
case .latest: return Timelines.home(sinceId: nil, maxId: nil, minId: nil) case .latest: return Timelines.home(sinceId: nil, maxId: nil, minId: nil, limit: limit)
case .resume: return Timelines.home(sinceId: nil, maxId: nil, minId: nil) case .resume: return Timelines.home(sinceId: nil, maxId: nil, minId: nil, limit: limit)
case .home: return Timelines.home(sinceId: sinceId, maxId: maxId, minId: minId) case .home: return Timelines.home(sinceId: sinceId, maxId: maxId, minId: minId, limit: limit)
case .trending: return Trends.statuses(offset: offset) case .trending: return Trends.statuses(offset: offset)
case let .link(url, _): return Timelines.link(url: url, sinceId: sinceId, maxId: maxId, minId: minId) case let .link(url, _): return Timelines.link(url: url, sinceId: sinceId, maxId: maxId, minId: minId)
case let .list(list): return Timelines.list(listId: list.id, sinceId: sinceId, maxId: maxId, minId: minId) case let .list(list): return Timelines.list(listId: list.id, sinceId: sinceId, maxId: maxId, minId: minId)

View file

@ -25,7 +25,8 @@ struct TimelineStatusFetcher: TimelineStatusFetching {
return try await client.get(endpoint: timeline.endpoint(sinceId: nil, return try await client.get(endpoint: timeline.endpoint(sinceId: nil,
maxId: nil, maxId: nil,
minId: nil, minId: nil,
offset: 0)) offset: 0,
limit: 40))
} }
func fetchNewPages(client: Client?, timeline: TimelineFilter, minId: String, maxPages: Int) async throws -> [Status] { func fetchNewPages(client: Client?, timeline: TimelineFilter, minId: String, maxPages: Int) async throws -> [Status] {
@ -39,7 +40,8 @@ struct TimelineStatusFetcher: TimelineStatusFetching {
sinceId: nil, sinceId: nil,
maxId: nil, maxId: nil,
minId: latestMinId, minId: latestMinId,
offset: nil offset: nil,
limit: 40
)) ))
if newStatuses.isEmpty { break } if newStatuses.isEmpty { break }
@ -55,6 +57,7 @@ struct TimelineStatusFetcher: TimelineStatusFetching {
return try await client.get(endpoint: timeline.endpoint(sinceId: nil, return try await client.get(endpoint: timeline.endpoint(sinceId: nil,
maxId: lastId, maxId: lastId,
minId: nil, minId: nil,
offset: offset)) offset: offset,
limit: 40))
} }
} }