Skip to content

Commit 91e3848

Browse files
authored
Merge branch 'main' into np/hmac-push-notifications
2 parents 76ada2b + 80559d7 commit 91e3848

35 files changed

+1447
-852
lines changed

example/src/main/java/org/xmtp/android/example/MainViewModel.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.emptyFlow
1515
import kotlinx.coroutines.flow.flowOn
1616
import kotlinx.coroutines.flow.mapLatest
1717
import kotlinx.coroutines.launch
18+
import kotlinx.coroutines.runBlocking
1819
import org.xmtp.android.example.extension.flowWhileShared
1920
import org.xmtp.android.example.extension.stateFlow
2021
import org.xmtp.android.example.pushnotifications.PushNotificationTokenManager
@@ -88,7 +89,7 @@ class MainViewModel : ViewModel() {
8889

8990
@WorkerThread
9091
private fun fetchMostRecentMessage(conversation: Conversation): DecodedMessage? {
91-
return conversation.messages(limit = 1).firstOrNull()
92+
return runBlocking { conversation.messages(limit = 1).firstOrNull() }
9293
}
9394

9495
@OptIn(ExperimentalCoroutinesApi::class)

example/src/main/java/org/xmtp/android/example/conversation/ConversationDetailViewModel.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.emptyFlow
1515
import kotlinx.coroutines.flow.flowOn
1616
import kotlinx.coroutines.flow.mapLatest
1717
import kotlinx.coroutines.launch
18+
import kotlinx.coroutines.runBlocking
1819
import org.xmtp.android.example.ClientManager
1920
import org.xmtp.android.example.extension.flowWhileShared
2021
import org.xmtp.android.example.extension.stateFlow
@@ -77,7 +78,12 @@ class ConversationDetailViewModel(private val savedStateHandle: SavedStateHandle
7778
stateFlow(viewModelScope, null) { subscriptionCount ->
7879
if (conversation == null) {
7980
conversation =
80-
ClientManager.client.fetchConversation(conversationTopic, includeGroups = false)
81+
runBlocking {
82+
ClientManager.client.fetchConversation(
83+
conversationTopic,
84+
includeGroups = false
85+
)
86+
}
8187
}
8288
if (conversation != null) {
8389
conversation!!.streamMessages()

example/src/main/java/org/xmtp/android/example/pushnotifications/PushNotificationsService.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.google.firebase.messaging.RemoteMessage
1414
import kotlinx.coroutines.Dispatchers
1515
import kotlinx.coroutines.GlobalScope
1616
import kotlinx.coroutines.launch
17+
import kotlinx.coroutines.runBlocking
1718
import org.xmtp.android.example.ClientManager
1819
import org.xmtp.android.example.R
1920
import org.xmtp.android.example.conversation.ConversationDetailActivity
@@ -56,7 +57,8 @@ class PushNotificationsService : FirebaseMessagingService() {
5657
GlobalScope.launch(Dispatchers.Main) {
5758
ClientManager.createClient(keysData, applicationContext)
5859
}
59-
val conversation = ClientManager.client.fetchConversation(topic, includeGroups = true)
60+
val conversation =
61+
runBlocking { ClientManager.client.fetchConversation(topic, includeGroups = true) }
6062
if (conversation == null) {
6163
Log.e(TAG, "No keys or conversation persisted")
6264
return

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.xmtp.android.library
22

33
import androidx.test.ext.junit.runners.AndroidJUnit4
44
import com.google.protobuf.kotlin.toByteStringUtf8
5+
import kotlinx.coroutines.runBlocking
56
import org.junit.Assert.assertEquals
67
import org.junit.Test
78
import org.junit.runner.RunWith
@@ -24,14 +25,17 @@ class AttachmentTest {
2425

2526
val fixtures = fixtures()
2627
val aliceClient = fixtures.aliceClient
27-
val aliceConversation =
28+
val aliceConversation = runBlocking {
2829
aliceClient.conversations.newConversation(fixtures.bob.walletAddress)
30+
}
2931

30-
aliceConversation.send(
31-
content = attachment,
32-
options = SendOptions(contentType = ContentTypeAttachment),
33-
)
34-
val messages = aliceConversation.messages()
32+
runBlocking {
33+
aliceConversation.send(
34+
content = attachment,
35+
options = SendOptions(contentType = ContentTypeAttachment),
36+
)
37+
}
38+
val messages = runBlocking { aliceConversation.messages() }
3539
assertEquals(messages.size, 1)
3640
if (messages.size == 1) {
3741
val content: Attachment? = messages[0].content()

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

+25-19
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class ClientTest {
2626
fun testHasPrivateKeyBundleV1() {
2727
val fakeWallet = PrivateKeyBuilder()
2828
val client = Client().create(account = fakeWallet)
29-
assertEquals(1, client.privateKeyBundleV1?.preKeysList?.size)
30-
val preKey = client.privateKeyBundleV1?.preKeysList?.get(0)
29+
assertEquals(1, client.privateKeyBundleV1.preKeysList?.size)
30+
val preKey = client.privateKeyBundleV1.preKeysList?.get(0)
3131
assert(preKey?.publicKey?.hasSignature() ?: false)
3232
}
3333

@@ -50,15 +50,15 @@ class ClientTest {
5050
val fakeWallet = PrivateKeyBuilder()
5151
val client = Client().create(account = fakeWallet)
5252
val bundle = client.privateKeyBundle
53-
val clientFromV1Bundle = Client().buildFromBundle(bundle!!)
53+
val clientFromV1Bundle = Client().buildFromBundle(bundle)
5454
assertEquals(client.address, clientFromV1Bundle.address)
5555
assertEquals(
56-
client.privateKeyBundleV1?.identityKey,
57-
clientFromV1Bundle.privateKeyBundleV1?.identityKey,
56+
client.privateKeyBundleV1.identityKey,
57+
clientFromV1Bundle.privateKeyBundleV1.identityKey,
5858
)
5959
assertEquals(
60-
client.privateKeyBundleV1?.preKeysList,
61-
clientFromV1Bundle.privateKeyBundleV1?.preKeysList,
60+
client.privateKeyBundleV1.preKeysList,
61+
clientFromV1Bundle.privateKeyBundleV1.preKeysList,
6262
)
6363
}
6464

@@ -67,15 +67,15 @@ class ClientTest {
6767
val fakeWallet = PrivateKeyBuilder()
6868
val client = Client().create(account = fakeWallet)
6969
val bundleV1 = client.v1keys
70-
val clientFromV1Bundle = Client().buildFromV1Bundle(bundleV1!!)
70+
val clientFromV1Bundle = Client().buildFromV1Bundle(bundleV1)
7171
assertEquals(client.address, clientFromV1Bundle.address)
7272
assertEquals(
73-
client.privateKeyBundleV1?.identityKey,
74-
clientFromV1Bundle.privateKeyBundleV1?.identityKey,
73+
client.privateKeyBundleV1.identityKey,
74+
clientFromV1Bundle.privateKeyBundleV1.identityKey,
7575
)
7676
assertEquals(
77-
client.privateKeyBundleV1?.preKeysList,
78-
clientFromV1Bundle.privateKeyBundleV1?.preKeysList,
77+
client.privateKeyBundleV1.preKeysList,
78+
clientFromV1Bundle.privateKeyBundleV1.preKeysList,
7979
)
8080
}
8181

@@ -93,7 +93,8 @@ class ClientTest {
9393
assert(client.canMessageV3(listOf(client.address)))
9494

9595
val bundle = client.privateKeyBundle
96-
val clientFromV1Bundle = Client().buildFromBundle(bundle, account = fakeWallet, options = options)
96+
val clientFromV1Bundle =
97+
Client().buildFromBundle(bundle, account = fakeWallet, options = options)
9798
assertEquals(client.address, clientFromV1Bundle.address)
9899
assertEquals(
99100
client.privateKeyBundleV1.identityKey,
@@ -147,9 +148,12 @@ class ClientTest {
147148
appContext = context
148149
)
149150
)
150-
client.conversations.newGroup(listOf(client2.address,))
151-
runBlocking { client.conversations.syncGroups() }
152-
assertEquals(client.conversations.listGroups().size, 1)
151+
152+
runBlocking {
153+
client.conversations.newGroup(listOf(client2.address))
154+
client.conversations.syncGroups()
155+
assertEquals(client.conversations.listGroups().size, 1)
156+
}
153157

154158
client.deleteLocalDatabase()
155159

@@ -163,8 +167,10 @@ class ClientTest {
163167
)
164168
)
165169

166-
runBlocking { client.conversations.syncGroups() }
167-
assertEquals(client.conversations.listGroups().size, 0)
170+
runBlocking {
171+
client.conversations.syncGroups()
172+
assertEquals(client.conversations.listGroups().size, 0)
173+
}
168174
}
169175

170176
@Test
@@ -206,7 +212,7 @@ class ClientTest {
206212
val notOnNetwork = PrivateKeyBuilder()
207213
val opts = ClientOptions(ClientOptions.Api(XMTPEnvironment.LOCAL, false))
208214
val aliceClient = Client().create(aliceWallet, opts)
209-
aliceClient.ensureUserContactPublished()
215+
runBlocking { aliceClient.ensureUserContactPublished() }
210216

211217
val canMessage = Client.canMessage(aliceWallet.address, opts)
212218
val cannotMessage = Client.canMessage(notOnNetwork.address, opts)

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

+45-30
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.xmtp.android.library
22

33
import androidx.test.ext.junit.runners.AndroidJUnit4
44
import com.google.protobuf.kotlin.toByteStringUtf8
5+
import kotlinx.coroutines.runBlocking
56
import org.junit.Assert.assertEquals
67
import org.junit.Assert.assertTrue
78
import org.junit.Test
@@ -58,13 +59,16 @@ class CodecTest {
5859
Client.register(codec = NumberCodec())
5960
val fixtures = fixtures()
6061
val aliceClient = fixtures.aliceClient
61-
val aliceConversation =
62+
val aliceConversation = runBlocking {
6263
aliceClient.conversations.newConversation(fixtures.bob.walletAddress)
63-
aliceConversation.send(
64-
content = 3.14,
65-
options = SendOptions(contentType = NumberCodec().contentType),
66-
)
67-
val messages = aliceConversation.messages()
64+
}
65+
runBlocking {
66+
aliceConversation.send(
67+
content = 3.14,
68+
options = SendOptions(contentType = NumberCodec().contentType),
69+
)
70+
}
71+
val messages = runBlocking { aliceConversation.messages() }
6872
assertEquals(messages.size, 1)
6973
if (messages.size == 1) {
7074
val content: Double? = messages[0].content()
@@ -78,15 +82,18 @@ class CodecTest {
7882
Client.register(codec = CompositeCodec())
7983
val fixtures = fixtures()
8084
val aliceClient = fixtures.aliceClient
81-
val aliceConversation =
85+
val aliceConversation = runBlocking {
8286
aliceClient.conversations.newConversation(fixtures.bob.walletAddress)
87+
}
8388
val textContent = TextCodec().encode(content = "hiya")
8489
val source = DecodedComposite(encodedContent = textContent)
85-
aliceConversation.send(
86-
content = source,
87-
options = SendOptions(contentType = CompositeCodec().contentType),
88-
)
89-
val messages = aliceConversation.messages()
90+
runBlocking {
91+
aliceConversation.send(
92+
content = source,
93+
options = SendOptions(contentType = CompositeCodec().contentType),
94+
)
95+
}
96+
val messages = runBlocking { aliceConversation.messages() }
9097
val decoded: DecodedComposite? = messages[0].content()
9198
assertEquals("hiya", decoded?.content())
9299
}
@@ -97,8 +104,9 @@ class CodecTest {
97104
Client.register(codec = NumberCodec())
98105
val fixtures = fixtures()
99106
val aliceClient = fixtures.aliceClient!!
100-
val aliceConversation =
107+
val aliceConversation = runBlocking {
101108
aliceClient.conversations.newConversation(fixtures.bob.walletAddress)
109+
}
102110
val textContent = TextCodec().encode(content = "sup")
103111
val numberContent = NumberCodec().encode(content = 3.14)
104112
val source = DecodedComposite(
@@ -107,11 +115,13 @@ class CodecTest {
107115
DecodedComposite(parts = listOf(DecodedComposite(encodedContent = numberContent))),
108116
),
109117
)
110-
aliceConversation.send(
111-
content = source,
112-
options = SendOptions(contentType = CompositeCodec().contentType),
113-
)
114-
val messages = aliceConversation.messages()
118+
runBlocking {
119+
aliceConversation.send(
120+
content = source,
121+
options = SendOptions(contentType = CompositeCodec().contentType),
122+
)
123+
}
124+
val messages = runBlocking { aliceConversation.messages() }
115125
val decoded: DecodedComposite? = messages[0].content()
116126
val part1 = decoded!!.parts[0]
117127
val part2 = decoded.parts[1].parts[0]
@@ -124,14 +134,17 @@ class CodecTest {
124134
val codec = NumberCodec()
125135
Client.register(codec = codec)
126136
val fixtures = fixtures()
127-
val aliceClient = fixtures.aliceClient!!
128-
val aliceConversation =
137+
val aliceClient = fixtures.aliceClient
138+
val aliceConversation = runBlocking {
129139
aliceClient.conversations.newConversation(fixtures.bob.walletAddress)
130-
aliceConversation.send(
131-
content = 3.14,
132-
options = SendOptions(contentType = codec.contentType),
133-
)
134-
val messages = aliceConversation.messages()
140+
}
141+
runBlocking {
142+
aliceConversation.send(
143+
content = 3.14,
144+
options = SendOptions(contentType = codec.contentType),
145+
)
146+
}
147+
val messages = runBlocking { aliceConversation.messages() }
135148
assert(messages.isNotEmpty())
136149

137150
val message = MessageV2Builder.buildEncode(
@@ -156,12 +169,14 @@ class CodecTest {
156169
repeat(5) {
157170
val account = PrivateKeyBuilder()
158171
val client = Client().create(account, clientOptions)
159-
conversations.add(
160-
alixClient.conversations.newConversation(
161-
client.address,
162-
context = InvitationV1ContextBuilder.buildFromConversation(conversationId = "hi")
172+
runBlocking {
173+
conversations.add(
174+
alixClient.conversations.newConversation(
175+
client.address,
176+
context = InvitationV1ContextBuilder.buildFromConversation(conversationId = "hi")
177+
)
163178
)
164-
)
179+
}
165180
}
166181

167182
val thirtyDayPeriodsSinceEpoch = Instant.now().epochSecond / 60 / 60 / 24 / 30

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package org.xmtp.android.library
22

33
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import kotlinx.coroutines.runBlocking
45
import org.junit.Assert.assertEquals
56
import org.junit.Test
67
import org.junit.runner.RunWith
78
import org.xmtp.android.library.messages.walletAddress
9+
810
@RunWith(AndroidJUnit4::class)
911
class ContactsTest {
1012

1113
@Test
1214
fun testNormalizesAddresses() {
1315
val fixtures = fixtures()
14-
fixtures.bobClient.ensureUserContactPublished()
16+
runBlocking { fixtures.bobClient.ensureUserContactPublished() }
1517
val bobAddressLowerCased = fixtures.bobClient.address.lowercase()
1618
val bobContact = fixtures.aliceClient.getUserContact(peerAddress = bobAddressLowerCased)
1719
assert(bobContact != null)
@@ -20,15 +22,15 @@ class ContactsTest {
2022
@Test
2123
fun testCanFindContact() {
2224
val fixtures = fixtures()
23-
fixtures.bobClient.ensureUserContactPublished()
25+
runBlocking { fixtures.bobClient.ensureUserContactPublished() }
2426
val contactBundle = fixtures.aliceClient.contacts.find(fixtures.bob.walletAddress)
2527
assertEquals(contactBundle?.walletAddress, fixtures.bob.walletAddress)
2628
}
2729

2830
@Test
2931
fun testCachesContacts() {
3032
val fixtures = fixtures()
31-
fixtures.bobClient.ensureUserContactPublished()
33+
runBlocking { fixtures.bobClient.ensureUserContactPublished() }
3234
// Look up the first time
3335
fixtures.aliceClient.contacts.find(fixtures.bob.walletAddress)
3436
fixtures.fakeApiClient.assertNoQuery {
@@ -47,7 +49,7 @@ class ContactsTest {
4749

4850
assert(!result)
4951

50-
contacts.allow(listOf(fixtures.alice.walletAddress))
52+
runBlocking { contacts.allow(listOf(fixtures.alice.walletAddress)) }
5153

5254
result = contacts.isAllowed(fixtures.alice.walletAddress)
5355
assert(result)
@@ -62,7 +64,7 @@ class ContactsTest {
6264

6365
assert(!result)
6466

65-
contacts.deny(listOf(fixtures.alice.walletAddress))
67+
runBlocking { contacts.deny(listOf(fixtures.alice.walletAddress)) }
6668

6769
result = contacts.isDenied(fixtures.alice.walletAddress)
6870
assert(result)

0 commit comments

Comments
 (0)