Skip to content

Commit

Permalink
Read Receipts (#101)
Browse files Browse the repository at this point in the history
* add api client with grpc kotlin

* add read receipt content type to kotlin
  • Loading branch information
nplasterer authored Aug 3, 2023
1 parent cc04c43 commit e9dc790
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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.ContentTypeReadReceipt
import org.xmtp.android.library.codecs.ReadReceipt
import org.xmtp.android.library.codecs.ReadReceiptCodec
import org.xmtp.android.library.messages.walletAddress

@RunWith(AndroidJUnit4::class)
class ReadReceiptTest {

@Test
fun testCanUseReadReceiptCodec() {
Client.register(codec = ReadReceiptCodec())

val fixtures = fixtures()
val aliceClient = fixtures.aliceClient
val aliceConversation =
aliceClient.conversations.newConversation(fixtures.bob.walletAddress)

aliceConversation.send(text = "hey alice 2 bob")

val readReceipt = ReadReceipt(timestamp = "2019-09-26T07:58:30.996+0200")

aliceConversation.send(
content = readReceipt,
options = SendOptions(contentType = ContentTypeReadReceipt),
)
val messages = aliceConversation.messages()
assertEquals(messages.size, 2)
if (messages.size == 2) {
val content: ReadReceipt? = messages.first().content()
assertEquals("2019-09-26T07:58:30.996+0200", content?.timestamp)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.xmtp.android.library.codecs

import com.google.protobuf.ByteString
import org.xmtp.android.library.XMTPException

val ContentTypeReadReceipt = ContentTypeIdBuilder.builderFromAuthorityId(
"xmtp.org",
"readReceipt",
versionMajor = 1,
versionMinor = 0
)

data class ReadReceipt(
// The timestamp the read receipt was sent, in ISO 8601 format
val timestamp: String,
)

data class ReadReceiptCodec(override var contentType: ContentTypeId = ContentTypeReadReceipt) :
ContentCodec<ReadReceipt> {

override fun encode(content: ReadReceipt): EncodedContent {
return EncodedContent.newBuilder().also {
it.type = ContentTypeReadReceipt
it.putParameters("timestamp", content.timestamp)
it.content = ByteString.EMPTY
}.build()
}

override fun decode(content: EncodedContent): ReadReceipt {
val timestamp = content.parametersMap["timestamp"] ?: throw XMTPException("Invalid Content")

return ReadReceipt(timestamp = timestamp)
}
}

0 comments on commit e9dc790

Please sign in to comment.