Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/xmtp/xmtp-android into np/v3
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 7, 2024
2 parents 2d4db06 + 3b17b28 commit 63bbfca
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 24 deletions.
64 changes: 64 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 @@ -54,6 +54,46 @@ class GroupTest {
caroClient = fixtures.caroClient
}

@Test
fun testsCanDualSendConversations() {
val v2Convo = runBlocking { alixClient.conversations.newConversation(bo.walletAddress) }
runBlocking {
alixClient.conversations.syncConversations()
boClient.conversations.syncConversations()
}
val alixDm = runBlocking { alixClient.findDm(bo.walletAddress) }
val boDm = runBlocking { boClient.findDm(alix.walletAddress) }

assertEquals(alixDm?.id, boDm?.id)
assertEquals(runBlocking { alixClient.conversations.list().size }, 1)
assertEquals(runBlocking { alixClient.conversations.listDms().size }, 1)
assertEquals(runBlocking { boClient.conversations.listDms().size }, 1)
assertEquals(runBlocking { boClient.conversations.list().size }, 1)
assertEquals(v2Convo.topic, runBlocking { boClient.conversations.list().first().topic })
}

@Test
fun testsCanDualSendMessages() {
val alixV2Convo = runBlocking { alixClient.conversations.newConversation(bo.walletAddress) }
val boV2Convo = runBlocking { boClient.conversations.list().first() }
runBlocking { boClient.conversations.syncConversations() }
val alixDm = runBlocking { alixClient.findDm(bo.walletAddress) }
val boDm = runBlocking { boClient.findDm(alix.walletAddress) }

runBlocking { alixV2Convo.send("first") }
runBlocking { boV2Convo.send("second") }

runBlocking {
alixDm?.sync()
boDm?.sync()
}

assertEquals(runBlocking { alixV2Convo.messages().size }, 2)
assertEquals(runBlocking { alixV2Convo.messages().size }, runBlocking { boV2Convo.messages().size })
assertEquals(boDm?.messages()?.size, 2)
assertEquals(alixDm?.messages()?.size, 3) // We send the group membership update to the dm
}

@Test
fun testCanCreateAGroupWithDefaultPermissions() {
val boGroup = runBlocking {
Expand Down Expand Up @@ -519,6 +559,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
18 changes: 6 additions & 12 deletions library/src/main/java/org/xmtp/android/library/Conversation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,14 @@ sealed class Conversation {
*/
fun messages(
limit: Int? = null,
before: Date? = null,
after: Date? = null,
beforeNs: Long? = null,
afterNs: Long? = null,
direction: Message.SortDirection = Message.SortDirection.DESCENDING,
deliveryStatus: Message.MessageDeliveryStatus = Message.MessageDeliveryStatus.ALL,
): List<DecodedMessage> {
return when (this) {
is Group -> {
group.messages(
limit = limit,
before = before,
after = after,
direction = direction,
)
}

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

Expand All @@ -156,6 +149,7 @@ sealed class Conversation {
}
}


suspend fun processMessage(envelopeBytes: ByteArray): Message {
return when (this) {
is Group -> group.processMessage(envelopeBytes)
Expand Down
10 changes: 4 additions & 6 deletions library/src/main/java/org/xmtp/android/library/Dm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,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 @@ -100,15 +98,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: SortDirection = SortDirection.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 Down
10 changes: 4 additions & 6 deletions library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,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 @@ -119,15 +117,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: SortDirection = SortDirection.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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ data class Message(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 63bbfca

Please sign in to comment.