Skip to content

Commit 75a9566

Browse files
authored
feat: include topic in the DecodedMessage (#110)
1 parent bc76b60 commit 75a9566

File tree

5 files changed

+71
-42
lines changed

5 files changed

+71
-42
lines changed

library/src/androidTest/java/org/xmtp/android/library/ConversationTest.kt

+64-41
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
77
import org.junit.Assert
88
import org.junit.Assert.assertEquals
99
import org.junit.Assert.assertThrows
10+
import org.junit.Assert.assertTrue
1011
import org.junit.Before
1112
import org.junit.Ignore
1213
import org.junit.Test
@@ -180,13 +181,11 @@ class ConversationTest {
180181
@Test
181182
fun testCanLoadV2Messages() {
182183
val bobConversation = bobClient.conversations.newConversation(
183-
aliceWallet.address,
184-
InvitationV1ContextBuilder.buildFromConversation("hi")
184+
aliceWallet.address, InvitationV1ContextBuilder.buildFromConversation("hi")
185185
)
186186

187187
val aliceConversation = aliceClient.conversations.newConversation(
188-
bobWallet.address,
189-
InvitationV1ContextBuilder.buildFromConversation("hi")
188+
bobWallet.address, InvitationV1ContextBuilder.buildFromConversation("hi")
190189
)
191190
bobConversation.send(content = "hey alice")
192191
val messages = aliceConversation.messages()
@@ -214,31 +213,23 @@ class ConversationTest {
214213
val preKey = aliceClient.keys?.preKeysList?.get(0)
215214
val signature = preKey?.sign(digest)
216215
val bundle = aliceClient.privateKeyBundleV1?.toV2()?.getPublicKeyBundle()
217-
val signedContent =
218-
SignedContentBuilder.builderFromPayload(
219-
payload = originalPayload,
220-
sender = bundle,
221-
signature = signature
222-
)
216+
val signedContent = SignedContentBuilder.builderFromPayload(
217+
payload = originalPayload, sender = bundle, signature = signature
218+
)
223219
val signedBytes = signedContent.toByteArray()
224-
val ciphertext =
225-
Crypto.encrypt(
226-
aliceConversation.keyMaterial!!,
227-
signedBytes,
228-
additionalData = headerBytes
229-
)
220+
val ciphertext = Crypto.encrypt(
221+
aliceConversation.keyMaterial!!, signedBytes, additionalData = headerBytes
222+
)
230223
val tamperedMessage =
231224
MessageV2Builder.buildFromCipherText(headerBytes = headerBytes, ciphertext = ciphertext)
232-
val tamperedEnvelope =
233-
EnvelopeBuilder.buildFromString(
234-
topic = aliceConversation.topic,
235-
timestamp = Date(),
236-
message = MessageBuilder.buildFromMessageV2(v2 = tamperedMessage).toByteArray()
237-
)
225+
val tamperedEnvelope = EnvelopeBuilder.buildFromString(
226+
topic = aliceConversation.topic,
227+
timestamp = Date(),
228+
message = MessageBuilder.buildFromMessageV2(v2 = tamperedMessage).toByteArray()
229+
)
238230
aliceClient.publish(envelopes = listOf(tamperedEnvelope))
239231
val bobConversation = bobClient.conversations.newConversation(
240-
aliceWallet.address,
241-
InvitationV1ContextBuilder.buildFromConversation("hi")
232+
aliceWallet.address, InvitationV1ContextBuilder.buildFromConversation("hi")
242233
)
243234
assertThrows("Invalid signature", XMTPException::class.java) {
244235
bobConversation.decode(tamperedEnvelope)
@@ -330,11 +321,11 @@ class ConversationTest {
330321
val invitationContext = Invitation.InvitationV1.Context.newBuilder().also {
331322
it.conversationId = "https://example.com/1"
332323
}.build()
333-
val invitationv1 =
334-
InvitationV1.newBuilder().build().createDeterministic(
335-
sender = client.keys,
336-
recipient = fakeContactClient.keys.getPublicKeyBundle(), context = invitationContext
337-
)
324+
val invitationv1 = InvitationV1.newBuilder().build().createDeterministic(
325+
sender = client.keys,
326+
recipient = fakeContactClient.keys.getPublicKeyBundle(),
327+
context = invitationContext
328+
)
338329
val senderBundle = client.privateKeyBundleV1?.toV2()
339330
assertEquals(
340331
senderBundle?.identityKey?.publicKey?.recoverWalletSignerPublicKey()?.walletAddress,
@@ -397,13 +388,11 @@ class ConversationTest {
397388
@Test
398389
fun testCanPaginateV2Messages() {
399390
val bobConversation = bobClient.conversations.newConversation(
400-
alice.walletAddress,
401-
context = InvitationV1ContextBuilder.buildFromConversation("hi")
391+
alice.walletAddress, context = InvitationV1ContextBuilder.buildFromConversation("hi")
402392
)
403393

404394
val aliceConversation = aliceClient.conversations.newConversation(
405-
bob.walletAddress,
406-
context = InvitationV1ContextBuilder.buildFromConversation("hi")
395+
bob.walletAddress, context = InvitationV1ContextBuilder.buildFromConversation("hi")
407396
)
408397
val date = Date()
409398
date.time = date.time - 1000000
@@ -421,18 +410,24 @@ class ConversationTest {
421410
@Test
422411
fun testListBatchMessages() {
423412
val bobConversation = aliceClient.conversations.newConversation(bob.walletAddress)
424-
val steveConversation = aliceClient.conversations.newConversation(fixtures.steve.walletAddress)
413+
val steveConversation =
414+
aliceClient.conversations.newConversation(fixtures.steve.walletAddress)
425415

426416
bobConversation.send(text = "hey alice 1")
427417
bobConversation.send(text = "hey alice 2")
428418
steveConversation.send(text = "hey alice 3")
429419
val messages = aliceClient.conversations.listBatchMessages(
430420
listOf(
431-
Pair(steveConversation.topic, null),
432-
Pair(bobConversation.topic, null)
421+
Pair(steveConversation.topic, null), Pair(bobConversation.topic, null)
433422
)
434423
)
424+
val isSteveOrBobConversation = { topic: String ->
425+
(topic.equals(steveConversation.topic) || topic.equals(bobConversation.topic))
426+
}
435427
assertEquals(3, messages.size)
428+
assertTrue(isSteveOrBobConversation(messages[0].topic))
429+
assertTrue(isSteveOrBobConversation(messages[1].topic))
430+
assertTrue(isSteveOrBobConversation(messages[2].topic))
436431
}
437432

438433
@Test
@@ -586,9 +581,7 @@ class ConversationTest {
586581
header = Invitation.SealedInvitationHeaderV1.newBuilder().build()
587582
)
588583
val envelope = EnvelopeBuilder.buildFromString(
589-
topic = topic,
590-
timestamp = Date(),
591-
message = envelopeMessage
584+
topic = topic, timestamp = Date(), message = envelopeMessage
592585
)
593586
assertThrows("pre-key not signed by identity key", XMTPException::class.java) {
594587
conversation.decodeEnvelope(envelope)
@@ -627,8 +620,38 @@ class ConversationTest {
627620
fun testFetchConversation() {
628621
// Generated from JS script
629622
val ints = arrayOf(
630-
31, 116, 198, 193, 189, 122, 19, 254, 191, 189, 211, 215, 255, 131,
631-
171, 239, 243, 33, 4, 62, 143, 86, 18, 195, 251, 61, 128, 90, 34, 126, 219, 236
623+
31,
624+
116,
625+
198,
626+
193,
627+
189,
628+
122,
629+
19,
630+
254,
631+
191,
632+
189,
633+
211,
634+
215,
635+
255,
636+
131,
637+
171,
638+
239,
639+
243,
640+
33,
641+
4,
642+
62,
643+
143,
644+
86,
645+
18,
646+
195,
647+
251,
648+
61,
649+
128,
650+
90,
651+
34,
652+
126,
653+
219,
654+
236
632655
)
633656
val bytes =
634657
ints.foldIndexed(ByteArray(ints.size)) { i, a, v -> a.apply { set(i, v.toByte()) } }

library/src/androidTest/java/org/xmtp/android/library/MessageTest.kt

+2
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ class MessageTest {
242242
val messages = convo.messages()
243243
assertEquals(1, messages.size)
244244
assertEquals("hello from kotlin", messages[0].body)
245+
assertEquals(convo.topic.description, messages[0].topic)
245246
}
246247

247248
@Test
@@ -257,6 +258,7 @@ class MessageTest {
257258
val messages = convo.messages()
258259
assertEquals(1, messages.size)
259260
assertEquals("hello from kotlin", messages[0].body)
261+
assertEquals(convo.topic, messages[0].topic)
260262
}
261263

262264
@Test

library/src/main/java/org/xmtp/android/library/ConversationV1.kt

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ data class ConversationV1(
5959
val encodedMessage = EncodedContent.parseFrom(decrypted)
6060
val header = message.v1.header
6161
val decoded = DecodedMessage(
62+
topic = envelope.contentTopic,
6263
encodedContent = encodedMessage,
6364
senderAddress = header.sender.walletAddress,
6465
sent = message.v1.sentAt

library/src/main/java/org/xmtp/android/library/DecodedMessage.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import org.xmtp.proto.message.contents.Content
66
import java.util.Date
77

88
data class DecodedMessage(
9+
var topic: String,
910
var encodedContent: Content.EncodedContent,
1011
var senderAddress: String,
1112
var sent: Date
1213
) {
1314
var id: String = ""
1415
companion object {
15-
fun preview(body: String, senderAddress: String, sent: Date): DecodedMessage {
16+
fun preview(topic: String, body: String, senderAddress: String, sent: Date): DecodedMessage {
1617
val encoded = TextCodec().encode(content = body)
1718
return DecodedMessage(
19+
topic = topic,
1820
encodedContent = encoded,
1921
senderAddress = senderAddress,
2022
sent = sent

library/src/main/java/org/xmtp/android/library/messages/MessageV2.kt

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class MessageV2Builder {
7474
throw XMTPException("Topic mismatch")
7575
}
7676
return DecodedMessage(
77+
topic = header.topic,
7778
encodedContent = encodedMessage,
7879
senderAddress = signed.sender.walletAddress,
7980
sent = Date(header.createdNs / 1_000_000)

0 commit comments

Comments
 (0)