Skip to content

Commit cc70945

Browse files
committed
improve the publish and write a test
1 parent 0bb508d commit cc70945

File tree

2 files changed

+66
-50
lines changed

2 files changed

+66
-50
lines changed

library/src/androidTest/java/org/xmtp/android/library/ConversationTest.kt

+17
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,23 @@ class ConversationTest {
848848
assertTrue(isNowAllowed)
849849
}
850850

851+
@Test
852+
fun testCanPublishMultipleAddressConsentState() {
853+
runBlocking {
854+
val bobConversation = bobClient.conversations.newConversation(alice.walletAddress)
855+
val caroConversation =
856+
bobClient.conversations.newConversation(fixtures.caro.walletAddress)
857+
858+
assertEquals(bobClient.contacts.consentList.entries.size, 2)
859+
assertTrue(bobConversation.consentState() == ConsentState.ALLOWED)
860+
assertTrue(caroConversation.consentState() == ConsentState.ALLOWED)
861+
bobClient.contacts.deny(listOf(alice.walletAddress, fixtures.caro.walletAddress))
862+
assertEquals(bobClient.contacts.consentList.entries.size, 2)
863+
assertTrue(bobConversation.consentState() == ConsentState.DENIED)
864+
assertTrue(caroConversation.consentState() == ConsentState.DENIED)
865+
}
866+
}
867+
851868
@Test
852869
fun testCanValidateTopicsInsideConversation() {
853870
val validId = "sdfsadf095b97a9284dcd82b2274856ccac8a21de57bebe34e7f9eeb855fb21126d3b8f"

library/src/main/java/org/xmtp/android/library/Contacts.kt

+49-50
Original file line numberDiff line numberDiff line change
@@ -109,63 +109,62 @@ class ConsentList(
109109
}
110110

111111
suspend fun publish(entries: List<ConsentListEntry>) {
112-
val envelopes = entries.map { entry ->
113-
val payload =
114-
PrivatePreferencesAction.newBuilder().also {
115-
when (entry.entryType) {
116-
ConsentListEntry.EntryType.ADDRESS -> {
117-
when (entry.consentType) {
118-
ConsentState.ALLOWED ->
119-
it.setAllowAddress(
120-
PrivatePreferencesAction.AllowAddress.newBuilder()
121-
.addWalletAddresses(entry.value),
122-
)
123-
124-
ConsentState.DENIED ->
125-
it.setDenyAddress(
126-
PrivatePreferencesAction.DenyAddress.newBuilder()
127-
.addWalletAddresses(entry.value),
128-
)
129-
130-
ConsentState.UNKNOWN -> it.clearMessageType()
131-
}
112+
val payload = PrivatePreferencesAction.newBuilder().also {
113+
entries.forEach { entry ->
114+
when (entry.entryType) {
115+
ConsentListEntry.EntryType.ADDRESS -> {
116+
when (entry.consentType) {
117+
ConsentState.ALLOWED ->
118+
it.setAllowAddress(
119+
PrivatePreferencesAction.AllowAddress.newBuilder()
120+
.addWalletAddresses(entry.value),
121+
)
122+
123+
ConsentState.DENIED ->
124+
it.setDenyAddress(
125+
PrivatePreferencesAction.DenyAddress.newBuilder()
126+
.addWalletAddresses(entry.value),
127+
)
128+
129+
ConsentState.UNKNOWN -> it.clearMessageType()
132130
}
131+
}
133132

134-
ConsentListEntry.EntryType.GROUP_ID -> {
135-
when (entry.consentType) {
136-
ConsentState.ALLOWED ->
137-
it.setAllowGroup(
138-
PrivatePreferencesAction.AllowGroup.newBuilder()
139-
.addGroupIds(entry.value.toByteStringUtf8()),
140-
)
141-
142-
ConsentState.DENIED ->
143-
it.setDenyGroup(
144-
PrivatePreferencesAction.DenyGroup.newBuilder()
145-
.addGroupIds(entry.value.toByteStringUtf8()),
146-
)
147-
148-
ConsentState.UNKNOWN -> it.clearMessageType()
149-
}
133+
ConsentListEntry.EntryType.GROUP_ID -> {
134+
when (entry.consentType) {
135+
ConsentState.ALLOWED ->
136+
it.setAllowGroup(
137+
PrivatePreferencesAction.AllowGroup.newBuilder()
138+
.addGroupIds(entry.value.toByteStringUtf8()),
139+
)
140+
141+
ConsentState.DENIED ->
142+
it.setDenyGroup(
143+
PrivatePreferencesAction.DenyGroup.newBuilder()
144+
.addGroupIds(entry.value.toByteStringUtf8()),
145+
)
146+
147+
ConsentState.UNKNOWN -> it.clearMessageType()
150148
}
151149
}
152-
}.build()
153-
154-
val message =
155-
uniffi.xmtpv3.userPreferencesEncrypt(
156-
publicKey.toByteArray(),
157-
privateKey.toByteArray(),
158-
payload.toByteArray(),
159-
)
150+
}
151+
}
152+
}.build()
160153

161-
EnvelopeBuilder.buildFromTopic(
162-
Topic.preferenceList(identifier),
163-
Date(),
164-
ByteArray(message.size) { message[it] },
154+
val message =
155+
uniffi.xmtpv3.userPreferencesEncrypt(
156+
publicKey.toByteArray(),
157+
privateKey.toByteArray(),
158+
payload.toByteArray(),
165159
)
166-
}
167160

168-
client.publish(envelopes)
161+
val envelope = EnvelopeBuilder.buildFromTopic(
162+
Topic.preferenceList(identifier),
163+
Date(),
164+
ByteArray(message.size) { message[it] },
165+
)
166+
167+
client.publish(listOf(envelope))
169168
}
170169

171170
fun allow(address: String): ConsentListEntry {

0 commit comments

Comments
 (0)