Skip to content

Commit

Permalink
Introduce easier invite/intro decoding (#57)
Browse files Browse the repository at this point in the history
* add api client with grpc kotlin

* easier invite intro decoding for conversation stream

* fix up the linter
  • Loading branch information
nplasterer authored Mar 20, 2023
1 parent 045748a commit 035a94c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
28 changes: 7 additions & 21 deletions library/src/main/java/org/xmtp/android/library/Conversations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -250,34 +250,20 @@ data class Conversations(
client.subscribeTopic(
listOf(Topic.userIntro(client.address), Topic.userInvite(client.address))
).collect { envelope ->

if (envelope.contentTopic == Topic.userIntro(client.address).description) {
val messageV1 = MessageV1Builder.buildFromBytes(envelope.message.toByteArray())
val senderAddress = messageV1.header.sender.walletAddress
val recipientAddress = messageV1.header.recipient.walletAddress
val peerAddress =
if (client.address == senderAddress) recipientAddress else senderAddress
val conversationV1 = ConversationV1(
client = client,
peerAddress = peerAddress,
sentAt = messageV1.sentAt
)
if (!streamedConversationTopics.contains(conversationV1.topic.description)) {
streamedConversationTopics.add(conversationV1.topic.description)
emit(Conversation.V1(conversationV1))
val conversationV1 = fromIntro(envelope = envelope)
if (!streamedConversationTopics.contains(conversationV1.topic)) {
streamedConversationTopics.add(conversationV1.topic)
emit(conversationV1)
}
}

if (envelope.contentTopic == Topic.userInvite(client.address).description) {
val sealedInvitation = SealedInvitation.parseFrom(envelope.message)
val unsealed = sealedInvitation.v1.getInvitation(viewer = client.keys)
val conversationV2 = ConversationV2.create(
client = client,
invitation = unsealed,
header = sealedInvitation.v1.header
)
val conversationV2 = fromInvite(envelope = envelope)
if (!streamedConversationTopics.contains(conversationV2.topic)) {
streamedConversationTopics.add(conversationV2.topic)
emit(Conversation.V2(conversationV2))
emit(conversationV2)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.xmtp.android.library

import org.junit.Assert.assertEquals
import org.junit.Test
import org.xmtp.android.library.codecs.TextCodec
import org.xmtp.android.library.messages.EnvelopeBuilder
import org.xmtp.android.library.messages.InvitationV1
import org.xmtp.android.library.messages.MessageBuilder
import org.xmtp.android.library.messages.MessageV1Builder
import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.SealedInvitationBuilder
import org.xmtp.android.library.messages.Topic
import org.xmtp.android.library.messages.createRandom
import org.xmtp.android.library.messages.getPublicKeyBundle
import org.xmtp.android.library.messages.toPublicKeyBundle
import org.xmtp.android.library.messages.walletAddress
import java.util.Date

class ConversationsTest {

@Test
fun testCanGetConversationFromIntroEnvelope() {
val fixtures = fixtures()
val client = fixtures.aliceClient
val created = Date()
val newWallet = PrivateKeyBuilder()
val newClient = Client().create(account = newWallet, apiClient = fixtures.fakeApiClient)
val message = MessageV1Builder.buildEncode(sender = newClient.privateKeyBundleV1, recipient = fixtures.aliceClient.v1keys.toPublicKeyBundle(), message = TextCodec().encode(content = "hello").toByteArray(), timestamp = created)
val envelope = EnvelopeBuilder.buildFromTopic(topic = Topic.userIntro(client.address), timestamp = created, message = MessageBuilder.buildFromMessageV1(v1 = message).toByteArray())
val conversation = client.conversations.fromIntro(envelope = envelope)
assertEquals(conversation.peerAddress, newWallet.address)
assertEquals(conversation.createdAt.time, created.time)
}

@Test
fun testCanGetConversationFromInviteEnvelope() {
val fixtures = fixtures()
val client = fixtures.aliceClient
val created = Date()
val newWallet = PrivateKeyBuilder()
val newClient = Client().create(account = newWallet, apiClient = fixtures.fakeApiClient)
val invitation = InvitationV1.newBuilder().build().createRandom(context = null)
val sealed = SealedInvitationBuilder.buildFromV1(sender = newClient.keys, recipient = client.keys.getPublicKeyBundle(), created = created, invitation = invitation)
val peerAddress = fixtures.alice.walletAddress
val envelope = EnvelopeBuilder.buildFromTopic(topic = Topic.userInvite(peerAddress), timestamp = created, message = sealed.toByteArray())
val conversation = client.conversations.fromInvite(envelope = envelope)
assertEquals(conversation.peerAddress, newWallet.address)
assertEquals(conversation.createdAt.time, created.time)
}
}

0 comments on commit 035a94c

Please sign in to comment.