Cotton code
89 lines · 3.7 KB · RustRaw
//! `people.printsNeeded` — the people the Mac owes a print (B7).//!//! A person's print is a small square copy of their face (about 512 px) that//! lives in `cotton_people.avatar_key` and syncs with the row, so every device//! draws it offline. Rust has no image code, so the Mac's Swift cuts it from//! the ORIGINAL in Photos and hands it to `capture.avatar {photoId}`; this read//! tells it whom to cut and from where.//!//! Owed: a live person with no print who has a confirmed face//! (`cotton_media_faces.person_id`, never only a suggestion) on a photo not in//! the bin — and a person whose print was cut from a photo that has since gone//! to the bin. Each answers ONE face, the best: highest quality, then the//! largest box, then the newest photo. Asked at attach and after each Photos//! pass, it is also the one-time pass for the people who were there before.//! A person the Mac makes a print for drops off the list.use rusqlite::types::Value as SqlValue;use serde_json::{json, Value};use cotton_store::driver::Driver;use crate::handle::CottonHandle;use crate::methods::MethodError;pub const METHODS: [&str; 1] = ["people.printsNeeded"];const LABEL: &str = "people.printsNeeded";/// `people.printsNeeded {}` — answers `{prints: [{personId, photoId,/// originHandle, box: {x, y, w, h}, orientation}]}`, the whole set: one row/// per person owed, at personal scale, and each drops off once made.pub fn call(handle: &CottonHandle, _request: &Value) -> Result<Value, MethodError> {    handle        .client()        .read_rows(LABEL, &mut |ctx| needed(ctx.db(), ctx.workspace_id))        .map_err(|reason| MethodError::new("read_refused", reason))}/// The read itself, over any file.pub fn needed(db: &Driver, workspace_id: &str) -> Result<Value, String> {    let rows = db        .all(            "WITH owed AS ( \               SELECT p.id FROM cotton_people p \               WHERE p.workspace_id = ?1 AND p.archived_at IS NULL \                 AND ( p.avatar_key IS NULL OR p.avatar_key = '' \                   OR EXISTS (SELECT 1 FROM cotton_photos o \                              WHERE o.id = p.avatar_photo_id AND o.trashed_at IS NOT NULL) ) \             ), ranked AS ( \               SELECT f.person_id, ph.id AS photo_id, ph.origin_handle, ph.orientation, \                      f.x, f.y, f.w, f.h, \                      row_number() OVER ( \                        PARTITION BY f.person_id \                        ORDER BY coalesce(f.quality, -1) DESC, f.w * f.h DESC, \                                 ph.taken_at DESC, f.id \                      ) AS rank \               FROM cotton_media_faces f \               JOIN owed ON owed.id = f.person_id \               JOIN cotton_photos ph ON ph.id = f.record_id \               WHERE f.workspace_id = ?1 AND f.record_kind = 'photo' \                 AND ph.workspace_id = ?1 AND ph.trashed_at IS NULL \             ) \             SELECT * FROM ranked WHERE rank = 1 ORDER BY person_id",            &[SqlValue::Text(workspace_id.to_string())],        )        .map_err(|error| error.to_string())?;    let prints: Vec<Value> = rows        .iter()        .map(|row| {            json!({                "personId": row.text("person_id"),                "photoId": row.text("photo_id"),                "originHandle": row.text("origin_handle"),                "box": {                    "x": row.real("x"),                    "y": row.real("y"),                    "w": row.real("w"),                    "h": row.real("h"),                },                "orientation": row.int("orientation"),            })        })        .collect();    Ok(json!({ "prints": prints }))}#[cfg(test)]#[path = "people_prints_tests.rs"]mod tests;