Skip to content

Commit

Permalink
Respect limit in message pagination (#103)
Browse files Browse the repository at this point in the history
* add api client with grpc kotlin

* respect the limit for pagination

* fix the lint issue
  • Loading branch information
nplasterer authored Aug 3, 2023
1 parent e9dc790 commit 18f1788
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,17 @@ class LocalInstrumentedTest {
date.time = date.time - 5000
convo.send(text = "10 seconds ago", sentAt = date)
Thread.sleep(5000)
convo.send(text = "now first")
convo.send(text = "now")
val messages = convo.messages()
assertEquals(2, messages.size)
assertEquals(3, messages.size)
val messagesLimit = convo.messages(limit = 2)
assertEquals(2, messagesLimit.size)
val nowMessage = messages[0]
assertEquals("now", nowMessage.body)
val messages2 = convo.messages(before = nowMessage.sent)
val messages2 = convo.messages(limit = 1, before = nowMessage.sent)
val tenSecondsAgoMessage = messages2[0]
assertEquals("10 seconds ago", tenSecondsAgoMessage.body)
assertEquals("now first", tenSecondsAgoMessage.body)
val messages3 = convo.messages(after = tenSecondsAgoMessage.sent)
val nowMessage2 = messages3[0]
assertEquals("now", nowMessage2.body)
Expand Down
6 changes: 5 additions & 1 deletion library/src/main/java/org/xmtp/android/library/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ data class GRPCApiClient(
}

override suspend fun envelopes(topic: String, pagination: Pagination?): List<Envelope> {
val envelopes: MutableList<Envelope> = mutableListOf()
var envelopes: MutableList<Envelope> = mutableListOf()
var hasNextPage = true
var cursor: Cursor? = null
while (hasNextPage) {
val response = query(topic = topic, pagination = pagination, cursor = cursor)
envelopes.addAll(response.envelopesList)
cursor = response.pagingInfo.cursor
hasNextPage = response.envelopesList.isNotEmpty() && response.pagingInfo.hasCursor()
if (pagination?.limit != null && envelopes.size >= pagination.limit) {
envelopes = envelopes.take(pagination.limit).toMutableList()
break
}
}
return envelopes
}
Expand Down

0 comments on commit 18f1788

Please sign in to comment.