Skip to content

Commit

Permalink
write tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 25, 2023
1 parent 761dffd commit c06ffea
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import org.xmtp.proto.message.contents.Invitation
import org.xmtp.proto.message.contents.Invitation.InvitationV1.Context
import org.xmtp.proto.message.contents.PrivatePreferences
import java.nio.charset.StandardCharsets
import java.util.Date

Expand Down Expand Up @@ -713,4 +714,34 @@ class ConversationTest {
assertEquals(1, messages.size)
assertEquals("hi", messages[0].content())
}

@Test
fun testCanHaveAllowState() {
val bobConversation = bobClient.conversations.newConversation(alice.walletAddress, null)
val isAllowed = bobConversation.allowState() == PrivatePreferences.PrivatePreferencesAction.MessageTypeCase.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

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

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

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

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

aliceClient2.contacts.refreshAllowList()

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

assertTrue(isBobAllowed2)
}
}
50 changes: 33 additions & 17 deletions library/src/main/java/org/xmtp/android/library/Contacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import kotlinx.coroutines.runBlocking
import org.xmtp.android.library.messages.ContactBundle
import org.xmtp.android.library.messages.ContactBundleBuilder
import org.xmtp.android.library.messages.EnvelopeBuilder
import org.xmtp.android.library.messages.Message
import org.xmtp.android.library.messages.MessageV2Builder
import org.xmtp.android.library.messages.Topic
import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.message.contents.PrivatePreferences
import org.xmtp.proto.message.contents.PrivatePreferences.PrivatePreferencesAction
import java.util.Date

typealias MessageType = PrivatePreferences.PrivatePreferencesAction.MessageTypeCase
typealias MessageType = PrivatePreferencesAction.MessageTypeCase

data class AllowListEntry(
val value: String,
Expand All @@ -24,7 +21,10 @@ data class AllowListEntry(
}

companion object {
fun address(address: String, type: MessageType = MessageType.MESSAGETYPE_NOT_SET): AllowListEntry {
fun address(
address: String,
type: MessageType = MessageType.MESSAGETYPE_NOT_SET,
): AllowListEntry {
return AllowListEntry(address, EntryType.ADDRESS, type)
}
}
Expand All @@ -48,6 +48,7 @@ class AllowList {
)
val envelopes = client.query(Topic.allowList(identifier))
val allowList = AllowList()
val preferences: MutableList<PrivatePreferencesAction> = mutableListOf()

for (envelope in envelopes.envelopesList) {
val payload = uniffi.xmtp_dh.eciesDecryptK256Sha3256(
Expand All @@ -56,13 +57,20 @@ class AllowList {
envelope.message.toByteArray().toUByteArray().toList()
)

val entry = PrivatePreferencesAction.parseFrom(payload.toUByteArray().toByteArray())
when (entry.messageTypeCase) {
PrivatePreferencesAction.MessageTypeCase.ALLOW -> entry.allow.getWalletAddresses(0)
PrivatePreferencesAction.MessageTypeCase.BLOCK -> entry.block.getWalletAddresses(0)
else -> TODO()
preferences.add(
PrivatePreferencesAction.parseFrom(
payload.toUByteArray().toByteArray()
)
)
}

preferences.forEach { preference ->
preference.allow?.walletAddressesList?.forEach { address ->
allowList.allow(address)
}
preference.block?.walletAddressesList?.forEach { address ->
allowList.block(address)
}
allowList.entries[entry.key] = entry.messageTypeCase
}
return allowList
}
Expand All @@ -71,8 +79,14 @@ class AllowList {
fun publish(entry: AllowListEntry, client: Client) {
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.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()
}
}.build()
Expand Down Expand Up @@ -126,8 +140,10 @@ data class Contacts(

var allowList: AllowList = AllowList()

suspend fun refreshAllowList() {
allowList = AllowList.load(client)
fun refreshAllowList() {
runBlocking {
allowList = AllowList.load(client)
}
}

fun isAllowed(address: String): Boolean {
Expand All @@ -138,13 +154,13 @@ data class Contacts(
return allowList.state(address) == MessageType.BLOCK
}

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

suspend fun block(addresses: List<String>) {
fun block(addresses: List<String>) {
for (address in addresses) {
AllowList.publish(allowList.block(address), client)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ sealed class Conversation {
}
}

fun allowState(): AllowState {
fun allowState(): MessageType {
val client: Client = when (this) {
is V1 -> conversationV1.client
is V2 -> conversationV2.client
Expand Down
30 changes: 30 additions & 0 deletions library/src/test/java/org/xmtp/android/library/ContactsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,34 @@ class ContactsTest {
}
assert(fixtures.aliceClient.contacts.has(fixtures.bob.walletAddress))
}

@Test
fun testAllowAddress() {
val fixtures = fixtures()

val contacts = fixtures.bobClient.contacts
var result = contacts.isAllowed(fixtures.alice.walletAddress)

assert(!result)

contacts.allow(listOf(fixtures.alice.walletAddress))

result = contacts.isAllowed(fixtures.alice.walletAddress)
assert(result)
}

@Test
fun testBlockAddress() {
val fixtures = fixtures()

val contacts = fixtures.bobClient.contacts
var result = contacts.isAllowed(fixtures.alice.walletAddress)

assert(!result)

contacts.block(listOf(fixtures.alice.walletAddress))

result = contacts.isBlocked(fixtures.alice.walletAddress)
assert(result)
}
}

0 comments on commit c06ffea

Please sign in to comment.