@@ -42,6 +42,7 @@ import org.xmtp.android.library.Conversation
42
42
import org.xmtp.android.library.Conversations.*
43
43
import org.xmtp.android.library.EntryType
44
44
import org.xmtp.android.library.PreEventCallback
45
+ import org.xmtp.android.library.PreferenceType
45
46
import org.xmtp.android.library.SendOptions
46
47
import org.xmtp.android.library.SigningKey
47
48
import org.xmtp.android.library.WalletType
@@ -208,6 +209,7 @@ class XMTPModule : Module() {
208
209
" message" ,
209
210
" conversationMessage" ,
210
211
" consent" ,
212
+ " preferences" ,
211
213
)
212
214
213
215
Function (" address" ) { installationId: String ->
@@ -1262,6 +1264,12 @@ class XMTPModule : Module() {
1262
1264
}
1263
1265
}
1264
1266
1267
+ Function (" subscribeToPreferenceUpdates" ) { installationId: String ->
1268
+ logV(" subscribeToPreferenceUpdates" )
1269
+
1270
+ subscribeToPreferenceUpdates(installationId = installationId)
1271
+ }
1272
+
1265
1273
Function (" subscribeToConsent" ) { installationId: String ->
1266
1274
logV(" subscribeToConsent" )
1267
1275
@@ -1289,6 +1297,11 @@ class XMTPModule : Module() {
1289
1297
}
1290
1298
}
1291
1299
1300
+ Function (" unsubscribeFromPreferenceUpdates" ) { installationId: String ->
1301
+ logV(" unsubscribeFromPreferenceUpdates" )
1302
+ subscriptions[getPreferenceUpdatesKey(installationId)]?.cancel()
1303
+ }
1304
+
1292
1305
Function (" unsubscribeFromConsent" ) { installationId: String ->
1293
1306
logV(" unsubscribeFromConsent" )
1294
1307
subscriptions[getConsentKey(installationId)]?.cancel()
@@ -1417,6 +1430,35 @@ class XMTPModule : Module() {
1417
1430
}
1418
1431
}
1419
1432
1433
+ private fun preferenceTypeToString (type : PreferenceType ): String {
1434
+ return when (type) {
1435
+ PreferenceType .HMAC_KEYS -> " hmac_keys"
1436
+ }
1437
+ }
1438
+
1439
+ private fun subscribeToPreferenceUpdates (installationId : String ) {
1440
+ val client = clients[installationId] ? : throw XMTPException (" No client" )
1441
+
1442
+ subscriptions[getPreferenceUpdatesKey(installationId)]?.cancel()
1443
+ subscriptions[getPreferenceUpdatesKey(installationId)] =
1444
+ CoroutineScope (Dispatchers .IO ).launch {
1445
+ try {
1446
+ client.preferences.streamPreferenceUpdates().collect { type ->
1447
+ sendEvent(
1448
+ " preferences" ,
1449
+ mapOf (
1450
+ " installationId" to installationId,
1451
+ " preferenceType" to preferenceTypeToString(type)
1452
+ )
1453
+ )
1454
+ }
1455
+ } catch (e: Exception ) {
1456
+ Log .e(" XMTPModule" , " Error in preference subscription: $e " )
1457
+ subscriptions[getPreferenceUpdatesKey(installationId)]?.cancel()
1458
+ }
1459
+ }
1460
+ }
1461
+
1420
1462
private fun subscribeToConsent (installationId : String ) {
1421
1463
val client = clients[installationId] ? : throw XMTPException (" No client" )
1422
1464
@@ -1434,7 +1476,7 @@ class XMTPModule : Module() {
1434
1476
)
1435
1477
}
1436
1478
} catch (e: Exception ) {
1437
- Log .e(" XMTPModule" , " Error in group subscription: $e " )
1479
+ Log .e(" XMTPModule" , " Error in consent subscription: $e " )
1438
1480
subscriptions[getConsentKey(installationId)]?.cancel()
1439
1481
}
1440
1482
}
@@ -1513,6 +1555,10 @@ class XMTPModule : Module() {
1513
1555
}
1514
1556
}
1515
1557
1558
+ private fun getPreferenceUpdatesKey (installationId : String ): String {
1559
+ return " preferences:$installationId "
1560
+ }
1561
+
1516
1562
private fun getConsentKey (installationId : String ): String {
1517
1563
return " consent:$installationId "
1518
1564
}
0 commit comments