Skip to content

Commit

Permalink
Add direction to pagination (#155)
Browse files Browse the repository at this point in the history
* fix: add direction to the pagination of messages

* add a test for it

* get the test passing
  • Loading branch information
nplasterer authored Sep 13, 2023
1 parent 7ec1338 commit 09c79ae
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Sources/XMTP/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func makeQueryRequest(topic: String, pagination: Pagination? = nil, cursor: Curs
}
if let endAt = pagination?.before {
$0.endTimeNs = UInt64(endAt.millisecondsSinceEpoch) * 1_000_000
$0.pagingInfo.direction = .descending
$0.pagingInfo.direction = pagination?.direction ?? .descending
}
if let startAt = pagination?.after {
$0.startTimeNs = UInt64(startAt.millisecondsSinceEpoch) * 1_000_000
$0.pagingInfo.direction = .descending
$0.pagingInfo.direction = pagination?.direction ?? .descending
}
if let cursor {
$0.pagingInfo.cursor = cursor
Expand Down
6 changes: 3 additions & 3 deletions Sources/XMTP/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ public enum Conversation: Sendable {
}

/// List messages in the conversation
public func messages(limit: Int? = nil, before: Date? = nil, after: Date? = nil) async throws -> [DecodedMessage] {
public func messages(limit: Int? = nil, before: Date? = nil, after: Date? = nil, direction: PagingInfoSortDirection? = .descending) async throws -> [DecodedMessage] {
switch self {
case let .v1(conversationV1):
return try await conversationV1.messages(limit: limit, before: before, after: after)
return try await conversationV1.messages(limit: limit, before: before, after: after, direction: direction)
case let .v2(conversationV2):
return try await conversationV2.messages(limit: limit, before: before, after: after)
return try await conversationV2.messages(limit: limit, before: before, after: after, direction: direction)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTP/ConversationV1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ public struct ConversationV1 {
}
}

func messages(limit: Int? = nil, before: Date? = nil, after: Date? = nil) async throws -> [DecodedMessage] {
let pagination = Pagination(limit: limit, before: before, after: after)
func messages(limit: Int? = nil, before: Date? = nil, after: Date? = nil, direction: PagingInfoSortDirection? = .descending) async throws -> [DecodedMessage] {
let pagination = Pagination(limit: limit, before: before, after: after, direction: direction)

let envelopes = try await client.apiClient.envelopes(
topic: Topic.directMessageV1(client.address, peerAddress).description,
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTP/ConversationV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public struct ConversationV2 {
return try await prepareMessage(encodedContent: encoded, options: options)
}

func messages(limit: Int? = nil, before: Date? = nil, after: Date? = nil) async throws -> [DecodedMessage] {
let pagination = Pagination(limit: limit, before: before, after: after)
func messages(limit: Int? = nil, before: Date? = nil, after: Date? = nil, direction: PagingInfoSortDirection? = .descending) async throws -> [DecodedMessage] {
let pagination = Pagination(limit: limit, before: before, after: after, direction: direction)
let envelopes = try await client.apiClient.envelopes(topic: topic.description, pagination: pagination)

return envelopes.compactMap { envelope in
Expand Down
7 changes: 4 additions & 3 deletions Sources/XMTP/Messages/PagingInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import Foundation

typealias PagingInfo = Xmtp_MessageApi_V1_PagingInfo
typealias PagingInfoCursor = Xmtp_MessageApi_V1_Cursor
typealias PagingInfoSortDirection = Xmtp_MessageApi_V1_SortDirection
public typealias PagingInfoSortDirection = Xmtp_MessageApi_V1_SortDirection

public struct Pagination {
public var limit: Int?
public var before: Date?
public var after: Date?
var direction: PagingInfoSortDirection?
public var direction: PagingInfoSortDirection?

public init(limit: Int? = nil, before: Date? = nil, after: Date? = nil) {
public init(limit: Int? = nil, before: Date? = nil, after: Date? = nil, direction: PagingInfoSortDirection? = .descending) {
self.limit = limit
self.before = before
self.after = after
self.direction = direction
}

var pagingInfo: PagingInfo {
Expand Down
9 changes: 9 additions & 0 deletions Sources/XMTPTestHelpers/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ public class FakeApiClient: ApiClient {
}
}
}

if let direction = pagination?.direction {
switch direction {
case .ascending:
result = Array(result.reversed())
default:
break
}
}

var queryResponse = QueryResponse()
queryResponse.envelopes = result
Expand Down
6 changes: 6 additions & 0 deletions Tests/XMTPTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ final class IntegrationTests: XCTestCase {
XCTAssertEqual(1, messages3.count)
let nowMessage2 = messages3[0]
XCTAssertEqual("now", nowMessage2.body)

let messagesAsc = try await convo.messages(direction: .ascending)
XCTAssertEqual("10 seconds ago", messagesAsc[0].body)

let messagesDesc = try await convo.messages(direction: .descending)
XCTAssertEqual("now", messagesDesc[0].body)
}

func testStreamingMessagesShouldBeReceived() async throws {
Expand Down
2 changes: 1 addition & 1 deletion XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "XMTP"
spec.version = "0.5.3-alpha0"
spec.version = "0.5.4-alpha0"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down

0 comments on commit 09c79ae

Please sign in to comment.