// What the notification extension says for a chat push, read from the push// itself without the core or the network: the sealed box the object put in// it, opened (`ChatBox`), titled with the name the app last wrote into the// app group (`ChatNames`), the number where it has none.//// It lands nothing: the app's `chat.incoming` takes, checks and lands every// item when it next opens. And it stays quiet rather than guess: a stranger's// request comes with no box (the object keeps their words off a lock screen// until the person accepts them), and a box that does not open, or opens to// something other than a message, keeps "New message".importFoundation/// A rewritten alert: the sender, and the words when the message has some.publicstructChatAlertWords: Equatable, Sendable {publicvar title: Stringpublicvar body: String?/// The sender's number (`+` and digits), as the box says it.publicvar from: String/// The group it was said in (its id), when it was.publicvar group: String?publicinit(title: String, body: String?, from: String = "", group: String? = nil) {self.title = titleself.body = bodyself.from = fromself.group = group }/// The conversation the alert is grouped under when the app has not yet/// said its thread (`ChatThreads`): the group, else the number.publicvar conversation: String { group ?? from }}publicenumChatAlert {structPayload: Decodable {var kind: Stringvar from: Stringvar body: String?var group: String? }/// The words for a pushed box, or nil to keep the push's own.publicstaticfunc words(box: String?, privateKey: String, names: [String: String]) -> ChatAlertWords? {guardlet box,let opened = ChatBox.open(box, privateKey: privateKey),let payload = try? JSONDecoder().decode(Payload.self, from: opened.payload), payload.kind == "message", !payload.from.isEmptyelse { returnnil }let body = payload.body?.trimmingCharacters(in: .whitespacesAndNewlines)returnChatAlertWords(title: names[payload.from] ?? payload.from, body: body?.isEmpty == false ? body : nil, from: payload.from, group: payload.group?.isEmpty == false ? payload.group : nil) }}/// The thread the core lands each Cotton conversation in, by its number/// (`chat.incoming`'s `landed` and `chat.start` say both), kept in the app/// group's defaults so the extension files an alert under the thread the/// app's `chat.notices` names, and a tap or a reply finds it. Only ids and/// numbers: never words, never keys.publicenumChatThreads {staticlet key = "cotton.chat.threads"publicstaticfunc read(from defaults: UserDefaults? = ChatNames.defaults()) -> [String: String] { defaults?.dictionary(forKey: key) as? [String: String] ?? [:] }publicstaticfunc remember(_ threads: [String: String], in defaults: UserDefaults? = ChatNames.defaults()) {guardlet defaults else { return }var held = read(from: defaults)let fresh = threads.filter { !$0.key.isEmpty && !$0.value.isEmpty }guard fresh.contains(where: { held[$0.key] != $0.value }) else { return } held.merge(fresh) { _, new in new } defaults.set(held, forKey: key) }publicstaticfunc forget(in defaults: UserDefaults? = ChatNames.defaults()) { defaults?.removeObject(forKey: key) }}/// The names the app knows its Cotton chats by, number to name, kept in the/// app group's defaults for the extension. Only names: never words, never keys.publicenumChatNames {staticlet key = "cotton.chat.names"publicstaticfunc defaults(bundleIdentifier: String? = Bundle.main.bundleIdentifier) -> UserDefaults? {UserDefaults(suiteName: CottonDefaults.appGroup(bundleIdentifier: bundleIdentifier)) }publicstaticfunc read(from defaults: UserDefaults? = defaults()) -> [String: String] { defaults?.dictionary(forKey: key) as? [String: String] ?? [:] }/// Names merged into what is kept; a blank name is not one.publicstaticfunc remember(_ names: [String: String], in defaults: UserDefaults? = defaults()) {guardlet defaults else { return }var held = read(from: defaults)let fresh = names.filter { !$0.value.trimmingCharacters(in: .whitespaces).isEmpty }guard fresh.contains(where: { held[$0.key] != $0.value }) else { return } held.merge(fresh) { _, new in new } defaults.set(held, forKey: key) }publicstaticfunc forget(in defaults: UserDefaults? = defaults()) { defaults?.removeObject(forKey: key) }}/// The names Cotton Chat's notifications are filed and answered under, the/// same in the extension that rewrites a push and in the app that registers/// the categories and answers their actions.publicenumChatNoticeNames {/// A conversation's notification: Reply and Mark as read.publicstaticlet category = "chat.message"publicstaticlet reply = "chat.reply"publicstaticlet markRead = "chat.read"/// A missed call's notification: Call back.publicstaticlet missedCategory = "chat.missed"publicstaticlet callBack = "chat.callBack"/// A missed call's notification identifier: this, then the call.publicstaticlet missedPrefix = "chat.missed."}