From 51de59606958343ceb224329a21994712be0643e Mon Sep 17 00:00:00 2001 From: sbertix Date: Wed, 6 Jan 2021 19:21:14 +0100 Subject: [PATCH 1/2] Deprecate `timeline` endpoint --- Sources/Swiftagram/Endpoints/EndpointMedia.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/Swiftagram/Endpoints/EndpointMedia.swift b/Sources/Swiftagram/Endpoints/EndpointMedia.swift index dc3d24c3..22983573 100644 --- a/Sources/Swiftagram/Endpoints/EndpointMedia.swift +++ b/Sources/Swiftagram/Endpoints/EndpointMedia.swift @@ -179,6 +179,7 @@ public extension Endpoint.Media { /// Timeline. /// /// - parameter page: An optional `String` holding reference to a valid cursor. Defaults to `nil`. + @available(*, deprecated, message: "visit https://github.com/sbertix/Swiftagram/discussions/128") public static func timeline(startingAt page: String? = nil) -> Endpoint.Paginated { Endpoint.version1.feed.appendingDefaultHeader() .appending(path: "timeline/") @@ -196,7 +197,7 @@ public extension Endpoint.Media { } case let response?: guard let nextMaxId = try? response.get().nextMaxId.string() else { return nil } - return $0.appending(query: ["max_id": nextMaxId, "reason": "pagination"]) + return try? $0.appending(query: ["max_id": nextMaxId]).appending(body: ["reason": "pagination"]) } } .locking(Secret.self) { From 6b1569a4f97614ab054354f9ad244ce2f8316c72 Mon Sep 17 00:00:00 2001 From: sbertix Date: Wed, 6 Jan 2021 19:21:51 +0100 Subject: [PATCH 2/2] Fix `inbox` endpoint pagination --- Sources/Swiftagram/Endpoints/EndpointDirect.swift | 14 ++++++++++---- Sources/Swiftagram/Extensions/Requestable.swift | 4 ++-- .../Models/Specialized/Conversation.swift | 6 ++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Sources/Swiftagram/Endpoints/EndpointDirect.swift b/Sources/Swiftagram/Endpoints/EndpointDirect.swift index 1c924e6f..ef9e510e 100644 --- a/Sources/Swiftagram/Endpoints/EndpointDirect.swift +++ b/Sources/Swiftagram/Endpoints/EndpointDirect.swift @@ -17,15 +17,21 @@ public extension Endpoint { /// All threads. /// - /// - parameter page: An optional `String` holding reference to a valid cursor. Defaults to `nil`. - public static func inbox(startingAt page: String? = nil) -> Paginated { + /// - parameters: + /// - page: An optional `String` holding reference to a valid cursor. Defaults to `nil`. + /// - rank: A valid `Int` making sure users are paginated consistently. Defaults to a random `Int` between `1_000` and `10_000`. + public static func inbox(startingAt page: String? = nil, rank: Int = Int.random(in: 1_000..<10_000)) -> Paginated { base.inbox .appending(query: ["visual_message_return_type": "unseen", "direction": page.flatMap { _ in "older" }, "thread_message_limit": "10", "persistent_badging": "true", - "limit": "20"]) - .paginating(process: Conversation.Collection.self, key: "cursor", keyPath: \.oldestCursor, value: page) + "limit": "20", + "seq_id": String(rank)]) + .paginating(process: Conversation.Collection.self, + key: "cursor", + keyPath: \.inbox.oldestCursor, + value: page) .locking(Secret.self) } diff --git a/Sources/Swiftagram/Extensions/Requestable.swift b/Sources/Swiftagram/Extensions/Requestable.swift index 35f05917..ced1a76d 100644 --- a/Sources/Swiftagram/Extensions/Requestable.swift +++ b/Sources/Swiftagram/Extensions/Requestable.swift @@ -42,8 +42,8 @@ public extension Requestable where Self: QueryComposable & QueryParsable { self.appending(query: key, with: value) .prepare(process: Mapped.self) { request, response in guard let response = try? response?.get().wrapper() else { return request } - return (keyPath(response).string() ?? keyPath(response).int().flatMap(String.init)) - .flatMap { request.appending(query: key, with: $0) } + guard let nextCursor = keyPath(response).string(converting: true) else { return nil } + return request.appending(query: key, with: nextCursor) } } } diff --git a/Sources/Swiftagram/Models/Specialized/Conversation.swift b/Sources/Swiftagram/Models/Specialized/Conversation.swift index 61ddaa73..4b3474f1 100644 --- a/Sources/Swiftagram/Models/Specialized/Conversation.swift +++ b/Sources/Swiftagram/Models/Specialized/Conversation.swift @@ -92,6 +92,12 @@ public extension Conversation { /// The logged in user. public var viewer: User? { self["viewer"].optional().flatMap(User.init) } + /// The pagination parameters. + public var pagination: Pagination { + /// The current cursor is always `nil` for inboxes. + .init(current: nil, next: self["inbox"]["oldestCursor"].string()) + } + /// Init. /// - parameter wrapper: A valid `Wrapper`. public init(wrapper: @escaping () -> Wrapper) {