Skip to content

Commit

Permalink
Consent Streaming (#333)
Browse files Browse the repository at this point in the history
* bump bindings

* bump bindings

* add test for consent streaming

* add ability to stream consent

* fix lint issue

* Update library/src/main/java/org/xmtp/android/library/PrivatePreferences.kt

Co-authored-by: Cameron Voell <[email protected]>

---------

Co-authored-by: Cameron Voell <[email protected]>
  • Loading branch information
nplasterer and cameronvoell authored Nov 25, 2024
1 parent 0e831ca commit da59bca
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ class ConversationsTest {
boDm?.sync()
alixClient.conversations.sync()
alixClient2.conversations.sync()
alixClient2.syncConsent()
alixClient2.preferences.syncConsent()
alixClient.conversations.syncAllConversations()
Thread.sleep(2000)
alixClient2.conversations.syncAllConversations()
Thread.sleep(2000)
val dm2 = alixClient2.findConversation(dm.id)!!
assertEquals(ConsentState.DENIED, dm2.consentState())
alixClient2.preferences.consentList.setConsentState(
alixClient2.preferences.setConsentState(
listOf(
ConsentListEntry(
dm2.id,
Expand All @@ -228,10 +228,76 @@ class ConversationsTest {
)
)
assertEquals(
alixClient2.preferences.consentList.conversationState(dm2.id),
alixClient2.preferences.conversationState(dm2.id),
ConsentState.ALLOWED
)
assertEquals(dm2.consentState(), ConsentState.ALLOWED)
}
}

@Test
fun testStreamConsent() {
val key = SecureRandom().generateSeed(32)
val context = InstrumentationRegistry.getInstrumentation().targetContext
val alixWallet = PrivateKeyBuilder()

val alixClient = runBlocking {
Client().create(
account = alixWallet,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
appContext = context,
dbEncryptionKey = key
)
)
}
val alixGroup = runBlocking { alixClient.conversations.newGroup(listOf(bo.walletAddress)) }

val alixClient2 = runBlocking {
Client().create(
account = alixWallet,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
appContext = context,
dbEncryptionKey = key,
dbDirectory = context.filesDir.absolutePath.toString()
)
)
}

runBlocking {
alixGroup.send("Hello")
alixClient2.conversations.sync()
alixClient.conversations.syncAllConversations()
alixClient2.conversations.syncAllConversations()
}
val alix2Group = alixClient2.findGroup(alixGroup.id)!!
val consent = mutableListOf<ConsentListEntry>()
val job = CoroutineScope(Dispatchers.IO).launch {
try {
alixClient.preferences.streamConsent()
.collect { entry ->
consent.add(entry)
}
} catch (e: Exception) {
}
}

Thread.sleep(1000)

runBlocking {
alix2Group.updateConsentState(ConsentState.DENIED)
val dm3 = alixClient2.conversations.newConversation(caro.walletAddress)
dm3.updateConsentState(ConsentState.DENIED)
alixClient.conversations.sync()
alixClient2.conversations.sync()
alixClient.conversations.syncAllConversations()
alixClient2.conversations.syncAllConversations()
}

Thread.sleep(2000)
assertEquals(3, consent.size)
assertEquals(alixGroup.consentState(), ConsentState.DENIED)
job.cancel()
}
}
12 changes: 6 additions & 6 deletions library/src/androidTest/java/org/xmtp/android/library/DmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class DmTest {
dm.send("gm")
dm.sync()
assertEquals(
boClient.preferences.consentList.conversationState(dm.id),
boClient.preferences.conversationState(dm.id),
ConsentState.ALLOWED
)
assertEquals(dm.consentState(), ConsentState.ALLOWED)
Expand Down Expand Up @@ -330,13 +330,13 @@ class DmTest {
val dm =
boClient.conversations.findOrCreateDm(alix.walletAddress)
assertEquals(
boClient.preferences.consentList.conversationState(dm.id),
boClient.preferences.conversationState(dm.id),
ConsentState.ALLOWED
)

assertEquals(dm.consentState(), ConsentState.ALLOWED)

boClient.preferences.consentList.setConsentState(
boClient.preferences.setConsentState(
listOf(
ConsentListEntry(
dm.id,
Expand All @@ -346,12 +346,12 @@ class DmTest {
)
)
assertEquals(
boClient.preferences.consentList.conversationState(dm.id),
boClient.preferences.conversationState(dm.id),
ConsentState.DENIED
)
assertEquals(dm.consentState(), ConsentState.DENIED)

boClient.preferences.consentList.setConsentState(
boClient.preferences.setConsentState(
listOf(
ConsentListEntry(
dm.id,
Expand All @@ -361,7 +361,7 @@ class DmTest {
)
)
assertEquals(
boClient.preferences.consentList.conversationState(dm.id),
boClient.preferences.conversationState(dm.id),
ConsentState.ALLOWED
)
assertEquals(dm.consentState(), ConsentState.ALLOWED)
Expand Down
30 changes: 15 additions & 15 deletions library/src/androidTest/java/org/xmtp/android/library/GroupTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ class GroupTest {

runBlocking {
assertEquals(
boClient.preferences.consentList.conversationState(boGroup.id),
boClient.preferences.conversationState(boGroup.id),
ConsentState.ALLOWED
)
assertEquals(
alixClient.preferences.consentList.conversationState(alixGroup.id),
alixClient.preferences.conversationState(alixGroup.id),
ConsentState.UNKNOWN
)
}
Expand Down Expand Up @@ -420,7 +420,7 @@ class GroupTest {
group.sync()
assertEquals(group.consentState(), ConsentState.ALLOWED)
assertEquals(
boClient.preferences.consentList.conversationState(group.id),
boClient.preferences.conversationState(group.id),
ConsentState.ALLOWED
)
}
Expand Down Expand Up @@ -731,12 +731,12 @@ class GroupTest {
)
)
assertEquals(
boClient.preferences.consentList.conversationState(group.id),
boClient.preferences.conversationState(group.id),
ConsentState.ALLOWED
)
assertEquals(group.consentState(), ConsentState.ALLOWED)

boClient.preferences.consentList.setConsentState(
boClient.preferences.setConsentState(
listOf(
ConsentListEntry(
group.id,
Expand All @@ -746,14 +746,14 @@ class GroupTest {
)
)
assertEquals(
boClient.preferences.consentList.conversationState(group.id),
boClient.preferences.conversationState(group.id),
ConsentState.DENIED
)
assertEquals(group.consentState(), ConsentState.DENIED)

group.updateConsentState(ConsentState.ALLOWED)
assertEquals(
boClient.preferences.consentList.conversationState(group.id),
boClient.preferences.conversationState(group.id),
ConsentState.ALLOWED
)
assertEquals(group.consentState(), ConsentState.ALLOWED)
Expand All @@ -765,10 +765,10 @@ class GroupTest {
runBlocking {
val boGroup = boClient.conversations.newGroup(listOf(alix.walletAddress))
assertEquals(
boClient.preferences.consentList.inboxIdState(alixClient.inboxId),
boClient.preferences.inboxIdState(alixClient.inboxId),
ConsentState.UNKNOWN
)
boClient.preferences.consentList.setConsentState(
boClient.preferences.setConsentState(
listOf(
ConsentListEntry(
alixClient.inboxId,
Expand All @@ -781,11 +781,11 @@ class GroupTest {
assertEquals(alixMember!!.consentState, ConsentState.ALLOWED)

assertEquals(
boClient.preferences.consentList.inboxIdState(alixClient.inboxId),
boClient.preferences.inboxIdState(alixClient.inboxId),
ConsentState.ALLOWED
)

boClient.preferences.consentList.setConsentState(
boClient.preferences.setConsentState(
listOf(
ConsentListEntry(
alixClient.inboxId,
Expand All @@ -798,11 +798,11 @@ class GroupTest {
assertEquals(alixMember!!.consentState, ConsentState.DENIED)

assertEquals(
boClient.preferences.consentList.inboxIdState(alixClient.inboxId),
boClient.preferences.inboxIdState(alixClient.inboxId),
ConsentState.DENIED
)

boClient.preferences.consentList.setConsentState(
boClient.preferences.setConsentState(
listOf(
ConsentListEntry(
alixClient.address,
Expand All @@ -814,11 +814,11 @@ class GroupTest {
alixMember = boGroup.members().firstOrNull { it.inboxId == alixClient.inboxId }
assertEquals(alixMember!!.consentState, ConsentState.ALLOWED)
assertEquals(
boClient.preferences.consentList.inboxIdState(alixClient.inboxId),
boClient.preferences.inboxIdState(alixClient.inboxId),
ConsentState.ALLOWED
)
assertEquals(
boClient.preferences.consentList.addressState(alixClient.address),
boClient.preferences.addressState(alixClient.address),
ConsentState.ALLOWED
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ class SmartContractWalletTest {
)
}
assertEquals(
davonSCWClient.preferences.consentList.conversationState(davonGroup.id),
davonSCWClient.preferences.conversationState(davonGroup.id),
ConsentState.ALLOWED
)
assertEquals(davonGroup.consentState(), ConsentState.ALLOWED)

davonSCWClient.preferences.consentList.setConsentState(
davonSCWClient.preferences.setConsentState(
listOf(
ConsentListEntry(
davonGroup.id,
Expand All @@ -227,14 +227,14 @@ class SmartContractWalletTest {
)
)
assertEquals(
davonSCWClient.preferences.consentList.conversationState(davonGroup.id),
davonSCWClient.preferences.conversationState(davonGroup.id),
ConsentState.DENIED
)
assertEquals(davonGroup.consentState(), ConsentState.DENIED)

davonGroup.updateConsentState(ConsentState.ALLOWED)
assertEquals(
davonSCWClient.preferences.consentList.conversationState(davonGroup.id),
davonSCWClient.preferences.conversationState(davonGroup.id),
ConsentState.ALLOWED
)
assertEquals(davonGroup.consentState(), ConsentState.ALLOWED)
Expand All @@ -253,10 +253,10 @@ class SmartContractWalletTest {
)
}
assertEquals(
davonSCWClient.preferences.consentList.inboxIdState(boEOAClient.inboxId),
davonSCWClient.preferences.inboxIdState(boEOAClient.inboxId),
ConsentState.UNKNOWN
)
davonSCWClient.preferences.consentList.setConsentState(
davonSCWClient.preferences.setConsentState(
listOf(
ConsentListEntry(
boEOAClient.inboxId,
Expand All @@ -269,11 +269,11 @@ class SmartContractWalletTest {
assertEquals(alixMember!!.consentState, ConsentState.ALLOWED)

assertEquals(
davonSCWClient.preferences.consentList.inboxIdState(boEOAClient.inboxId),
davonSCWClient.preferences.inboxIdState(boEOAClient.inboxId),
ConsentState.ALLOWED
)

davonSCWClient.preferences.consentList.setConsentState(
davonSCWClient.preferences.setConsentState(
listOf(
ConsentListEntry(
boEOAClient.inboxId,
Expand All @@ -286,11 +286,11 @@ class SmartContractWalletTest {
assertEquals(alixMember!!.consentState, ConsentState.DENIED)

assertEquals(
davonSCWClient.preferences.consentList.inboxIdState(boEOAClient.inboxId),
davonSCWClient.preferences.inboxIdState(boEOAClient.inboxId),
ConsentState.DENIED
)

davonSCWClient.preferences.consentList.setConsentState(
davonSCWClient.preferences.setConsentState(
listOf(
ConsentListEntry(
eriSCWClient.address,
Expand All @@ -302,11 +302,11 @@ class SmartContractWalletTest {
alixMember = davonGroup.members().firstOrNull { it.inboxId == eriSCWClient.inboxId }
assertEquals(alixMember!!.consentState, ConsentState.ALLOWED)
assertEquals(
davonSCWClient.preferences.consentList.inboxIdState(eriSCWClient.inboxId),
davonSCWClient.preferences.inboxIdState(eriSCWClient.inboxId),
ConsentState.ALLOWED
)
assertEquals(
davonSCWClient.preferences.consentList.addressState(eriSCWClient.address),
davonSCWClient.preferences.addressState(eriSCWClient.address),
ConsentState.ALLOWED
)
}
Expand Down
4 changes: 0 additions & 4 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,6 @@ class Client() {
ffiClient.sendSyncRequest(FfiDeviceSyncKind.MESSAGES)
}

suspend fun syncConsent() {
ffiClient.sendSyncRequest(FfiDeviceSyncKind.CONSENT)
}

suspend fun inboxStatesForInboxIds(
refreshFromNetwork: Boolean,
inboxIds: List<String>,
Expand Down
Loading

0 comments on commit da59bca

Please sign in to comment.