From 6908d1eaf9ffcbe90cf0fe10ce496c2d55dd4aac Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 9 Oct 2024 11:01:01 -0700 Subject: [PATCH] setup the logic for finding existing conversations --- .../org/xmtp/android/library/Conversations.kt | 43 +++++++------------ .../main/java/org/xmtp/android/library/Dm.kt | 6 --- 2 files changed, 15 insertions(+), 34 deletions(-) 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 57dc1394a..69173f023 100644 --- a/library/src/main/java/org/xmtp/android/library/Conversations.kt +++ b/library/src/main/java/org/xmtp/android/library/Conversations.kt @@ -129,11 +129,6 @@ data class Conversations( ) } - // mark this as private until we enable we sunset V2 - private suspend fun newDm(accountAddress: String): Dm { - return newDmInternal(accountAddress) - } - suspend fun newGroupCustomPermissions( accountAddresses: List, permissionPolicySet: PermissionPolicySet, @@ -191,21 +186,6 @@ data class Conversations( return Group(client, group) } - private suspend fun newDmInternal( - accountAddress: String, - ): Dm { - if (accountAddress.lowercase() == client.address.lowercase()) { - throw XMTPException("Recipient is sender") - } - - val inboxId = client.inboxIdFromAddress(accountAddress) ?: throw XMTPException("Error getting inbox id, ${accountAddress} not on network") - val dm = - libXMTPConversations?.createDm(inboxId)?: throw XMTPException("Client does not support V3 Dms") - client.contacts.allowGroups(groupIds = listOf(dm.id().toHex())) - - return Dm(client, dm) - } - // Sync from the network the latest list of groups suspend fun syncGroups() { libXMTPConversations?.sync() @@ -264,20 +244,27 @@ data class Conversations( if (peerAddress.lowercase() == client.address.lowercase()) { throw XMTPException("Recipient is sender") } - if (client.v3Client != null) { - val conversationV3 = libXMTPConversations?.createDm(peerAddress) ?: throw XMTPException("Client does not support V3 Dms") - if (!client.hasV2Client) { - val conversation = Conversation.Dm(Dm(client, conversationV3)) - conversationsByTopic[conversation.topic] = conversation - return conversation - } - } val existingConversation = conversationsByTopic.values.firstOrNull { it.peerAddress == peerAddress && it.conversationId == context?.conversationId } if (existingConversation != null) { return existingConversation } + if (client.v3Client != null) { + val falseAddresses = + client.canMessageV3(listOf(peerAddress)).filter { !it.value }.map { it.key } + if (falseAddresses.isNotEmpty()) { + throw XMTPException("${falseAddresses.joinToString()} not on network") + } + val dm = libXMTPConversations?.createDm(peerAddress) + ?: throw XMTPException("Client does not support V3 Dms") + client.contacts.allowGroups(groupIds = listOf(dm.id().toHex())) + val conversation = Conversation.Dm(Dm(client, dm)) + conversationsByTopic[conversation.topic] = conversation + if (!client.hasV2Client) { + return conversation + } + } val contact = client.contacts.find(peerAddress) ?: throw XMTPException("Recipient not on network") // See if we have an existing v1 convo diff --git a/library/src/main/java/org/xmtp/android/library/Dm.kt b/library/src/main/java/org/xmtp/android/library/Dm.kt index 0b0952d91..8ca930830 100644 --- a/library/src/main/java/org/xmtp/android/library/Dm.kt +++ b/library/src/main/java/org/xmtp/android/library/Dm.kt @@ -3,11 +3,6 @@ package org.xmtp.android.library import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow -import org.xmtp.android.library.Client -import org.xmtp.android.library.ConsentState -import org.xmtp.android.library.DecodedMessage -import org.xmtp.android.library.SendOptions -import org.xmtp.android.library.XMTPException import org.xmtp.android.library.codecs.ContentCodec import org.xmtp.android.library.codecs.EncodedContent import org.xmtp.android.library.codecs.compress @@ -17,7 +12,6 @@ import org.xmtp.android.library.messages.DecryptedMessage import org.xmtp.android.library.messages.MessageDeliveryStatus import org.xmtp.android.library.messages.PagingInfoSortDirection import org.xmtp.android.library.messages.Topic -import org.xmtp.android.library.toHex import org.xmtp.proto.message.api.v1.MessageApiOuterClass import uniffi.xmtpv3.FfiDeliveryStatus import uniffi.xmtpv3.FfiGroup