import CottonKit import CottonValues import Foundation // The Stories row contract (mail7, 7 Oct): presentation only. Reading a // strip never stamps seen_at or calls a source; Keep uses records.update. enum StoriesPresentation { enum Identity: Hashable, Sendable { case person(String) case sender(source: String, account: String, handle: String) case unassigned(String) } struct Group: Identifiable, Hashable, Sendable { let id: Identity let title: String let filter: [String: RecordValue] var records: [TableRecord] } static func groups(_ records: [TableRecord], personColumn: String = "person_id", personNames: [String: String] = [:]) -> [Group] { var ordered: [(index: Int, record: TableRecord, posted: Date)] = [] for (index, record) in records.enumerated() { let posted: Date = record["posted_at"].text.flatMap { CottonDate.parse($0) } ?? .distantPast ordered.append((index: index, record: record, posted: posted)) } ordered.sort { a, b in if a.posted != b.posted { return a.posted > b.posted } return a.index < b.index } var result: [Group] = [] var positions: [Identity: Int] = [:] for row in ordered { let record = row.record let identity: Identity let filter: [String: RecordValue] if let person = record[personColumn].text { identity = .person(person) filter = [personColumn: .text(person)] } else if let sender = record["sender"].text, let account = record["account"].text { let source = record["source"].text ?? "" identity = .sender(source: source, account: account, handle: sender) filter = ["source": .text(source), "account": .text(account), "sender": .text(sender)] } else { identity = .unassigned(record.id) filter = ["id": .text(record.id)] } if let position = positions[identity] { result[position].records.append(record) } else { let title = record[personColumn].text.flatMap { personNames[$0] } ?? record["sender_label"].text ?? record["sender"].text ?? ShellLines.line("record", "unknown_person") positions[identity] = result.count result.append(Group(id: identity, title: title, filter: filter, records: [record])) } } return result } static func mediaKey(of record: TableRecord, column: String = "media_key") -> String? { guard RecordValue.isBlank(record["media_dropped_at"]) else { return nil } return record[column].text } static func displayed(_ record: TableRecord, mediaColumns: [String]) -> TableRecord { guard !RecordValue.isBlank(record["media_dropped_at"]) else { return record } var shown = record for column in mediaColumns { shown.values[column] = .null } return shown } static func displayed(_ record: TableRecord, definition: TableDefinition) -> TableRecord { guard definition.stories != nil else { return record } return displayed(record, mediaColumns: definition.columns.filter { $0.type == .photo || $0.type == .file }.map(\.key)) } static func pictureDefinition(_ definition: TableDefinition) -> TableDefinition { guard let stories = definition.stories else { return definition } var print = definition print.polaroid = stories.picture return print } static func isVideo(_ record: TableRecord, _ stories: StoriesLayout) -> Bool { record[stories.video.column].text.map(stories.video.values.contains) ?? false } static func isStrip(_ filter: [String: RecordValue], personColumn: String = "person_id") -> Bool { if filter[personColumn]?.text != nil || filter["id"]?.text != nil { return true } return ["source", "account", "sender"].allSatisfy { filter[$0]?.text != nil } } }