// "Keep on this phone" and "Stop keeping" — the act on a group's page and in// a record's stack button: a GROUP or RECORD rule kept always, for THIS device, written// by `keep.set` and taken back by `keep.clear` (`CottonKit/KeepMethods.swift`,// the `set_keep_rule` / `clear_keep_rule` tools). Offered only where this// device is a phone: a Mac keeps everything and the core says so// (`keep.rules` answers `phone: false`).//// The act's receipt is an ordinary change: its Undo is the other method,// sent back through `CommandUndoCall`, so no surface puts an undo on a toast.importCottonKitimportFoundationenumKeepOnThisPhone {staticvar keepLabel: String { ShellLines.line("definitions", "keep.keep") }staticvar stopLabel: String { ShellLines.line("definitions", "keep.stop") }staticvar keptSaid: String { ShellLines.line("definitions", "keep.kept") }staticvar stoppedSaid: String { ShellLines.line("definitions", "keep.stopped") }/// Whether a table's records are offered "Keep on this phone": only ones/// that hold bytes — a file or a picture column (architect, r6). A secret,/// a todo or a note has nothing to keep. A face is not bytes the record/// holds: the picture a person or a chat heads its page with is kept by/// the face rule, so neither page offers it (architect, r8).staticfunc offered(_ definition: TableDefinition) -> Bool {let face = isFaced(definition) ? definition.slip.photo : nilreturn definition.columns.contains { ($0.type == .file || $0.type == .photo) && $0.key != face } }/// Whether `definition`'s slip photo is a face: somebody to reach, or a/// conversation with them.privatestaticfunc isFaced(_ definition: TableDefinition) -> Bool { definition.conversation != nil || definition.columns.contains { $0.reach != nil } }/// Whether THIS device keeps `target` always — `nil` where it is not a/// phone, or the core could not say: offer nothing then.staticfunc kept(kind: String, target: String, core: (any CottonCalling)?) async -> Bool? {guardlet core, let answer = try? await core.callJSON(CottonMethod.keepRules, KeepRulesRequest()), answer["phone"]?.bool == trueelse { returnnil }return answer.list("rules").contains { line inguard line.text("kind") == kind, let held = line.untrimmed("target") else { returnfalse }// A record's target names its store table; the id after the// table is the record either way.if kind == "record" { return held.split(separator: ":", maxSplits: 1).last == target.split(separator: ":", maxSplits: 1).last }return held == target } }/// RULE: "Keep on this phone" writes kind/target `always` for this device, "Stop keeping" clears it, and each receipt's Undo is the other.staticfunc act(kept: Bool, kind: String, target: String, table: String, record: String, core: any CottonCalling)asyncthrows -> ChangeRecord {let args: JSONValue = .object(["kind": .string(kind), "target": .string(target)])let back: CommandUndoCalldo {if kept { _ = tryawait core.callJSON(CottonMethod.keepClear, KeepClearRequest(kind: kind, target: target)) back = CommandUndoCall(method: CottonMethod.keepSet, args: json(args, rule: "always")) } else { _ = tryawait core.callJSON(CottonMethod.keepSet, KeepSetRequest(kind: kind, target: target, rule: "always")) back = CommandUndoCall(method: CottonMethod.keepClear, args: json(args, rule: nil)) } } catchlet refusal asCottonCallError {throwActionRefused(refusal.message) }returnChangeRecord( id: "keep-\(UUID().uuidString.lowercased())", byline: "You", before: nil, after: nil, undoable: true, said: ChangeSaid(kind: "command", said: kept ? stoppedSaid : keptSaid), command: CommandTaken(table: table, record: record, undo: nil, undoCall: back)) }privatestaticfunc json(_ args: JSONValue, rule: String?) -> String {guardcase .object(var fields) = args else { return"{}" }iflet rule { fields["rule"] = .string(rule) }let plain = fields.mapValues { $0.string ?? "" }let data = (try? JSONSerialization.data(withJSONObject: plain, options: [.sortedKeys])) ?? Data("{}".utf8)returnString(decoding: data, as: UTF8.self) }}