Skip to content

Commit

Permalink
Fix inbox endpoint pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
sbertix committed Jan 6, 2021
1 parent 51de596 commit 6b1569a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
14 changes: 10 additions & 4 deletions Sources/Swiftagram/Endpoints/EndpointDirect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Conversation.Collection> {
/// - 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<Conversation.Collection> {
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)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Swiftagram/Extensions/Requestable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
6 changes: 6 additions & 0 deletions Sources/Swiftagram/Models/Specialized/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6b1569a

Please sign in to comment.