Skip to content

Commit

Permalink
Reply Content Type (#98)
Browse files Browse the repository at this point in the history
* add api client with grpc kotlin

* add reply codec type
  • Loading branch information
nplasterer authored Jul 27, 2023
1 parent e246d63 commit 74a3299
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
48 changes: 48 additions & 0 deletions library/src/androidTest/java/org/xmtp/android/library/ReplyTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Reply> {

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)
}
}

0 comments on commit 74a3299

Please sign in to comment.