Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consent Streaming #333

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking if I understand - there are 3 consent entries because when alixClient2 creates the dm3 group with caro, it automatically makes an entry of ALLOWED, and the other two entries are making the alix2Group DENIED, and setting the dm3 group DENIED. Is that right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct!

assertEquals(alixGroup.consentState(), ConsentState.DENIED)
job.cancel()
}
}
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
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
Loading