@@ -2,6 +2,7 @@ package org.xmtp.android.library
2
2
3
3
import android.util.Log
4
4
import kotlinx.coroutines.flow.Flow
5
+ import kotlinx.coroutines.flow.collect
5
6
import kotlinx.coroutines.flow.flow
6
7
import kotlinx.coroutines.runBlocking
7
8
import org.web3j.crypto.Hash
@@ -97,8 +98,8 @@ data class ConversationV1(
97
98
return preparedMessage.messageId
98
99
}
99
100
100
- fun send (encodedContent : EncodedContent ): String {
101
- val preparedMessage = prepareMessage(encodedContent = encodedContent)
101
+ fun send (encodedContent : EncodedContent , options : SendOptions ? = null ): String {
102
+ val preparedMessage = prepareMessage(encodedContent = encodedContent, options = options )
102
103
preparedMessage.send()
103
104
return preparedMessage.messageId
104
105
}
@@ -123,10 +124,13 @@ data class ConversationV1(
123
124
if (compression != null ) {
124
125
encoded = encoded.compress(compression)
125
126
}
126
- return prepareMessage(encodedContent = encoded)
127
+ return prepareMessage(encodedContent = encoded, options = options )
127
128
}
128
129
129
- fun prepareMessage (encodedContent : EncodedContent ): PreparedMessage {
130
+ fun prepareMessage (
131
+ encodedContent : EncodedContent ,
132
+ options : SendOptions ? = null,
133
+ ): PreparedMessage {
130
134
val contact = client.contacts.find(peerAddress) ? : throw XMTPException (" address not found" )
131
135
val recipient = contact.toPublicKeyBundle()
132
136
if (! recipient.identityKey.hasSignature()) {
@@ -139,9 +143,12 @@ data class ConversationV1(
139
143
message = encodedContent.toByteArray(),
140
144
timestamp = date
141
145
)
146
+
147
+ val isEphemeral: Boolean = options != null && options.ephemeral
148
+
142
149
val messageEnvelope =
143
- EnvelopeBuilder .buildFromTopic (
144
- topic = Topic .directMessageV1(client.address, peerAddress) ,
150
+ EnvelopeBuilder .buildFromString (
151
+ topic = if (isEphemeral) ephemeralTopic else topic.description ,
145
152
timestamp = date,
146
153
message = MessageBuilder .buildFromMessageV1(v1 = message).toByteArray()
147
154
)
@@ -150,7 +157,7 @@ data class ConversationV1(
150
157
conversation = Conversation .V1 (this )
151
158
) {
152
159
val envelopes = mutableListOf (messageEnvelope)
153
- if (client.contacts.needsIntroduction(peerAddress)) {
160
+ if (client.contacts.needsIntroduction(peerAddress) && ! isEphemeral ) {
154
161
envelopes.addAll(
155
162
listOf (
156
163
EnvelopeBuilder .buildFromTopic(
@@ -173,4 +180,13 @@ data class ConversationV1(
173
180
174
181
private fun generateId (envelope : Envelope ): String =
175
182
Hash .sha256(envelope.message.toByteArray()).toHex()
183
+
184
+ val ephemeralTopic: String
185
+ get() = topic.description.replace(" /xmtp/0/dm-" , " /xmtp/0/dmE-" )
186
+
187
+ fun streamEphemeral (): Flow <Envelope > = flow {
188
+ client.subscribe(topics = listOf (ephemeralTopic)).collect {
189
+ emit(it)
190
+ }
191
+ }
176
192
}
0 commit comments