@@ -10,7 +10,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
10
10
import kotlinx.coroutines.flow.callbackFlow
11
11
import kotlinx.coroutines.flow.flow
12
12
import kotlinx.coroutines.flow.merge
13
- import kotlinx.coroutines.runBlocking
14
13
import org.xmtp.android.library.GRPCApiClient.Companion.makeQueryRequest
15
14
import org.xmtp.android.library.GRPCApiClient.Companion.makeSubscribeRequest
16
15
import org.xmtp.android.library.libxmtp.Message
@@ -127,17 +126,19 @@ data class Conversations(
127
126
libXMTPConversations?.sync()
128
127
}
129
128
130
- fun listGroups (after : Date ? = null, before : Date ? = null, limit : Int? = null): List <Group > {
131
- return runBlocking {
132
- libXMTPConversations?.list(
133
- opts = FfiListConversationsOptions (
134
- after?.time?.nanoseconds?.toLong(DurationUnit .NANOSECONDS ),
135
- before?.time?.nanoseconds?.toLong(DurationUnit .NANOSECONDS ),
136
- limit?.toLong()
137
- )
138
- )?.map {
139
- Group (client, it)
140
- }
129
+ suspend fun listGroups (
130
+ after : Date ? = null,
131
+ before : Date ? = null,
132
+ limit : Int? = null,
133
+ ): List <Group > {
134
+ return libXMTPConversations?.list(
135
+ opts = FfiListConversationsOptions (
136
+ after?.time?.nanoseconds?.toLong(DurationUnit .NANOSECONDS ),
137
+ before?.time?.nanoseconds?.toLong(DurationUnit .NANOSECONDS ),
138
+ limit?.toLong()
139
+ )
140
+ )?.map {
141
+ Group (client, it)
141
142
} ? : emptyList()
142
143
}
143
144
@@ -234,7 +235,7 @@ data class Conversations(
234
235
* Get the list of conversations that current user has
235
236
* @return The list of [Conversation] that the current [Client] has.
236
237
*/
237
- fun list (includeGroups : Boolean = false): List <Conversation > {
238
+ suspend fun list (includeGroups : Boolean = false): List <Conversation > {
238
239
val newConversations = mutableListOf<Conversation >()
239
240
val mostRecent = conversationsByTopic.values.maxOfOrNull { it.createdAt }
240
241
val pagination = Pagination (after = mostRecent)
@@ -264,10 +265,8 @@ data class Conversations(
264
265
}.map { Pair (it.topic, it) }
265
266
266
267
if (includeGroups) {
267
- val groups = runBlocking {
268
- syncGroups()
269
- listGroups()
270
- }
268
+ syncGroups()
269
+ val groups = listGroups()
271
270
conversationsByTopic + = groups.map { Pair (it.id.toString(), Conversation .Group (it)) }
272
271
}
273
272
return conversationsByTopic.values.sortedByDescending { it.createdAt }
@@ -338,14 +337,11 @@ data class Conversations(
338
337
return hmacKeysResponse.build()
339
338
}
340
339
341
- private fun listIntroductionPeers (pagination : Pagination ? = null): Map <String , Date > {
342
- val envelopes =
343
- runBlocking {
344
- client.apiClient.queryTopic(
345
- topic = Topic .userIntro(client.address),
346
- pagination = pagination,
347
- ).envelopesList
348
- }
340
+ private suspend fun listIntroductionPeers (pagination : Pagination ? = null): Map <String , Date > {
341
+ val envelopes = client.apiClient.queryTopic(
342
+ topic = Topic .userIntro(client.address),
343
+ pagination = pagination,
344
+ ).envelopesList
349
345
val messages = envelopes.mapNotNull { envelope ->
350
346
try {
351
347
val message = MessageV1Builder .buildFromBytes(envelope.message.toByteArray())
@@ -381,10 +377,9 @@ data class Conversations(
381
377
* @param pagination Information of the topics, ranges (dates), etc.
382
378
* @return List of [SealedInvitation] that are inside of the range specified by [pagination]
383
379
*/
384
- private fun listInvitations (pagination : Pagination ? = null): List <SealedInvitation > {
385
- val envelopes = runBlocking {
380
+ private suspend fun listInvitations (pagination : Pagination ? = null): List <SealedInvitation > {
381
+ val envelopes =
386
382
client.apiClient.envelopes(Topic .userInvite(client.address).description, pagination)
387
- }
388
383
return envelopes.map { envelope ->
389
384
SealedInvitation .parseFrom(envelope.message)
390
385
}
@@ -404,7 +399,7 @@ data class Conversations(
404
399
* This pulls messages from multiple conversations in a single call.
405
400
* @see Conversation.messages
406
401
*/
407
- fun listBatchMessages (
402
+ suspend fun listBatchMessages (
408
403
topics : List <Pair <String , Pagination ?>>,
409
404
): List <DecodedMessage > {
410
405
val requests = topics.map { (topic, page) ->
@@ -416,21 +411,19 @@ data class Conversations(
416
411
val messages: MutableList <DecodedMessage > = mutableListOf ()
417
412
val batches = requests.chunked(maxQueryRequestsPerBatch)
418
413
for (batch in batches) {
419
- runBlocking {
420
- messages.addAll(
421
- client.batchQuery(batch).responsesOrBuilderList.flatMap { res ->
422
- res.envelopesList.mapNotNull { envelope ->
423
- val conversation = conversationsByTopic[envelope.contentTopic]
424
- if (conversation == null ) {
425
- Log .d(TAG , " discarding message, unknown conversation $envelope " )
426
- return @mapNotNull null
427
- }
428
- val msg = conversation.decodeOrNull(envelope)
429
- msg
414
+ messages.addAll(
415
+ client.batchQuery(batch).responsesOrBuilderList.flatMap { res ->
416
+ res.envelopesList.mapNotNull { envelope ->
417
+ val conversation = conversationsByTopic[envelope.contentTopic]
418
+ if (conversation == null ) {
419
+ Log .d(TAG , " discarding message, unknown conversation $envelope " )
420
+ return @mapNotNull null
430
421
}
431
- },
432
- )
433
- }
422
+ val msg = conversation.decodeOrNull(envelope)
423
+ msg
424
+ }
425
+ },
426
+ )
434
427
}
435
428
return messages
436
429
}
@@ -440,7 +433,7 @@ data class Conversations(
440
433
* This pulls messages from multiple conversations in a single call.
441
434
* @see listBatchMessages
442
435
*/
443
- fun listBatchDecryptedMessages (
436
+ suspend fun listBatchDecryptedMessages (
444
437
topics : List <Pair <String , Pagination ?>>,
445
438
): List <DecryptedMessage > {
446
439
val requests = topics.map { (topic, page) ->
@@ -452,26 +445,24 @@ data class Conversations(
452
445
val messages: MutableList <DecryptedMessage > = mutableListOf ()
453
446
val batches = requests.chunked(maxQueryRequestsPerBatch)
454
447
for (batch in batches) {
455
- runBlocking {
456
- messages.addAll(
457
- client.batchQuery(batch).responsesOrBuilderList.flatMap { res ->
458
- res.envelopesList.mapNotNull { envelope ->
459
- val conversation = conversationsByTopic[envelope.contentTopic]
460
- if (conversation == null ) {
461
- Log .d(TAG , " discarding message, unknown conversation $envelope " )
462
- return @mapNotNull null
463
- }
464
- try {
465
- val msg = conversation.decrypt(envelope)
466
- msg
467
- } catch (e: Exception ) {
468
- Log .e(TAG , " Error decrypting message: $envelope " , e)
469
- null
470
- }
448
+ messages.addAll(
449
+ client.batchQuery(batch).responsesOrBuilderList.flatMap { res ->
450
+ res.envelopesList.mapNotNull { envelope ->
451
+ val conversation = conversationsByTopic[envelope.contentTopic]
452
+ if (conversation == null ) {
453
+ Log .d(TAG , " discarding message, unknown conversation $envelope " )
454
+ return @mapNotNull null
471
455
}
472
- },
473
- )
474
- }
456
+ try {
457
+ val msg = conversation.decrypt(envelope)
458
+ msg
459
+ } catch (e: Exception ) {
460
+ Log .e(TAG , " Error decrypting message: $envelope " , e)
461
+ null
462
+ }
463
+ }
464
+ },
465
+ )
475
466
}
476
467
return messages
477
468
}
0 commit comments