@@ -48,8 +48,11 @@ data class ConsentListEntry(
48
48
get() = " ${entryType.name} -$value "
49
49
}
50
50
51
- class ConsentList (val client : Client ) {
52
- val entries: MutableMap <String , ConsentListEntry > = mutableMapOf ()
51
+ class ConsentList (
52
+ val client : Client ,
53
+ val entries : MutableMap <String , ConsentListEntry > = mutableMapOf(),
54
+ ) {
55
+ private var lastFetched: Date ? = null
53
56
private val publicKey =
54
57
client.privateKeyBundleV1.identityKey.publicKey.secp256K1Uncompressed.bytes
55
58
private val privateKey = client.privateKeyBundleV1.identityKey.secp256K1.bytes
@@ -60,13 +63,17 @@ class ConsentList(val client: Client) {
60
63
)
61
64
62
65
@OptIn(ExperimentalUnsignedTypes ::class )
63
- suspend fun load (): ConsentList {
66
+ suspend fun load (): List <ConsentListEntry > {
67
+ val newDate = Date ()
64
68
val envelopes =
65
69
client.apiClient.envelopes(
66
70
Topic .preferenceList(identifier).description,
67
- Pagination (direction = MessageApiOuterClass .SortDirection .SORT_DIRECTION_ASCENDING ),
71
+ Pagination (
72
+ after = lastFetched,
73
+ direction = MessageApiOuterClass .SortDirection .SORT_DIRECTION_ASCENDING
74
+ ),
68
75
)
69
- val consentList = ConsentList (client)
76
+ lastFetched = newDate
70
77
val preferences: MutableList <PrivatePreferencesAction > = mutableListOf ()
71
78
for (envelope in envelopes) {
72
79
val payload =
@@ -79,64 +86,70 @@ class ConsentList(val client: Client) {
79
86
preferences.add(
80
87
PrivatePreferencesAction .parseFrom(
81
88
payload.toUByteArray().toByteArray(),
82
- ),
89
+ )
83
90
)
84
91
}
85
92
86
93
preferences.iterator().forEach { preference ->
87
94
preference.allowAddress?.walletAddressesList?.forEach { address ->
88
- consentList. allow(address)
95
+ allow(address)
89
96
}
90
97
preference.denyAddress?.walletAddressesList?.forEach { address ->
91
- consentList. deny(address)
98
+ deny(address)
92
99
}
93
100
preference.allowGroup?.groupIdsList?.forEach { groupId ->
94
- consentList. allowGroup(groupId.toByteArray())
101
+ allowGroup(groupId.toByteArray())
95
102
}
96
103
preference.denyGroup?.groupIdsList?.forEach { groupId ->
97
- consentList. denyGroup(groupId.toByteArray())
104
+ denyGroup(groupId.toByteArray())
98
105
}
99
106
}
100
107
101
- return consentList
108
+ return entries.values.toList()
102
109
}
103
110
104
- suspend fun publish (entry : ConsentListEntry ) {
105
- val payload =
106
- PrivatePreferencesAction .newBuilder(). also {
111
+ suspend fun publish (entries : List < ConsentListEntry > ) {
112
+ val payload = PrivatePreferencesAction .newBuilder(). also {
113
+ entries.forEach { entry ->
107
114
when (entry.entryType) {
108
115
ConsentListEntry .EntryType .ADDRESS -> {
109
116
when (entry.consentType) {
110
117
ConsentState .ALLOWED ->
111
118
it.setAllowAddress(
112
- PrivatePreferencesAction .AllowAddress .newBuilder().addWalletAddresses(entry.value),
119
+ PrivatePreferencesAction .AllowAddress .newBuilder()
120
+ .addWalletAddresses(entry.value),
113
121
)
114
122
115
123
ConsentState .DENIED ->
116
124
it.setDenyAddress(
117
- PrivatePreferencesAction .DenyAddress .newBuilder().addWalletAddresses(entry.value),
125
+ PrivatePreferencesAction .DenyAddress .newBuilder()
126
+ .addWalletAddresses(entry.value),
118
127
)
119
128
120
129
ConsentState .UNKNOWN -> it.clearMessageType()
121
130
}
122
131
}
132
+
123
133
ConsentListEntry .EntryType .GROUP_ID -> {
124
134
when (entry.consentType) {
125
135
ConsentState .ALLOWED ->
126
136
it.setAllowGroup(
127
- PrivatePreferencesAction .AllowGroup .newBuilder().addGroupIds(entry.value.toByteStringUtf8()),
137
+ PrivatePreferencesAction .AllowGroup .newBuilder()
138
+ .addGroupIds(entry.value.toByteStringUtf8()),
128
139
)
129
140
130
141
ConsentState .DENIED ->
131
142
it.setDenyGroup(
132
- PrivatePreferencesAction .DenyGroup .newBuilder().addGroupIds(entry.value.toByteStringUtf8()),
143
+ PrivatePreferencesAction .DenyGroup .newBuilder()
144
+ .addGroupIds(entry.value.toByteStringUtf8()),
133
145
)
134
146
135
147
ConsentState .UNKNOWN -> it.clearMessageType()
136
148
}
137
149
}
138
150
}
139
- }.build()
151
+ }
152
+ }.build()
140
153
141
154
val message =
142
155
uniffi.xmtpv3.userPreferencesEncrypt(
@@ -145,40 +158,39 @@ class ConsentList(val client: Client) {
145
158
payload.toByteArray(),
146
159
)
147
160
148
- val envelope =
149
- EnvelopeBuilder .buildFromTopic(
150
- Topic .preferenceList(identifier),
151
- Date (),
152
- ByteArray (message.size) { message[it] },
153
- )
161
+ val envelope = EnvelopeBuilder .buildFromTopic(
162
+ Topic .preferenceList(identifier),
163
+ Date (),
164
+ ByteArray (message.size) { message[it] },
165
+ )
154
166
155
167
client.publish(listOf (envelope))
156
168
}
157
169
158
170
fun allow (address : String ): ConsentListEntry {
159
171
val entry = ConsentListEntry .address(address, ConsentState .ALLOWED )
160
- entries[ConsentListEntry .address(address) .key] = entry
172
+ entries[entry .key] = entry
161
173
162
174
return entry
163
175
}
164
176
165
177
fun deny (address : String ): ConsentListEntry {
166
178
val entry = ConsentListEntry .address(address, ConsentState .DENIED )
167
- entries[ConsentListEntry .address(address) .key] = entry
179
+ entries[entry .key] = entry
168
180
169
181
return entry
170
182
}
171
183
172
184
fun allowGroup (groupId : ByteArray ): ConsentListEntry {
173
185
val entry = ConsentListEntry .groupId(groupId, ConsentState .ALLOWED )
174
- entries[ConsentListEntry .groupId(groupId) .key] = entry
186
+ entries[entry .key] = entry
175
187
176
188
return entry
177
189
}
178
190
179
191
fun denyGroup (groupId : ByteArray ): ConsentListEntry {
180
192
val entry = ConsentListEntry .groupId(groupId, ConsentState .DENIED )
181
- entries[ConsentListEntry .groupId(groupId) .key] = entry
193
+ entries[entry .key] = entry
182
194
183
195
return entry
184
196
}
@@ -200,38 +212,40 @@ data class Contacts(
200
212
var client : Client ,
201
213
val knownBundles : MutableMap <String , ContactBundle > = mutableMapOf(),
202
214
val hasIntroduced : MutableMap <String , Boolean > = mutableMapOf(),
215
+ var consentList : ConsentList = ConsentList (client),
203
216
) {
204
- var consentList: ConsentList = ConsentList (client)
205
217
206
- fun refreshConsentList (): ConsentList {
207
- runBlocking {
208
- consentList = ConsentList (client).load()
209
- }
218
+ suspend fun refreshConsentList (): ConsentList {
219
+ consentList.load()
210
220
return consentList
211
221
}
212
222
213
223
suspend fun allow (addresses : List <String >) {
214
- for (address in addresses) {
215
- ConsentList (client).publish( consentList.allow(address) )
224
+ val entries = addresses.map {
225
+ consentList.allow(it )
216
226
}
227
+ consentList.publish(entries)
217
228
}
218
229
219
230
suspend fun deny (addresses : List <String >) {
220
- for (address in addresses) {
221
- ConsentList (client).publish( consentList.deny(address) )
231
+ val entries = addresses.map {
232
+ consentList.deny(it )
222
233
}
234
+ consentList.publish(entries)
223
235
}
224
236
225
237
suspend fun allowGroup (groupIds : List <ByteArray >) {
226
- for (id in groupIds) {
227
- ConsentList (client).publish( consentList.allowGroup(id) )
238
+ val entries = groupIds.map {
239
+ consentList.allowGroup(it )
228
240
}
241
+ consentList.publish(entries)
229
242
}
230
243
231
244
suspend fun denyGroup (groupIds : List <ByteArray >) {
232
- for (id in groupIds) {
233
- ConsentList (client).publish( consentList.denyGroup(id) )
245
+ val entries = groupIds.map {
246
+ consentList.denyGroup(it )
234
247
}
248
+ consentList.publish(entries)
235
249
}
236
250
237
251
fun isAllowed (address : String ): Boolean {
0 commit comments