// "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. import CottonKit import Foundation enum KeepOnThisPhone { static var keepLabel: String { ShellLines.line("definitions", "keep.keep") } static var stopLabel: String { ShellLines.line("definitions", "keep.stop") } static var keptSaid: String { ShellLines.line("definitions", "keep.kept") } static var 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). static func offered(_ definition: TableDefinition) -> Bool { let face = isFaced(definition) ? definition.slip.photo : nil return 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. private static func 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. static func kept(kind: String, target: String, core: (any CottonCalling)?) async -> Bool? { guard let core, let answer = try? await core.callJSON(CottonMethod.keepRules, KeepRulesRequest()), answer["phone"]?.bool == true else { return nil } return answer.list("rules").contains { line in guard line.text("kind") == kind, let held = line.untrimmed("target") else { return false } // 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. static func act(kept: Bool, kind: String, target: String, table: String, record: String, core: any CottonCalling) async throws -> ChangeRecord { let args: JSONValue = .object(["kind": .string(kind), "target": .string(target)]) let back: CommandUndoCall do { if kept { _ = try await core.callJSON(CottonMethod.keepClear, KeepClearRequest(kind: kind, target: target)) back = CommandUndoCall(method: CottonMethod.keepSet, args: json(args, rule: "always")) } else { _ = try await core.callJSON(CottonMethod.keepSet, KeepSetRequest(kind: kind, target: target, rule: "always")) back = CommandUndoCall(method: CottonMethod.keepClear, args: json(args, rule: nil)) } } catch let refusal as CottonCallError { throw ActionRefused(refusal.message) } return ChangeRecord( 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)) } private static func json(_ args: JSONValue, rule: String?) -> String { guard case .object(var fields) = args else { return "{}" } if let rule { fields["rule"] = .string(rule) } let plain = fields.mapValues { $0.string ?? "" } let data = (try? JSONSerialization.data(withJSONObject: plain, options: [.sortedKeys])) ?? Data("{}".utf8) return String(decoding: data, as: UTF8.self) } }