1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
import Foundation
// MARK: - Response types (file-private to avoid @MainActor Decodable issues)
private struct MeProfileResponse: Decodable, Sendable {
let me: UserProfile
}
private struct UpdateUserResponse: Decodable, Sendable {
let updateUser: UpdatedUser
}
private struct UpdatedUser: Decodable, Sendable {
let username: String
let email: String
let url: String?
let location: String?
let bio: String?
let avatar: String?
}
private struct CreateSSHKeyResponse: Decodable, Sendable {
let createSSHKey: SSHKey
}
private struct DeleteSSHKeyResponse: Decodable, Sendable {
let deleteSSHKey: DeleteResult?
}
private struct CreatePGPKeyResponse: Decodable, Sendable {
let createPGPKey: PGPKey
}
private struct DeletePGPKeyResponse: Decodable, Sendable {
let deletePGPKey: DeleteResult?
}
private struct DeleteResult: Decodable, Sendable {
let id: Int?
}
private struct PATListResponse: Decodable, Sendable {
let personalAccessTokens: [PersonalAccessToken]
}
// MARK: - View Model
@Observable
@MainActor
final class SettingsViewModel {
private(set) var profile: UserProfile?
private(set) var sshKeys: [SSHKey] = []
private(set) var pgpKeys: [PGPKey] = []
private(set) var personalAccessTokens: [PersonalAccessToken] = []
private(set) var isLoading = false
private(set) var isLoadingPATs = false
private(set) var isSavingProfile = false
private(set) var isUploadingAvatar = false
var error: String?
var isEditingProfile = false
// Add SSH key fields
var newSSHKey = ""
var isAddingSSHKey = false
// Add PGP key fields
var newPGPKey = ""
var isAddingPGPKey = false
private let client: SRHTClient
init(client: SRHTClient) {
self.client = client
}
// MARK: - Queries
private static let profileQuery = """
query me {
me {
username
canonicalName
email
url
location
bio
avatar
userType
sshKeys {
results { id fingerprint comment created lastUsed }
cursor
}
pgpKeys {
results { id fingerprint created }
cursor
}
paymentStatus
subscription { status autorenew interval }
}
}
"""
private static let updateUserMutation = """
mutation updateUser($input: UserInput!) {
updateUser(input: $input) {
username email url location bio avatar
}
}
"""
private static let createSSHKeyMutation = """
mutation createSSHKey($key: String!) {
createSSHKey(key: $key) {
id fingerprint comment created lastUsed
}
}
"""
private static let deleteSSHKeyMutation = """
mutation deleteSSHKey($id: Int!) {
deleteSSHKey(id: $id) { id }
}
"""
private static let createPGPKeyMutation = """
mutation createPGPKey($key: String!) {
createPGPKey(key: $key) {
id fingerprint created
}
}
"""
private static let deletePGPKeyMutation = """
mutation deletePGPKey($id: Int!) {
deletePGPKey(id: $id) { id }
}
"""
private static let personalAccessTokensQuery = """
query personalAccessTokens {
personalAccessTokens { id issued expires comment grants }
}
"""
// MARK: - Load Profile
func loadProfile() async {
guard !isLoading else { return }
isLoading = true
error = nil
do {
let result = try await client.execute(
service: .meta,
query: Self.profileQuery,
responseType: MeProfileResponse.self
)
profile = result.me
sshKeys = result.me.sshKeys.results
pgpKeys = result.me.pgpKeys.results
} catch {
self.error = error.userFacingMessage
}
isLoading = false
}
// MARK: - Update Profile
func saveProfile(email: String, url: String, location: String, bio: String) async {
guard !isSavingProfile else { return }
isSavingProfile = true
error = nil
do {
let input: [String: any Sendable] = [
"email": email,
"url": url.isEmpty ? nil as String? as Any : url,
"location": location.isEmpty ? nil as String? as Any : location,
"bio": bio.isEmpty ? nil as String? as Any : bio
]
let result = try await client.execute(
service: .meta,
query: Self.updateUserMutation,
variables: ["input": input],
responseType: UpdateUserResponse.self
)
let updated = result.updateUser
if let p = profile {
profile = UserProfile(
username: p.username,
canonicalName: p.canonicalName,
email: updated.email,
url: updated.url,
location: updated.location,
bio: updated.bio,
avatar: updated.avatar ?? p.avatar,
userType: p.userType,
sshKeys: p.sshKeys,
pgpKeys: p.pgpKeys,
paymentStatus: p.paymentStatus,
subscription: p.subscription
)
}
isEditingProfile = false
} catch {
self.error = error.userFacingMessage
}
isSavingProfile = false
}
// MARK: - Avatar
func uploadAvatar(jpegData: Data) async {
guard !isUploadingAvatar else { return }
isUploadingAvatar = true
error = nil
do {
// The input variable has avatar set to null; the actual file
// is sent as a separate multipart part per graphql-multipart-request-spec.
let input: [String: any Sendable] = ["avatar": nil as String? as Any]
let result = try await client.executeMultipart(
service: .meta,
query: Self.updateUserMutation,
variables: ["input": input],
fileVariablePath: "input.avatar",
fileData: jpegData,
fileName: "avatar.jpg",
mimeType: "image/jpeg",
responseType: UpdateUserResponse.self
)
let updated = result.updateUser
if let p = profile {
profile = UserProfile(
username: p.username,
canonicalName: p.canonicalName,
email: updated.email,
url: updated.url,
location: updated.location,
bio: updated.bio,
avatar: updated.avatar ?? p.avatar,
userType: p.userType,
sshKeys: p.sshKeys,
pgpKeys: p.pgpKeys,
paymentStatus: p.paymentStatus,
subscription: p.subscription
)
}
} catch {
self.error = error.userFacingMessage
}
isUploadingAvatar = false
}
func removeAvatar() async {
guard !isUploadingAvatar else { return }
isUploadingAvatar = true
error = nil
do {
let input: [String: any Sendable] = ["avatar": nil as String? as Any]
let result = try await client.execute(
service: .meta,
query: Self.updateUserMutation,
variables: ["input": input],
responseType: UpdateUserResponse.self
)
let updated = result.updateUser
if let p = profile {
profile = UserProfile(
username: p.username,
canonicalName: p.canonicalName,
email: updated.email,
url: updated.url,
location: updated.location,
bio: updated.bio,
avatar: nil,
userType: p.userType,
sshKeys: p.sshKeys,
pgpKeys: p.pgpKeys,
paymentStatus: p.paymentStatus,
subscription: p.subscription
)
}
} catch {
self.error = error.userFacingMessage
}
isUploadingAvatar = false
}
// MARK: - SSH Keys
func addSSHKey() async {
let key = newSSHKey.trimmingCharacters(in: .whitespacesAndNewlines)
guard !key.isEmpty else { return }
error = nil
do {
let result = try await client.execute(
service: .meta,
query: Self.createSSHKeyMutation,
variables: ["key": key],
responseType: CreateSSHKeyResponse.self
)
sshKeys.append(result.createSSHKey)
newSSHKey = ""
isAddingSSHKey = false
} catch {
self.error = error.userFacingMessage
}
}
func deleteSSHKey(_ key: SSHKey) async {
error = nil
do {
_ = try await client.execute(
service: .meta,
query: Self.deleteSSHKeyMutation,
variables: ["id": key.id],
responseType: DeleteSSHKeyResponse.self
)
sshKeys.removeAll { $0.id == key.id }
} catch {
self.error = error.userFacingMessage
}
}
// MARK: - PGP Keys
func addPGPKey() async {
let key = newPGPKey.trimmingCharacters(in: .whitespacesAndNewlines)
guard !key.isEmpty else { return }
error = nil
do {
let result = try await client.execute(
service: .meta,
query: Self.createPGPKeyMutation,
variables: ["key": key],
responseType: CreatePGPKeyResponse.self
)
pgpKeys.append(result.createPGPKey)
newPGPKey = ""
isAddingPGPKey = false
} catch {
self.error = error.userFacingMessage
}
}
func deletePGPKey(_ key: PGPKey) async {
error = nil
do {
_ = try await client.execute(
service: .meta,
query: Self.deletePGPKeyMutation,
variables: ["id": key.id],
responseType: DeletePGPKeyResponse.self
)
pgpKeys.removeAll { $0.id == key.id }
} catch {
self.error = error.userFacingMessage
}
}
// MARK: - Personal Access Tokens
func loadPersonalAccessTokens() async {
guard !isLoadingPATs else { return }
isLoadingPATs = true
do {
let result = try await client.execute(
service: .meta,
query: Self.personalAccessTokensQuery,
responseType: PATListResponse.self
)
personalAccessTokens = result.personalAccessTokens
} catch {
self.error = error.userFacingMessage
}
isLoadingPATs = false
}
}
|