Skip to content

Commit

Permalink
setup the logic for finding existing conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 9, 2024
1 parent 8e971cb commit 6908d1e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
43 changes: 15 additions & 28 deletions library/src/main/java/org/xmtp/android/library/Conversations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
permissionPolicySet: PermissionPolicySet,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions library/src/main/java/org/xmtp/android/library/Dm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6908d1e

Please sign in to comment.