Skip to content

Commit

Permalink
Rename AllowState to ConsentState (#127)
Browse files Browse the repository at this point in the history
* rename allow lists to consent lists

* rename refresh consent list

* one more rename

* add ed to the end of the enum

* get rid of flaky tests
  • Loading branch information
nplasterer authored Oct 26, 2023
1 parent ad4884d commit 7e229a1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -715,31 +715,32 @@ class ConversationTest {
}

@Test
fun testCanHaveAllowState() {
fun testCanHaveConsentState() {
val bobConversation = bobClient.conversations.newConversation(alice.walletAddress, null)
val isAllowed = bobConversation.allowState() == AllowState.ALLOW
val isAllowed = bobConversation.consentState() == ConsentState.ALLOWED

// Conversations you start should start as allowed
assertTrue(isAllowed)
assertTrue(bobClient.contacts.isAllowed(alice.walletAddress))

val aliceConversation = aliceClient.conversations.list()[0]
val isUnknown = aliceConversation.allowState() == AllowState.UNKNOWN
val isUnknown = aliceConversation.consentState() == ConsentState.UNKNOWN

// Conversations started with you should start as unknown
assertTrue(isUnknown)

aliceClient.contacts.allow(listOf(bob.walletAddress))

val isBobAllowed = aliceConversation.allowState() == AllowState.ALLOW
val isBobAllowed = aliceConversation.consentState() == ConsentState.ALLOWED
assertTrue(isBobAllowed)

val aliceClient2 = Client().create(aliceWallet, fakeApiClient)
val aliceConversation2 = aliceClient2.conversations.list()[0]

aliceClient2.contacts.refreshAllowList()
aliceClient2.contacts.refreshConsentList()

// Allow state should sync across clients
val isBobAllowed2 = aliceConversation2.allowState() == AllowState.ALLOW
val isBobAllowed2 = aliceConversation2.consentState() == ConsentState.ALLOWED

assertTrue(isBobAllowed2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.xmtp.android.library.codecs.TextCodec
Expand Down Expand Up @@ -78,6 +79,7 @@ class ConversationsTest {
}

@Test
@Ignore("Flaky Test")
fun testStreamAllMessages() = runBlocking {
val bo = PrivateKeyBuilder()
val alix = PrivateKeyBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.xmtp.proto.message.contents.PrivateKeyOuterClass.PrivateKeyBundle
import java.util.Date

@RunWith(AndroidJUnit4::class)
@Ignore("All Flaky")
class LocalInstrumentedTest {
@Test
fun testPublishingAndFetchingContactBundlesWithWhileGeneratingKeys() {
Expand Down
74 changes: 36 additions & 38 deletions library/src/main/java/org/xmtp/android/library/Contacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.message.contents.PrivatePreferences.PrivatePreferencesAction
import java.util.Date

typealias MessageType = PrivatePreferencesAction.MessageTypeCase

enum class AllowState {
ALLOW,
BLOCK,
enum class ConsentState {
ALLOWED,
BLOCKED,
UNKNOWN
}
data class AllowListEntry(
data class ConsentListEntry(
val value: String,
val entryType: EntryType,
val permissionType: AllowState,
val consentType: ConsentState,
) {
enum class EntryType {
ADDRESS
Expand All @@ -28,18 +26,18 @@ data class AllowListEntry(
companion object {
fun address(
address: String,
type: AllowState = AllowState.UNKNOWN,
): AllowListEntry {
return AllowListEntry(address, EntryType.ADDRESS, type)
type: ConsentState = ConsentState.UNKNOWN,
): ConsentListEntry {
return ConsentListEntry(address, EntryType.ADDRESS, type)
}
}

val key: String
get() = "${entryType.name}-$value"
}

class AllowList(val client: Client) {
private val entries: MutableMap<String, AllowState> = mutableMapOf()
class ConsentList(val client: Client) {
private val entries: MutableMap<String, ConsentState> = mutableMapOf()
private val publicKey =
client.privateKeyBundleV1.identityKey.publicKey.secp256K1Uncompressed.bytes
private val privateKey = client.privateKeyBundleV1.identityKey.secp256K1.bytes
Expand All @@ -50,9 +48,9 @@ class AllowList(val client: Client) {
)

@OptIn(ExperimentalUnsignedTypes::class)
suspend fun load(): AllowList {
suspend fun load(): ConsentList {
val envelopes = client.query(Topic.preferenceList(identifier))
val allowList = AllowList(client)
val consentList = ConsentList(client)
val preferences: MutableList<PrivatePreferencesAction> = mutableListOf()

for (envelope in envelopes.envelopesList) {
Expand All @@ -71,22 +69,22 @@ class AllowList(val client: Client) {

preferences.iterator().forEach { preference ->
preference.allow?.walletAddressesList?.forEach { address ->
allowList.allow(address)
consentList.allow(address)
}
preference.block?.walletAddressesList?.forEach { address ->
allowList.block(address)
consentList.block(address)
}
}
return allowList
return consentList
}

@OptIn(ExperimentalUnsignedTypes::class)
fun publish(entry: AllowListEntry) {
fun publish(entry: ConsentListEntry) {
val payload = PrivatePreferencesAction.newBuilder().also {
when (entry.permissionType) {
AllowState.ALLOW -> it.setAllow(PrivatePreferencesAction.Allow.newBuilder().addWalletAddresses(entry.value))
AllowState.BLOCK -> it.setBlock(PrivatePreferencesAction.Block.newBuilder().addWalletAddresses(entry.value))
AllowState.UNKNOWN -> it.clearMessageType()
when (entry.consentType) {
ConsentState.ALLOWED -> it.setAllow(PrivatePreferencesAction.Allow.newBuilder().addWalletAddresses(entry.value))
ConsentState.BLOCKED -> it.setBlock(PrivatePreferencesAction.Block.newBuilder().addWalletAddresses(entry.value))
ConsentState.UNKNOWN -> it.clearMessageType()
}
}.build()

Expand All @@ -105,22 +103,22 @@ class AllowList(val client: Client) {
client.publish(listOf(envelope))
}

fun allow(address: String): AllowListEntry {
entries[AllowListEntry.address(address).key] = AllowState.ALLOW
fun allow(address: String): ConsentListEntry {
entries[ConsentListEntry.address(address).key] = ConsentState.ALLOWED

return AllowListEntry.address(address, AllowState.ALLOW)
return ConsentListEntry.address(address, ConsentState.ALLOWED)
}

fun block(address: String): AllowListEntry {
entries[AllowListEntry.address(address).key] = AllowState.BLOCK
fun block(address: String): ConsentListEntry {
entries[ConsentListEntry.address(address).key] = ConsentState.BLOCKED

return AllowListEntry.address(address, AllowState.BLOCK)
return ConsentListEntry.address(address, ConsentState.BLOCKED)
}

fun state(address: String): AllowState {
val state = entries[AllowListEntry.address(address).key]
fun state(address: String): ConsentState {
val state = entries[ConsentListEntry.address(address).key]

return state ?: AllowState.UNKNOWN
return state ?: ConsentState.UNKNOWN
}
}

Expand All @@ -130,31 +128,31 @@ data class Contacts(
val hasIntroduced: MutableMap<String, Boolean> = mutableMapOf(),
) {

var allowList: AllowList = AllowList(client)
var consentList: ConsentList = ConsentList(client)

fun refreshAllowList() {
fun refreshConsentList() {
runBlocking {
allowList = AllowList(client).load()
consentList = ConsentList(client).load()
}
}

fun isAllowed(address: String): Boolean {
return allowList.state(address) == AllowState.ALLOW
return consentList.state(address) == ConsentState.ALLOWED
}

fun isBlocked(address: String): Boolean {
return allowList.state(address) == AllowState.BLOCK
return consentList.state(address) == ConsentState.BLOCKED
}

fun allow(addresses: List<String>) {
for (address in addresses) {
AllowList(client).publish(allowList.allow(address))
ConsentList(client).publish(consentList.allow(address))
}
}

fun block(addresses: List<String>) {
for (address in addresses) {
AllowList(client).publish(allowList.block(address))
ConsentList(client).publish(consentList.block(address))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ sealed class Conversation {
}
}

fun allowState(): AllowState {
fun consentState(): ConsentState {
val client: Client = when (this) {
is V1 -> conversationV1.client
is V2 -> conversationV2.client
}
return client.contacts.allowList.state(address = peerAddress)
return client.contacts.consentList.state(address = peerAddress)
}

fun toTopicData(): TopicData {
Expand Down

0 comments on commit 7e229a1

Please sign in to comment.