Skip to content

Commit

Permalink
Fix pagination not being respected in group messages (#318)
Browse files Browse the repository at this point in the history
* add a test for it

* update to nanoseconds

* fix up linter issue
  • Loading branch information
nplasterer authored Nov 4, 2024
1 parent bded2b7 commit 3b17b28
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import org.xmtp.proto.message.contents.Invitation
import org.xmtp.proto.message.contents.Invitation.InvitationV1.Context
import java.nio.charset.StandardCharsets
import java.util.Date
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.DurationUnit

@RunWith(AndroidJUnit4::class)
class ConversationTest {
Expand Down Expand Up @@ -418,7 +420,12 @@ class ConversationTest {
val messages = aliceConversation.messages(limit = 1)
assertEquals(1, messages.size)
assertEquals("hey alice 3", messages[0].body)
val messages2 = aliceConversation.messages(limit = 1, after = date)
val messages2 = aliceConversation.messages(
limit = 1,
afterNs = date.time.nanoseconds.toLong(
DurationUnit.NANOSECONDS
)
)
assertEquals(1, messages2.size)
assertEquals("hey alice 3", messages2[0].body)
val messagesAsc =
Expand Down
24 changes: 24 additions & 0 deletions library/src/androidTest/java/org/xmtp/android/library/GroupTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,30 @@ class GroupTest {
assertEquals(sameGroup.messages(deliveryStatus = MessageDeliveryStatus.PUBLISHED).size, 2)
}

@Test
fun testCanListGroupMessagesAfter() {
val group = runBlocking { boClient.conversations.newGroup(listOf(alix.walletAddress)) }
val messageId = runBlocking {
group.send("howdy")
group.send("gm")
}
val message = boClient.findMessage(messageId)
assertEquals(group.messages().size, 3)
assertEquals(group.messages(afterNs = message?.sentAtNs).size, 0)
runBlocking {
group.send("howdy")
group.send("gm")
}
assertEquals(group.messages().size, 5)
assertEquals(group.messages(afterNs = message?.sentAtNs).size, 2)

runBlocking { alixClient.conversations.syncConversations() }
val sameGroup = runBlocking { alixClient.conversations.listGroups().last() }
runBlocking { sameGroup.sync() }
assertEquals(sameGroup.messages().size, 4)
assertEquals(sameGroup.messages(afterNs = message?.sentAtNs).size, 2)
}

@Test
fun testCanSendContentTypesToGroup() {
Client.register(codec = ReactionCodec())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import org.xmtp.proto.message.contents.PrivateKeyOuterClass
import org.xmtp.proto.message.contents.PrivateKeyOuterClass.PrivateKeyBundle
import uniffi.xmtpv3.createV2Client
import java.util.Date
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.DurationUnit

@RunWith(AndroidJUnit4::class)
class LocalInstrumentedTest {
Expand Down Expand Up @@ -163,10 +165,19 @@ class LocalInstrumentedTest {
assertEquals(2, messagesLimit.size)
val nowMessage = messages[0]
assertEquals("now", nowMessage.body)
val messages2 = convo.messages(limit = 1, before = nowMessage.sent)
val messages2 = convo.messages(
limit = 1,
beforeNs = nowMessage.sent.time.nanoseconds.toLong(
DurationUnit.NANOSECONDS
)
)
val tenSecondsAgoMessage = messages2[0]
assertEquals("now first", tenSecondsAgoMessage.body)
val messages3 = convo.messages(after = tenSecondsAgoMessage.sent)
val messages3 = convo.messages(
afterNs = tenSecondsAgoMessage.sent.time.nanoseconds.toLong(
DurationUnit.NANOSECONDS
)
)
val nowMessage2 = messages3[0]
assertEquals("now", nowMessage2.body)
val messagesAsc =
Expand Down
51 changes: 36 additions & 15 deletions library/src/main/java/org/xmtp/android/library/Conversation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,50 +169,71 @@ sealed class Conversation {
*/
suspend fun messages(
limit: Int? = null,
before: Date? = null,
after: Date? = null,
beforeNs: Long? = null,
afterNs: Long? = null,
direction: PagingInfoSortDirection = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING,
): List<DecodedMessage> {
return when (this) {
is V1 -> conversationV1.messages(
limit = limit,
before = before,
after = after,
before = beforeNs?.let { Date(it / 1_000_000) },
after = afterNs?.let { Date(it / 1_000_000) },
direction = direction,
)

is V2 ->
conversationV2.messages(
limit = limit,
before = before,
after = after,
before = beforeNs?.let { Date(it / 1_000_000) },
after = afterNs?.let { Date(it / 1_000_000) },
direction = direction,
)

is Group -> {
group.messages(
limit = limit,
before = before,
after = after,
beforeNs = beforeNs,
afterNs = afterNs,
direction = direction,
)
}

is Dm -> dm.messages(limit, before, after, direction)
is Dm -> dm.messages(limit, beforeNs, afterNs, direction)
}
}

suspend fun decryptedMessages(
limit: Int? = null,
before: Date? = null,
after: Date? = null,
beforeNs: Long? = null,
afterNs: Long? = null,
direction: PagingInfoSortDirection = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING,
): List<DecryptedMessage> {
return when (this) {
is V1 -> conversationV1.decryptedMessages(limit, before, after, direction)
is V2 -> conversationV2.decryptedMessages(limit, before, after, direction)
is Group -> group.decryptedMessages(limit, before, after, direction)
is Dm -> dm.decryptedMessages(limit, before, after, direction)
is V1 -> conversationV1.decryptedMessages(
limit = limit,
before = beforeNs?.let { Date(it / 1_000_000) },
after = afterNs?.let { Date(it / 1_000_000) },
direction = direction,
)

is V2 ->
conversationV2.decryptedMessages(
limit = limit,
before = beforeNs?.let { Date(it / 1_000_000) },
after = afterNs?.let { Date(it / 1_000_000) },
direction = direction,
)

is Group -> {
group.decryptedMessages(
limit = limit,
beforeNs = beforeNs,
afterNs = afterNs,
direction = direction,
)
}

is Dm -> dm.decryptedMessages(limit, beforeNs, afterNs, direction)
}
}

Expand Down
18 changes: 8 additions & 10 deletions library/src/main/java/org/xmtp/android/library/Dm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import uniffi.xmtpv3.FfiMessage
import uniffi.xmtpv3.FfiMessageCallback
import uniffi.xmtpv3.FfiSubscribeException
import java.util.Date
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.DurationUnit

class Dm(val client: Client, private val libXMTPGroup: FfiConversation) {
val id: String
Expand Down Expand Up @@ -103,15 +101,15 @@ class Dm(val client: Client, private val libXMTPGroup: FfiConversation) {

fun messages(
limit: Int? = null,
before: Date? = null,
after: Date? = null,
beforeNs: Long? = null,
afterNs: Long? = null,
direction: PagingInfoSortDirection = SortDirection.SORT_DIRECTION_DESCENDING,
deliveryStatus: MessageDeliveryStatus = MessageDeliveryStatus.ALL,
): List<DecodedMessage> {
return libXMTPGroup.findMessages(
opts = FfiListMessagesOptions(
sentBeforeNs = before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
sentAfterNs = after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
sentBeforeNs = beforeNs,
sentAfterNs = afterNs,
limit = limit?.toLong(),
deliveryStatus = when (deliveryStatus) {
MessageDeliveryStatus.PUBLISHED -> FfiDeliveryStatus.PUBLISHED
Expand All @@ -131,15 +129,15 @@ class Dm(val client: Client, private val libXMTPGroup: FfiConversation) {

fun decryptedMessages(
limit: Int? = null,
before: Date? = null,
after: Date? = null,
beforeNs: Long? = null,
afterNs: Long? = null,
direction: PagingInfoSortDirection = SortDirection.SORT_DIRECTION_DESCENDING,
deliveryStatus: MessageDeliveryStatus = MessageDeliveryStatus.ALL,
): List<DecryptedMessage> {
return libXMTPGroup.findMessages(
opts = FfiListMessagesOptions(
sentBeforeNs = before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
sentAfterNs = after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
sentBeforeNs = beforeNs,
sentAfterNs = afterNs,
limit = limit?.toLong(),
deliveryStatus = when (deliveryStatus) {
MessageDeliveryStatus.PUBLISHED -> FfiDeliveryStatus.PUBLISHED
Expand Down
18 changes: 8 additions & 10 deletions library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import uniffi.xmtpv3.FfiSubscribeException
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionOption
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionPolicySet
import java.util.Date
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.DurationUnit

class Group(val client: Client, private val libXMTPGroup: FfiConversation) {
val id: String
Expand Down Expand Up @@ -121,15 +119,15 @@ class Group(val client: Client, private val libXMTPGroup: FfiConversation) {

fun messages(
limit: Int? = null,
before: Date? = null,
after: Date? = null,
beforeNs: Long? = null,
afterNs: Long? = null,
direction: PagingInfoSortDirection = SORT_DIRECTION_DESCENDING,
deliveryStatus: MessageDeliveryStatus = MessageDeliveryStatus.ALL,
): List<DecodedMessage> {
return libXMTPGroup.findMessages(
opts = FfiListMessagesOptions(
sentBeforeNs = before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
sentAfterNs = after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
sentBeforeNs = beforeNs,
sentAfterNs = afterNs,
limit = limit?.toLong(),
deliveryStatus = when (deliveryStatus) {
MessageDeliveryStatus.PUBLISHED -> FfiDeliveryStatus.PUBLISHED
Expand All @@ -149,15 +147,15 @@ class Group(val client: Client, private val libXMTPGroup: FfiConversation) {

fun decryptedMessages(
limit: Int? = null,
before: Date? = null,
after: Date? = null,
beforeNs: Long? = null,
afterNs: Long? = null,
direction: PagingInfoSortDirection = SORT_DIRECTION_DESCENDING,
deliveryStatus: MessageDeliveryStatus = MessageDeliveryStatus.ALL,
): List<DecryptedMessage> {
return libXMTPGroup.findMessages(
opts = FfiListMessagesOptions(
sentBeforeNs = before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
sentAfterNs = after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
sentBeforeNs = beforeNs,
sentAfterNs = afterNs,
limit = limit?.toLong(),
deliveryStatus = when (deliveryStatus) {
MessageDeliveryStatus.PUBLISHED -> FfiDeliveryStatus.PUBLISHED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ data class MessageV3(val client: Client, private val libXMTPMessage: FfiMessage)
val sentAt: Date
get() = Date(libXMTPMessage.sentAtNs / 1_000_000)

val sentAtNs: Long
get() = libXMTPMessage.sentAtNs

val deliveryStatus: MessageDeliveryStatus
get() = when (libXMTPMessage.deliveryStatus) {
FfiDeliveryStatus.UNPUBLISHED -> MessageDeliveryStatus.UNPUBLISHED
Expand Down

0 comments on commit 3b17b28

Please sign in to comment.