Skip to content

Commit 9b6acda

Browse files
authored
Add ability to send EncodedContent objects directly (#85)
* add api client with grpc kotlin * add android side of enncoded sending * write tests for it * fix up th elint * fix the test
1 parent 63a8dd5 commit 9b6acda

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -628,4 +628,27 @@ class ConversationTest {
628628
val noConversation = client.fetchConversation("invalid_topic")
629629
Assert.assertEquals(null, noConversation)
630630
}
631+
632+
@Test
633+
fun testCanSendEncodedContentV1Message() {
634+
fixtures.publishLegacyContact(client = bobClient)
635+
fixtures.publishLegacyContact(client = aliceClient)
636+
val bobConversation = bobClient.conversations.newConversation(aliceWallet.address)
637+
val aliceConversation = aliceClient.conversations.newConversation(bobWallet.address)
638+
val encodedContent = TextCodec().encode(content = "hi")
639+
bobConversation.send(encodedContent = encodedContent)
640+
val messages = aliceConversation.messages()
641+
assertEquals(1, messages.size)
642+
assertEquals("hi", messages[0].content())
643+
}
644+
645+
@Test
646+
fun testCanSendEncodedContentV2Message() {
647+
val bobConversation = bobClient.conversations.newConversation(aliceWallet.address)
648+
val encodedContent = TextCodec().encode(content = "hi")
649+
bobConversation.send(encodedContent = encodedContent)
650+
val messages = bobConversation.messages()
651+
assertEquals(1, messages.size)
652+
assertEquals("hi", messages[0].content())
653+
}
631654
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.xmtp.android.library
22

33
import android.util.Log
44
import kotlinx.coroutines.flow.Flow
5+
import org.xmtp.android.library.codecs.EncodedContent
56
import org.xmtp.android.library.messages.Envelope
67
import java.util.Date
78

@@ -104,6 +105,12 @@ sealed class Conversation {
104105
}
105106
}
106107

108+
fun send(encodedContent: EncodedContent): String {
109+
return when (this) {
110+
is V1 -> conversationV1.send(encodedContent = encodedContent)
111+
is V2 -> conversationV2.send(encodedContent = encodedContent)
112+
}
113+
}
107114
val topic: String
108115
get() {
109116
return when (this) {

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,13 @@ data class ConversationV1(
9797
return preparedMessage.messageId
9898
}
9999

100+
fun send(encodedContent: EncodedContent): String {
101+
val preparedMessage = prepareMessage(encodedContent = encodedContent)
102+
preparedMessage.send()
103+
return preparedMessage.messageId
104+
}
105+
100106
fun <T> prepareMessage(content: T, options: SendOptions?): PreparedMessage {
101-
val contact = client.contacts.find(peerAddress) ?: throw XMTPException("address not found")
102107
val codec = Client.codecRegistry.find(options?.contentType)
103108

104109
fun <Codec : ContentCodec<T>> encode(codec: Codec, content: Any?): EncodedContent {
@@ -118,6 +123,11 @@ data class ConversationV1(
118123
if (compression != null) {
119124
encoded = encoded.compress(compression)
120125
}
126+
return prepareMessage(encodedContent = encoded)
127+
}
128+
129+
fun prepareMessage(encodedContent: EncodedContent): PreparedMessage {
130+
val contact = client.contacts.find(peerAddress) ?: throw XMTPException("address not found")
121131
val recipient = contact.toPublicKeyBundle()
122132
if (!recipient.identityKey.hasSignature()) {
123133
throw Exception("no signature for id key")
@@ -126,7 +136,7 @@ data class ConversationV1(
126136
val message = MessageV1Builder.buildEncode(
127137
sender = client.privateKeyBundleV1,
128138
recipient = recipient,
129-
message = encoded.toByteArray(),
139+
message = encodedContent.toByteArray(),
130140
timestamp = date
131141
)
132142
val messageEnvelope =

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ data class ConversationV2(
3737
client: Client,
3838
invitation: Invitation.InvitationV1,
3939
header: SealedInvitationHeaderV1,
40-
isGroup: Boolean = false
40+
isGroup: Boolean = false,
4141
): ConversationV2 {
4242
val myKeys = client.keys.getPublicKeyBundle()
4343
val peer =
@@ -114,6 +114,12 @@ data class ConversationV2(
114114
return preparedMessage.messageId
115115
}
116116

117+
fun send(encodedContent: EncodedContent): String {
118+
val preparedMessage = prepareMessage(encodedContent = encodedContent)
119+
preparedMessage.send()
120+
return preparedMessage.messageId
121+
}
122+
117123
fun <Codec : ContentCodec<T>, T> encode(codec: Codec, content: T): ByteArray {
118124
val encodedContent = codec.encode(content = content)
119125
val message = MessageV2Builder.buildEncode(
@@ -150,9 +156,13 @@ data class ConversationV2(
150156
if (compression != null) {
151157
encoded = encoded.compress(compression)
152158
}
159+
return prepareMessage(encoded)
160+
}
161+
162+
fun prepareMessage(encodedContent: EncodedContent): PreparedMessage {
153163
val message = MessageV2Builder.buildEncode(
154164
client = client,
155-
encodedContent = encoded,
165+
encodedContent = encodedContent,
156166
topic = topic,
157167
keyMaterial = keyMaterial
158168
)

0 commit comments

Comments
 (0)