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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
import SwiftUI
private struct ProjectMailingListThreadsResponse: Decodable, Sendable {
let list: ProjectMailingListThreads
}
private struct ProjectMailingListThreads: Decodable, Sendable {
let threads: ProjectMailingListThreadPage
}
private struct ProjectMailingListThreadPage: Decodable, Sendable {
let results: [ProjectMailingListThreadPayload]
}
private struct ProjectMailingListThreadPayload: Decodable, Sendable {
let updated: Date
let subject: String
let replies: Int
let sender: Entity
let root: ProjectMailingListRootPayload
}
private struct ProjectMailingListRootPayload: Decodable, Sendable {
let id: Int
let messageID: String
let patch: InboxPatchPreview?
/// Null unless the thread's root email opens a patchset. `MailingList` has no
/// patchsets field, so this is the only way to enumerate a list's patchsets.
let patchset: PatchsetSummaryPayload?
}
private struct PatchsetSummaryPayload: Decodable, Sendable {
let id: Int
let subject: String
let version: Int
let prefix: String?
let status: PatchsetStatus
}
@Observable
@MainActor
final class MailingListDetailViewModel {
private(set) var threads: [InboxThreadSummary] = []
/// Patchsets on this list, derived from thread roots — see the query below.
private(set) var patchsets: [PatchsetSummary] = []
private(set) var isLoading = false
var error: String?
var searchText = ""
private let mailingList: InboxMailingListReference
private let client: SRHTClient
private let defaults: UserDefaults
private let accountID: String
private static let listThreadsQuery = """
query projectMailingListThreads($rid: ID!) {
list(rid: $rid) {
threads {
results {
updated
subject
replies
sender { canonicalName }
root {
id
messageID
patch { subject }
patchset {
id
subject
version
prefix
status
}
}
}
}
}
}
"""
init(mailingList: InboxMailingListReference, client: SRHTClient, defaults: UserDefaults, accountID: String) {
self.mailingList = mailingList
self.client = client
self.defaults = defaults
self.accountID = accountID
}
var filteredThreads: [InboxThreadSummary] {
Self.filterThreads(threads, matching: searchText)
}
func loadThreads() async {
guard !isLoading else { return }
isLoading = true
error = nil
defer { isLoading = false }
do {
let response = try await client.execute(
service: .lists,
query: Self.listThreadsQuery,
variables: ["rid": mailingList.rid],
responseType: ProjectMailingListThreadsResponse.self
)
threads = deduplicateThreads(
response.list.threads.results.map(makeSummary(from:))
)
patchsets = Self.patchsets(from: response.list.threads.results)
} catch {
self.error = "Failed to load mailing list"
}
}
var filteredPatchsets: [PatchsetSummary] {
let query = searchText.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
guard !query.isEmpty else { return patchsets }
return patchsets.filter { $0.subject.lowercased().contains(query) }
}
/// Collects the patchsets opened by these threads, newest first.
///
/// A revised series arrives as its own thread, so the same subject can appear
/// at several versions; they are kept as distinct patchsets and the version
/// chain is shown in the detail view.
private nonisolated static func patchsets(
from threads: [ProjectMailingListThreadPayload]
) -> [PatchsetSummary] {
var seenIDs = Set<Int>()
var results: [PatchsetSummary] = []
for thread in threads {
guard let payload = thread.root.patchset, !seenIDs.contains(payload.id) else { continue }
seenIDs.insert(payload.id)
results.append(
PatchsetSummary(
id: payload.id,
subject: payload.subject,
version: payload.version,
prefix: payload.prefix,
status: payload.status
)
)
}
return results
}
func markThreadRead(_ thread: InboxThreadSummary) {
let viewedAt = max(Date(), thread.lastActivityAt)
InboxReadStateStore.markViewed(viewedAt, for: thread.threadGroupingKey, defaults: defaults)
updateThread(thread, isUnread: false)
NeedsAttentionSnapshotStore.adjustUnreadInboxThreads(by: -1, accountID: accountID)
}
func markThreadUnread(_ thread: InboxThreadSummary) {
InboxReadStateStore.markUnread(for: thread.threadGroupingKey, defaults: defaults)
updateThread(thread, isUnread: true)
NeedsAttentionSnapshotStore.adjustUnreadInboxThreads(by: 1, accountID: accountID)
}
func markAllThreadsRead() {
let unreadThreads = threads.filter(\.isUnread)
guard !unreadThreads.isEmpty else { return }
let viewedAt = Date()
for thread in unreadThreads {
InboxReadStateStore.markViewed(max(viewedAt, thread.lastActivityAt), for: thread.threadGroupingKey, defaults: defaults)
}
threads = threads.map { thread in
guard thread.isUnread else { return thread }
return InboxThreadSummary(
rootEmailID: thread.rootEmailID,
rootMessageID: thread.rootMessageID,
threadRootEmailIDs: thread.threadRootEmailIDs,
threadRootMessageIDs: thread.threadRootMessageIDs,
listID: thread.listID,
listRID: thread.listRID,
listName: thread.listName,
listOwner: thread.listOwner,
subject: thread.subject,
latestSender: thread.latestSender,
lastActivityAt: thread.lastActivityAt,
messageCount: thread.messageCount,
repo: thread.repo,
containsPatch: thread.containsPatch,
isUnread: false
)
}
NeedsAttentionSnapshotStore.adjustUnreadInboxThreads(by: -unreadThreads.count, accountID: accountID)
}
private func makeSummary(from thread: ProjectMailingListThreadPayload) -> InboxThreadSummary {
let normalizedSubject = thread.subject
.replacingOccurrences(of: #"\s+"#, with: " ", options: .regularExpression)
.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: #"^(?:(?:re|fwd?)\s*:\s*)+"#, with: "", options: [.regularExpression, .caseInsensitive])
.lowercased()
let threadID = "\(mailingList.rid)#\(normalizedSubject)"
return InboxThreadSummary(
rootEmailID: thread.root.id,
rootMessageID: thread.root.messageID,
threadRootEmailIDs: [thread.root.id],
threadRootMessageIDs: [thread.root.messageID],
listID: 0,
listRID: mailingList.rid,
listName: mailingList.name,
listOwner: mailingList.owner,
subject: thread.subject,
latestSender: thread.sender,
lastActivityAt: thread.updated,
messageCount: thread.replies + 1,
repo: nil,
containsPatch: thread.root.patch != nil || thread.subject.localizedCaseInsensitiveContains("[patch"),
isUnread: InboxReadStateStore.isUnread(threadID: threadID, lastActivityAt: thread.updated, defaults: defaults)
)
}
private func updateThread(_ thread: InboxThreadSummary, isUnread: Bool) {
guard let index = threads.firstIndex(where: { $0.id == thread.id }) else { return }
let current = threads[index]
threads[index] = InboxThreadSummary(
rootEmailID: current.rootEmailID,
rootMessageID: current.rootMessageID,
threadRootEmailIDs: current.threadRootEmailIDs,
threadRootMessageIDs: current.threadRootMessageIDs,
listID: current.listID,
listRID: current.listRID,
listName: current.listName,
listOwner: current.listOwner,
subject: current.subject,
latestSender: current.latestSender,
lastActivityAt: current.lastActivityAt,
messageCount: current.messageCount,
repo: current.repo,
containsPatch: current.containsPatch,
isUnread: isUnread
)
}
private func deduplicateThreads(_ threads: [InboxThreadSummary]) -> [InboxThreadSummary] {
var grouped: [String: InboxThreadSummary] = [:]
for thread in threads {
guard let existing = grouped[thread.threadGroupingKey] else {
grouped[thread.threadGroupingKey] = thread
continue
}
let latest = thread.lastActivityAt >= existing.lastActivityAt ? thread : existing
let mergedRootEmailIDs = Array(Set(existing.threadRootEmailIDs + thread.threadRootEmailIDs)).sorted()
let mergedRootMessageIDs = Array(Set(existing.threadRootMessageIDs + thread.threadRootMessageIDs)).sorted()
let mergedMessageCount = max(
existing.messageCount ?? existing.threadRootMessageIDs.count,
thread.messageCount ?? thread.threadRootMessageIDs.count,
mergedRootMessageIDs.count
)
grouped[thread.threadGroupingKey] = InboxThreadSummary(
rootEmailID: latest.rootEmailID,
rootMessageID: latest.rootMessageID,
threadRootEmailIDs: mergedRootEmailIDs,
threadRootMessageIDs: mergedRootMessageIDs,
listID: latest.listID,
listRID: latest.listRID,
listName: latest.listName,
listOwner: latest.listOwner,
subject: latest.subject,
latestSender: latest.latestSender,
lastActivityAt: max(existing.lastActivityAt, thread.lastActivityAt),
messageCount: mergedMessageCount,
repo: latest.repo ?? existing.repo,
containsPatch: latest.containsPatch || existing.containsPatch,
isUnread: latest.isUnread || existing.isUnread
)
}
return grouped.values.sorted { lhs, rhs in
if lhs.lastActivityAt == rhs.lastActivityAt {
return lhs.displaySubject.localizedCaseInsensitiveCompare(rhs.displaySubject) == .orderedAscending
}
return lhs.lastActivityAt > rhs.lastActivityAt
}
}
nonisolated static func filterThreads(
_ threads: [InboxThreadSummary],
matching query: String
) -> [InboxThreadSummary] {
let q = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
guard !q.isEmpty else { return threads }
return threads.filter {
normalizedSubject(from: $0.subject).contains(q) ||
$0.latestSender.canonicalName.lowercased().contains(q)
}
}
private nonisolated static func normalizedSubject(from subject: String) -> String {
subject
.replacingOccurrences(of: #"\s+"#, with: " ", options: .regularExpression)
.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(
of: #"^(?:(?:re|fwd?)\s*:\s*)+"#,
with: "",
options: [.regularExpression, .caseInsensitive]
)
.lowercased()
}
}
enum MailingListScope: String, CaseIterable, Hashable {
case threads
case patches
var displayName: String {
switch self {
case .threads: "Threads"
case .patches: "Patches"
}
}
}
struct MailingListDetailView: View {
let mailingList: InboxMailingListReference
@Environment(AppState.self) private var appState
@State private var viewModel: MailingListDetailViewModel?
@State private var pinChangeCount = 0
@State private var scope: MailingListScope = .threads
private var currentUserKey: String? {
appState.currentUser?.canonicalName
}
private var isPinnedToHome: Bool {
_ = pinChangeCount
guard let currentUserKey else { return false }
return HomePinStore.isPinned(.mailingList(mailingList), for: currentUserKey, defaults: appState.accountDefaults)
}
private var hasUnreadThreads: Bool {
viewModel?.threads.contains(where: \.isUnread) == true
}
var body: some View {
Group {
if let viewModel {
content(viewModel)
} else {
SRHTLoadingStateView(message: "Loading mailing list…")
}
}
.navigationTitle(mailingList.name)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button("Mark All Read") {
viewModel?.markAllThreadsRead()
}
.disabled(hasUnreadThreads == false)
}
if currentUserKey != nil {
ToolbarItem(placement: .topBarTrailing) {
Button {
togglePinnedState()
} label: {
Image(systemName: isPinnedToHome ? "pin.fill" : "pin")
}
.accessibilityLabel(isPinnedToHome ? "Unpin from Home" : "Pin to Home")
}
}
}
.task {
if viewModel == nil {
let viewModel = MailingListDetailViewModel(
mailingList: mailingList,
client: appState.client,
defaults: appState.accountDefaults,
accountID: appState.activeAccountID
)
self.viewModel = viewModel
await viewModel.loadThreads()
}
}
.onAppear {
guard let viewModel else { return }
Task {
await viewModel.loadThreads()
}
}
}
private func togglePinnedState() {
guard let currentUserKey else { return }
HomePinStore.togglePin(.mailingList(mailingList), for: currentUserKey, defaults: appState.accountDefaults)
pinChangeCount += 1
}
@ViewBuilder
private func content(_ viewModel: MailingListDetailViewModel) -> some View {
@Bindable var vm = viewModel
List {
// Only offered when the list actually carries patches, so discussion
// lists do not grow an empty tab.
if !viewModel.patchsets.isEmpty {
Picker("Scope", selection: $scope) {
ForEach(MailingListScope.allCases, id: \.self) { scope in
Text(scope.displayName).tag(scope)
}
}
.pickerStyle(.segmented)
.listRowInsets(EdgeInsets(top: 4, leading: 12, bottom: 4, trailing: 12))
.themedRow()
}
if showingPatches(viewModel) {
ForEach(viewModel.filteredPatchsets) { patchset in
// Pushed directly rather than by value: this view is also shown
// from a project, whose stack declares no MoreRoute destination.
NavigationLink {
PatchsetDetailView(patchsetID: patchset.id, listName: mailingList.name)
} label: {
PatchsetRow(patchset: patchset)
}
.themedRow()
}
} else {
ForEach(viewModel.filteredThreads) { thread in
NavigationLink {
ThreadDetailView(
thread: thread,
onViewed: {
viewModel.markThreadRead(thread)
},
onMarkRead: {
viewModel.markThreadRead(thread)
},
onMarkUnread: {
viewModel.markThreadUnread(thread)
}
)
} label: {
InboxThreadRow(thread: thread)
}
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
Button {
withAnimation(.easeInOut(duration: 0.2)) {
if thread.isUnread {
viewModel.markThreadRead(thread)
} else {
viewModel.markThreadUnread(thread)
}
}
} label: {
Label(
thread.isUnread ? "Mark as Read" : "Mark as Unread",
systemImage: thread.isUnread ? "envelope.open" : "envelope.badge"
)
}
.tint(thread.isUnread ? .blue : .gray)
}
}
.themedRow()
}
}
.themedList()
.listStyle(.plain)
.searchable(
text: $vm.searchText,
placement: .navigationBarDrawer(displayMode: .always),
prompt: showingPatches(viewModel) ? "Search patches" : "Search messages"
)
.overlay {
if viewModel.isLoading, viewModel.threads.isEmpty {
SRHTLoadingStateView(message: "Loading mailing list…")
} else if let error = viewModel.error, viewModel.threads.isEmpty {
SRHTErrorStateView(
title: "Couldn't Load Mailing List",
message: error,
retryAction: { await viewModel.loadThreads() }
)
} else if showingPatches(viewModel) {
if !viewModel.patchsets.isEmpty, viewModel.filteredPatchsets.isEmpty {
ContentUnavailableView.search(text: viewModel.searchText)
}
} else if !viewModel.threads.isEmpty, viewModel.filteredThreads.isEmpty {
ContentUnavailableView.search(text: viewModel.searchText)
} else if viewModel.threads.isEmpty {
ContentUnavailableView(
"No Threads",
systemImage: "tray",
description: Text("This mailing list does not have any recent threads.")
)
}
}
.refreshable {
await viewModel.loadThreads()
}
.srhtErrorBanner(error: $vm.error)
}
private func showingPatches(_ viewModel: MailingListDetailViewModel) -> Bool {
scope == .patches && !viewModel.patchsets.isEmpty
}
}
struct PatchsetRow: View {
let patchset: PatchsetSummary
var body: some View {
VStack(alignment: .leading, spacing: 6) {
Text(patchset.subject)
.font(.subheadline.weight(.medium))
.lineLimit(2)
HStack(spacing: 8) {
PatchsetStatusBadge(status: patchset.status)
if let versionLabel = patchset.versionLabel {
Text(versionLabel)
.font(.caption.weight(.medium))
.foregroundStyle(.secondary)
}
}
}
.padding(.vertical, 2)
.accessibilityElement(children: .combine)
.accessibilityLabel("\(patchset.subject), \(patchset.status.displayName)")
}
}
struct ProjectMailingListView: View {
let mailingList: Project.MailingList
var body: some View {
MailingListDetailView(mailingList: mailingList.inboxReference)
}
}
|