Skip to content

Commit bcb2976

Browse files
authored
Add direction to pagination (#115)
* fix: allow the passing of direction to the pagination of messages * add tests for it * fix ci issue
1 parent 6edea60 commit bcb2976

File tree

10 files changed

+53
-16
lines changed

10 files changed

+53
-16
lines changed

dev/local/docker-compose.yml

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ services:
55
environment:
66
- GOWAKU-NODEKEY=8a30dcb604b0b53627a5adc054dbf434b446628d4bd1eccc681d223f0550ce67
77
command:
8-
- --ws
9-
- --store
10-
- --message-db-connection-string=postgres://postgres:xmtp@db:5432/postgres?sslmode=disable
11-
- --message-db-reader-connection-string=postgres://postgres:xmtp@db:5432/postgres?sslmode=disable
12-
- --lightpush
13-
- --filter
14-
- --ws-port=9001
8+
- --store.enable
9+
- --store.db-connection-string=postgres://postgres:xmtp@db:5432/postgres?sslmode=disable
10+
- --store.reader-db-connection-string=postgres://postgres:xmtp@db:5432/postgres?sslmode=disable
1511
- --wait-for-db=30s
1612
- --api.authn.enable
1713
ports:

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

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import org.xmtp.android.library.messages.toPublicKeyBundle
3737
import org.xmtp.android.library.messages.toSignedPublicKeyBundle
3838
import org.xmtp.android.library.messages.toV2
3939
import org.xmtp.android.library.messages.walletAddress
40+
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
4041
import org.xmtp.proto.message.contents.Invitation
4142
import org.xmtp.proto.message.contents.Invitation.InvitationV1.Context
4243
import java.nio.charset.StandardCharsets
@@ -405,6 +406,10 @@ class ConversationTest {
405406
val messages2 = aliceConversation.messages(limit = 1, after = date)
406407
assertEquals(1, messages2.size)
407408
assertEquals("hey alice 1", messages2[0].body)
409+
val messagesAsc = aliceConversation.messages(direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING)
410+
assertEquals("hey alice 1", messagesAsc[0].body)
411+
val messagesDesc = aliceConversation.messages(direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING)
412+
assertEquals("hey alice 3", messagesDesc[0].body)
408413
}
409414

410415
@Test

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

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.xmtp.android.library.messages.secp256K1Uncompressed
2121
import org.xmtp.android.library.messages.toPublicKeyBundle
2222
import org.xmtp.android.library.messages.walletAddress
2323
import org.xmtp.proto.keystore.api.v1.Keystore
24+
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
2425
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryRequest
2526
import org.xmtp.proto.message.contents.Contact
2627
import org.xmtp.proto.message.contents.InvitationV1Kt.context
@@ -142,6 +143,10 @@ class LocalInstrumentedTest {
142143
val messages3 = convo.messages(after = tenSecondsAgoMessage.sent)
143144
val nowMessage2 = messages3[0]
144145
assertEquals("now", nowMessage2.body)
146+
val messagesAsc = convo.messages(direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING)
147+
assertEquals("10 seconds ago", messagesAsc[0].body)
148+
val messagesDesc = convo.messages(direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING)
149+
assertEquals("now", messagesDesc[0].body)
145150
}
146151

147152
@Test

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

+10
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ class FakeApiClient : ApiClient {
168168
}
169169
}
170170

