Skip to content

Commit 3b17b28

Browse files
authored
Fix pagination not being respected in group messages (#318)
* add a test for it * update to nanoseconds * fix up linter issue
1 parent bded2b7 commit 3b17b28

File tree

7 files changed

+100
-38
lines changed

7 files changed

+100
-38
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import org.xmtp.proto.message.contents.Invitation
4545
import org.xmtp.proto.message.contents.Invitation.InvitationV1.Context
4646
import java.nio.charset.StandardCharsets
4747
import java.util.Date
48+
import kotlin.time.Duration.Companion.nanoseconds
49+
import kotlin.time.DurationUnit
4850

4951
@RunWith(AndroidJUnit4::class)
5052
class ConversationTest {
@@ -418,7 +420,12 @@ class ConversationTest {
418420
val messages = aliceConversation.messages(limit = 1)
419421
assertEquals(1, messages.size)
420422
assertEquals("hey alice 3", messages[0].body)
421-
val messages2 = aliceConversation.messages(limit = 1, after = date)
423+
val messages2 = aliceConversation.messages(
424+
limit = 1,
425+
afterNs = date.time.nanoseconds.toLong(
426+
DurationUnit.NANOSECONDS
427+
)
428+
)
422429
assertEquals(1, messages2.size)
423430
assertEquals("hey alice 3", messages2[0].body)
424431
val messagesAsc =

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

+24
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,30 @@ class GroupTest {
585585
assertEquals(sameGroup.messages(deliveryStatus = MessageDeliveryStatus.PUBLISHED).size, 2)
586586
}
587587

588+
@Test
589+
fun testCanListGroupMessagesAfter() {
590+
val group = runBlocking { boClient.conversations.newGroup(listOf(alix.walletAddress)) }
591+
val messageId = runBlocking {
592+
group.send("howdy")
593+
group.send("gm")
594+
}
595+
val message = boClient.findMessage(messageId)
596+
assertEquals(group.messages().size, 3)
597+
assertEquals(group.messages(afterNs = message?.sentAtNs).size, 0)
598+
runBlocking {
599+
group.send("howdy")
600+
group.send("gm")
601+
}
602+
assertEquals(group.messages().size, 5)
603+
assertEquals(group.messages(afterNs = message?.sentAtNs).size, 2)
604+
605+
runBlocking { alixClient.conversations.syncConversations() }
606+
val sameGroup = runBlocking { alixClient.conversations.listGroups().last() }
607+
runBlocking { sameGroup.sync() }
608+
assertEquals(sameGroup.messages().size, 4)
609+
assertEquals(sameGroup.messages(afterNs = message?.sentAtNs).size, 2)
610+
}
611+
588612
@Test
589613
fun testCanSendContentTypesToGroup() {
590614
Client.register(codec = ReactionCodec())

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import org.xmtp.proto.message.contents.PrivateKeyOuterClass
2929
import org.xmtp.proto.message.contents.PrivateKeyOuterClass.PrivateKeyBundle
3030
import uniffi.xmtpv3.createV2Client
3131
import java.util.Date
32+
import kotlin.time.Duration.Companion.nanoseconds
33+
import kotlin.time.DurationUnit
3234

3335
@RunWith(AndroidJUnit4::class)
3436
class LocalInstrumentedTest {
@@ -163,10 +165,19 @@ class LocalInstrumentedTest {
163165
assertEquals(2, messagesLimit.size)
164166
val nowMessage = messages[0]
165167
assertEquals("now", nowMessage.body)
166-
val messages2 = convo.messages(limit = 1, before = nowMessage.sent)
168+
val messages2 = convo.messages(
169+
limit = 1,
170+
beforeNs = nowMessage.sent.time.nanoseconds.toLong(
171+
DurationUnit.NANOSECONDS
172+
)
173+
)
167174
val tenSecondsAgoMessage = messages2[0]
168175
assertEquals("now first", tenSecondsAgoMessage.body)
169-
val messages3 = convo.messages(after = tenSecondsAgoMessage.sent)
176+
val messages3 = convo.messages(
177+
afterNs = tenSecondsAgoMessage.sent.time.nanoseconds.toLong(
178+
DurationUnit.NANOSECONDS
179+
)
180+
)
170181
val nowMessage2 = messages3[0]
171182
assertEquals("now", nowMessage2.body)
172183
val messagesAsc =

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

+36-15
Original file line numberDiff line numberDiff line change
@@ -169,50 +169,71 @@ sealed class Conversation {
169169
*/
170170
suspend fun messages(
171171
limit: Int? = null,
172-
before: Date? = null,
173-
after: Date? = null,
172+
beforeNs: Long? = null,
173+
afterNs: Long? = null,
174174
direction: PagingInfoSortDirection = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING,
175175
): List<DecodedMessage> {
176176
return when (this) {
177177
is V1 -> conversationV1.messages(
178178
limit = limit,
179-
before = before,
180-
after = after,
179+
before = beforeNs?.let { Date(it / 1_000_000) },
180+
after = afterNs?.let { Date(it / 1_000_000) },
181181
direction = direction,
182182
)
183183

184184
is V2 ->
185185
conversationV2.messages(
186186
limit = limit,
187-
before = before,
188-
after = after,
187+
before = beforeNs?.let { Date(it / 1_000_000) },
188+
after = afterNs?.let { Date(it / 1_000_000) },
189189
direction = direction,
190190
)
191191

192192
is Group -> {
193193
group.messages(
194194
limit = limit,
195-
before = before,
196-
after = after,
195+
beforeNs = beforeNs,
196+
afterNs = afterNs,
197197
direction = direction,
198198
)
199199
}
200200

201-
is Dm -> dm.messages(limit, before, after, direction)
201+
is Dm -> dm.messages(limit, beforeNs, afterNs, direction)
202202
}
203203
}
204204

205205
suspend fun decryptedMessages(
206206
limit: Int? = null,
207-
before: Date? = null,
208-
after: Date? = null,
207+
beforeNs: Long? = null,
208+
afterNs: Long? = null,
209209
direction: PagingInfoSortDirection = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING,
210210
): List<DecryptedMessage> {
211211
return when (this) {
212-
is V1 -> conversationV1.decryptedMessages(limit, before, after, direction)
213-
is V2 -> conversationV2.decryptedMessages(limit, before, after, direction)
214-
is Group -> group.decryptedMessages(limit, before, after, direction)
215-
is Dm -> dm.decryptedMessages(limit, before, after, direction)
212+
is V1 -> conversationV1.decryptedMessages(
213+
limit = limit,
214+
before = beforeNs?.let { Date(it / 1_000_000) },
215+
after = afterNs?.let { Date(it / 1_000_000) },
216+
direction = direction,
217+
)
218+
219+
is V2 ->
220+
conversationV2.decryptedMessages(
221+
limit = limit,
222+
before = beforeNs?.let { Date(it / 1_000_000) },
223+
after = afterNs?.let { Date(it / 1_000_000) },
224+
direction = direction,
225+
)
226+
227+
is Group -> {
228+
group.decryptedMessages(
229+
limit = limit,
230+
beforeNs = beforeNs,
231+
afterNs = afterNs,
232+
direction = direction,
233+
)
234+
}
235+
236+
is Dm -> dm.decryptedMessages(limit, beforeNs, afterNs, direction)
216237
}
217238
}
218239

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import uniffi.xmtpv3.FfiMessage
2323
import uniffi.xmtpv3.FfiMessageCallback
2424
import uniffi.xmtpv3.FfiSubscribeException
2525
import java.util.Date
26-
import kotlin.time.Duration.Companion.nanoseconds
27-
import kotlin.time.DurationUnit
2826

2927
class Dm(val client: Client, private val libXMTPGroup: FfiConversation) {
3028
val id: String
@@ -103,15 +101,15 @@ class Dm(val client: Client, private val libXMTPGroup: FfiConversation) {
103101

104102
fun messages(
105103
limit: Int? = null,
106-
before: Date? = null,
107-
after: Date? = null,
104+
beforeNs: Long? = null,
105+
afterNs: Long? = null,
108106
direction: PagingInfoSortDirection = SortDirection.SORT_DIRECTION_DESCENDING,
109107
deliveryStatus: MessageDeliveryStatus = MessageDeliveryStatus.ALL,
110108
): List<DecodedMessage> {
111109
return libXMTPGroup.findMessages(
112110
opts = FfiListMessagesOptions(
113-
sentBeforeNs = before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
114-
sentAfterNs = after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
111+
sentBeforeNs = beforeNs,
112+
sentAfterNs = afterNs,
115113
limit = limit?.toLong(),
116114
deliveryStatus = when (deliveryStatus) {
117115
MessageDeliveryStatus.PUBLISHED -> FfiDeliveryStatus.PUBLISHED
@@ -131,15 +129,15 @@ class Dm(val client: Client, private val libXMTPGroup: FfiConversation) {
131129

132130
fun decryptedMessages(
133131
limit: Int? = null,
134-
before: Date? = null,
135-
after: Date? = null,
132+
beforeNs: Long? = null,
133+
afterNs: Long? = null,
136134
direction: PagingInfoSortDirection = SortDirection.SORT_DIRECTION_DESCENDING,
137135
deliveryStatus: MessageDeliveryStatus = MessageDeliveryStatus.ALL,
138136
): List<DecryptedMessage> {
139137
return libXMTPGroup.findMessages(
140138
opts = FfiListMessagesOptions(
141-
sentBeforeNs = before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
142-
sentAfterNs = after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
139+
sentBeforeNs = beforeNs,
140+
sentAfterNs = afterNs,
143141
limit = limit?.toLong(),
144142
deliveryStatus = when (deliveryStatus) {
145143
MessageDeliveryStatus.PUBLISHED -> FfiDeliveryStatus.PUBLISHED

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import uniffi.xmtpv3.FfiSubscribeException
2929
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionOption
3030
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionPolicySet
3131
import java.util.Date
32-
import kotlin.time.Duration.Companion.nanoseconds
33-
import kotlin.time.DurationUnit
3432

3533
class Group(val client: Client, private val libXMTPGroup: FfiConversation) {
3634
val id: String
@@ -121,15 +119,15 @@ class Group(val client: Client, private val libXMTPGroup: FfiConversation) {
121119

122120
fun messages(
123121
limit: Int? = null,
124-
before: Date? = null,
125-
after: Date? = null,
122+
beforeNs: Long? = null,
123+
afterNs: Long? = null,
126124
direction: PagingInfoSortDirection = SORT_DIRECTION_DESCENDING,
127125
deliveryStatus: MessageDeliveryStatus = MessageDeliveryStatus.ALL,
128126
): List<DecodedMessage> {
129127
return libXMTPGroup.findMessages(
130128
opts = FfiListMessagesOptions(
131-
sentBeforeNs = before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
132-
sentAfterNs = after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
129+
sentBeforeNs = beforeNs,
130+
sentAfterNs = afterNs,
133131
limit = limit?.toLong(),
134132
deliveryStatus = when (deliveryStatus) {
135133
MessageDeliveryStatus.PUBLISHED -> FfiDeliveryStatus.PUBLISHED
@@ -149,15 +147,15 @@ class Group(val client: Client, private val libXMTPGroup: FfiConversation) {
149147

150148
fun decryptedMessages(
151149
limit: Int? = null,
152-
before: Date? = null,
153-
after: Date? = null,
150+
beforeNs: Long? = null,
151+
afterNs: Long? = null,
154152
direction: PagingInfoSortDirection = SORT_DIRECTION_DESCENDING,
155153
deliveryStatus: MessageDeliveryStatus = MessageDeliveryStatus.ALL,
156154
): List<DecryptedMessage> {
157155
return libXMTPGroup.findMessages(
158156
opts = FfiListMessagesOptions(
159-
sentBeforeNs = before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
160-
sentAfterNs = after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
157+
sentBeforeNs = beforeNs,
158+
sentAfterNs = afterNs,
161159
limit = limit?.toLong(),
162160
deliveryStatus = when (deliveryStatus) {
163161
MessageDeliveryStatus.PUBLISHED -> FfiDeliveryStatus.PUBLISHED

library/src/main/java/org/xmtp/android/library/libxmtp/MessageV3.kt

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ data class MessageV3(val client: Client, private val libXMTPMessage: FfiMessage)
2929
val sentAt: Date
3030
get() = Date(libXMTPMessage.sentAtNs / 1_000_000)
3131

32+
val sentAtNs: Long
33+
get() = libXMTPMessage.sentAtNs
34+
3235
val deliveryStatus: MessageDeliveryStatus
3336
get() = when (libXMTPMessage.deliveryStatus) {
3437
FfiDeliveryStatus.UNPUBLISHED -> MessageDeliveryStatus.UNPUBLISHED

0 commit comments

Comments
 (0)