Skip to content

Commit

Permalink
make it easier to check
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 26, 2023
1 parent 0d60680 commit 19595dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -718,20 +718,20 @@ class ConversationTest {
@Test
fun testCanHaveAllowState() {
val bobConversation = bobClient.conversations.newConversation(alice.walletAddress, null)
val isAllowed = bobConversation.allowState() == PrivatePreferences.PrivatePreferencesAction.MessageTypeCase.ALLOW
val isAllowed = bobConversation.allowState() == AllowState.ALLOW

// Conversations you start should start as allowed
assertTrue(isAllowed)

val aliceConversation = aliceClient.conversations.list()[0]
val isUnknown = aliceConversation.allowState() == PrivatePreferences.PrivatePreferencesAction.MessageTypeCase.MESSAGETYPE_NOT_SET
val isUnknown = aliceConversation.allowState() == AllowState.UNKNOWN

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

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

val isBobAllowed = aliceConversation.allowState() == PrivatePreferences.PrivatePreferencesAction.MessageTypeCase.ALLOW
val isBobAllowed = aliceConversation.allowState() == AllowState.ALLOW
assertTrue(isBobAllowed)

val aliceClient2 = Client().create(aliceWallet, fakeApiClient)
Expand All @@ -740,7 +740,7 @@ class ConversationTest {
aliceClient2.contacts.refreshAllowList()

// Allow state should sync across clients
val isBobAllowed2 = aliceConversation2.allowState() == PrivatePreferences.PrivatePreferencesAction.MessageTypeCase.ALLOW
val isBobAllowed2 = aliceConversation2.allowState() == AllowState.ALLOW

assertTrue(isBobAllowed2)
}
Expand Down
39 changes: 19 additions & 20 deletions library/src/main/java/org/xmtp/android/library/Contacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import java.util.Date

typealias MessageType = PrivatePreferencesAction.MessageTypeCase

enum class AllowState {
ALLOW,
BLOCK,
UNKNOWN
}
data class AllowListEntry(
val value: String,
val entryType: EntryType,
val permissionType: MessageType,
val permissionType: AllowState,
) {
enum class EntryType {
ADDRESS
Expand All @@ -23,7 +28,7 @@ data class AllowListEntry(
companion object {
fun address(
address: String,
type: MessageType = MessageType.MESSAGETYPE_NOT_SET,
type: AllowState = AllowState.UNKNOWN,
): AllowListEntry {
return AllowListEntry(address, EntryType.ADDRESS, type)
}
Expand All @@ -34,7 +39,7 @@ data class AllowListEntry(
}

class AllowList(val client: Client) {
private val entries: MutableMap<String, MessageType> = mutableMapOf()
private val entries: MutableMap<String, AllowState> = mutableMapOf()
private val publicKey =
client.privateKeyBundleV1.identityKey.publicKey.secp256K1Uncompressed.bytes
private val privateKey = client.privateKeyBundleV1.identityKey.secp256K1.bytes
Expand Down Expand Up @@ -79,15 +84,9 @@ class AllowList(val client: Client) {
fun publish(entry: AllowListEntry) {
val payload = PrivatePreferencesAction.newBuilder().also {
when (entry.permissionType) {
PrivatePreferencesAction.MessageTypeCase.ALLOW -> it.setAllow(
PrivatePreferencesAction.Allow.newBuilder().addWalletAddresses(entry.value)
)

PrivatePreferencesAction.MessageTypeCase.BLOCK -> it.setBlock(
PrivatePreferencesAction.Block.newBuilder().addWalletAddresses(entry.value)
)

PrivatePreferencesAction.MessageTypeCase.MESSAGETYPE_NOT_SET -> it.clearMessageType()
AllowState.ALLOW -> it.setAllow(PrivatePreferencesAction.Allow.newBuilder().addWalletAddresses(entry.value))
AllowState.BLOCK -> it.setBlock(PrivatePreferencesAction.Block.newBuilder().addWalletAddresses(entry.value))
AllowState.UNKNOWN -> it.clearMessageType()
}
}.build()

Expand All @@ -107,21 +106,21 @@ class AllowList(val client: Client) {
}

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

return AllowListEntry.address(address, MessageType.ALLOW)
return AllowListEntry.address(address, AllowState.ALLOW)
}

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

return AllowListEntry.address(address, MessageType.BLOCK)
return AllowListEntry.address(address, AllowState.BLOCK)
}

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

return state ?: MessageType.MESSAGETYPE_NOT_SET
return state ?: AllowState.UNKNOWN
}
}

Expand All @@ -140,11 +139,11 @@ data class Contacts(
}

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

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

fun allow(addresses: List<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ sealed class Conversation {
}
}

fun allowState(): MessageType {
fun allowState(): AllowState {
val client: Client = when (this) {
is V1 -> conversationV1.client
is V2 -> conversationV2.client
Expand Down

0 comments on commit 19595dc

Please sign in to comment.