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
|
import Foundation
struct InboxThreadSummary: Identifiable, Hashable, Sendable {
let rootEmailID: Int
let rootMessageID: String
let threadRootEmailIDs: [Int]
let threadRootMessageIDs: [String]
let listID: Int
let listRID: String
let listName: String
let listOwner: Entity
let subject: String
let latestSender: Entity
let lastActivityAt: Date
let messageCount: Int?
let repo: String?
let containsPatch: Bool
let isUnread: Bool
var id: String {
threadGroupingKey
}
var listDisplayName: String {
"\(listOwner.canonicalName)/\(listName)"
}
var displaySubject: String {
Self.normalizedSubject(from: subject)
}
var metadataLine: String {
var parts = [latestSenderDisplayName]
if let messageCount, messageCount > 1 {
let replyCount = max(messageCount - 1, 1)
parts.append("\(replyCount) repl\(replyCount == 1 ? "y" : "ies")")
}
parts.append(lastActivityAt.relativeDescription)
return parts.joined(separator: " • ")
}
var latestSenderDisplayName: String {
let canonicalName = latestSender.canonicalName.trimmingCharacters(in: .whitespacesAndNewlines)
if canonicalName.contains("@") {
return canonicalName
}
return canonicalName
}
var debugIdentifierSummary: String {
"subject=\(subject) listRID=\(listRID) listID=\(listID) rootEmailID=\(rootEmailID) rootMessageID=\(rootMessageID) groupingKey=\(threadGroupingKey)"
}
var threadGroupingKey: String {
"\(listRID)#\(Self.normalizationKey(for: subject))"
}
nonisolated static func normalizationKey(for subject: String) -> String {
normalizedSubject(from: subject).lowercased()
}
private nonisolated static func normalizedSubject(from subject: String) -> String {
let collapsedWhitespace = subject
.replacingOccurrences(of: #"\s+"#, with: " ", options: .regularExpression)
.trimmingCharacters(in: .whitespacesAndNewlines)
let pattern = #"^(?:(?:re|fwd?)\s*:\s*)+"#
return collapsedWhitespace.replacingOccurrences(
of: pattern,
with: "",
options: [.regularExpression, .caseInsensitive]
)
}
}
struct InboxMessage: Identifiable, Hashable, Sendable {
let id: Int
let author: Entity
let date: Date
let subject: String
let body: String
let senderDisplayName: String
let senderEmailAddress: String?
let isPatch: Bool
let contentBlocks: [InboxMessageContentBlock]
let rawMessageURL: URL?
}
enum InboxMessageContentBlock: Hashable, Sendable {
case plainText(String)
case diff(String)
}
struct InboxThreadDetail: Sendable {
let id: String
let rootEmailID: Int
let rootMessageID: String
let subject: String
let author: Entity
let lastActivityAt: Date
let mailto: String?
let listID: Int
let listRID: String
let listName: String
let listOwner: Entity
let messageCount: Int?
let messages: [InboxMessage]
var listDisplayName: String {
"\(listOwner.canonicalName)/\(listName)"
}
}
extension InboxThreadDetail {
var replyRecipient: String {
"\(listOwner.canonicalName)/\(listName)@lists.sr.ht"
}
var replySubject: String {
subject.lowercased().hasPrefix("re:") ? subject : "Re: \(subject)"
}
var displaySubject: String {
InboxThreadSummary(
rootEmailID: rootEmailID,
rootMessageID: rootMessageID,
threadRootEmailIDs: [rootEmailID],
threadRootMessageIDs: [rootMessageID],
listID: listID,
listRID: listRID,
listName: listName,
listOwner: listOwner,
subject: subject,
latestSender: author,
lastActivityAt: lastActivityAt,
messageCount: messageCount,
repo: nil,
containsPatch: messages.contains(where: \.isPatch),
isUnread: false
).displaySubject
}
}
struct MailComposeDraft: Sendable {
let recipients: [String]
let ccRecipients: [String]
let subject: String
let body: String
var id: String {
([subject] + recipients + ccRecipients).joined(separator: "|")
}
}
extension MailComposeDraft: Identifiable {}
struct InboxMailingListReference: Decodable, Sendable, Hashable, Identifiable {
let id: Int
let rid: String
let name: String
let owner: Entity
}
struct InboxPatchPreview: Decodable, Sendable, Hashable {
let subject: String?
}
enum InboxReadStateStore {
private static let key = "InboxThreadLastViewed"
static func lastViewedAt(for threadID: String, defaults: UserDefaults = .standard) -> Date? {
guard let dictionary = defaults.dictionary(forKey: key) as? [String: TimeInterval],
let timestamp = dictionary[threadID] else {
return nil
}
return Date(timeIntervalSince1970: timestamp)
}
static func markViewed(_ date: Date, for threadID: String, defaults: UserDefaults = .standard) {
var dictionary = defaults.dictionary(forKey: key) as? [String: TimeInterval] ?? [:]
dictionary[threadID] = date.timeIntervalSince1970
defaults.set(dictionary, forKey: key)
}
static func markUnread(for threadID: String, defaults: UserDefaults = .standard) {
var dictionary = defaults.dictionary(forKey: key) as? [String: TimeInterval] ?? [:]
dictionary.removeValue(forKey: threadID)
defaults.set(dictionary, forKey: key)
}
static func isUnread(threadID: String, lastActivityAt: Date, defaults: UserDefaults = .standard) -> Bool {
guard let lastViewedAt = lastViewedAt(for: threadID, defaults: defaults) else {
return true
}
return lastActivityAt > lastViewedAt
}
}
|