Skip to content

Commit 74a3299

Browse files
authored
Reply Content Type (#98)
* add api client with grpc kotlin * add reply codec type
1 parent e246d63 commit 74a3299

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.xmtp.android.library
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import org.junit.Assert.assertEquals
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.xmtp.android.library.codecs.ContentTypeReply
8+
import org.xmtp.android.library.codecs.ContentTypeText
9+
import org.xmtp.android.library.codecs.Reply
10+
import org.xmtp.android.library.codecs.ReplyCodec
11+
import org.xmtp.android.library.messages.walletAddress
12+
13+
@RunWith(AndroidJUnit4::class)
14+
class ReplyTest {
15+
16+
@Test
17+
fun testCanUseReplyCodec() {
18+
Client.register(codec = ReplyCodec())
19+
20+
val fixtures = fixtures()
21+
val aliceClient = fixtures.aliceClient
22+
val aliceConversation =
23+
aliceClient.conversations.newConversation(fixtures.bob.walletAddress)
24+
25+
aliceConversation.send(text = "hey alice 2 bob")
26+
27+
val messageToReact = aliceConversation.messages()[0]
28+
29+
val attachment = Reply(
30+
reference = messageToReact.id,
31+
content = "Hello",
32+
contentType = ContentTypeText
33+
)
34+
35+
aliceConversation.send(
36+
content = attachment,
37+
options = SendOptions(contentType = ContentTypeReply),
38+
)
39+
val messages = aliceConversation.messages()
40+
assertEquals(messages.size, 2)
41+
if (messages.size == 2) {
42+
val content: Reply? = messages.first().content()
43+
assertEquals("Hello", content?.content)
44+
assertEquals(messageToReact.id, content?.reference)
45+
assertEquals(ContentTypeText, content?.contentType)
46+
}
47+
}
48+
}

library/src/main/java/org/xmtp/android/library/codecs/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ When you build an app with XMTP, all messages are encoded with a [content type](
88
- `AttachmentCodec`: Enables sending attachments.
99
- `RemoteAttachmentCodec`: Enables sending remote attachments.
1010
- `ReactionCodec`: Enables sending of reactions.
11+
- `ReplyCodec`: Enables sending of replies.
12+
1113

1214
## Support remote media attachments
1315

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.xmtp.android.library.codecs
2+
3+
import com.google.gson.GsonBuilder
4+
import com.google.protobuf.kotlin.toByteStringUtf8
5+
6+
val ContentTypeReply = ContentTypeIdBuilder.builderFromAuthorityId(
7+
"xmtp.org",
8+
"reply",
9+
versionMajor = 1,
10+
versionMinor = 0
11+
)
12+
13+
data class Reply(
14+
val reference: String,
15+
val content: Any,
16+
val contentType: ContentTypeId,
17+
)
18+
19+
data class ReplyCodec(override var contentType: ContentTypeId = ContentTypeReply) :
20+
ContentCodec<Reply> {
21+
22+
override fun encode(content: Reply): EncodedContent {
23+
val gson = GsonBuilder().create()
24+
return EncodedContent.newBuilder().also {
25+
it.type = ContentTypeReply
26+
it.content = gson.toJson(content).toByteStringUtf8()
27+
}.build()
28+
}
29+
30+
override fun decode(content: EncodedContent): Reply {
31+
val gson = GsonBuilder().create()
32+
return gson.fromJson(content.content.toStringUtf8(), Reply::class.java)
33+
}
34+
}

0 commit comments

Comments
 (0)