Skip to content

Commit

Permalink
functions for mls dms
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell authored and nplasterer committed Oct 9, 2024
1 parent 5bb081e commit 8e971cb
Show file tree
Hide file tree
Showing 3 changed files with 470 additions and 3 deletions.
29 changes: 28 additions & 1 deletion library/src/main/java/org/xmtp/android/library/Conversation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ sealed class Conversation {
data class V2(val conversationV2: ConversationV2) : Conversation()

data class Group(val group: org.xmtp.android.library.Group) : Conversation()
data class Dm(val dm: org.xmtp.android.library.Dm) : Conversation()

enum class Version { V1, V2, GROUP }
enum class Version { V1, V2, GROUP, DM }

// This indicates whether this a v1 or v2 conversation.
val version: Version
Expand All @@ -38,6 +39,7 @@ sealed class Conversation {
is V1 -> Version.V1
is V2 -> Version.V2
is Group -> Version.GROUP
is Dm -> Version.DM
}
}

Expand All @@ -48,6 +50,7 @@ sealed class Conversation {
is V1 -> conversationV1.sentAt
is V2 -> conversationV2.createdAt
is Group -> group.createdAt
is Dm -> dm.createdAt
}
}

Expand All @@ -58,6 +61,7 @@ sealed class Conversation {
is V1 -> conversationV1.peerAddress
is V2 -> conversationV2.peerAddress
is Group -> runBlocking { group.peerInboxIds().joinToString(",") }
is Dm -> runBlocking { dm.peerInboxIds().joinToString(",") }
}
}

Expand All @@ -67,6 +71,7 @@ sealed class Conversation {
is V1 -> listOf(conversationV1.peerAddress)
is V2 -> listOf(conversationV2.peerAddress)
is Group -> runBlocking { group.peerInboxIds() }
is Dm -> runBlocking { dm.peerInboxIds() }
}
}

Expand All @@ -78,6 +83,7 @@ sealed class Conversation {
is V1 -> null
is V2 -> conversationV2.context.conversationId
is Group -> null
is Dm -> null
}
}

Expand All @@ -87,6 +93,7 @@ sealed class Conversation {
is V1 -> null
is V2 -> conversationV2.keyMaterial
is Group -> null
is Dm -> null
}
}

Expand All @@ -95,6 +102,7 @@ sealed class Conversation {
is V1 -> conversationV1.client.contacts.consentList.state(address = peerAddress)
is V2 -> conversationV2.client.contacts.consentList.state(address = peerAddress)
is Group -> group.consentState()
is Dm -> dm.client.contacts.consentList.groupState(groupId = dm.id)
}
}

Expand All @@ -120,6 +128,7 @@ sealed class Conversation {
).build()

is Group -> throw XMTPException("Groups do not support topics")
is Dm -> throw XMTPException("DMs do not support topics")
}
}

Expand All @@ -128,6 +137,7 @@ sealed class Conversation {
is V1 -> conversationV1.decode(envelope)
is V2 -> conversationV2.decodeEnvelope(envelope)
is Group -> message?.decode() ?: throw XMTPException("Groups require message be passed")
is Dm -> throw XMTPException("DMs require message be passed")
}
}

Expand All @@ -151,6 +161,7 @@ sealed class Conversation {
}

is Group -> throw XMTPException("Groups do not support prepared messages") // We return a encoded content not a preparedmessage which requires a envelope
is Dm -> throw XMTPException("DMs do not support prepared messages")
}
}

Expand All @@ -168,6 +179,7 @@ sealed class Conversation {
}

is Group -> throw XMTPException("Groups do not support prepared messages") // We return a encoded content not a preparedmessage which requires a envelope
is Dm -> throw XMTPException("DMs do not support prepared messages")
}
}

Expand All @@ -176,6 +188,7 @@ sealed class Conversation {
is V1 -> conversationV1.send(prepared = prepared)
is V2 -> conversationV2.send(prepared = prepared)
is Group -> throw XMTPException("Groups do not support prepared messages") // We return a encoded content not a prepared Message which requires a envelope
is Dm -> throw XMTPException("DMs do not support prepared messages")
}
}

