-
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 read receipt content type to kotlin
- Loading branch information
1 parent
cc04c43
commit e9dc790
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
library/src/androidTest/java/org/xmtp/android/library/ReadReceiptTest.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,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) | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
library/src/main/java/org/xmtp/android/library/codecs/ReadReceiptCodec.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.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) | ||
} | ||
} |