Skip to content

Commit

Permalink
Bump consent pagination (#224)
Browse files Browse the repository at this point in the history
* get pagination working

* add logging to the envelopes

* update the logic for pagination

* remove testing code

* potential fix for flaky test
  • Loading branch information
nplasterer authored Apr 12, 2024
1 parent 66af542 commit fe5b6e0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ class ConversationTest {
val caroConversation =
bobClient.conversations.newConversation(fixtures.caro.walletAddress)
bobClient.contacts.refreshConsentList()
Thread.sleep(1000)
assertEquals(bobClient.contacts.consentList.entries.size, 2)
assertTrue(bobConversation.consentState() == ConsentState.ALLOWED)
assertTrue(caroConversation.consentState() == ConsentState.ALLOWED)
Expand Down
11 changes: 8 additions & 3 deletions library/src/main/java/org/xmtp/android/library/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,25 @@ data class GRPCApiClient(
* It yields all the envelopes in the query using the paging info
* from the prior response to fetch the next page.
*/
override suspend fun envelopes(topic: String, pagination: Pagination?): List<Envelope> {
override suspend fun envelopes(
topic: String,
pagination: Pagination?,
): List<Envelope> {
var envelopes: MutableList<Envelope> = mutableListOf()
var hasNextPage = true
var cursor: Cursor? = null
while (hasNextPage) {
val response = query(topic = topic, pagination = pagination, cursor = cursor)
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) {
if (pagination?.limit != null && pagination.limit <= 100 && envelopes.size >= pagination.limit) {
envelopes = envelopes.take(pagination.limit).toMutableList()
break
}
}

return envelopes
}

Expand Down
4 changes: 3 additions & 1 deletion library/src/main/java/org/xmtp/android/library/Contacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ class ConsentList(
Topic.preferenceList(identifier).description,
Pagination(
after = lastFetched,
direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING
direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING,
limit = 500
),
)

lastFetched = newDate
val preferences: MutableList<PrivatePreferencesAction> = mutableListOf()
for (envelope in envelopes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,13 @@ data class Pagination(
) {
val pagingInfo: PagingInfo
get() {
return PagingInfo.newBuilder().also {
if (limit != null) {
it.limit = limit
return PagingInfo.newBuilder().also { page ->
limit?.let {
page.limit = it
}
if (direction != null) {
it.direction = direction
page.direction = direction
}
}.build()
}
}

class PagingInfoBuilder {
companion object {
fun buildFromPagingInfo(
limit: Int? = null,
cursor: PagingInfoCursor? = null,
direction: PagingInfoSortDirection? = null,
): PagingInfo {
return PagingInfo.newBuilder().also {
if (limit != null) {
it.limit = limit
}
if (cursor != null) {
it.cursor = cursor
}
if (direction != null) {
it.direction = direction
}
}.build()
}
}
}

0 comments on commit fe5b6e0

Please sign in to comment.