//! A note the APP files about itself in the database's own `cotton_feedback`, //! at most once ever: a definition this build drew its own copy of //! (`catalogue`), a board part it skipped (`definitions.passedOver`), a theme //! family it does not ship (`theme_save`). The owner is the author, there is //! no picture, and the row travels by the outbox like every other write. //! //! A person's own report is not this: it is `feedback.send` //! (`feedback_send.rs`), which goes to Cotton, never into the person's data. use serde_json::{Map, Value}; use cotton_tools::mcp::dispatch::Ctx; use cotton_tools::mcp::handlers::common::insert_row; /// What `cotton_feedback.app` says for a row this build wrote. `list_feedback` /// readers group on it. Decided when the crate is COMPILED, per XCFramework /// slice: the iOS slices are the phone, the macOS slice is CottonDB for Mac. /// Never taken from the caller. pub const APP: &str = if cfg!(target_os = "macos") { "cotton-mac" } else { "cotton-ios" }; /// File `note` once: the row's id is derived from `mark`, so filing it again /// finds the row and writes nothing. Answers whether it wrote. pub fn file_once(ctx: &mut Ctx<'_>, mark: &str, note: &str, context: Map) -> Result { use sha2::{Digest, Sha256}; let digest = Sha256::digest(format!("cotton-feedback-once:{mark}").as_bytes()); let hex: String = digest.iter().take(16).map(|byte| format!("{byte:02x}")).collect(); let id = format!("{}-{}-5{}-8{}-{}", &hex[0..8], &hex[8..12], &hex[13..16], &hex[17..20], &hex[20..32]); let filed = ctx .read .driver() .get("SELECT id FROM cotton_feedback WHERE id = ?", &[rusqlite::types::Value::Text(id.clone())]) .map_err(|error| error.to_string())?; if filed.is_some() { return Ok(false); } let mut patch = Map::new(); patch.insert(cotton_core::rules::feedback::CAPTURE_APP_COLUMN.to_string(), Value::from(APP)); patch.insert("body".to_string(), Value::from(note)); // The owner, by id: the CHECK ties `user` to a non-null `author_user_id`, // and a label is what a GUEST gets. patch.insert("author_kind".to_string(), Value::from("user")); patch.insert("author_user_id".to_string(), Value::from(ctx.owner_user_id)); patch.insert("author_label".to_string(), Value::Null); patch.insert("context".to_string(), Value::Object(context)); patch.insert("status".to_string(), Value::from("open")); patch.insert(cotton_core::rules::feedback::CAPTURE_PARENT_COLUMN.to_string(), Value::Null); patch.insert("x".to_string(), Value::Null); patch.insert("y".to_string(), Value::Null); patch.insert("image_sha256".to_string(), Value::Null); patch.insert("image_ext".to_string(), Value::Null); insert_row(ctx, "cotton_feedback", &id, patch).map_err(|failure| failure.to_string())?; Ok(true) } #[cfg(test)] mod tests { use serde_json::json; use crate::shell::client::phone_tests::attached; use crate::shell::client::ClientController; fn bodies(client: &ClientController) -> Vec { client .with_writer(|replica| { Ok(replica .driver .all("SELECT body FROM cotton_feedback", &[]) .map_err(|error| error.to_string())? .iter() .filter_map(|row| row.text("body").map(str::to_string)) .collect()) }) .expect("the rows") } /// RULE: A board part this build cannot draw is said ONCE in Feedback — one row per database, board and part, however often a device meets it (design note 26203e21 §9). #[test] fn a_skipped_board_part_is_one_feedback_row_however_often_it_is_met() { let (_dir, client) = attached(); let request = json!({"board": "home", "part": "carousel", "databaseId": "db-invented", "databaseName": "Invented Studio"}); let first = crate::phone::records::call(&client, "definitions.passedOver", &request).expect("filed"); let again = crate::phone::records::call(&client, "definitions.passedOver", &request).expect("found"); assert_eq!((first["answer"]["filed"].clone(), again["answer"]["filed"].clone()), (json!(true), json!(false))); let said = bodies(&client); assert_eq!(said.len(), 1); assert!(said[0].contains("carousel") && said[0].contains("Invented Studio") && said[0].contains("home"), "{}", said[0]); let other = json!({"board": "home", "part": "shelf", "databaseId": "db-invented"}); crate::phone::records::call(&client, "definitions.passedOver", &other).expect("filed"); assert_eq!(bodies(&client).len(), 2); } }