Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/xmtp/xmtp-android into gg/e…
Browse files Browse the repository at this point in the history
…nhancement/add_sender_hmac_to_message_v2
  • Loading branch information
nplasterer committed Feb 20, 2024
2 parents 887f1f4 + a59fe92 commit 4da951f
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import org.xmtp.android.example.R
import org.xmtp.android.example.conversation.ConversationDetailViewModel
import org.xmtp.android.example.databinding.ListItemMessageBinding
import org.xmtp.android.example.extension.margins
import org.xmtp.proto.mls.message.contents.TranscriptMessages
import uniffi.xmtpv3.org.xmtp.android.library.codecs.GroupMembershipChanges
import java.text.SimpleDateFormat
import java.util.Locale

class MessageViewHolder(
private val binding: ListItemMessageBinding,
Expand Down Expand Up @@ -45,6 +46,9 @@ class MessageViewHolder(
binding.messageContainer.layoutParams = params
if (item.message.content<Any>() is String) {
binding.messageBody.text = item.message.body
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
binding.messageDate.text = sdf.format(item.message.sent)

} else if (item.message.content<Any>() is GroupMembershipChanges) {
val changes = item.message.content() as? GroupMembershipChanges
binding.messageBody.text =
Expand Down
24 changes: 18 additions & 6 deletions example/src/main/res/layout/list_item_message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@
app:cardCornerRadius="8dp"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">

<TextView
android:id="@+id/messageBody"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_margin="8dp" />
android:layout_height="wrap_content">
<TextView
android:id="@+id/messageBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_margin="8dp" />
<TextView
android:id="@+id/messageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_margin="8dp"
/>
</LinearLayout>



</androidx.cardview.widget.CardView>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ class ClientTest {
assertEquals(client.address.lowercase(), v3Client?.accountAddress()?.lowercase())
}

@Test
fun testCreatesAV3DevClient() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val fakeWallet = PrivateKeyBuilder()
val client =
Client().create(
account = fakeWallet,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.DEV, true),
enableAlphaMls = true,
appContext = context
)
)
val v3Client = client.libXMTPClient
assertEquals(client.address.lowercase(), v3Client?.accountAddress()?.lowercase())
}

@Test
fun testDoesNotCreateAV3Client() {
val fakeWallet = PrivateKeyBuilder()
Expand Down
45 changes: 39 additions & 6 deletions library/src/androidTest/java/org/xmtp/android/library/GroupTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.xmtp.android.library
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import app.cash.turbine.test
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
Expand Down Expand Up @@ -113,6 +112,45 @@ class GroupTest {
)
}

@Test
fun testCanRemoveGroupMembersWhenNotCreator() {
boClient.conversations.newGroup(
listOf(
alix.walletAddress,
caro.walletAddress
)
)
runBlocking { alixClient.conversations.syncGroups() }
val group = alixClient.conversations.listGroups().first()
group.removeMembers(listOf(caro.walletAddress))
assertEquals(
group.memberAddresses().sorted(),
listOf(
alix.walletAddress.lowercase(),
bo.walletAddress.lowercase()
).sorted()
)
}

@Test
fun testIsActiveReturnsCorrectly() {
val group = boClient.conversations.newGroup(
listOf(
alix.walletAddress,
caro.walletAddress
)
)
runBlocking { caroClient.conversations.syncGroups() }
val caroGroup = caroClient.conversations.listGroups().first()
runBlocking { caroGroup.sync() }
assert(caroGroup.isActive())
assert(group.isActive())
group.removeMembers(listOf(caro.walletAddress))
runBlocking { caroGroup.sync() }
assert(group.isActive())
assert(!caroGroup.isActive())
}

@Test
fun testCanListGroups() {
boClient.conversations.newGroup(listOf(alix.walletAddress))
Expand Down Expand Up @@ -200,11 +238,9 @@ class GroupTest {
assertEquals(ReactionSchema.Unicode, content?.schema)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
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)
Expand All @@ -213,7 +249,6 @@ class GroupTest {
}
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun testCanStreamDecryptedGroupMessages() = kotlinx.coroutines.test.runTest {
val group = boClient.conversations.newGroup(listOf(alix.walletAddress))
Expand All @@ -226,7 +261,6 @@ class GroupTest {
}
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun testCanStreamGroups() = kotlinx.coroutines.test.runTest {
boClient.conversations.streamGroups().test {
Expand All @@ -239,7 +273,6 @@ class GroupTest {
}
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun testCanStreamGroupsAndConversations() = kotlinx.coroutines.test.runTest {
boClient.conversations.streamAll().test {
Expand Down
4 changes: 3 additions & 1 deletion library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryRequest
import uniffi.xmtpv3.FfiXmtpClient
import uniffi.xmtpv3.LegacyIdentitySource
import uniffi.xmtpv3.createClient
import uniffi.xmtpv3.getVersionInfo
import java.io.File
import java.nio.charset.StandardCharsets
import java.security.KeyStore
Expand Down Expand Up @@ -81,6 +82,7 @@ class Client() {
lateinit var conversations: Conversations
var logger: XMTPLogger = XMTPLogger()
var libXMTPClient: FfiXmtpClient? = null
val libXMTPVersion: String = getVersionInfo()

companion object {
private const val TAG = "Client"
Expand Down Expand Up @@ -347,7 +349,7 @@ class Client() {
throw XMTPException("No signer passed but signer was required.")
}
}

Log.i(TAG, "LibXMTP $libXMTPVersion")
return v3Client
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ data class Conversations(
}

val group = runBlocking {
libXMTPConversations?.createGroup(accountAddresses)
libXMTPConversations?.createGroup(accountAddresses, permissions = null)
?: throw XMTPException("Client does not support Groups")
}
return Group(client, group)
Expand Down
6 changes: 5 additions & 1 deletion library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,17 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
fun decrypt(message: Message): DecryptedMessage {
return DecryptedMessage(
id = message.id.toHex(),
topic = message.id.toHex(),
topic = message.convoId.toHex(),
encodedContent = message.decode().encodedContent,
senderAddress = message.senderAddress,
sentAt = Date()
)
}

fun isActive(): Boolean {
return libXMTPGroup.isActive()
}

fun addMembers(addresses: List<String>) {
runBlocking { libXMTPGroup.addMembers(addresses) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ data class Message(val client: Client, private val libXMTPMessage: FfiMessage) {
val id: ByteArray
get() = libXMTPMessage.id

val convoId: ByteArray
get() = libXMTPMessage.convoId

val senderAddress: String
get() = libXMTPMessage.addrFrom

Expand Down
Loading

0 comments on commit 4da951f

Please sign in to comment.