Skip to content

Commit 275714c

Browse files
authored
Paginate per batched query (#96)
* add api client with grpc kotlin * take a pagination object so you can paginate per request * add more complex test for it * fix linter issue
1 parent a12829b commit 275714c

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

library/src/androidTest/java/org/xmtp/android/library/ConversationTest.kt

+33-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.xmtp.android.library.messages.MessageBuilder
2020
import org.xmtp.android.library.messages.MessageHeaderV2Builder
2121
import org.xmtp.android.library.messages.MessageV1Builder
2222
import org.xmtp.android.library.messages.MessageV2Builder
23+
import org.xmtp.android.library.messages.Pagination
2324
import org.xmtp.android.library.messages.PrivateKey
2425
import org.xmtp.android.library.messages.PrivateKeyBuilder
2526
import org.xmtp.android.library.messages.SealedInvitationBuilder
@@ -419,27 +420,48 @@ class ConversationTest {
419420

420421
@Test
421422
fun testListBatchMessages() {
422-
val bobConversation = bobClient.conversations.newConversation(
423-
alice.walletAddress,
424-
context = InvitationV1ContextBuilder.buildFromConversation("hi")
425-
)
423+
val bobConversation = aliceClient.conversations.newConversation(bob.walletAddress)
424+
val steveConversation = aliceClient.conversations.newConversation(fixtures.steve.walletAddress)
426425

427-
val aliceConversation = aliceClient.conversations.newConversation(
428-
bob.walletAddress,
429-
context = InvitationV1ContextBuilder.buildFromConversation("hi")
430-
)
431426
bobConversation.send(text = "hey alice 1")
432427
bobConversation.send(text = "hey alice 2")
433-
bobConversation.send(text = "hey alice 3")
428+
steveConversation.send(text = "hey alice 3")
434429
val messages = aliceClient.conversations.listBatchMessages(
435430
listOf(
436-
aliceConversation.topic,
437-
bobConversation.topic
431+
Pair(steveConversation.topic, null),
432+
Pair(bobConversation.topic, null)
438433
)
439434
)
440435
assertEquals(3, messages.size)
441436
}
442437

438+
@Test
439+
fun testListBatchMessagesWithPagination() {
440+
val bobConversation = aliceClient.conversations.newConversation(bob.walletAddress)
441+
val steveConversation =
442+
aliceClient.conversations.newConversation(fixtures.steve.walletAddress)
443+
444+
bobConversation.send(text = "hey alice 1 bob")
445+
steveConversation.send(text = "hey alice 1 steve")
446+
447+
Thread.sleep(100)
448+
val date = Date()
449+
450+
bobConversation.send(text = "hey alice 2 bob")
451+
bobConversation.send(text = "hey alice 3 bob")
452+
steveConversation.send(text = "hey alice 2 steve")
453+
steveConversation.send(text = "hey alice 3 steve")
454+
455+
val messages = aliceClient.conversations.listBatchMessages(
456+
listOf(
457+
Pair(steveConversation.topic, Pagination(after = date)),
458+
Pair(bobConversation.topic, Pagination(after = date))
459+
)
460+
)
461+
462+
assertEquals(4, messages.size)
463+
}
464+
443465
@Test
444466
fun testImportV1ConversationFromJS() {
445467
val jsExportJSONData =

library/src/androidTest/java/org/xmtp/android/library/TestHelpers.kt

+18-6
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ class FakeApiClient : ApiClient {
117117
}
118118

119119
override suspend fun batchQuery(requests: List<MessageApiOuterClass.QueryRequest>): MessageApiOuterClass.BatchQueryResponse {
120-
val response = query(requests.first().getContentTopics(0))
120+
val responses = requests.map {
121+
query(it.getContentTopics(0), Pagination(after = Date(it.startTimeNs)))
122+
}
121123

122124
return MessageApiOuterClass.BatchQueryResponse.newBuilder().also {
123-
it.addResponses(response)
125+
it.addAllResponses(responses)
124126
}.build()
125127
}
126128

@@ -142,12 +144,14 @@ class FakeApiClient : ApiClient {
142144

143145
val startAt = pagination?.before
144146
if (startAt != null) {
145-
result = result.filter { it.timestampNs < startAt.time * 1_000_000 }
147+
result = result.filter { it.timestampNs < startAt.time }
146148
.sortedBy { it.timestampNs }.toMutableList()
147149
}
148150
val endAt = pagination?.after
149151
if (endAt != null) {
150-
result = result.filter { it.timestampNs > endAt.time * 1_000_000 }
152+
result = result.filter {
153+
it.timestampNs > endAt.time
154+
}
151155
.sortedBy { it.timestampNs }.toMutableList()
152156
}
153157
val limit = pagination?.limit
@@ -187,7 +191,11 @@ class FakeApiClient : ApiClient {
187191
}
188192
}
189193

190-
data class Fixtures(val aliceAccount: PrivateKeyBuilder, val bobAccount: PrivateKeyBuilder, val steveAccount: PrivateKeyBuilder) {
194+
data class Fixtures(
195+
val aliceAccount: PrivateKeyBuilder,
196+
val bobAccount: PrivateKeyBuilder,
197+
val steveAccount: PrivateKeyBuilder,
198+
) {
191199
var fakeApiClient: FakeApiClient = FakeApiClient()
192200
var alice: PrivateKey = aliceAccount.getPrivateKey()
193201
var aliceClient: Client = Client().create(account = aliceAccount, apiClient = fakeApiClient)
@@ -196,7 +204,11 @@ data class Fixtures(val aliceAccount: PrivateKeyBuilder, val bobAccount: Private
196204
var steve: PrivateKey = steveAccount.getPrivateKey()
197205
var steveClient: Client = Client().create(account = steveAccount, apiClient = fakeApiClient)
198206

199-
constructor() : this(aliceAccount = PrivateKeyBuilder(), bobAccount = PrivateKeyBuilder(), steveAccount = PrivateKeyBuilder())
207+
constructor() : this(
208+
aliceAccount = PrivateKeyBuilder(),
209+
bobAccount = PrivateKeyBuilder(),
210+
steveAccount = PrivateKeyBuilder()
211+
)
200212

201213
fun publishLegacyContact(client: Client) {
202214
val contactBundle = ContactBundle.newBuilder().also { builder ->

library/src/main/java/org/xmtp/android/library/Conversations.kt

+3-7
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,10 @@ data class Conversations(
285285
}
286286

287287
fun listBatchMessages(
288-
topics: List<String>,
289-
limit: Int? = null,
290-
before: Date? = null,
291-
after: Date? = null,
288+
topics: List<Pair<String, Pagination?>>,
292289
): List<DecodedMessage> {
293-
val pagination = Pagination(limit = limit, before = before, after = after)
294-
val requests = topics.map { topic ->
295-
makeQueryRequest(topic = topic, pagination = pagination)
290+
val requests = topics.map { (topic, page) ->
291+
makeQueryRequest(topic = topic, pagination = page)
296292
}
297293

298294
// The maximum number of requests permitted in a single batch call.

0 commit comments

Comments
 (0)