@@ -15,7 +15,7 @@ import java.util.Date
15
15
enum class ConsentState {
16
16
ALLOWED ,
17
17
DENIED ,
18
- UNKNOWN
18
+ UNKNOWN ,
19
19
}
20
20
21
21
data class ConsentListEntry (
@@ -25,7 +25,7 @@ data class ConsentListEntry(
25
25
) {
26
26
enum class EntryType {
27
27
ADDRESS ,
28
- GROUP_ID
28
+ GROUP_ID ,
29
29
}
30
30
31
31
companion object {
@@ -54,29 +54,32 @@ class ConsentList(val client: Client) {
54
54
client.privateKeyBundleV1.identityKey.publicKey.secp256K1Uncompressed.bytes
55
55
private val privateKey = client.privateKeyBundleV1.identityKey.secp256K1.bytes
56
56
57
- private val identifier: String = uniffi.xmtpv3.generatePrivatePreferencesTopicIdentifier(
58
- privateKey.toByteArray()
59
- )
57
+ private val identifier: String =
58
+ uniffi.xmtpv3.generatePrivatePreferencesTopicIdentifier(
59
+ privateKey.toByteArray(),
60
+ )
60
61
61
62
@OptIn(ExperimentalUnsignedTypes ::class )
62
63
suspend fun load (): ConsentList {
63
- val envelopes = client.apiClient.envelopes(
64
- Topic .preferenceList(identifier).description,
65
- Pagination (direction = MessageApiOuterClass .SortDirection .SORT_DIRECTION_ASCENDING )
66
- )
64
+ val envelopes =
65
+ client.apiClient.envelopes(
66
+ Topic .preferenceList(identifier).description,
67
+ Pagination (direction = MessageApiOuterClass .SortDirection .SORT_DIRECTION_ASCENDING ),
68
+ )
67
69
val consentList = ConsentList (client)
68
70
val preferences: MutableList <PrivatePreferencesAction > = mutableListOf ()
69
71
for (envelope in envelopes) {
70
- val payload = uniffi.xmtpv3.userPreferencesDecrypt(
71
- publicKey.toByteArray(),
72
- privateKey.toByteArray(),
73
- envelope.message.toByteArray()
74
- )
72
+ val payload =
73
+ uniffi.xmtpv3.userPreferencesDecrypt(
74
+ publicKey.toByteArray(),
75
+ privateKey.toByteArray(),
76
+ envelope.message.toByteArray(),
77
+ )
75
78
76
79
preferences.add(
77
80
PrivatePreferencesAction .parseFrom(
78
- payload.toUByteArray().toByteArray()
79
- )
81
+ payload.toUByteArray().toByteArray(),
82
+ ),
80
83
)
81
84
}
82
85
@@ -99,48 +102,55 @@ class ConsentList(val client: Client) {
99
102
}
100
103
101
104
fun publish (entry : ConsentListEntry ) {
102
- val payload = PrivatePreferencesAction .newBuilder().also {
103
- when (entry.entryType) {
104
- ConsentListEntry .EntryType .ADDRESS -> {
105
- when (entry.consentType) {
106
- ConsentState .ALLOWED -> it.setAllowDm(
107
- PrivatePreferencesAction .AllowDM .newBuilder().addWalletAddresses(entry.value)
108
- )
109
-
110
- ConsentState .DENIED -> it.setDenyDm(
111
- PrivatePreferencesAction .DenyDM .newBuilder().addWalletAddresses(entry.value)
112
- )
113
-
114
- ConsentState .UNKNOWN -> it.clearMessageType()
105
+ val payload =
106
+ PrivatePreferencesAction .newBuilder().also {
107
+ when (entry.entryType) {
108
+ ConsentListEntry .EntryType .ADDRESS -> {
109
+ when (entry.consentType) {
110
+ ConsentState .ALLOWED ->
111
+ it.setAllowDm(
112
+ PrivatePreferencesAction .AllowDM .newBuilder().addWalletAddresses(entry.value),
113
+ )
114
+
115
+ ConsentState .DENIED ->
116
+ it.setDenyDm(
117
+ PrivatePreferencesAction .DenyDM .newBuilder().addWalletAddresses(entry.value),
118
+ )
119
+
120
+ ConsentState .UNKNOWN -> it.clearMessageType()
121
+ }
115
122
}
116
- }
117
- ConsentListEntry .EntryType .GROUP_ID -> {
118
- when (entry.consentType) {
119
- ConsentState .ALLOWED -> it.setAllowGroup(
120
- PrivatePreferencesAction .AllowGroup .newBuilder().addGroupIds(entry.value.toByteStringUtf8())
121
- )
122
-
123
- ConsentState .DENIED -> it.setDenyGroup(
124
- PrivatePreferencesAction .DenyGroup .newBuilder().addGroupIds(entry.value.toByteStringUtf8())
125
- )
126
-
127
- ConsentState .UNKNOWN -> it.clearMessageType()
123
+ ConsentListEntry .EntryType .GROUP_ID -> {
124
+ when (entry.consentType) {
125
+ ConsentState .ALLOWED ->
126
+ it.setAllowGroup(
127
+ PrivatePreferencesAction .AllowGroup .newBuilder().addGroupIds(entry.value.toByteStringUtf8()),
128
+ )
129
+
130
+ ConsentState .DENIED ->
131
+ it.setDenyGroup(
132
+ PrivatePreferencesAction .DenyGroup .newBuilder().addGroupIds(entry.value.toByteStringUtf8()),
133
+ )
134
+
135
+ ConsentState .UNKNOWN -> it.clearMessageType()
136
+ }
128
137
}
129
138
}
130
- }
131
- }.build()
139
+ }.build()
132
140
133
- val message = uniffi.xmtpv3.userPreferencesEncrypt(
134
- publicKey.toByteArray(),
135
- privateKey.toByteArray(),
136
- payload.toByteArray()
137
- )
141
+ val message =
142
+ uniffi.xmtpv3.userPreferencesEncrypt(
143
+ publicKey.toByteArray(),
144
+ privateKey.toByteArray(),
145
+ payload.toByteArray(),
146
+ )
138
147
139
- val envelope = EnvelopeBuilder .buildFromTopic(
140
- Topic .preferenceList(identifier),
141
- Date (),
142
- ByteArray (message.size) { message[it].toByte() }
143
- )
148
+ val envelope =
149
+ EnvelopeBuilder .buildFromTopic(
150
+ Topic .preferenceList(identifier),
151
+ Date (),
152
+ ByteArray (message.size) { message[it].toByte() },
153
+ )
144
154
145
155
client.publish(listOf (envelope))
146
156
}
@@ -178,7 +188,7 @@ class ConsentList(val client: Client) {
178
188
179
189
return entry?.consentType ? : ConsentState .UNKNOWN
180
190
}
181
-
191
+
182
192
fun groupState (groupId : ByteArray ): ConsentState {
183
193
val entry = entries[ConsentListEntry .groupId(groupId).key]
184
194
@@ -191,7 +201,6 @@ data class Contacts(
191
201
val knownBundles : MutableMap <String , ContactBundle > = mutableMapOf(),
192
202
val hasIntroduced : MutableMap <String , Boolean > = mutableMapOf(),
193
203
) {
194
-
195
204
var consentList: ConsentList = ConsentList (client)
196
205
197
206
fun refreshConsentList (): ConsentList {
@@ -241,11 +250,9 @@ data class Contacts(
241
250
return consentList.groupState(groupId) == ConsentState .DENIED
242
251
}
243
252
244
- fun has (peerAddress : String ): Boolean =
245
- knownBundles[peerAddress] != null
253
+ fun has (peerAddress : String ): Boolean = knownBundles[peerAddress] != null
246
254
247
- fun needsIntroduction (peerAddress : String ): Boolean =
248
- hasIntroduced[peerAddress] != true
255
+ fun needsIntroduction (peerAddress : String ): Boolean = hasIntroduced[peerAddress] != true
249
256
250
257
fun find (peerAddress : String ): ContactBundle ? {
251
258
val knownBundle = knownBundles[peerAddress]
0 commit comments