//! Ownership is an admission chain. The serving writer verifies the owner's //! person-key signature before appending a fact; this reader checks the //! signer at the fact's stamp, as membership does for other admissions. use crate::{driver::Driver, error::Result}; use rusqlite::types::Value; #[derive(Clone)] pub(super) struct Owner { pub id: String, pub person: String, pub stamp: String, pub signer: String, } pub(super) struct History { pub owners: Vec, } impl History { pub fn owner(&self) -> Option<&Owner> { self.owners.last() } pub fn last_owned(&self, person: &str) -> Option<&Owner> { self.owners .iter() .rev() .find(|owner| owner.person == person) } } /// A historical read is exclusive of `before`, just like membership::at. /// Ignore unauthorized admissions; a malformed newest row /// cannot displace the last valid owner or rewrite the birth admission. pub(super) fn read(db: &Driver, before: Option<&str>) -> Result { let bound = Value::Text(before.unwrap_or("").into()); let rows = db.all( "SELECT id,workspace_id,person_key,admitted_by_person,invite_id,p_sig,admitted_at,_hlc \ FROM cotton_admissions WHERE rights='owner' AND (?='' OR _hlc