-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add api client with grpc kotlin * add reply codec type
- Loading branch information
1 parent
e246d63
commit 74a3299
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
library/src/androidTest/java/org/xmtp/android/library/ReplyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
library/src/main/java/org/xmtp/android/library/codecs/ReplyCodec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |