Skip to content

Commit

Permalink
get the new codec working as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 30, 2024
1 parent 816106f commit b579a65
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ dependencies {
implementation 'org.web3j:crypto:5.0.0'
implementation "net.java.dev.jna:jna:5.13.0@aar"
api 'com.google.protobuf:protobuf-kotlin-lite:3.22.3'
api 'org.xmtp:proto-kotlin:3.38.0'
api 'org.xmtp:proto-kotlin:3.40.1'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'app.cash.turbine:turbine:0.12.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.xmtp.android.library

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -64,10 +65,10 @@ class GroupMembershipChangeTest {
assertEquals(messages.size, 1)
val content: GroupMembershipChanges? = messages.first().content()
assertEquals(
listOf(bo.walletAddress, caro.walletAddress),
content?.membersAdded?.map { it.accountAddress }?.sorted()
listOf(bo.walletAddress.lowercase(), caro.walletAddress.lowercase()).sorted(),
content?.membersAddedList?.map { it.accountAddress.lowercase() }?.sorted()
)
assert(content?.membersRemoved.isNullOrEmpty())
assert(content?.membersRemovedList.isNullOrEmpty())
}

@Test
Expand All @@ -82,13 +83,15 @@ class GroupMembershipChangeTest {
)
val messages = group.messages()
assertEquals(messages.size, 1)
assertEquals(group.memberAddresses().size, 3)
group.removeMembers(listOf(caro.walletAddress.lowercase()))
assertEquals(messages.size, 2)
assertEquals(group.messages().size, 2)
assertEquals(group.memberAddresses().size, 2)
val content: GroupMembershipChanges? = messages.first().content()
assertEquals(
listOf(caro.walletAddress),
content?.membersRemoved?.map { it.accountAddress }?.sorted()
listOf(caro.walletAddress.lowercase()),
content?.membersRemovedList?.map { it.accountAddress.lowercase() }?.sorted()
)
assert(content?.membersAdded.isNullOrEmpty())
assert(content?.membersAddedList.isNullOrEmpty())
}
}
62 changes: 52 additions & 10 deletions library/src/androidTest/java/org/xmtp/android/library/GroupTest.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package org.xmtp.android.library

import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import org.junit.Assert
import app.cash.turbine.test
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.xmtp.android.library.codecs.ContentTypeReaction
import org.xmtp.android.library.codecs.Reaction
import org.xmtp.android.library.codecs.ReactionAction
import org.xmtp.android.library.codecs.ReactionCodec
import org.xmtp.android.library.codecs.ReactionSchema
import org.xmtp.android.library.messages.PrivateKey
import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.walletAddress

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class GroupTest {
lateinit var fakeApiClient: FakeApiClient
Expand Down Expand Up @@ -139,24 +144,61 @@ class GroupTest {

@Test
fun testCanSendContentTypesToGroup() {
Client.register(codec = ReactionCodec())

val group = boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
group.send("gm")
val messageToReact = group.messages()[0]

}
val reaction = Reaction(
reference = messageToReact.id,
action = ReactionAction.Added,
content = "U+1F603",
schema = ReactionSchema.Unicode
)

@Test
fun testCanStreamGroupMessages() {
val group = boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
group.send(content = reaction, options = SendOptions(contentType = ContentTypeReaction))

val messages = group.messages()
assertEquals(messages.size, 3)
val content: Reaction? = messages.first().content()
assertEquals("U+1F603", content?.content)
assertEquals(messageToReact.id, content?.reference)
assertEquals(ReactionAction.Added, content?.action)
assertEquals(ReactionSchema.Unicode, content?.schema)
}

@Test
fun testCanStreamGroups() {
fun testCanStreamGroupMessages() = kotlinx.coroutines.test.runTest {
val group = boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))

group.streamMessages().test {
group.send("hi")
assertEquals("hi", awaitItem().body)
awaitComplete()
}
}

@Test
fun testCanStreamGroupsAndConversations() {
val group = boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
fun testCanStreamGroups() = kotlinx.coroutines.test.runTest {
boClient.conversations.streamGroups().test {
val conversation =
boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
conversation.send(content = "hi")
assertEquals("hi", awaitItem().messages().first().body)
awaitComplete()
}
}

@Test
fun testCanStreamGroupsAndConversations() = kotlinx.coroutines.test.runTest {
boClient.conversations.stream(includeGroups = true).test {
val group =
boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
val conversation =
boClient.conversations.newConversation(alix.walletAddress.lowercase())
assertEquals("hi", awaitItem().messages().first().body)
awaitComplete()
}
}
}
1 change: 1 addition & 0 deletions library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.xmtp.android.library

import android.util.Log
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
package uniffi.xmtpv3.org.xmtp.android.library.codecs

import com.google.gson.GsonBuilder
import com.google.protobuf.kotlin.toByteStringUtf8
import org.xmtp.android.library.codecs.ContentCodec
import org.xmtp.android.library.codecs.ContentTypeId
import org.xmtp.android.library.codecs.ContentTypeIdBuilder
import org.xmtp.android.library.codecs.EncodedContent

typealias GroupMembershipChanges = org.xmtp.proto.mls.message.contents.TranscriptMessages.GroupMembershipChanges

val ContentTypeGroupMembershipChange = ContentTypeIdBuilder.builderFromAuthorityId(
"xmtp.org",
"group_membership_change",
versionMajor = 1,
versionMinor = 0
)

data class GroupMembershipChanges(
val membersAdded: List<MembershipChange>,
val membersRemoved: List<MembershipChange>,
val installationsAdded: List<MembershipChange>,
val installationsRemoved: List<MembershipChange>,
)

data class MembershipChange(
val installationIds: List<ByteArray>,
val accountAddress: String,
val initiatedByAccountAddress: String,
)

data class GroupMembershipChangeCodec(override var contentType: ContentTypeId = ContentTypeGroupMembershipChange) :
ContentCodec<GroupMembershipChanges> {

override fun encode(content: GroupMembershipChanges): EncodedContent {
val gson = GsonBuilder().create()
return EncodedContent.newBuilder().also {
it.type = ContentTypeGroupMembershipChange
it.content = gson.toJson(content).toByteStringUtf8()
it.content = content.toByteString()
}.build()
}

override fun decode(content: EncodedContent): GroupMembershipChanges {
val gson = GsonBuilder().create()
return gson.fromJson(content.content.toStringUtf8(), GroupMembershipChanges::class.java)
return GroupMembershipChanges.parseFrom(content.content)
}

override fun fallback(content: GroupMembershipChanges): String? {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.xmtp.android.library.libxmtp

import android.util.Log
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import uniffi.xmtpv3.FfiMessage
Expand Down

0 comments on commit b579a65

Please sign in to comment.