171+
val direction = pagination?.direction
172+
if (direction != null) {
173+
when (direction) {
174+
MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING -> {
175+
result = result.reversed().toMutableList()
176+
}
177+
else -> Unit
178+
}
179+
}
180+
171181
return QueryResponse.newBuilder().also {
172182
it.addAllEnvelopes(result)
173183
}.build()

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,13 @@ data class GRPCApiClient(
6666
if (pagination?.before != null) {
6767
it.endTimeNs = pagination.before.time * 1_000_000
6868
it.pagingInfo = it.pagingInfo.toBuilder().also { info ->
69-
info.direction =
70-
MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING
69+
info.direction = pagination.direction
7170
}.build()
7271
}
7372
if (pagination?.after != null) {
7473
it.startTimeNs = pagination.after.time * 1_000_000
7574
it.pagingInfo = it.pagingInfo.toBuilder().also { info ->
76-
info.direction =
77-
MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING
75+
info.direction = pagination.direction
7876
}.build()
7977
}
8078
if (cursor != null) {

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import com.google.protobuf.kotlin.toByteString
55
import kotlinx.coroutines.flow.Flow
66
import org.xmtp.android.library.codecs.EncodedContent
77
import org.xmtp.android.library.messages.Envelope
8+
import org.xmtp.android.library.messages.PagingInfoSortDirection
89
import org.xmtp.proto.keystore.api.v1.Keystore.TopicMap.TopicData
10+
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
911
import org.xmtp.proto.message.contents.Invitation
1012
import org.xmtp.proto.message.contents.Invitation.InvitationV1.Aes256gcmHkdfsha256
1113
import java.util.Date
@@ -150,19 +152,22 @@ sealed class Conversation {
150152
limit: Int? = null,
151153
before: Date? = null,
152154
after: Date? = null,
155+
direction: PagingInfoSortDirection = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING,
153156
): List<DecodedMessage> {
154157
return when (this) {
155158
is V1 -> conversationV1.messages(
156159
limit = limit,
157160
before = before,
158-
after = after
161+
after = after,
162+
direction = direction
159163
)
160164

161165
is V2 ->
162166
conversationV2.messages(
163167
limit = limit,
164168
before = before,
165-
after = after
169+
after = after,
170+
direction = direction
166171
)
167172
}
168173
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import org.xmtp.android.library.messages.Message
1414
import org.xmtp.android.library.messages.MessageBuilder
1515
import org.xmtp.android.library.messages.MessageV1Builder
1616
import org.xmtp.android.library.messages.Pagination
17+
import org.xmtp.android.library.messages.PagingInfoSortDirection
1718
import org.xmtp.android.library.messages.Topic
1819
import org.xmtp.android.library.messages.decrypt
1920
import org.xmtp.android.library.messages.header
2021
import org.xmtp.android.library.messages.sentAt
2122
import org.xmtp.android.library.messages.toPublicKeyBundle
2223
import org.xmtp.android.library.messages.walletAddress
24+
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
2325
import java.util.Date
2426

2527
data class ConversationV1(
@@ -41,8 +43,10 @@ data class ConversationV1(
4143
limit: Int? = null,
4244
before: Date? = null,
4345
after: Date? = null,
46+
direction: PagingInfoSortDirection = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING,
4447
): List<DecodedMessage> {
45-
val pagination = Pagination(limit = limit, before = before, after = after)
48+
val pagination =
49+
Pagination(limit = limit, before = before, after = after, direction = direction)
4650
val result = runBlocking {
4751
client.apiClient.envelopes(topic = topic.description, pagination = pagination)
4852
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import org.xmtp.android.library.messages.MessageBuilder
1616
import org.xmtp.android.library.messages.MessageV2
1717
import org.xmtp.android.library.messages.MessageV2Builder
1818
import org.xmtp.android.library.messages.Pagination
19+
import org.xmtp.android.library.messages.PagingInfoSortDirection
1920
import org.xmtp.android.library.messages.SealedInvitationHeaderV1
2021
import org.xmtp.android.library.messages.getPublicKeyBundle
2122
import org.xmtp.android.library.messages.walletAddress
23+
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
2224
import org.xmtp.proto.message.contents.Invitation
2325
import java.util.Date
2426

@@ -59,8 +61,10 @@ data class ConversationV2(
5961
limit: Int? = null,
6062
before: Date? = null,
6163
after: Date? = null,
64+
direction: PagingInfoSortDirection = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING,
6265
): List<DecodedMessage> {
63-
val pagination = Pagination(limit = limit, before = before, after = after)
66+
val pagination =
67+
Pagination(limit = limit, before = before, after = after, direction = direction)
6468
val result = runBlocking {
6569
client.apiClient.envelopes(
6670
topic = topic,

library/src/main/java/org/xmtp/android/library/messages/PagingInfo.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ typealias PagingInfoSortDirection = SortDirection
1010

1111
data class Pagination(
1212
val limit: Int? = null,
13-
val direction: PagingInfoSortDirection? = null,
13+
val direction: PagingInfoSortDirection? = SortDirection.SORT_DIRECTION_DESCENDING,
1414
val before: Date? = null,
1515
val after: Date? = null,
1616
) {

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

+10
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ class FakeApiClient : ApiClient {
164164
}
165165
}
166166

167+
val direction = pagination?.direction
168+
if (direction != null) {
169+
when (direction) {
170+
MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING -> {
171+
result = result.reversed().toMutableList()
172+
}
173+
else -> Unit
174+
}
175+
}
176+
167177
return QueryResponse.newBuilder().also {
168178
it.addAllEnvelopes(result)
169179
}.build()

0 commit comments

Comments
 (0)