//! Weighing feedback — the two readings the owner decides from (schema v25).//! The port of `packages/core/src/engine/feedback.ts`.//!//! Neither is a stored field, and that is the point. "How many people said//! this" and "is this at risk" are answers over rows the file already holds; a//! column holding either would be a second, staler copy of a count, and the day//! it disagreed with the rows nobody could tell which one was lying. So they//! are computed here, once, and the server's `list_feedback` and the app's//! feedback list read the SAME function — two spellings of "how many agree" is//! a badge disagreeing with a queue.use std::collections::HashMap;/// Immutable provenance the capture source writes beside the owner's words.pubconst CAPTURE_APP_COLUMN: &str = "app";pubconst CAPTURE_PARENT_COLUMN: &str = "parent_id";pubconst CAPTURE_FACT_COLUMNS: &[&str] = &[CAPTURE_APP_COLUMN, CAPTURE_PARENT_COLUMN];/// The `same_as` graph is one level deep on purpose: a duplicate points at the/// CANONICAL item, never at another duplicate. Nothing enforces that in the/// file, so the reader states it — a pointer at a pointer contributes its own/// voice to whatever it names, and no chain is walked.#[derive(Debug, Clone, PartialEq, Eq)]pubstructFeedbackVoiceRow {pub id: String,/// Who said it, when the file can name them; `None` for an unidentified/// reporter — a guest through a share link.pub person_id: Option<String>,/// The canonical item this one repeats, or `None` when it is canonical.pub same_as: Option<String>,}#[derive(Debug, Clone, PartialEq, Eq, Default)]pubstructFeedbackVoices {/// How many DISTINCT people are behind this item and everything repeating/// it. A named person counts once however many times they reported it; an/// unidentified reporter counts as one voice of their own, because a count/// that ignored them would under-weigh exactly the reports that arrive from/// somebody who has no row yet.pub voices: usize,/// The distinct people among them, in the order they were first seen — who/// agreed, not just how many. Empty when nobody is named yet.pub people: Vec<String>,}/// The voices behind each ROOT in `rows`, keyed by its id.////// Takes the whole set in one list rather than one item at a time: the/// duplicates of an item are other rows in the same table, so an/// item-at-a-time reading is a query per row on a list screen.pubfn feedback_voices(rows: &[FeedbackVoiceRow]) -> HashMap<String, FeedbackVoices> {letmut contributors: HashMap<&str, Vec<&FeedbackVoiceRow>> = HashMap::new();for row in rows {// A row always speaks for itself; a duplicate ALSO speaks for what it// repeats. Both, never one — an item that stopped counting its own// reporter the moment it was marked a duplicate would lose a voice on// being filed. contributors.entry(&row.id).or_default().push(row);ifletSome(same_as) = row.same_as.as_deref() { contributors.entry(same_as).or_default().push(row); } }letmut out = HashMap::new();for (id, bucket) in contributors {letmut people: Vec<String> = Vec::new();letmut unnamed = 0usize;for row in bucket {match row.person_id.as_deref() {None => unnamed += 1,Some(person) => {if !people.iter().any(|seen| seen == person) { people.push(person.to_string()); } } } } out.insert( id.to_string(),FeedbackVoices { voices: people.len() + unnamed, people, }, ); } out}/// How long a feedback body may run when it is standing in for a title. Two/// lines on a phone, which is what a row can draw without becoming the page.const FEEDBACK_LINE_MAX: usize = 120;/// A report's first line, as its own title.////// `cotton_feedback` has no title column and nothing invents one: a heading/// written for somebody's report would be a machine putting words in their/// mouth. So the row's own words stand in, cut at the first line break and/// capped — the whole body is one tap away, always.pubfn feedback_line(body: &str) -> String {let plain = crate::transforms::note_title::plain_preview(body);let body = plain.as_str();let is_space = cotton_store::protocol::js::is_js_whitespace;let first_line = body.split('\n').next().unwrap_or("").trim_matches(is_space);let line = if first_line.is_empty() { body.trim_matches(is_space) } else { first_line };let units: Vec<u16> = line.encode_utf16().collect();if units.len() <= FEEDBACK_LINE_MAX {return line.to_string(); }let cut = String::from_utf16_lossy(&units[..FEEDBACK_LINE_MAX - 1]); format!("{}…", cut.trim_end_matches(is_space))}#[cfg(test)]#[path = "feedback_tests.rs"]mod tests;/// Whether the capture describes a system notice rather than a report.pubfn is_system_notice(context: &serde_json::Value) -> bool { matches!(context["kind"].as_str(), Some("backup-failed" | "build-failed" | "system-notice"))}/// What a phone says when a table stored in a database could not be drawn/// and it drew its own copy: the tool's and the table's names in/// `project_feedback`'s readings (`fell_back_tool`, `fell_back_table`), never/// the parse error, which stays in the report's context (294, walker293: the/// title read "…definition JSON does not parse: unknown variant/// `account_number`…"). None for a table this build does not ship.pubfn definition_fallback_line(table: &str) -> Option<String> {usecrate::definitions::{builtin_table_definition, builtin_table_definitions};let words = builtin_table_definition("project_feedback")?;let reading = |key: &str| words.column(key).and_then(|column| column.reading.as_deref());let drawn = builtin_table_definition(table)?;let tool = builtin_table_definitions().iter().find_map(|home| { home.tool.as_ref().filter(|tool| tool.tables.iter().any(|id| id == table)).map(|tool| tool.name.as_str()) });letmut line = String::new();iflet (Some(tool), Some(template)) = (tool, reading("fell_back_tool")) { line.push_str(&template.replace("{value}", tool)); } line.push_str(&reading("fell_back_table")?.replace("{value}", &drawn.plural));Some(line)}/// A backup notice's list title: the backup's one line (`records::backup`);/// diagnostics stay with the captured context.pubfn system_notice_line(context: &serde_json::Value) -> Option<String> {crate::records::backup::notice_line(context, chrono::Utc::now())}