Expand All @@ -184,6 +197,7 @@ sealed class Conversation {
is V1 -> conversationV1.send(content = content, options = options)
is V2 -> conversationV2.send(content = content, options = options)
is Group -> group.send(content = content, options = options)
is Dm -> dm.send(content = content, options = options)
}
}

Expand All @@ -192,6 +206,7 @@ sealed class Conversation {
is V1 -> conversationV1.send(text = text, sendOptions, sentAt)
is V2 -> conversationV2.send(text = text, sendOptions, sentAt)
is Group -> group.send(text)
is Dm -> dm.send(text)
}
}

Expand All @@ -200,6 +215,7 @@ sealed class Conversation {
is V1 -> conversationV1.send(encodedContent = encodedContent, options = options)
is V2 -> conversationV2.send(encodedContent = encodedContent, options = options)
is Group -> group.send(encodedContent = encodedContent)
is Dm -> dm.send(encodedContent = encodedContent)
}
}

Expand All @@ -215,6 +231,7 @@ sealed class Conversation {
is V1 -> conversationV1.topic.description
is V2 -> conversationV2.topic
is Group -> group.topic
is Dm -> dm.topic
}
}

Expand Down Expand Up @@ -261,6 +278,7 @@ sealed class Conversation {
direction = direction,
)
}
is Dm -> dm.messages(limit, before, after, direction)
}
}

Expand All @@ -274,6 +292,7 @@ sealed class Conversation {
is V1 -> conversationV1.decryptedMessages(limit, before, after, direction)
is V2 -> conversationV2.decryptedMessages(limit, before, after, direction)
is Group -> group.decryptedMessages(limit, before, after, direction)
is Dm -> dm.decryptedMessages(limit, before, after, direction)
}
}

Expand All @@ -287,6 +306,9 @@ sealed class Conversation {
is Group -> {
message?.decrypt() ?: throw XMTPException("Groups require message be passed")
}
is Dm -> {
message?.decrypt() ?: throw XMTPException("DMs require message be passed")
}
}
}

Expand All @@ -296,6 +318,7 @@ sealed class Conversation {
is V1 -> return null
is V2 -> conversationV2.consentProof
is Group -> return null
is Dm -> return null
}
}

Expand All @@ -306,6 +329,7 @@ sealed class Conversation {
is V1 -> conversationV1.client
is V2 -> conversationV2.client
is Group -> group.client
is Dm -> dm.client
}
}

Expand All @@ -318,6 +342,7 @@ sealed class Conversation {
is V1 -> conversationV1.streamMessages()
is V2 -> conversationV2.streamMessages()
is Group -> group.streamMessages()
is Dm -> dm.streamMessages()
}
}

Expand All @@ -326,6 +351,7 @@ sealed class Conversation {
is V1 -> conversationV1.streamDecryptedMessages()
is V2 -> conversationV2.streamDecryptedMessages()
is Group -> group.streamDecryptedMessages()
is Dm -> dm.streamDecryptedMessages()
}
}

Expand All @@ -334,6 +360,7 @@ sealed class Conversation {
is V1 -> return conversationV1.streamEphemeral()
is V2 -> return conversationV2.streamEphemeral()
is Group -> throw XMTPException("Groups do not support ephemeral messages")
is Dm -> throw XMTPException("DMs do not support ephemeral messages")
}
}
}
24 changes: 22 additions & 2 deletions library/src/main/java/org/xmtp/android/library/Conversations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ 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 @@ -186,6 +191,21 @@ 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 @@ -245,9 +265,9 @@ data class Conversations(
throw XMTPException("Recipient is sender")
}
if (client.v3Client != null) {
val conversationV3 = libXMTPConversations?.createDm(peerAddress)
val conversationV3 = libXMTPConversations?.createDm(peerAddress) ?: throw XMTPException("Client does not support V3 Dms")
if (!client.hasV2Client) {
val conversation = Conversation.V3(conversationV3)
val conversation = Conversation.Dm(Dm(client, conversationV3))
conversationsByTopic[conversation.topic] = conversation
return conversation
}
Expand Down
Loading

0 comments on commit 8e971cb

Please sign in to comment.