From 09c79ae9fd59cba19093960c1a8c88559c606be5 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 13 Sep 2023 16:21:47 -0700 Subject: [PATCH] Add direction to pagination (#155) * fix: add direction to the pagination of messages * add a test for it * get the test passing --- Sources/XMTP/ApiClient.swift | 4 ++-- Sources/XMTP/Conversation.swift | 6 +++--- Sources/XMTP/ConversationV1.swift | 4 ++-- Sources/XMTP/ConversationV2.swift | 4 ++-- Sources/XMTP/Messages/PagingInfo.swift | 7 ++++--- Sources/XMTPTestHelpers/TestHelpers.swift | 9 +++++++++ Tests/XMTPTests/IntegrationTests.swift | 6 ++++++ XMTP.podspec | 2 +- 8 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Sources/XMTP/ApiClient.swift b/Sources/XMTP/ApiClient.swift index 5c9ef31d..708a4393 100644 --- a/Sources/XMTP/ApiClient.swift +++ b/Sources/XMTP/ApiClient.swift @@ -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 diff --git a/Sources/XMTP/Conversation.swift b/Sources/XMTP/Conversation.swift index 32924adf..5d922af7 100644 --- a/Sources/XMTP/Conversation.swift +++ b/Sources/XMTP/Conversation.swift @@ -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) } } diff --git a/Sources/XMTP/ConversationV1.swift b/Sources/XMTP/ConversationV1.swift index d30d766b..f35ba789 100644 --- a/Sources/XMTP/ConversationV1.swift +++ b/Sources/XMTP/ConversationV1.swift @@ -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, diff --git a/Sources/XMTP/ConversationV2.swift b/Sources/XMTP/ConversationV2.swift index eba42062..2d20bb7d 100644 --- a/Sources/XMTP/ConversationV2.swift +++ b/Sources/XMTP/ConversationV2.swift @@ -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 diff --git a/Sources/XMTP/Messages/PagingInfo.swift b/Sources/XMTP/Messages/PagingInfo.swift index 2fbd6c30..9a19ec1d 100644 --- a/Sources/XMTP/Messages/PagingInfo.swift +++ b/Sources/XMTP/Messages/PagingInfo.swift @@ -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 { diff --git a/Sources/XMTPTestHelpers/TestHelpers.swift b/Sources/XMTPTestHelpers/TestHelpers.swift index d0f568af..eb67dac6 100644 --- a/Sources/XMTPTestHelpers/TestHelpers.swift +++ b/Sources/XMTPTestHelpers/TestHelpers.swift @@ -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 diff --git a/Tests/XMTPTests/IntegrationTests.swift b/Tests/XMTPTests/IntegrationTests.swift index bdc84b71..be93d24f 100644 --- a/Tests/XMTPTests/IntegrationTests.swift +++ b/Tests/XMTPTests/IntegrationTests.swift @@ -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 { diff --git a/XMTP.podspec b/XMTP.podspec index 0255b1e2..613842e6 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -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.