@@ -16,12 +16,16 @@ public enum ConsentState: String, Codable {
16
16
17
17
public struct ConsentListEntry : Codable , Hashable {
18
18
public enum EntryType : String , Codable {
19
- case address
19
+ case address, groupId
20
20
}
21
21
22
22
static func address( _ address: String , type: ConsentState = . unknown) -> ConsentListEntry {
23
23
ConsentListEntry ( value: address, entryType: . address, consentType: type)
24
24
}
25
+
26
+ static func groupId( groupId: String , type: ConsentState = ConsentState . unknown) -> ConsentListEntry {
27
+ ConsentListEntry ( value: groupId, entryType: . groupId, consentType: type)
28
+ }
25
29
26
30
public var value : String
27
31
public var entryType : EntryType
@@ -48,17 +52,14 @@ public class ConsentList {
48
52
self . client = client
49
53
privateKey = client. privateKeyBundleV1. identityKey. secp256K1. bytes
50
54
publicKey = client. privateKeyBundleV1. identityKey. publicKey. secp256K1Uncompressed. bytes
51
- // swiftlint:disable no_optional_try
52
55
identifier = try ? LibXMTP . generatePrivatePreferencesTopicIdentifier ( privateKey: privateKey)
53
- // swiftlint:enable no_optional_try
54
56
}
55
57
56
58
func load( ) async throws -> ConsentList {
57
59
guard let identifier = identifier else {
58
60
throw ContactError . invalidIdentifier
59
61
}
60
62
61
-
62
63
let envelopes = try await client. apiClient. envelopes ( topic: Topic . preferenceList ( identifier) . description, pagination: Pagination ( direction: . ascending) )
63
64
let consentList = ConsentList ( client: client)
64
65
@@ -71,13 +72,21 @@ public class ConsentList {
71
72
}
72
73
73
74
for preference in preferences {
74
- for address in preference. allow . walletAddresses {
75
+ for address in preference. allowAddress . walletAddresses {
75
76
_ = consentList. allow ( address: address)
76
77
}
77
78
78
- for address in preference. block . walletAddresses {
79
+ for address in preference. denyAddress . walletAddresses {
79
80
_ = consentList. deny ( address: address)
80
81
}
82
+
83
+ for groupId in preference. allowGroup. groupIds {
84
+ _ = consentList. allowGroup ( groupId: groupId)
85
+ }
86
+
87
+ for groupId in preference. denyGroup. groupIds {
88
+ _ = consentList. denyGroup ( groupId: groupId)
89
+ }
81
90
}
82
91
83
92
return consentList
@@ -89,13 +98,31 @@ public class ConsentList {
89
98
}
90
99
91
100
var payload = PrivatePreferencesAction ( )
92
- switch entry. consentType {
93
- case . allowed:
94
- payload. allow. walletAddresses = [ entry. value]
95
- case . denied:
96
- payload. block. walletAddresses = [ entry. value]
97
- case . unknown:
98
- payload. messageType = nil
101
+ switch entry. entryType {
102
+
103
+ case . address:
104
+ switch entry. consentType {
105
+ case . allowed:
106
+ payload. allowAddress. walletAddresses = [ entry. value]
107
+ case . denied:
108
+ payload. denyAddress. walletAddresses = [ entry. value]
109
+ case . unknown:
110
+ payload. messageType = nil
111
+ }
112
+
113
+ case . groupId:
114
+ switch entry. consentType {
115
+ case . allowed:
116
+ if let valueData = entry. value. data ( using: . utf8) {
117
+ payload. allowGroup. groupIds = [ valueData]
118
+ }
119
+ case . denied:
120
+ if let valueData = entry. value. data ( using: . utf8) {
121
+ payload. denyGroup. groupIds = [ valueData]
122
+ }
123
+ case . unknown:
124
+ payload. messageType = nil
125
+ }
99
126
}
100
127
101
128
let message = try LibXMTP . userPreferencesEncrypt (
@@ -127,12 +154,36 @@ public class ConsentList {
127
154
return entry
128
155
}
129
156
157
+ func allowGroup( groupId: Data ) -> ConsentListEntry {
158
+ let groupIdString = groupId. toHex
159
+ let entry = ConsentListEntry . groupId ( groupId: groupIdString, type: ConsentState . allowed)
160
+ entries [ ConsentListEntry . groupId ( groupId: groupIdString) . key] = entry
161
+
162
+ return entry
163
+ }
164
+
165
+ func denyGroup( groupId: Data ) -> ConsentListEntry {
166
+ let groupIdString = groupId. toHex
167
+ let entry = ConsentListEntry . groupId ( groupId: groupIdString, type: ConsentState . denied)
168
+ entries [ ConsentListEntry . groupId ( groupId: groupIdString) . key] = entry
169
+
170
+ return entry
171
+ }
172
+
130
173
func state( address: String ) -> ConsentState {
131
- let entry = entries [ ConsentListEntry . address ( address) . key]
174
+ guard let entry = entries [ ConsentListEntry . address ( address) . key] else {
175
+ return . unknown
176
+ }
177
+
178
+ return entry. consentType
179
+ }
180
+
181
+ func groupState( groupId: Data ) -> ConsentState {
182
+ guard let entry = entries [ ConsentListEntry . groupId ( groupId: groupId. toHex) . key] else {
183
+ return . unknown
184
+ }
132
185
133
- // swiftlint:disable no_optional_try
134
- return entry? . consentType ?? . unknown
135
- // swiftlint:enable no_optional_try
186
+ return entry. consentType
136
187
}
137
188
}
138
189
@@ -166,6 +217,14 @@ public actor Contacts {
166
217
return consentList. state ( address: address) == . denied
167
218
}
168
219
220
+ public func isGroupAllowed( groupId: Data ) -> Bool {
221
+ return consentList. groupState ( groupId: groupId) == . allowed
222
+ }
223
+
224
+ public func isGroupDenied( groupId: Data ) -> Bool {
225
+ return consentList. groupState ( groupId: groupId) == . denied
226
+ }
227
+
169
228
public func allow( addresses: [ String ] ) async throws {
170
229
for address in addresses {
171
230
try await ConsentList ( client: client) . publish ( entry: consentList. allow ( address: address) )
@@ -178,6 +237,20 @@ public actor Contacts {
178
237
}
179
238
}
180
239
240
+ public func allowGroup( groupIds: [ Data ] ) async throws {
241
+ for groupId in groupIds {
242
+ let entry = consentList. allowGroup ( groupId: groupId)
243
+ try await ConsentList ( client: client) . publish ( entry: entry)
244
+ }
245
+ }
246
+
247
+ public func denyGroup( groupIds: [ Data ] ) async throws {
248
+ for groupId in groupIds {
249
+ let entry = consentList. denyGroup ( groupId: groupId)
250
+ try await ConsentList ( client: client) . publish ( entry: entry)
251
+ }
252
+ }
253
+
181
254
func markIntroduced( _ peerAddress: String , _ isIntroduced: Bool ) {
182
255
hasIntroduced [ peerAddress] = isIntroduced
183
256
}
@@ -198,15 +271,11 @@ public actor Contacts {
198
271
let response = try await client. query ( topic: . contact( peerAddress) )
199
272
200
273
for envelope in response. envelopes {
201
- // swiftlint:disable no_optional_try
202
274
if let contactBundle = try ? ContactBundle . from ( envelope: envelope) {
203
275
knownBundles [ peerAddress] = contactBundle
204
-
205
276
return contactBundle
206
277
}
207
- // swiftlint:enable no_optional_try
208
278
}
209
-
210
279
return nil
211
280
}
212
281
}
0 commit comments