diff --git a/library/src/androidTest/java/org/xmtp/android/library/ReplyTest.kt b/library/src/androidTest/java/org/xmtp/android/library/ReplyTest.kt new file mode 100644 index 000000000..8fbf65451 --- /dev/null +++ b/library/src/androidTest/java/org/xmtp/android/library/ReplyTest.kt @@ -0,0 +1,48 @@ +package org.xmtp.android.library + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.xmtp.android.library.codecs.ContentTypeReply +import org.xmtp.android.library.codecs.ContentTypeText +import org.xmtp.android.library.codecs.Reply +import org.xmtp.android.library.codecs.ReplyCodec +import org.xmtp.android.library.messages.walletAddress + +@RunWith(AndroidJUnit4::class) +class ReplyTest { + + @Test + fun testCanUseReplyCodec() { + Client.register(codec = ReplyCodec()) + + val fixtures = fixtures() + val aliceClient = fixtures.aliceClient + val aliceConversation = + aliceClient.conversations.newConversation(fixtures.bob.walletAddress) + + aliceConversation.send(text = "hey alice 2 bob") + + val messageToReact = aliceConversation.messages()[0] + + val attachment = Reply( + reference = messageToReact.id, + content = "Hello", + contentType = ContentTypeText + ) + + aliceConversation.send( + content = attachment, + options = SendOptions(contentType = ContentTypeReply), + ) + val messages = aliceConversation.messages() + assertEquals(messages.size, 2) + if (messages.size == 2) { + val content: Reply? = messages.first().content() + assertEquals("Hello", content?.content) + assertEquals(messageToReact.id, content?.reference) + assertEquals(ContentTypeText, content?.contentType) + } + } +} diff --git a/library/src/main/java/org/xmtp/android/library/codecs/README.md b/library/src/main/java/org/xmtp/android/library/codecs/README.md index e8c9c663d..361cd1f2d 100644 --- a/library/src/main/java/org/xmtp/android/library/codecs/README.md +++ b/library/src/main/java/org/xmtp/android/library/codecs/README.md @@ -8,6 +8,8 @@ When you build an app with XMTP, all messages are encoded with a [content type]( - `AttachmentCodec`: Enables sending attachments. - `RemoteAttachmentCodec`: Enables sending remote attachments. - `ReactionCodec`: Enables sending of reactions. +- `ReplyCodec`: Enables sending of replies. + ## Support remote media attachments diff --git a/library/src/main/java/org/xmtp/android/library/codecs/ReplyCodec.kt b/library/src/main/java/org/xmtp/android/library/codecs/ReplyCodec.kt new file mode 100644 index 000000000..d3fd8a2c6 --- /dev/null +++ b/library/src/main/java/org/xmtp/android/library/codecs/ReplyCodec.kt @@ -0,0 +1,34 @@ +package org.xmtp.android.library.codecs + +import com.google.gson.GsonBuilder +import com.google.protobuf.kotlin.toByteStringUtf8 + +val ContentTypeReply = ContentTypeIdBuilder.builderFromAuthorityId( + "xmtp.org", + "reply", + versionMajor = 1, + versionMinor = 0 +) + +data class Reply( + val reference: String, + val content: Any, + val contentType: ContentTypeId, +) + +data class ReplyCodec(override var contentType: ContentTypeId = ContentTypeReply) : + ContentCodec { + + override fun encode(content: Reply): EncodedContent { + val gson = GsonBuilder().create() + return EncodedContent.newBuilder().also { + it.type = ContentTypeReply + it.content = gson.toJson(content).toByteStringUtf8() + }.build() + } + + override fun decode(content: EncodedContent): Reply { + val gson = GsonBuilder().create() + return gson.fromJson(content.content.toStringUtf8(), Reply::class.java) + } +}