From 035a94cafd18b44d945f75432e07fa6d822ebb02 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Mon, 20 Mar 2023 10:26:09 -0700 Subject: [PATCH] Introduce easier invite/intro decoding (#57) * add api client with grpc kotlin * easier invite intro decoding for conversation stream * fix up the linter --- .../org/xmtp/android/library/Conversations.kt | 28 +++-------- .../xmtp/android/library/ConversationsTest.kt | 50 +++++++++++++++++++ 2 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 library/src/test/java/org/xmtp/android/library/ConversationsTest.kt diff --git a/library/src/main/java/org/xmtp/android/library/Conversations.kt b/library/src/main/java/org/xmtp/android/library/Conversations.kt index 8054f3604..b06dca04f 100644 --- a/library/src/main/java/org/xmtp/android/library/Conversations.kt +++ b/library/src/main/java/org/xmtp/android/library/Conversations.kt @@ -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) } } } diff --git a/library/src/test/java/org/xmtp/android/library/ConversationsTest.kt b/library/src/test/java/org/xmtp/android/library/ConversationsTest.kt new file mode 100644 index 000000000..aaacab865 --- /dev/null +++ b/library/src/test/java/org/xmtp/android/library/ConversationsTest.kt @@ -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) + } +}