Skip to content

Commit

Permalink
Merge branch 'main' into bwcDvorak-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer authored Nov 1, 2023
2 parents f2e273c + 56cd929 commit 9f98de5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ class ContactsTest {

assert(!result)

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

result = contacts.isBlocked(fixtures.alice.walletAddress)
result = contacts.isDenied(fixtures.alice.walletAddress)
assert(result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,11 @@ class ConversationTest {
val messages2 = aliceConversation.messages(limit = 1, after = date)
assertEquals(1, messages2.size)
assertEquals("hey alice 1", messages2[0].body)
val messagesAsc = aliceConversation.messages(direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING)
val messagesAsc =
aliceConversation.messages(direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING)
assertEquals("hey alice 1", messagesAsc[0].body)
val messagesDesc = aliceConversation.messages(direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING)
val messagesDesc =
aliceConversation.messages(direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_DESCENDING)
assertEquals("hey alice 3", messagesDesc[0].body)
}

Expand Down Expand Up @@ -723,11 +725,11 @@ class ConversationTest {
assertTrue(isAllowed)
assertTrue(bobClient.contacts.isAllowed(alice.walletAddress))

bobClient.contacts.block(listOf(alice.walletAddress))
bobClient.contacts.deny(listOf(alice.walletAddress))
bobClient.contacts.refreshConsentList()

val isBlocked = bobConversation.consentState() == ConsentState.BLOCKED
assertTrue(isBlocked)
val isDenied = bobConversation.consentState() == ConsentState.DENIED
assertTrue(isDenied)

val aliceConversation = aliceClient.conversations.list()[0]
val isUnknown = aliceConversation.consentState() == ConsentState.UNKNOWN
Expand All @@ -750,4 +752,26 @@ class ConversationTest {

assertTrue(isBobAllowed2)
}

@Test
fun testCanHaveImplicitConsentOnMessageSend() {
val bobConversation = bobClient.conversations.newConversation(alice.walletAddress, null)
val isAllowed = bobConversation.consentState() == ConsentState.ALLOWED

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

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

// Conversations you receive should start as unknown
assertTrue(isUnknown)

aliceConversation.send(content = "hey bob")
aliceClient.contacts.refreshConsentList()
val isNowAllowed = aliceConversation.consentState() == ConsentState.ALLOWED

// Conversations you send a message to get marked as allowed
assertTrue(isNowAllowed)
}
}
29 changes: 17 additions & 12 deletions library/src/main/java/org/xmtp/android/library/Contacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ 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.Pagination
import org.xmtp.android.library.messages.Topic
import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import org.xmtp.proto.message.contents.PrivatePreferences.PrivatePreferencesAction
import java.util.Date

enum class ConsentState {
ALLOWED,
BLOCKED,
DENIED,
UNKNOWN
}

Expand Down Expand Up @@ -50,7 +52,10 @@ class ConsentList(val client: Client) {

@OptIn(ExperimentalUnsignedTypes::class)
suspend fun load(): ConsentList {
val envelopes = client.query(Topic.preferenceList(identifier))
val envelopes = client.query(
Topic.preferenceList(identifier),
Pagination(direction = MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING)
)
val consentList = ConsentList(client)
val preferences: MutableList<PrivatePreferencesAction> = mutableListOf()

Expand All @@ -68,12 +73,12 @@ class ConsentList(val client: Client) {
)
}

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

Expand All @@ -88,7 +93,7 @@ class ConsentList(val client: Client) {
PrivatePreferencesAction.Allow.newBuilder().addWalletAddresses(entry.value)
)

ConsentState.BLOCKED -> it.setBlock(
ConsentState.DENIED -> it.setBlock(
PrivatePreferencesAction.Block.newBuilder().addWalletAddresses(entry.value)
)

Expand Down Expand Up @@ -117,10 +122,10 @@ class ConsentList(val client: Client) {
return ConsentListEntry.address(address, ConsentState.ALLOWED)
}

fun block(address: String): ConsentListEntry {
entries[ConsentListEntry.address(address).key] = ConsentState.BLOCKED
fun deny(address: String): ConsentListEntry {
entries[ConsentListEntry.address(address).key] = ConsentState.DENIED

return ConsentListEntry.address(address, ConsentState.BLOCKED)
return ConsentListEntry.address(address, ConsentState.DENIED)
}

fun state(address: String): ConsentState {
Expand Down Expand Up @@ -148,8 +153,8 @@ data class Contacts(
return consentList.state(address) == ConsentState.ALLOWED
}

fun isBlocked(address: String): Boolean {
return consentList.state(address) == ConsentState.BLOCKED
fun isDenied(address: String): Boolean {
return consentList.state(address) == ConsentState.DENIED
}

fun allow(addresses: List<String>) {
Expand All @@ -158,9 +163,9 @@ data class Contacts(
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ data class ConversationV1(

fun send(prepared: PreparedMessage): String {
client.publish(envelopes = prepared.envelopes)
if (client.contacts.consentList.state(address = peerAddress) == ConsentState.UNKNOWN) {
client.contacts.allow(addresses = listOf(peerAddress))
}
return prepared.messageId
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ data class ConversationV2(

fun send(prepared: PreparedMessage): String {
client.publish(envelopes = prepared.envelopes)
if (client.contacts.consentList.state(address = peerAddress) == ConsentState.UNKNOWN) {
client.contacts.allow(addresses = listOf(peerAddress))
}
return prepared.messageId
}

Expand Down

0 comments on commit 9f98de5

Please sign in to comment.