//! Views: how a collection's records are drawn, as data. A view document //! (`place.hud.view`) names the collection it renders and, for a //! page of records (`list`) and for one record (`record`), the joins that //! gather their related records (run by the server, `join`), the summary a //! tool answers with (`summary`), and a layout: a tree of components from a //! fixed catalog (`catalog`) whose props are literal values or bindings, //! paths into the data. A mode left out is drawn generically. A mode, a //! query view's too, may also join a few related queries, each called once //! on the whole answer (`QueryJoin`). //! //! A view is anyone's document: a model writes one inline, and later views //! will be records anyone publishes. So it is held to its shape strictly, //! every problem collected with where it is, for a model to fix them all at //! once. The bundled views (`views/.json`) are checked the same way, //! and they and the operator's (`operator`, added at startup) are the only //! ones trusted: only they may name `origins`, the https //! origins whose URLs their bindings may load, which the UI resource's CSP //! then allows, and `thumbnails`, where on those origins a picture drawn //! small (an avatar) is had smaller. A view given inline can neither name //! origins nor widen the CSP, and its description reaches no tool //! description. A trusted view also names its app (`app`, as "Bluesky", //! `apps`) and may add a section to accounts' profiles (`profile`, //! `Section`), which `describe_repo` draws with the view's own layout. //! Everything that reads the trusted views reads them through `bundled()`: //! the bundled ones, as the operator's replace and add to them (`Views`). use crate::catalog::{self, Given, Ty}; use serde_json::{Map, Value}; use std::collections::BTreeSet; use std::sync::{Arc, LazyLock, OnceLock}; /// The most thumbnail rules a trusted view may name. const MAX_THUMBNAILS: usize = 8; include!(concat!(env!("OUT_DIR"), "/views.rs")); /// The view document's type. pub const TYPE: &str = "place.hud.view"; /// How wide a view would be drawn, where its host can give it the room: the /// host's reading width (the default), a wider one, or all it has. pub const MEASURES: &[&str] = &["content", "wide", "full"]; /// The most bytes a view document may take, as JSON. pub const MAX_BYTES: usize = 64 * 1024; /// The deepest a layout may nest. const MAX_DEPTH: usize = 12; /// The most nodes a layout may have. const MAX_NODES: usize = 200; /// The deepest joins may nest. const MAX_JOIN_DEPTH: usize = 3; /// The most joins in one join array. const MAX_JOINS: usize = 8; /// The most keys a path may have. const MAX_PATH_KEYS: usize = 10; /// The most paths one binding may try. const MAX_BINDING_PATHS: usize = 4; /// The most `orderBy` paths of a join. const MAX_ORDER_BY: usize = 4; /// The most names a counts join counts. const MAX_COUNTS: usize = 8; /// A join's limit, by default and at most. pub const JOIN_LIMIT: usize = 50; pub const JOIN_LIMIT_MAX: usize = 500; /// The most origins a trusted view may name. const MAX_ORIGINS: usize = 8; /// The most problems an error names. const PROBLEMS_SHOWN: usize = 20; /// The most bytes an action's record may take, as JSON. const ACTION_RECORD_MAX: usize = 4096; /// The most params an `open` query names. const MAX_OPEN_PARAMS: usize = 8; /// The most query joins a mode makes. pub const MAX_QUERY_JOINS: usize = 6; /// The keys of the main scope a query join's params read, by mode: never /// `joins`, another join's result. A query's `at` is what its view shows /// first, where it shows one at a time (a story's archive, opened at it). const QUERY_SCOPE: &[&str] = &["method", "service", "params", "response", "me", "at"]; const LIST_SCOPE: &[&str] = &["did", "handle", "collection", "records", "cursor"]; const RECORD_SCOPE: &[&str] = &[ "did", "handle", "collection", "records", "cursor", "uri", "cid", "value", ]; /// An app page's main scope (`describe_app`): the app, the page's account /// and the signed-in one, whether they are one (`mine`), and the app's /// entry points. const PAGE_SCOPE: &[&str] = &["domain", "name", "icon", "actor", "me", "mine", "entries"]; /// A path into data: keys from a scope, an array's taken as its index. #[derive(Clone, Debug, PartialEq)] pub struct Path(Vec); impl Path { /// A dotted path, each key `[A-Za-z0-9_$]+`, at most 10; or `.`, the /// scope itself (an item of an array of strings, say: a post's tags); /// none if neither. A key `@name` takes the element of an array whose /// `$type` is named so (`preferences.@photoMetadataPref`: its /// `…#photoMetadataPref`), as a union's array holds one of each. pub fn parse(s: &str) -> Option { if s == "." { return Some(Path(Vec::new())); } let keys: Vec = s.split('.').map(str::to_string).collect(); let key = |k: &String| { let name = k.strip_prefix('@').unwrap_or(k); !name.is_empty() && name .bytes() .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'$') }; (keys.len() <= MAX_PATH_KEYS && keys.iter().all(key)).then_some(Path(keys)) } /// The value at the path in `v`, if there is one: an object's key, or /// an array's element by its index, or by its type (`@name`). pub fn get<'a>(&self, v: &'a Value) -> Option<&'a Value> { Self::get_rest(&self.0, v) } /// The path's first key, and the rest. pub fn split_first(&self) -> Option<(&str, &[String])> { self.0.split_first().map(|(k, rest)| (k.as_str(), rest)) } /// The value at the path below a first key already taken. pub fn get_rest<'a>(rest: &[String], v: &'a Value) -> Option<&'a Value> { rest.iter().try_fold(v, |v, k| match v { Value::Object(o) => o.get(k), Value::Array(a) => match k.strip_prefix('@') { Some(name) => a.iter().find(|e| { e["$type"] .as_str() .is_some_and(|t| t.rsplit_once('#').map_or(t, |(_, n)| n) == name) }), None => a.get(k.parse::().ok()?), }, _ => None, }) } } /// A value from the data: the first of its paths that resolves to /// something other than null. #[derive(Clone, Debug, PartialEq)] pub struct Binding(Vec); impl Binding { pub fn get<'a>(&self, scope: &'a Value) -> Option<&'a Value> { self.0 .iter() .find_map(|p| p.get(scope).filter(|v| !v.is_null())) } } /// A literal value, or a binding to one. #[derive(Clone, Debug, PartialEq)] pub enum Prop { Literal(Value), Bound(Binding), } impl Prop { pub fn get<'a>(&'a self, scope: &'a Value) -> Option<&'a Value> { match self { Prop::Literal(v) => Some(v), Prop::Bound(b) => b.get(scope), } } } /// How a join finds its records. #[derive(Clone, Debug, PartialEq)] pub enum Source { /// The record an at-uri or strongRef at this path names. Ref(Path), /// The records each element of an array names, at `ref` within it. Each { each: Path, at: Path }, /// The records of `collection` in the same repository whose `path` /// holds the record's uri. Backlinks { collection: String, path: Path }, /// Counts from a backlink index of the records pointing at the record: /// each name's collection and path (as the index writes it). Counts(Vec<(String, String, String)>), } /// A join: gathers related records for each record in scope, at `joins.`. #[derive(Clone, Debug, PartialEq)] pub struct Join { pub name: String, pub source: Source, /// The collection a ref's target must be of. pub collection: Option, pub order_by: Vec, pub limit: usize, /// Joins of its result records. pub join: Vec, } /// A query join: one query called once on the whole answer (a query /// view's, or a page of records, never each record), after it and beside /// the others, its answer at `joins.`, or null. #[derive(Clone, Debug, PartialEq)] pub struct QueryJoin { pub name: String, /// Where it is in its document, as its problems name it /// (`query.join[0]`). pub at: String, pub method: String, /// `did:…#fragment`; none for the view's own (a query view's), or else /// the method's bundled view's. pub service: Option, pub params: Vec<(String, Param)>, /// What the tool's text says of its answer, if anything. pub say: Option, /// Paths in the main scope: called only where `when` holds something /// (an app page's `mine`: one's own favorites) and `unless` nothing /// (another's known followers). pub when: Option, pub unless: Option, /// Asked each time, never an answer HUD keeps (`xrpc`). pub fresh: bool, } impl QueryJoin { /// The service it calls: its own, else `own` (a query view's), else /// its method's trusted view's. pub fn service_or(&self, own: Option<&str>) -> Option { self.service_among(own, views()) } /// `service_or`, the method's view among `views`. pub fn service_among(&self, own: Option<&str>, views: &Views) -> Option { self.service .clone() .or_else(|| own.map(str::to_string)) .or_else(|| views.service(&self.method).map(str::to_string)) } /// Whether it reads the page: a page of records' `records`, or a /// query's answer (`response`), so runs on every page of them, not on /// the first alone. pub fn per_page(&self) -> bool { self.bound().any(|p| { p.0.first() .is_some_and(|k| k == "records" || k == "response") }) } /// Whether all it reads of its main scope, its params, `when` and /// `unless`, is under the keys `known`: so it can run once those are, /// before the rest of the scope (a query's, beside its call). pub fn reads_only(&self, known: &[&str]) -> bool { self.bound() .chain(&self.when) .chain(&self.unless) .all(|p| p.0.first().is_some_and(|k| known.contains(&k.as_str()))) } /// It asking for the page at `cursor`, in place of any cursor it names. pub fn at_cursor(&self, cursor: &str) -> QueryJoin { let mut j = self.clone(); j.params.retain(|(k, _)| k != "cursor"); j.params .push(("cursor".into(), Param::Literal(cursor.into()))); j } /// The paths in the main scope its params read. fn bound(&self) -> impl Iterator { self.params.iter().flat_map(|(_, p)| match p { Param::Literal(_) => &[][..], Param::Bound(b) => &b.0[..], Param::Each { each, .. } => std::slice::from_ref(each), }) } } /// A query join's param: a literal, a value from the main scope, or the /// values at `at` in each element of the array at `each` there (each /// record's uri), each once, at most `MAX_PARAM_ARRAY` and the method's /// `maxLength` for it. #[derive(Clone, Debug, PartialEq)] pub enum Param { Literal(Value), Bound(Binding), Each { each: Path, at: Path }, } /// What a tool's text says of a query join's answer: how many there are /// (`count`, an array or an integer in it: `5 recent posts`), or its /// `label`'s first line (`README: knotless keeps…`). #[derive(Clone, Debug, PartialEq)] pub struct Say { pub noun: [String; 2], pub count: Option, pub label: Option, } /// The summary a tool answers with. #[derive(Clone, Debug, PartialEq)] pub struct Summary { pub noun: [String; 2], pub label: Option, /// Who an answer that is one thing is by: a query mode's without items. pub by: Option, /// A count and its noun; a count bound to an array counts its /// elements, and with the path to each one's children (as `replies`), /// theirs too, all the way down. pub detail: Option<(Prop, [String; 2], Option)>, } /// A mode of a view: a page of records, or one. #[derive(Clone, Debug, PartialEq)] pub struct Mode { pub join: Vec, /// The queries it joins, once a page. pub queries: Vec, pub summary: Option, } /// A method view's one mode: a query's answer. #[derive(Clone, Debug, PartialEq)] pub struct QueryMode { /// The answer's array of items, from the answer (`feed`); none for an /// answer that is one thing (a thread). pub items: Option, /// The answer's cursor for the next page, from the answer. pub cursor: Option, /// Each item's key, within it (`post.uri`): with it, a tree says how /// to tell something newer came (`_meta.ui.latest`). pub latest: Option, pub summary: Option, /// The queries it joins, once an answer. pub queries: Vec, /// Its query asked each time, never an answer HUD keeps (`xrpc`). pub fresh: bool, } /// An app page view's one mode: the queries it joins on the page's main /// scope (`PAGE_SCOPE`), its layout, in `doc`, what a #tag in its /// app's text opens (`{ query, params, service? }`, its params read from /// `{ tag }`), where the app has a search for one, and the join that pages /// the page (`pages`, its `as`): its answer's `cursor` the page more. #[derive(Clone, Debug, PartialEq)] pub struct PageMode { pub queries: Vec, pub tag: Option, pub pages: Option, } impl PageMode { /// The join that pages the page, asking for the page at `cursor`. pub fn more(&self, cursor: &str) -> Option { let name = self.pages.as_deref()?; let found = self.queries.iter().find(|j| j.name == name); Some(found?.at_cursor(cursor)) } } /// A view, checked: of a collection's records (`list`, `record`), of a /// query's answers (`query`), or a bundled app's page (`domain`, `page`). /// Its layouts stay in `doc`, which the view draws. #[derive(Clone, Debug, PartialEq)] pub struct View { /// The collection it draws, for a collection view. pub collection: Option, /// The query it draws, for a method view. pub method: Option, /// The app whose page it draws, for an app page view: its domain, as /// `apps` names one (`knotless.dev`). pub domain: Option, /// A method view's service, the query tool's default; an app page's, /// its joins': `did:…#fragment`. pub service: Option, pub description: String, /// The https origins its URL bindings may load: a trusted view's alone. pub origins: Vec, pub page_size: Option, pub list: Option, pub record: Option, pub query: Option, pub page: Option, /// The feed interactions a trusted method view reports for its items. pub interactions: Option, /// What a trusted method view asks a person signed out to do when its /// query needs an account: "Sign in to see your notifications". pub sign_in: Option, /// What a trusted method view offers a workspace's launcher: each a /// title and the query's params, `{"$me": "did" | "handle"}` the person. pub launch: Vec, /// The records a trusted method view draws as its query (`get_record` /// of one answers as the query does): each its collection and the /// query's params read from the record. pub records: Vec, /// The app a trusted view's records or query are of, as people name it /// ("Bluesky"): its NSID's domain is named so (`apps`). pub app: Option, /// The section a trusted view adds to an account's profile /// (`describe_repo`), where the account uses what it draws. pub profile: Option
, /// What an account did in making one of a trusted collection view's /// records, a short past-tense verb ("Posted"), for its profile's /// recent activity: the verb, then the record's summary label. pub activity: Option, /// The procedures its layouts call (`Action`'s `call`, `Messages`' /// `send`), and the queries whose deletion plans they delete /// (`deletes`): a trusted view's alone. pub calls: Vec, /// What its layouts' `deletes` say beyond their query (`Plan`): a /// trusted view's alone. pub plans: Vec, /// The document as given. pub doc: Value, } /// What a trusted feed view reports to its feed as the person reads it: /// the feed's at-uri, bound in the query's scope, and the paths within /// each item to its uri and the feed's own context for it. #[derive(Clone, Debug, PartialEq)] pub struct Interactions { pub feed: Binding, pub item: Path, pub feed_context: Option, pub req_id: Option, } /// A procedure a trusted view's layout calls as the signed-in person: its /// method, the service it is sent to, and its input as a template, whose /// values the view fills in (bindings, `{"$root": path}`, `{"$now": true}`, /// `{"$strongRef": path}`, and in a `Messages`' `send`, `{"$text": true}`, /// what the person wrote). With `deletes`, the method is a query of the /// service answering a deletion plan, the input its params, and what it /// plans is deleted (`Action`'s and select's `deletes`). #[derive(Clone, Debug, PartialEq)] pub struct Call { pub method: String, pub service: String, pub input: Value, pub deletes: bool, } /// What a trusted view's `deletes` says beyond its query, which HUD reads /// from the view as it deletes (`Hud::delete_planned`), so no client /// changes it: the query and its service; whether it is asked again until /// a plan comes back empty (`repeat`, an account's data, a part at a /// time); the collections its deletes may reach beyond those the views /// write, which sign-in asks to delete; and the procedure called once /// nothing is left (`then`), its input literal but `{"$did": true}`, the /// account's DID. #[derive(Clone, Debug, PartialEq)] pub struct Plan { pub method: String, pub service: String, pub repeat: bool, pub collections: Vec, pub then: Option, } /// The problems found in a document, each where it is; whether the /// document is trusted; whether a layout sends interactions; the /// procedures its layouts call; the service each trusted method view /// names, by its method, which a records view's query join calls when it /// names none; and the plans its layouts commit. struct Problems<'a>( Vec, bool, bool, Vec, &'a [(String, String)], Vec, ); impl Problems<'_> { fn add(&mut self, at: &str, what: impl std::fmt::Display) { self.0.push(format!("{at}: {what}")); } /// `v` as an object with only `allowed` keys, the others each a problem. fn object<'a>( &mut self, v: &'a Value, at: &str, allowed: &[&str], ) -> Option<&'a Map> { let Some(o) = v.as_object() else { self.add(at, "must be an object"); return None; }; self.keys(o, at, allowed); Some(o) } /// Each of an object's keys but `allowed` a problem. fn keys(&mut self, o: &Map, at: &str, allowed: &[&str]) { for k in o.keys().filter(|k| !allowed.contains(&k.as_str())) { self.add(at, format_args!("unknown key \"{k}\"")); } } fn path(&mut self, v: &Value, at: &str) -> Option { match v.as_str() { Some(s) => { let p = Path::parse(s); if p.is_none() { self.add(at, format_args!("unknown path syntax \"{s}\"")); } p } None => { self.add(at, "must be a path, keys joined by dots"); None } } } fn nsid(&mut self, v: Option<&Value>, at: &str) -> Option { match v.and_then(Value::as_str) { Some(s) if atproto::syntax::is_nsid(s) => Some(s.to_string()), _ => { self.add(at, "must be an NSID"); None } } } /// `v`, true or false; false where it is missing or neither. fn flag(&mut self, v: Option<&Value>, at: &str) -> bool { match v { None => false, Some(Value::Bool(b)) => *b, Some(_) => { self.add(at, "must be true or false"); false } } } fn line(&mut self, v: Option<&Value>, at: &str, max: usize) -> Option { match v.and_then(Value::as_str) { Some(s) if !s.trim().is_empty() && s.chars().count() <= max && !s.chars().any(char::is_control) => { Some(s.to_string()) } _ => { self.add( at, format_args!("must be one line of text, {max} characters at most"), ); None } } } fn nouns(&mut self, v: Option<&Value>, at: &str) -> Option<[String; 2]> { match v.and_then(Value::as_array).map(Vec::as_slice) { Some([Value::String(a), Value::String(b)]) if [a, b] .iter() .all(|w| (1..=32).contains(&w.len()) && !w.contains(char::is_control)) => { Some([a.clone(), b.clone()]) } _ => { self.add(at, "must be [singular, plural]"); None } } } fn int(&mut self, v: &Value, at: &str, max: u64) -> Option { let n = v.as_u64().filter(|n| (1..=max).contains(n)); if n.is_none() { self.add(at, format_args!("must be an integer from 1 to {max}")); } n } /// A binding, if `v` is one: `{"$": path}` or `{"$": [paths]}`. fn binding(&mut self, v: &Value, at: &str) -> Option> { let o = v.as_object().filter(|o| o.contains_key("$"))?; if o.len() != 1 { self.add(at, "a binding has the key \"$\" alone"); return Some(None); } let paths: Vec<&Value> = match &o["$"] { Value::Array(a) if (1..=MAX_BINDING_PATHS).contains(&a.len()) => a.iter().collect(), Value::Array(_) => { self.add( at, format_args!("a binding tries 1 to {MAX_BINDING_PATHS} paths"), ); return Some(None); } p => vec![p], }; let paths: Vec> = paths .into_iter() .map(|p| self.path(p, &format!("{at}.$"))) .collect(); Some(paths.into_iter().collect::>>().map(Binding)) } /// `{"$root": path}`: a path from the view's own scope. fn root(&mut self, v: &Value, at: &str) { let Some(o) = v.as_object() else { return }; if o.len() != 1 { self.add(at, "a root binding has the key \"$root\" alone"); return; } self.path(&o["$root"], &format!("{at}.$root")); } /// A summary value: a binding, or a literal checked by `literal`. fn prop( &mut self, v: &Value, at: &str, literal: impl Fn(&Value) -> bool, want: &str, ) -> Option { match self.binding(v, at) { Some(b) => b.map(Prop::Bound), None if literal(v) => Some(Prop::Literal(v.clone())), None => { self.add(at, format_args!("must be {want} or a binding")); None } } } } /// What only a bundled view may say at its top, which a view given inline /// may not. const BUNDLED_ONLY: [&str; 7] = [ "interactions", "signIn", "launch", "records", "app", "profile", "activity", ]; /// A bundled view's document as a view given inline may be, for the /// playground to start from: less what only a bundled view may say at its /// top. What its layout does that only a bundled view may (a procedure /// called, a feed told what was read) stays, a problem to take out. pub fn inline_copy(doc: &Value) -> Value { let mut copy = doc.clone(); let top = copy.as_object_mut().into_iter(); top.for_each(|o| o.retain(|k, _| !BUNDLED_ONLY.contains(&k.as_str()))); copy } /// Check a document; a view, or every problem with it. An untrusted /// view may not name origins. pub fn parse(doc: &Value, trusted: bool) -> Result> { parse_in(doc, trusted, &views().services) } /// `parse`, a records view's query joins calling by default the services /// `services` names, by method. fn parse_in( doc: &Value, trusted: bool, services: &[(String, String)], ) -> Result> { let size = serde_json::to_vec(doc).map_or(usize::MAX, |b| b.len()); if size > MAX_BYTES { return Err(vec![format!( "view: {size} bytes, more than the {MAX_BYTES} a view may take" )]); } let mut p = Problems(Vec::new(), trusted, false, Vec::new(), services, Vec::new()); let view = parse_view(&mut p, doc, trusted); match view { Some(v) if p.0.is_empty() => Ok(v), _ => Err(p.0), } } /// A view's problems as a tool's one-line error. pub fn error_text(problems: &[String]) -> String { let n = problems.len(); let shown: Vec<&str> = problems .iter() .take(PROBLEMS_SHOWN) .map(String::as_str) .collect(); let more = if n > PROBLEMS_SHOWN { format!("; and {} more", n - PROBLEMS_SHOWN) } else { String::new() }; format!( "Invalid view: {n} problem{}: {}{more}", if n == 1 { "" } else { "s" }, shown.join("; ") ) } fn parse_view(p: &mut Problems, doc: &Value, trusted: bool) -> Option { let o = p.object( doc, "view", &[ "$schema", "$type", "collection", "method", "service", "description", "origins", "thumbnails", "pageSize", "list", "record", "query", "interactions", "signIn", "launch", "records", "app", "profile", "activity", "domain", "page", "measure", "measureWhen", ], )?; if let Some(t) = o.get("$type") && t != TYPE { p.add("view.$type", format_args!("must be {TYPE}")); } // A view draws a collection's records, a query's answers, or an app's // page: one. let is_page = o.contains_key("domain") || o.contains_key("page"); let (collection, method) = match (o.get("collection"), o.get("method")) { (Some(_), _) | (_, Some(_)) if is_page => { p.add( "view", "an app page draws its domain, not a collection or a method", ); return None; } _ if is_page => (Some(None), Some(None)), (Some(_), Some(_)) => { // Which the rest should be checked as is unknown: say only this. p.add("view", "a view draws a collection or a method, not both"); return None; } (Some(c), None) => (p.nsid(Some(c), "view.collection").map(Some), Some(None)), (None, Some(m)) => (Some(None), p.nsid(Some(m), "view.method").map(Some)), (None, None) => { p.add("view", "collection or method required"); (None, None) } }; let is_method = matches!(method, Some(Some(_))); let description = p.line(o.get("description"), "view.description", 200); let origins = match o.get("origins") { None => Vec::new(), Some(v) => origins(p, v, trusted), }; if let Some(v) = o.get("thumbnails") { thumbnails(p, v, &origins); } // How wide it would be drawn, where its host can give it the room: by // the answer, the first of 1 to 8 cases whose `when` it holds, else // `measure`. let measure = |p: &mut Problems, m: &Value, at: &str| { if !MEASURES.iter().any(|w| m == *w) { p.add(at, format_args!("must be one of {}", MEASURES.join(", "))); } }; if let Some(m) = o.get("measure") { measure(p, m, "view.measure"); } match o.get("measureWhen").map(Value::as_array) { None => {} Some(Some(cases)) if (1..=8).contains(&cases.len()) => { for (i, case) in cases.iter().enumerate() { let at = format!("view.measureWhen[{i}]"); if p.object(case, &at, &["when", "measure"]).is_some() { p.path(&case["when"], &format!("{at}.when")); measure(p, &case["measure"], &format!("{at}.measure")); } } } Some(_) => p.add("view.measureWhen", "must be 1 to 8 cases"), } let refuse = |p: &mut Problems, k: &str, why: &str| { if o.contains_key(k) { p.add(&format!("view.{k}"), why); } }; let service = if is_method || is_page { match o.get("service").and_then(Value::as_str) { Some(s) => match crate::xrpc::Service::parse(s) { Ok(s) => Some(s.name()), Err(_) => { p.add("view.service", "must be a DID with its service's #fragment, as did:web:api.bsky.app#bsky_appview"); None } }, None => { let why = if is_page { "an app page names the service its joins call" } else { "a method view names the service it calls" }; p.add("view.service", why); None } } } else { refuse( p, "service", "only a method view or an app page names a service", ); None }; let mode = |p: &mut Problems, k: &str| o.get(k).map(|m| parse_mode(p, m, k)); let (page_size, list, record, query) = if is_page { for k in ["pageSize", "list", "record", "query"] { refuse(p, k, "an app page has its page alone"); } (None, None, None, None) } else if is_method { for k in ["pageSize", "list", "record"] { refuse(p, k, "a method view has the query mode alone"); } let query = match o.get("query") { Some(q) => parse_query_mode(p, q), None => { p.add("view", "a method view needs its query mode"); None } }; (None, None, None, query) } else { refuse(p, "query", "only a method view has a query mode"); let page_size = o .get("pageSize") .and_then(|v| p.int(v, "view.pageSize", 100)) .map(|n| n as u32); let list = mode(p, "list"); let record = mode(p, "record"); if list.is_none() && record.is_none() { p.add("view", "list or record required"); } (page_size, list.flatten(), record.flatten(), None) }; let interactions = match o.get("interactions") { None => None, Some(_) if !trusted => { p.add( "view.interactions", "only a bundled view reports interactions", ); None } Some(v) => { if !query.as_ref().is_some_and(|q| q.items.is_some()) { p.add( "view.interactions", "only a method view with query.items reports interactions, for its items", ); } parse_interactions(p, v) } }; let sign_in = match o.get("signIn") { None => None, Some(_) if !trusted => { p.add( "view.signIn", "only a bundled view says what signing in shows", ); None } Some(_) if !is_method => { p.add("view.signIn", "only a method view's query asks for sign-in"); None } Some(v) => p.line(Some(v), "view.signIn", 80), }; let launch = match o.get("launch") { None => Vec::new(), Some(_) if !trusted => { p.add("view.launch", "only a bundled view offers launchers"); Vec::new() } Some(_) if !is_method => { p.add("view.launch", "only a method view's query is launched"); Vec::new() } Some(v) => parse_launch(p, v), }; let records = match o.get("records") { None => Vec::new(), Some(_) if !trusted => { p.add( "view.records", "only a bundled view names the records it draws", ); Vec::new() } Some(_) if !is_method => { p.add( "view.records", "only a method view draws records, as its query", ); Vec::new() } Some(v) => parse_records(p, v), }; let app = match o.get("app") { None => None, Some(_) if !trusted => { p.add("view.app", "only a bundled view names its app"); None } Some(v) => p.line(Some(v), "view.app", MAX_APP), }; let profile = match o.get("profile") { None => None, Some(_) if is_page => { p.add("view.profile", "an app page adds no section to profiles"); None } Some(_) if !trusted => { p.add( "view.profile", "only a bundled view adds a section to profiles", ); None } Some(v) => parse_section(p, v, is_method), }; let activity = match o.get("activity") { None => None, Some(_) if !trusted => { p.add("view.activity", "only a bundled view names its activity"); None } Some(_) if is_method => { p.add( "view.activity", "only a collection view's records are activity", ); None } Some(_) if list.as_ref().and_then(|m| m.summary.as_ref()).is_none() => { p.add( "view.activity", "activity names its records by the list's summary, which it needs", ); None } Some(v) => p.line(Some(v), "view.activity", MAX_ACTIVITY), }; let app_page = if is_page { Some(parse_app_page(p, o, trusted)?) } else { None }; let (domain, page) = app_page.unzip(); // An action that sends an interaction sends it for an item of the // feed the view reports to. if p.2 && trusted && !o.contains_key("interactions") { p.add( "view", "an Action with interaction needs the view's interactions, naming the feed and its items", ); } Some(View { collection: collection?, method: method?, domain, service, description: description?, origins, page_size, list, record, query, page, interactions, sign_in, launch, records, app, profile, activity, calls: std::mem::take(&mut p.3), plans: std::mem::take(&mut p.5), doc: doc.clone(), }) } /// An app page view's `domain` and `page`: `{ join?, layout, tag?, pages? }`, /// its joins query joins alone, their params reading the page's main scope /// (`PAGE_SCOPE`), `pages` one of them by its `as`. A bundled view's alone: /// a page is the app's own. fn parse_app_page( p: &mut Problems, o: &Map, trusted: bool, ) -> Option<(String, PageMode)> { if !trusted { p.add("view.page", "only a bundled view is an app's page"); } let domain = match o.get("domain").and_then(Value::as_str) { Some(d) => match crate::icons::checked_domain(d, false) { Some(c) if c == d => Some(c), _ => { p.add( "view.domain", "must be an app's domain, in lower case, as knotless.dev", ); None } }, None => { p.add("view.domain", "an app page names its app's domain"); None } }; let Some(v) = o.get("page") else { p.add( "view.page", "an app page needs its page: { join?, layout, tag?, pages? }", ); return None; }; let at = "page"; let po = p.object(v, at, &["join", "layout", "tag", "pages", "head"])?; let queries = match po.get("join") { Some(j) => joins(p, j, "page.join", 1, Some((PAGE_SCOPE, false))).1, None => Vec::new(), }; layout(p, po.get("layout"), at); if let Some(h) = po.get("head") { head(p, h, "page.head"); } let tag = po.get("tag").cloned(); if let Some(t) = &tag { open_query(p, t, "page.tag"); } let pages = match po.get("pages") { None => None, Some(Value::String(s)) if queries.iter().any(|j| &j.name == s) => Some(s.clone()), Some(_) => { p.add("page.pages", "the as of one of the page's joins"); None } }; Some(( domain?, PageMode { queries, tag, pages, }, )) } /// The most characters an app's name takes. const MAX_APP: usize = 40; /// The most characters a view's activity verb takes. const MAX_ACTIVITY: usize = 20; /// The most records a collection view's profile section shows, and how /// many where it names none. const MAX_SECTION: u64 = 10; const SECTION_LIMIT: u32 = 3; /// The section a bundled view adds to an account's profile: its title, and /// what it shows. A method view's calls its query with `params`, each a /// literal or `{"$actor": "did"}`, the account's DID, or an array of those; /// a collection view's shows that collection's newest `limit` records. A /// method view's `head` section is drawn in the profile's head, untitled, /// for anyone the signed-in person looks at (a Message button), not where /// the account uses its app. #[derive(Clone, Debug, PartialEq)] pub struct Section { pub title: String, pub params: Map, pub limit: u32, pub head: bool, } impl Section { /// Its params with `$actor` made the account's DID. pub fn params_for(&self, did: &str) -> Map { let made = |v: &Value| match actor(v) { Some(_) => Value::String(did.to_string()), None => v.clone(), }; self.params .iter() .map(|(k, v)| { let v = match v { Value::Array(items) => Value::Array(items.iter().map(made).collect()), v => made(v), }; (k.clone(), v) }) .collect() } } /// What `{"$actor": …}` names, if `v` is that. fn actor(v: &Value) -> Option<&str> { v.as_object() .filter(|o| o.len() == 1) .and_then(|o| o.get("$actor")) .and_then(Value::as_str) } /// A view's `profile`: `{ title, params?, head? }` for a method view, each /// param a string, number or boolean, or `{"$actor": "did"}`, or an array /// of those; `{ title, limit? }` for a collection view, 1 to 10 records, 3 /// where it names none. fn parse_section(p: &mut Problems, v: &Value, method: bool) -> Option
{ let at = "view.profile"; let keys: &[&str] = if method { &["title", "params", "head"] } else { &["title", "limit"] }; let o = p.object(v, at, keys)?; let title = p.line(o.get("title"), &format!("{at}.title"), MAX_APP); let limit = match o.get("limit") { None => SECTION_LIMIT, Some(l) => p .int(l, &format!("{at}.limit"), MAX_SECTION) .map_or(SECTION_LIMIT, |n| n as u32), }; let head = match o.get("head") { None => false, Some(Value::Bool(b)) => *b, Some(_) => { p.add(&format!("{at}.head"), "must be true or false"); false } }; let one = |v: &Value| match v { Value::String(_) | Value::Number(_) | Value::Bool(_) => true, Value::Object(_) => actor(v) == Some("did"), _ => false, }; let mut params = Map::new(); match o.get("params") { None => {} Some(Value::Object(given)) if given.len() <= MAX_OPEN_PARAMS => { for (k, v) in given { let ok = match v { Value::Array(items) => { (1..=MAX_OPEN_ARRAY).contains(&items.len()) && items.iter().all(one) } v => one(v), }; if ok { params.insert(k.clone(), v.clone()); } else { p.add( &format!("{at}.params.{k}"), "must be a string, number, boolean or {\"$actor\": \"did\"}, or an array of those", ); } } } Some(_) => p.add( &format!("{at}.params"), format_args!("must be an object of at most {MAX_OPEN_PARAMS} params"), ), } Some(Section { title: title?, params, limit, head, }) } /// The most launchers a view offers. const MAX_LAUNCH: usize = 4; /// A launcher a bundled method view offers: a title, and the query's params, /// each a literal or `{"$me": "did" | "handle"}`, the person launching it. #[derive(Clone, Debug, PartialEq)] pub struct Launch { pub title: String, pub params: Map, } impl Launch { /// Its params with `$me` made the person's DID or handle; none when /// one names the handle and the person has none. pub fn params_for(&self, did: &str, handle: Option<&str>) -> Option> { self.params .iter() .map(|(k, v)| { let v = match me(v) { Some("did") => Value::String(did.to_string()), Some(_) => Value::String(handle?.to_string()), None => v.clone(), }; Some((k.clone(), v)) }) .collect() } /// Whether it names the person launching it (`$me`), so needs one /// signed in. pub fn names_me(&self) -> bool { self.params.values().any(|v| me(v).is_some()) } } /// What `{"$me": …}` names, if `v` is that. fn me(v: &Value) -> Option<&str> { v.as_object() .filter(|o| o.len() == 1) .and_then(|o| o.get("$me")) .and_then(Value::as_str) } /// A view's `launch`: 1 to 4 of `{ title, params? }`, each param a string, /// number or boolean, or `{"$me": "did" | "handle"}`. fn parse_launch(p: &mut Problems, v: &Value) -> Vec { let Some(items) = v.as_array().filter(|a| (1..=MAX_LAUNCH).contains(&a.len())) else { p.add( "view.launch", format_args!("must be 1 to {MAX_LAUNCH} launchers"), ); return Vec::new(); }; let mut out = Vec::new(); for (i, item) in items.iter().enumerate() { let at = format!("view.launch[{i}]"); let Some(o) = p.object(item, &at, &["title", "params"]) else { continue; }; let title = p.line(o.get("title"), &format!("{at}.title"), 40); let mut params = Map::new(); match o.get("params") { None => {} Some(Value::Object(given)) => { for (k, v) in given { let kat = format!("{at}.params.{k}"); let ok = match v { Value::String(_) | Value::Number(_) | Value::Bool(_) => true, Value::Object(_) => matches!(me(v), Some("did" | "handle")), _ => false, }; if ok { params.insert(k.clone(), v.clone()); } else { p.add( &kat, "must be a string, number, boolean or {\"$me\": \"did\" | \"handle\"}", ); } } } Some(_) => p.add(&format!("{at}.params"), "must be an object of params"), } if let Some(title) = title { out.push(Launch { title, params }); } } out } /// The most collections a view names in `records`. const MAX_RECORDS: usize = 8; /// What a `records` entry's bindings read: a record as `join::rec` gives /// it. const ROUTED_SCOPE: &[&str] = &["uri", "cid", "did", "value"]; /// A collection a bundled method view draws as its query: a record of it /// opened by its at-uri (`get_record`) answers as the query does, with /// `params` read from the record, each a literal or a binding into /// `{ uri, cid, did, value }`, and `at`, what the answer shows first where /// it shows one at a time (a story in its account's archive), a binding /// into it too; and what making one is, for a profile's recent activity. #[derive(Clone, Debug, PartialEq)] pub struct Routed { pub collection: String, pub params: Vec<(String, Prop)>, pub at: Option, pub activity: Option, } /// What making a record is, as an account's recent activity says it: a /// short past-tense verb ("Posted"), then the record's label, or its noun /// where it has none, as `summary` gives them. #[derive(Clone, Debug, PartialEq)] pub struct RecordActivity { pub verb: String, pub summary: Summary, } impl Routed { /// Whether a param, or `at`, reads what only reading the record gives: /// its `value` or `cid`. pub fn reads_record(&self) -> bool { let mut props = self.params.iter().map(|(_, p)| p).chain(&self.at); props.any(|p| match p { Prop::Literal(_) => false, Prop::Bound(b) => { b.0.iter() .any(|p| matches!(p.0.first().map(String::as_str), Some("value" | "cid"))) } }) } /// The query's params for the record `rec` (as `join::rec` gives it); /// none where a binding finds no string, number or boolean. pub fn params_for(&self, rec: &Value) -> Option> { self.params .iter() .map(|(k, p)| { let v = p.get(rec).filter(|v| match v { Value::String(s) => !s.is_empty(), v => v.is_number() || v.is_boolean(), })?; Some((k.clone(), v.clone())) }) .collect() } /// The `query` tool's arguments for the record `rec`: `method`, its /// params (`params_for`) and, where `at` finds a string, what the /// answer shows first. None where a param finds nothing. pub fn args_for(&self, method: &str, rec: &Value) -> Option { let mut args = serde_json::json!({ "method": method, "params": self.params_for(rec)? }); if let Some(first) = self .at .as_ref() .and_then(|a| a.get(rec)) .filter(|v| v.is_string()) { args["at"] = first.clone(); } Some(args) } } /// A view's `records`: 1 to 8 of `{ collection, params, at?, activity? }`, /// each collection once; each param a string, number or boolean, or a /// binding into the record (`ROUTED_SCOPE`); at a binding into the record; /// activity `{ verb, noun, label? }`, the verb one line of at most 20 /// characters, the label a binding into the record. fn parse_records(p: &mut Problems, v: &Value) -> Vec { let Some(items) = v .as_array() .filter(|a| (1..=MAX_RECORDS).contains(&a.len())) else { p.add( "view.records", format_args!("must be 1 to {MAX_RECORDS} collections"), ); return Vec::new(); }; // A binding into the record. let bound = |p: &mut Problems, v: &Value, at: &str| -> Option { let b = match p.binding(v, at) { Some(b) => b?, None => { p.add(at, "must be a binding {\"$\": path} into the record"); return None; } }; let into = |path: &Path| { path.0 .first() .is_some_and(|k| ROUTED_SCOPE.contains(&k.as_str())) }; if !b.0.iter().all(into) { p.add( at, format_args!("reads the record: a path from {}", ROUTED_SCOPE.join(", ")), ); return None; } Some(b) }; let mut out: Vec = Vec::new(); for (i, item) in items.iter().enumerate() { let at = format!("view.records[{i}]"); let Some(o) = p.object(item, &at, &["collection", "params", "at", "activity"]) else { continue; }; let collection = p.nsid(o.get("collection"), &format!("{at}.collection")); if let Some(c) = &collection && out.iter().any(|r| r.collection == *c) { p.add( &format!("{at}.collection"), format_args!("{c} is named twice"), ); } let mut params = Vec::new(); match o.get("params") { Some(Value::Object(given)) if (1..=MAX_OPEN_PARAMS).contains(&given.len()) => { for (k, v) in given { let kat = format!("{at}.params.{k}"); let prop = match v { Value::String(_) | Value::Number(_) | Value::Bool(_) => { Some(Prop::Literal(v.clone())) } v => bound(p, v, &kat).map(Prop::Bound), }; if let Some(prop) = prop { params.push((k.clone(), prop)); } } } _ => p.add( &format!("{at}.params"), format_args!("must be an object of 1 to {MAX_OPEN_PARAMS} params"), ), } let first = o .get("at") .and_then(|a| bound(p, a, &format!("{at}.at")).map(Prop::Bound)); let activity = o.get("activity").and_then(|a| { let aat = format!("{at}.activity"); let ao = p.object(a, &aat, &["verb", "noun", "label"])?; let verb = p.line(ao.get("verb"), &format!("{aat}.verb"), MAX_ACTIVITY); let noun = p.nouns(ao.get("noun"), &format!("{aat}.noun")); let label = match ao.get("label") { None => None, Some(l) => Some(Prop::Bound(bound(p, l, &format!("{aat}.label"))?)), }; Some(RecordActivity { verb: verb?, summary: Summary { noun: noun?, label, by: None, detail: None, }, }) }); if let Some(collection) = collection { out.push(Routed { collection, params, at: first, activity, }); } } out } /// Whether a launcher's params are its method's, as its lexicon's /// `def` takes them, `$me` standing for a DID and a handle and a feed /// named as a person names them (`feeds::names_feeds`) for an at-uri: /// every problem. pub fn launch_problems( launch: &Launch, lexicons: &atproto::lexicon::Lexicons, method: &str, def: &Value, ) -> Vec { let mut params = launch .params_for( "did:plc:abcdefghijklmnopqrstuvwx", Some("alice.example.com"), ) .unwrap_or_default(); crate::feeds::stand_in(method, &mut params); crate::xrpc::params_problems(lexicons, method, def, ¶ms) } /// The launchers every bundled view gives, for a workspace's "New tile", /// each `{ app, domain, title, method, service, params }`: its app as /// `apps` names it for its method's domain (`dev.knotless.getFeed`: /// knotless.dev, knotless; `app.bsky.feed.getTimeline`: bsky.app, /// Bluesky), its params made the person's (`did`, `handle`). One whose /// params need a handle the person lacks is left out. pub fn launchers(did: &str, handle: Option<&str>) -> Vec { bundled() .iter() .filter_map(|b| { Some(( b.view.method.as_deref()?, b.view.service.as_deref()?, &b.view.launch, )) }) .flat_map(|(method, service, launch)| { let domain = crate::apps::domain(method).unwrap_or_default(); let app = crate::apps::name(&domain); launch.iter().filter_map(move |l| { let params = l.params_for(did, handle)?; Some( serde_json::json!({ "app": &app, "domain": &domain, "title": l.title, "method": method, "service": service, "params": params }), ) }) }) .collect() } /// A view's `interactions`: `{ feed: binding, item: path, feedContext?: /// path, reqId?: path }`. fn parse_interactions(p: &mut Problems, v: &Value) -> Option { let at = "view.interactions"; let o = p.object(v, at, &["feed", "item", "feedContext", "reqId"])?; let feed = match o.get("feed") { Some(f) => match p.binding(f, &format!("{at}.feed")) { Some(b) => b, None => { p.add( &format!("{at}.feed"), "must be a binding {\"$\": path} to the feed's at-uri, as params.feed", ); None } }, None => { p.add(&format!("{at}.feed"), "required"); None } }; let item = match o.get("item") { Some(i) => p.path(i, &format!("{at}.item")), None => { p.add(&format!("{at}.item"), "required: the path to an item's uri"); None } }; let feed_context = o .get("feedContext") .and_then(|c| p.path(c, &format!("{at}.feedContext"))); let req_id = o .get("reqId") .and_then(|r| p.path(r, &format!("{at}.reqId"))); Some(Interactions { feed: feed?, item: item?, feed_context, req_id, }) } /// A method view's `query` mode: where the answer's items and cursor are, /// if it has them, the queries it joins, its summary, its layout. fn parse_query_mode(p: &mut Problems, v: &Value) -> Option { let at = "query"; let o = p.object( v, at, &[ "items", "cursor", "latest", "join", "summary", "head", "layout", "fresh", ], )?; let items = o.get("items").and_then(|i| p.path(i, "query.items")); let cursor = o.get("cursor").and_then(|c| p.path(c, "query.cursor")); if o.contains_key("cursor") && !o.contains_key("items") { p.add( "query.cursor", "a cursor pages items: name query.items too, or no cursor", ); } let latest = o.get("latest").and_then(|l| p.path(l, "query.latest")); if o.contains_key("latest") && !o.contains_key("items") { p.add( "query.latest", "the path to each item's key: name query.items too, or no latest", ); } let queries = match o.get("join") { Some(j) => joins(p, j, "query.join", 1, Some((QUERY_SCOPE, false))).1, None => Vec::new(), }; let summary = o .get("summary") .and_then(|s| summary(p, s, "query.summary", true)); if let Some(h) = o.get("head") { head(p, h, "query.head"); } layout(p, o.get("layout"), at); let fresh = p.flag(o.get("fresh"), "query.fresh"); Some(QueryMode { items, cursor, latest, summary, queries, fresh, }) } /// Check a mode's layout, and its size. fn layout(p: &mut Problems, v: Option<&Value>, at: &str) { match v { Some(l) => { let mut nodes = 0; node(p, l, &format!("{at}.layout"), 1, &mut nodes); // A Compose for another names one drawn in its scope. for (path, label, form) in crate::ui::fors(l) { if form.is_none() { let path: String = path.iter().map(|i| format!(".children[{i}]")).collect(); p.add( &format!("{at}.layout{path}.props.for"), format_args!("no Compose labelled \"{label}\" is drawn in its scope"), ); } } if nodes > MAX_NODES { p.add( &format!("{at}.layout"), format_args!("{nodes} nodes, more than {MAX_NODES}"), ); } } None => p.add(at, "layout required"), } } /// A view's origins: `https://host[:port]` each, written as the URL /// standard serializes it (lower case, no default port, at most a trailing /// `/`). A bundled view may name any; a view given inline only those a /// bundled view names, which every page that draws a view lets it load /// already (`csp_origins`), so its CSP is never wider. fn origins(p: &mut Problems, v: &Value, trusted: bool) -> Vec { let Some(a) = v .as_array() .filter(|a| (1..=MAX_ORIGINS).contains(&a.len())) else { p.add( "view.origins", format_args!("must be 1 to {MAX_ORIGINS} origins, as https://host[:port]"), ); return Vec::new(); }; let mut out = Vec::new(); for (i, o) in a.iter().enumerate() { let at = format!("view.origins[{i}]"); match o.as_str().and_then(origin) { Some(o) if !trusted && !bundled_origins().contains(&o) => p.add( &at, format_args!( "a view given inline names only origins a bundled view names: {}", bundled_origins().join(", ") ), ), Some(o) => out.push(o), None => p.add(&at, "must be an origin, as https://host[:port]"), } } out } /// Every origin the trusted views name (the bundled and the operator's), sorted, each once. fn bundled_origins() -> &'static [String] { static ALL: LazyLock> = LazyLock::new(|| { let mut all: Vec = bundled() .iter() .flat_map(|b| b.view.origins.iter().cloned()) .collect(); all.sort(); all.dedup(); all }); &ALL } /// An origin written as `https://host[:port]` and nothing more. pub fn origin(s: &str) -> Option { let u = url::Url::parse(s).ok()?; let origin = u.origin().ascii_serialization(); (u.scheme() == "https" && s.strip_suffix('/').unwrap_or(s) == origin).then_some(origin) } /// A trusted view's thumbnails: `{ from, to }` each, two https URL /// prefixes on its origins, each ending in `/`: a picture the view draws /// small (an avatar) whose URL starts with `from` is loaded from `to` /// instead, with the rest of its URL, as a CDN serves a smaller size there. fn thumbnails(p: &mut Problems, v: &Value, origins: &[String]) { let Some(a) = v .as_array() .filter(|a| (1..=MAX_THUMBNAILS).contains(&a.len())) else { p.add( "view.thumbnails", format_args!("must be 1 to {MAX_THUMBNAILS} rules, as {{ from, to }}"), ); return; }; for (i, rule) in a.iter().enumerate() { let at = format!("view.thumbnails[{i}]"); let Some(o) = p.object(rule, &at, &["from", "to"]) else { continue; }; for k in ["from", "to"] { let prefix = o.get(k).and_then(Value::as_str).and_then(|s| { let u = url::Url::parse(s).ok()?; (u.scheme() == "https" && u.as_str() == s && s.ends_with('/') && u.query().is_none() && origins.contains(&u.origin().ascii_serialization())) .then_some(s) }); if prefix.is_none() { p.add( &format!("{at}.{k}"), "must be an https URL ending in / on one of the view's origins", ); } } } } fn parse_mode(p: &mut Problems, v: &Value, at: &str) -> Option { let o = p.object(v, at, &["join", "summary", "head", "layout"])?; let keys = if at == "list" { LIST_SCOPE } else { RECORD_SCOPE }; let (join, queries) = match o.get("join") { Some(j) => joins(p, j, &format!("{at}.join"), 1, Some((keys, true))), None => (Vec::new(), Vec::new()), }; let summary = o .get("summary") .and_then(|s| summary(p, s, &format!("{at}.summary"), false)); if let Some(h) = o.get("head") { head(p, h, &format!("{at}.head")); } layout(p, o.get("layout"), at); Some(Mode { join, queries, summary, }) } /// A join array: record joins and, at a mode's top level (`top`: the keys /// of the main scope its query joins read, and whether record joins go /// there too, as in a records mode), query joins. fn joins( p: &mut Problems, v: &Value, at: &str, depth: usize, top: Option<(&[&str], bool)>, ) -> (Vec, Vec) { let Some(a) = v.as_array() else { p.add(at, "must be an array of joins"); return (Vec::new(), Vec::new()); }; if depth > MAX_JOIN_DEPTH { p.add(at, format_args!("joins nest at most {MAX_JOIN_DEPTH} deep")); return (Vec::new(), Vec::new()); } if a.len() > MAX_JOINS { p.add(at, format_args!("{} joins, more than {MAX_JOINS}", a.len())); } let mut out: Vec = Vec::new(); let mut queries: Vec = Vec::new(); let mut names: Vec = Vec::new(); for (i, j) in a.iter().enumerate() { let at = format!("{at}[{i}]"); let is_query = j.as_object().is_some_and(|o| o.contains_key("query")); let name = match (is_query, top) { (true, Some((keys, records))) => query_join(p, j, &at, keys, records).map(|q| { let name = q.name.clone(); queries.push(q); name }), (true, None) => { p.add( &at, "a query join runs once on the whole answer, at a mode's top level: it never nests in a join", ); None } (false, Some((_, false))) => { p.add( &at, "a query view's joins are query joins: { as, query, params?, service?, say? }", ); None } (false, _) => join(p, j, &at, depth).map(|j| { let name = j.name.clone(); out.push(j); name }), }; if let Some(name) = name { if names.contains(&name) { p.add( &format!("{at}.as"), format_args!("\"{name}\" is used twice"), ); } names.push(name); } } if queries.len() > MAX_QUERY_JOINS { p.add( at, format_args!("{} query joins, more than {MAX_QUERY_JOINS}", queries.len()), ); } (out, queries) } /// A query join: `{ as, query, params?, service?, say? }`, its params each /// literal JSON (a string, number or boolean, or an array of them), a /// binding or `{"$root": path}` into the main scope's `keys`, never a /// join's result. fn query_join( p: &mut Problems, v: &Value, at: &str, keys: &[&str], records: bool, ) -> Option { let o = p.object( v, at, &[ "as", "query", "params", "service", "say", "when", "unless", "fresh", ], )?; let name = match o.get("as").and_then(Value::as_str) { Some(s) if Path::parse(s).is_some_and(|p| p.0.len() == 1) => Some(s.to_string()), _ => { p.add(&format!("{at}.as"), "must be a key, [A-Za-z0-9_$]+"); None } }; let method = p.nsid(o.get("query"), &format!("{at}.query")); let pat = format!("{at}.params"); let mut params = Vec::new(); match o.get("params") { None => {} Some(Value::Object(given)) => { if given.len() > MAX_OPEN_PARAMS { p.add( &pat, format_args!("{} params, more than {MAX_OPEN_PARAMS}", given.len()), ); } for (k, v) in given { let kat = format!("{pat}.{k}"); let named = (1..=64).contains(&k.len()) && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_'); if !named { p.add(&kat, "a param's name is [A-Za-z0-9_]+"); } if let Some(param) = query_param(p, v, &kat, keys) { params.push((k.clone(), param)); } } } Some(_) => p.add(&pat, "must be an object of params"), } let service = match o.get("service") { None => None, Some(s) => match s.as_str().map(crate::xrpc::Service::parse) { Some(Ok(s)) => Some(s.name()), _ => { p.add( &format!("{at}.service"), "must be a DID with its service's #fragment, as did:web:api.bsky.app#bsky_appview", ); None } }, }; // A query view's joins call its own service by default; a records // view has none, so its method's trusted view's. if records && !o.contains_key("service") && let Some(m) = &method && service_in(p.4, m).is_none() { p.add( &format!("{at}.service"), format_args!("required: no bundled view of {m} names its service"), ); } let say = o.get("say").and_then(|s| say(p, s, &format!("{at}.say"))); let gate = |p: &mut Problems, key: &str| { let w = o.get(key)?; let wat = format!("{at}.{key}"); let path = p.path(w, &wat); if let Some(path) = &path && !path.0.first().is_some_and(|k| keys.contains(&k.as_str())) { p.add( &wat, format_args!("a path in the main scope: from {}", keys.join(", ")), ); } path }; let when = gate(p, "when"); let unless = gate(p, "unless"); let fresh = p.flag(o.get("fresh"), &format!("{at}.fresh")); Some(QueryJoin { name: name?, at: at.to_string(), method: method?, service, params, say, when, unless, fresh, }) } /// The most elements a query join's array param holds: a literal, or an `$each`'s. pub(crate) const MAX_PARAM_ARRAY: usize = 50; /// A query join's param: a binding or `{"$root": path}`, each path into the /// main scope's `keys` and through no `joins`; `{"$each": [array, path]}`, /// the value at `path` in each element of `array` there; else a literal /// string, number or boolean, or an array of them. fn query_param(p: &mut Problems, v: &Value, at: &str, keys: &[&str]) -> Option { // A path the main scope has: through no join's result. let main = |p: &mut Problems, path: &Path| { let first = path.0.first().map(String::as_str); if path.0.iter().any(|k| k == "joins") { p.add( at, "a query join reads the main answer, never a join's result", ); false } else if !first.is_some_and(|k| keys.contains(&k)) { p.add( at, format_args!( "a query join's params read the main scope: a path from {}", keys.join(", ") ), ); false } else { true } }; if let Some(each) = v.as_object().and_then(|o| o.get("$each")) { let pair = v.as_object().filter(|o| o.len() == 1).and(each.as_array()); let Some([array, path]) = pair.map(Vec::as_slice) else { p.add( at, "$each is [array, path] alone: the path within each element of the array", ); return None; }; let each = p.path(array, &format!("{at}.$each[0]"))?; let item = p.path(path, &format!("{at}.$each[1]"))?; return main(p, &each).then_some(Param::Each { each, at: item }); } let binding = match (p.binding(v, at), root(v)) { (Some(b), _) => Some(b?), (None, Some(r)) => { if v.as_object().is_some_and(|o| o.len() != 1) { p.add(at, "a root binding has the key \"$root\" alone"); return None; } Some(Binding(vec![p.path(r, &format!("{at}.$root"))?])) } (None, None) => None, }; if let Some(b) = binding { let mut ok = true; for path in &b.0 { ok &= main(p, path); } return ok.then_some(Param::Bound(b)); } let scalar = |v: &Value| matches!(v, Value::String(_) | Value::Number(_) | Value::Bool(_)); let ok = match v { Value::Array(items) => items.len() <= MAX_PARAM_ARRAY && items.iter().all(scalar), v => scalar(v), }; if !ok { p.add( at, format_args!( "must be a string, number or boolean (or an array of at most {MAX_PARAM_ARRAY} of them), a binding {{\"$\": path}} or {{\"$root\": path}}" ), ); return None; } Some(Param::Literal(v.clone())) } /// A query join's `say`: `{ noun: [singular, plural], count: path }` or /// `{ noun, label: path }`, its path into the join's answer. fn say(p: &mut Problems, v: &Value, at: &str) -> Option { let o = p.object(v, at, &["noun", "count", "label"])?; let noun = p.nouns(o.get("noun"), &format!("{at}.noun")); if o.contains_key("count") == o.contains_key("label") { p.add(at, "takes exactly one of count or label"); return None; } let count = o .get("count") .and_then(|c| p.path(c, &format!("{at}.count"))); let label = o .get("label") .and_then(|l| p.path(l, &format!("{at}.label"))); if count.is_none() && label.is_none() { return None; } Some(Say { noun: noun?, count, label, }) } /// The problems a query join's params have against its method's lexicon /// (`docs`, its own among them, as `Hud::lexicon_docs` gathers them): a /// method that is no query, params it does not take, required ones not /// given, and literal values it refuses. What a binding gives is checked /// when the join runs. pub fn query_join_problems(j: &QueryJoin, docs: &[std::sync::Arc]) -> Vec { let at = &j.at; let method = j.method.as_str(); let Some(doc) = docs.iter().find(|d| d["id"] == method) else { return vec![format!("{at}.query: no lexicon for {method}")]; }; let def = match crate::xrpc::query_def(method, doc) { Ok(def) => def, Err(why) => return vec![format!("{at}.query: {why}")], }; let taken = def["parameters"]["properties"].as_object(); let mut out: Vec = j .params .iter() .filter(|(k, _)| taken.is_none_or(|t| !t.contains_key(k))) .map(|(k, _)| format!("{at}.params.{k}: not a parameter of {method}")) .collect(); for k in def["parameters"]["required"] .as_array() .into_iter() .flatten() .filter_map(Value::as_str) { if !j.params.iter().any(|(n, _)| n == k) { out.push(format!("{at}.params.{k}: {method} requires it")); } } // The literals alone, checked as if nothing were required: a bound // param is had only when the join runs. let literals: Map = j .params .iter() .filter_map(|(k, v)| match v { Param::Literal(v) => Some((k.clone(), v.clone())), Param::Bound(_) | Param::Each { .. } => None, }) .collect(); let docs: Vec> = docs .iter() .map(|d| { if d["id"] != method { return d.clone(); } let mut d = (**d).clone(); if let Some(params) = d["defs"]["main"]["parameters"].as_object_mut() { params.remove("required"); } std::sync::Arc::new(d) }) .collect(); let lexicons = crate::xrpc::lexicons_for(method, &docs); if let Err(e) = lexicons.validate_params(method, &literals) { out.push(format!("{at}.{e}")); } out } fn join(p: &mut Problems, v: &Value, at: &str, depth: usize) -> Option { let o = p.object( v, at, &[ "as", "ref", "each", "backlinks", "counts", "collection", "orderBy", "limit", "join", ], )?; let name = match o.get("as").and_then(Value::as_str) { Some(s) if Path::parse(s).is_some_and(|p| p.0.len() == 1) => Some(s.to_string()), _ => { p.add(&format!("{at}.as"), "must be a key, [A-Za-z0-9_$]+"); None } }; if let Some(c) = o.get("counts") { for k in o.keys().filter(|k| !matches!(k.as_str(), "as" | "counts")) { p.add( &format!("{at}.{k}"), "a counts join takes \"as\" and \"counts\" alone", ); } let counted = counts(p, c, &format!("{at}.counts")); return Some(Join { name: name?, source: Source::Counts(counted?), collection: None, order_by: Vec::new(), limit: JOIN_LIMIT, join: Vec::new(), }); } let path = |p: &mut Problems, k: &str| o.get(k).and_then(|v| p.path(v, &format!("{at}.{k}"))); let source = match (o.get("ref"), o.get("each"), o.get("backlinks")) { (_, _, Some(b)) => { if o.contains_key("ref") || o.contains_key("each") { p.add(at, "backlinks takes no \"ref\" or \"each\""); } if o.contains_key("collection") { p.add( &format!("{at}.collection"), "backlinks names its collection in backlinks.collection", ); } let bat = format!("{at}.backlinks"); p.object(b, &bat, &["collection", "path"]).and_then(|b| { let collection = p.nsid(b.get("collection"), &format!("{bat}.collection")); let path = match b.get("path") { Some(v) => p.path(v, &format!("{bat}.path")), None => { p.add(&format!("{bat}.path"), "required"); None } }; Some(Source::Backlinks { collection: collection?, path: path?, }) }) } (Some(_), Some(_), None) => Some(Source::Each { each: path(p, "each")?, at: path(p, "ref")?, }), (Some(_), None, None) => path(p, "ref").map(Source::Ref), (None, Some(_), None) => { p.add(at, "\"each\" needs \"ref\", the path within each element"); None } (None, None, None) => { p.add(at, "\"ref\", \"backlinks\" or \"counts\" required"); None } }; let collection = o .get("collection") .filter(|_| !o.contains_key("backlinks")) .and_then(|c| p.nsid(Some(c), &format!("{at}.collection"))); let backlinks = matches!(source, Some(Source::Backlinks { .. })); let single = matches!(source, Some(Source::Ref(_))); let order_by = match o.get("orderBy") { None => Vec::new(), Some(_) if !backlinks => { p.add(&format!("{at}.orderBy"), "only backlinks are ordered"); Vec::new() } Some(Value::Array(a)) if a.len() <= MAX_ORDER_BY => a .iter() .enumerate() .filter_map(|(i, k)| p.path(k, &format!("{at}.orderBy[{i}]"))) .collect(), Some(_) => { p.add( &format!("{at}.orderBy"), format_args!("must be at most {MAX_ORDER_BY} paths"), ); Vec::new() } }; let limit = match o.get("limit") { None => JOIN_LIMIT, Some(_) if single => { p.add( &format!("{at}.limit"), "a ref joins one record; limit is for each and backlinks", ); JOIN_LIMIT } Some(v) => p .int(v, &format!("{at}.limit"), JOIN_LIMIT_MAX as u64) .map_or(JOIN_LIMIT, |n| n as usize), }; let nested = match o.get("join") { Some(j) => joins(p, j, &format!("{at}.join"), depth + 1, None).0, None => Vec::new(), }; Some(Join { name: name?, source: source?, collection, order_by, limit, join: nested, }) } /// A counts join's names: 1 to 8, each `[A-Za-z0-9_]+`, counting records /// of a collection whose field at a path (as the index writes it, from /// `.`) names the record. fn counts(p: &mut Problems, v: &Value, at: &str) -> Option> { let Some(o) = v.as_object() else { p.add(at, "must be an object of names to { collection, path }"); return None; }; if !(1..=MAX_COUNTS).contains(&o.len()) { p.add(at, format_args!("must name 1 to {MAX_COUNTS} counts")); } let mut out = Some(Vec::new()); for (name, c) in o { let cat = format!("{at}.{name}"); let named = (1..=64).contains(&name.len()) && name.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_'); if !named { p.add(&cat, "a count's name is [A-Za-z0-9_]+"); } let Some(c) = p.object(c, &cat, &["collection", "path"]) else { out = None; continue; }; let collection = p.nsid(c.get("collection"), &format!("{cat}.collection")); let path = c .get("path") .and_then(Value::as_str) .filter(|s| is_index_path(s)); if path.is_none() { p.add( &format!("{cat}.path"), "must be a field's path as the index writes it, starting with \".\", as \".subject.uri\"", ); } match (named, collection, path, &mut out) { (true, Some(collection), Some(path), Some(v)) => { v.push((name.clone(), collection, path.to_string())) } _ => out = None, } } out } /// A path as a backlink index writes it: `.`, then keys (and its own /// markers for arrays and union types), as `.subject.uri`. fn is_index_path(s: &str) -> bool { s.len() > 1 && s.len() <= 256 && s.starts_with('.') && s.bytes() .all(|b| b.is_ascii_alphanumeric() || b"._$#[]-".contains(&b)) } /// What a mode's `head` may say. pub(crate) const HEAD: [&str; 8] = [ "title", "description", "image", "imageAlt", "type", "canonical", "noindex", "jsonLd", ]; /// The kinds of page a head's `type` names, as Open Graph names them. const HEAD_TYPES: [&str; 3] = ["website", "article", "profile"]; /// A mode's `head`: what a page showing it says of itself to search /// engines and link previews (`ui::head`). Its text, image, canonical /// path and noindex are templates, as an action's record is; its type a /// word of `HEAD_TYPES`; its jsonLd an object of templates (`structured`). fn head(p: &mut Problems, v: &Value, at: &str) { let Some(o) = p.object(v, at, &HEAD) else { return; }; for (k, x) in o.iter().filter(|(k, _)| HEAD.contains(&k.as_str())) { let kat = format!("{at}.{k}"); match k.as_str() { "type" if !x.as_str().is_some_and(|t| HEAD_TYPES.contains(&t)) => { p.add(&kat, "must be website, article or profile"); } "type" => {} "jsonLd" if !x.is_object() => p.add(&kat, "must be an object"), "jsonLd" => structured(p, x, &kat, 1), _ => record_value(p, x, &kat, 1, None), } } } /// A head's structured data: an object or array of templates, keys as /// schema.org's (`@type`), where `{"$each": [array, template]}` is the /// template read against each element of the array. fn structured(p: &mut Problems, v: &Value, at: &str, depth: usize) { if depth > MAX_DEPTH { p.add( at, format_args!("structured data nests at most {MAX_DEPTH} deep"), ); return; } match v { Value::Object(o) if o.contains_key("$each") => match o["$each"].as_array() { Some(a) if o.len() == 1 && a.len() == 2 => { record_value(p, &a[0], &format!("{at}.$each[0]"), depth + 1, None); structured(p, &a[1], &format!("{at}.$each[1]"), depth + 1); } _ => p.add( at, "{\"$each\": [array, template]} has the key $each alone, an array and a template", ), }, Value::Object(o) if o.keys().any(|k| k.starts_with('$')) => { record_value(p, v, at, depth, None); } Value::Object(o) => { for (k, c) in o { structured(p, c, &format!("{at}.{k}"), depth + 1); } } Value::Array(a) => { for (i, c) in a.iter().enumerate() { structured(p, c, &format!("{at}[{i}]"), depth + 1); } } _ => {} } } /// A summary; `by` only in a query mode's, where an answer may be one /// thing with its own author. fn summary(p: &mut Problems, v: &Value, at: &str, query: bool) -> Option { let o = p.object(v, at, &["noun", "label", "by", "detail"])?; let noun = p.nouns(o.get("noun"), &format!("{at}.noun")); let label = o .get("label") .and_then(|l| p.prop(l, &format!("{at}.label"), Value::is_string, "a string")); let by = match o.get("by") { Some(_) if !query => { p.add( &format!("{at}.by"), "only a query mode's summary names who by; a record's is its repository's", ); None } Some(b) => p.prop(b, &format!("{at}.by"), Value::is_string, "a string"), None => None, }; let detail = o.get("detail").and_then(|d| { let dat = format!("{at}.detail"); let d = p.object(d, &dat, &["count", "noun", "children"])?; let count = match d.get("count") { Some(c) => p.prop(c, &format!("{dat}.count"), Value::is_u64, "an integer"), None => { p.add(&format!("{dat}.count"), "required"); None } }; let noun = p.nouns(d.get("noun"), &format!("{dat}.noun")); let children = match d.get("children") { Some(c) => Some(p.path(c, &format!("{dat}.children"))?), None => None, }; Some((count?, noun?, children)) }); Some(Summary { noun: noun?, label, by, detail, }) } /// Check a layout node, its props against the catalog, its children, and /// its fallback, a node a kit that doesn't know its component draws. fn node(p: &mut Problems, v: &Value, at: &str, depth: usize, nodes: &mut usize) { *nodes += 1; if depth > MAX_DEPTH { p.add(at, format_args!("layouts nest at most {MAX_DEPTH} deep")); return; } let Some(o) = p.object(v, at, &["component", "props", "children", "fallback"]) else { return; }; if let Some(fallback) = o.get("fallback") { node(p, fallback, &format!("{at}.fallback"), depth + 1, nodes); } let Some(name) = o.get("component").and_then(Value::as_str) else { p.add(&format!("{at}.component"), "required: a component's name"); return; }; let Some(c) = catalog::component(name) else { p.add( &format!("{at}.component"), format_args!("unknown component \"{name}\""), ); return; }; // A conversation pages at its top, as the layout's own: never inside // another node. if name == "Messages" && depth != 1 { p.add( &format!("{at}.component"), "Messages is a layout's top node alone", ); } // The host's accounts are the host's to draw, where a bundled view asks. if name == "Accounts" && !p.1 { p.add( &format!("{at}.component"), "only a bundled view asks the host", ); } let empty = Map::new(); let props = match o.get("props") { None => &empty, Some(Value::Object(props)) => props, Some(_) => { p.add(&format!("{at}.props"), "must be an object"); &empty } }; for (k, v) in props { let pat = format!("{at}.props.{k}"); match c.props.iter().find(|d| d.name == k) { Some(d) => prop(p, v, &pat, d.ty, d.given), None => p.add(&pat, format_args!("{name} has no prop \"{k}\"")), } } for d in c.props.iter().filter(|d| d.required) { if !props.contains_key(d.name) { p.add( &format!("{at}.props.{}", d.name), format_args!("{name} requires it"), ); } } // A composer's writes read its fields' and given values' names alone. if name == "Compose" { let (_, mut names) = crate::compose::fields_problems(&props.get("fields").cloned().unwrap_or_default(), ""); names.extend( props .get("given") .and_then(Value::as_object) .into_iter() .flatten() .map(|(k, _)| k.clone()), ); if props.contains_key("writes") { for problem in crate::compose::problems( &props.get("writes").cloned().unwrap_or_default(), Some(&names), ) { p.add(&format!("{at}.props"), problem); } } // A text's or line's limit: its record's lexicon's, narrowed by max. for problem in crate::compose::limit_problems( props.get("fields").unwrap_or(&Value::Null), props.get("writes").unwrap_or(&Value::Null), ) { p.add(&format!("{at}.props"), problem); } // One for another takes that one's form; the rest ask their own. if props.contains_key("for") { for k in [ "fields", "given", "count", "closed", "icon", "title", "about", "send", "form", ] { if props.contains_key(k) { p.add( &format!("{at}.props.{k}"), "a Compose for another takes that one's", ); } } } else if !props.contains_key("fields") { p.add(&format!("{at}.props.fields"), "Compose requires it"); } } // A photo's review draws its first field, its one photo, full-bleed. if name == "Compose" && props.contains_key("form") && !props .get("fields") .and_then(|f| f.get(0)) .is_some_and(|f| f["kind"] == "photos" && f["max"] == 1) { p.add( &format!("{at}.props.form"), "photo: its first field is photos of max 1", ); } // A choice's default is one of its options' values. if name == "Choice" && let Some(d) = props.get("default") && !props .get("options") .and_then(Value::as_array) .is_some_and(|a| a.iter().any(|o| o["value"] == *d)) { p.add( &format!("{at}.props.default"), "must be one of its options' values", ); } // A split is two sides. if name == "Split" && o.get("children").and_then(Value::as_array).map(Vec::len) != Some(2) { p.add(&format!("{at}.children"), "Split takes two children"); } // A map's pins are somewhere: at a cell, or at lat and lng. if name == "Map" && !props.contains_key("cell") && !(props.contains_key("lat") && props.contains_key("lng")) { p.add(&format!("{at}.props"), "Map takes cell, or lat and lng"); } let one = catalog::exclusive(name); if !one.is_empty() && one.iter().filter(|k| props.contains_key(**k)).count() != 1 { p.add( &format!("{at}.props"), format_args!("{name} takes exactly one of {}", one.join(", ")), ); } match o.get("children") { None => {} Some(_) if !c.children => { p.add( &format!("{at}.children"), format_args!("{name} takes no children"), ); } Some(Value::Array(children)) => { for (i, child) in children.iter().enumerate() { node(p, child, &format!("{at}.children[{i}]"), depth + 1, nodes); } } Some(_) => p.add(&format!("{at}.children"), "must be an array of nodes"), } } /// Check a prop's value against its catalog entry. fn prop(p: &mut Problems, v: &Value, at: &str, ty: Ty, given: Given) { if given == Given::EitherOrRoot && root(v).is_some() { p.root(v, at); return; } match p.binding(v, at) { Some(_) if given == Given::Literal => { p.add(at, "must be a literal value, not a binding"); } Some(_) => {} None if given == Given::Binding => { p.add(at, "must be a binding {\"$\": path}"); } None => { if let Err(why) = ty.check_literal(v) { p.add(at, why); return; } // The catalog's check has made a Create, Delete, Interaction or // Call an object. match (ty, v) { // An array of strings may hold bindings among its literals. (Ty::Strings, _) => { for (i, item) in v.as_array().into_iter().flatten().enumerate() { let iat = format!("{at}[{i}]"); if p.binding(item, &iat).is_none() && !item.is_string() { p.add(&iat, "must be a string or a binding"); } } } (Ty::Path, _) | (Ty::Gate, Value::String(_)) => { p.path(v, at); } // false: the item opens nothing. (Ty::Open, Value::Bool(_)) => {} (Ty::Open, Value::Object(o)) if o.contains_key("url") || o.contains_key("host") => { open_elsewhere(p, o, at) } (Ty::Open, _) => open_query(p, v, at), (Ty::Where, _) => { for (k, path) in v.as_object().into_iter().flatten() { let kat = format!("{at}.{k}"); if Path::parse(k).is_none() { p.add(&kat, "a key is a path within each element"); } let (path, _) = catalog::negated(path); if !catalog::is_literal(path) { p.path(path, &kat); } } } (Ty::Create, Value::Object(o)) => action_create(p, o, at), (Ty::Fields, _) => { for problem in crate::compose::fields_problems(v, at).0 { p.0.push(problem); } // What a field opens holding: a binding's paths. for (i, f) in v.as_array().into_iter().flatten().enumerate() { p.binding(&f["value"], &format!("{at}[{i}].value")); } } (Ty::Given, _) => { for (k, x) in v.as_object().into_iter().flatten() { if !k.chars().next().is_some_and(|c| c.is_ascii_lowercase()) || !k.chars().all(|c| c.is_ascii_alphanumeric()) { p.add(&format!("{at}.{k}"), "a name, lowercase letters and digits"); } record_value(p, x, &format!("{at}.{k}"), 1, None); } } // Checked with its fields' names (`node`). (Ty::Writes, _) => {} (Ty::Delete, Value::Object(o)) => action_delete(p, o, at), (Ty::Interaction, Value::Object(o)) => action_interaction(p, o, at), (Ty::Items, _) => literal_items(p, v, at), (Ty::Row, _) => literal_row(p, v, at), (Ty::Tabs, _) => tabs(p, v, at), (Ty::More, Value::Object(o)) => tab(p, o, at, "chip"), (Ty::Cases, _) => cases(p, v, at), (Ty::Call { fill }, Value::Object(o)) => call_to(p, o, at, fill), (Ty::Deletes, Value::Object(o)) => deletes_to(p, o, at), (Ty::Select, Value::Object(o)) => select_to(p, o, at), _ => {} } } } } /// A List's literal items: each element one `literal_row`. fn literal_items(p: &mut Problems, v: &Value, at: &str) { for (i, element) in v.as_array().into_iter().flatten().enumerate() { literal_row(p, element, &format!("{at}[{i}]")); } } /// One literal item (a List's literal items' each, its `first`): its `open` /// an `Open`, its other values literal JSON or `{"$root": path}` at any depth. fn literal_row(p: &mut Problems, element: &Value, at: &str) { for (k, x) in element.as_object().into_iter().flatten() { let kat = format!("{at}.{k}"); if k == "open" { prop(p, x, &kat, Ty::Open, Given::Either); } else { literal_value(p, x, &kat); } } } /// A value in a literal item: `{"$root": path}`, or literal JSON whose /// objects name no key starting with `$` but `$type`. fn literal_value(p: &mut Problems, v: &Value, at: &str) { if root(v).is_some() { p.root(v, at); return; } match v { Value::Object(o) => { if o.keys().any(|k| k.starts_with('$') && k != "$type") { p.add( at, "a literal item's value is literal JSON or {\"$root\": path}", ); return; } for (k, x) in o { literal_value(p, x, &format!("{at}.{k}")); } } Value::Array(a) => { for (i, x) in a.iter().enumerate() { literal_value(p, x, &format!("{at}[{i}]")); } } _ => {} } } /// Tabs: each `{ label, open }`, label one short line and open an `Open` /// that opens something; each an object, as the catalog's check made it. fn tabs(p: &mut Problems, v: &Value, at: &str) { let tabs = v.as_array().into_iter().flatten(); for (i, o) in tabs.filter_map(Value::as_object).enumerate() { tab(p, o, &format!("{at}[{i}]"), "tab"); } } /// One `{ label, open }` (a tab, a List's `more` chip): label one short /// line and open an `Open` that names a query. fn tab(p: &mut Problems, o: &Map, at: &str, what: &str) { p.keys(o, at, &["label", "open"]); p.line(o.get("label"), &format!("{at}.label"), 40); let oat = format!("{at}.open"); match o.get("open") { None | Some(Value::Bool(_)) => p.add( &oat, format!("required: what the {what} opens, as List's open"), ), Some(open) if open.get("url").or_else(|| open.get("host")).is_some() => p.add( &oat, format!("a {what} opens a query, not a page or the host's own"), ), Some(open) => prop(p, open, &oat, Ty::Open, Given::Either), } } /// Opens by case: each `{ where?, open }`, where a `Where` and open an /// `Open`; each an object, as the catalog's check made it. fn cases(p: &mut Problems, v: &Value, at: &str) { let cases = v.as_array().into_iter().flatten(); for (i, o) in cases.filter_map(Value::as_object).enumerate() { let cat = format!("{at}[{i}]"); p.keys(o, &cat, &["where", "open"]); if let Some(w) = o.get("where") { prop(p, w, &format!("{cat}.where"), Ty::Where, Given::Literal); } match o.get("open") { Some(open) => prop(p, open, &format!("{cat}.open"), Ty::Open, Given::Literal), None => p.add(&format!("{cat}.open"), "required: what the element opens"), } } } /// What the host does itself where an `open` asks it (`{"host": …}`): sign /// the account out of the app, as an app's own Settings' Sign out. pub const HOSTED: &[&str] = &["signOut"]; /// An `open` that opens no query: `{ url }`, an https page opened in the /// host's browser (a site's page the app has no view of), or, in a bundled /// view alone, `{ host }`, one of `HOSTED`, which the host does itself. fn open_elsewhere(p: &mut Problems, o: &Map, at: &str) { let one: &[&str] = if o.contains_key("url") { &["url"] } else { &["host"] }; p.keys(o, at, one); if let Some(url) = o.get("url") { let page = url .as_str() .and_then(|s| url::Url::parse(s).ok()) .is_some_and(|u| u.scheme() == "https" && u.host().is_some()); if !page { p.add(&format!("{at}.url"), "must be an https URL"); } } let Some(host) = o.get("host") else { return; }; if !p.1 { p.add(at, "only a bundled view asks the host"); } else if !host.as_str().is_some_and(|h| HOSTED.contains(&h)) { p.add( &format!("{at}.host"), format_args!("must be one of {}", HOSTED.join(", ")), ); } } /// An `open` that calls a query: `{ query, params?, service?, at?, sheet?, /// compose?, unless? }`, the query an NSID, its params at most 8, each named /// as a lexicon names one and a param value (`open_param`), the service as /// the query tool's, `at` (what its view shows first) a param value too, /// `sheet` (its screen over what is open) a flag, `compose` (the form its /// screen opens at) true or a Compose's label, and `unless` a path in the /// scope that, holding something, drops `compose`, so given only with it. fn open_query(p: &mut Problems, v: &Value, at: &str) { let keys = [ "query", "params", "service", "at", "sheet", "compose", "unless", ]; let Some(o) = p.object(v, at, &keys) else { return; }; if let Some(first) = o.get("at") { open_param(p, first, &format!("{at}.at"), false); } if o.get("sheet").is_some_and(|s| !s.is_boolean()) { p.add(&format!("{at}.sheet"), "must be true or false"); } if o.get("compose").is_some_and(|f| !crate::ui::is_compose(f)) { p.add( &format!("{at}.compose"), "must be true or a Compose's label, 1 to 64 characters", ); } if let Some(unless) = o.get("unless") { let uat = format!("{at}.unless"); p.path(unless, &uat); if !o.contains_key("compose") { p.add(&uat, "is only for a compose"); } } p.nsid(o.get("query"), &format!("{at}.query")); let pat = format!("{at}.params"); match o.get("params") { None => {} Some(Value::Object(params)) => { if params.len() > MAX_OPEN_PARAMS { p.add( &pat, format_args!("{} params, more than {MAX_OPEN_PARAMS}", params.len()), ); } for (k, v) in params { let kat = format!("{pat}.{k}"); let named = (1..=64).contains(&k.len()) && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_'); if !named { p.add(&kat, "a param's name is [A-Za-z0-9_]+"); } open_param(p, v, &kat, true); } } Some(_) => p.add(&pat, "must be an object of params"), } if let Some(s) = o.get("service") { let ok = s .as_str() .is_some_and(|s| crate::xrpc::Service::parse(s).is_ok()); if !ok { p.add( &format!("{at}.service"), "must be a DID with its service's #fragment, as did:web:api.bsky.app#bsky_appview", ); } } } /// The value of `{"$root": …}`, if `v` is that. fn root(v: &Value) -> Option<&Value> { v.as_object() .filter(|o| o.contains_key("$root")) .map(|o| &o["$root"]) } /// The most elements an open param's array holds. const MAX_OPEN_ARRAY: usize = 50; /// A param of an `open` query: a binding into the item, `{"$root": path}` /// into the view's own scope, `{"$atUri": [authority, collection, rkey]}`, /// each of the three a string or one of those bindings, or at the top an /// array of any of those; anything else is a literal, as is, but an /// object whose keys start with `$` is one of these or a mistake. fn open_param(p: &mut Problems, v: &Value, at: &str, top: bool) { if p.binding(v, at).is_some() { return; } if root(v).is_some() { p.root(v, at); return; } match v { Value::Object(o) if o.contains_key("$atUri") => { if o.len() != 1 { p.add(at, "an at-uri to build has the key \"$atUri\" alone"); return; } let aat = format!("{at}.$atUri"); match o["$atUri"].as_array() { Some(parts) if parts.len() == 3 => { for (i, part) in parts.iter().enumerate() { let pat = format!("{aat}[{i}]"); if part.is_string() { continue; } if p.binding(part, &pat).is_none() && root(part).is_none() { p.add(&pat, "must be a string, a binding or {\"$root\": path}"); } else if root(part).is_some() { p.root(part, &pat); } } } _ => p.add(&aat, "must be [authority, collection, rkey]"), } } Value::Object(o) if o.contains_key("$after") => { let args = o["$after"].as_array().filter(|a| o.len() == 1 && a.len() == 2); match args { Some(a) if a[1].as_str().is_some_and(|s| !s.is_empty()) => { open_param(p, &a[0], &format!("{at}.$after[0]"), false) } _ => p.add( at, "{\"$after\": [value, prefix]} has the key $after alone, the prefix a string", ), } } Value::Object(o) if o.keys().any(|k| k.starts_with('$')) => p.add( at, "an object with a key starting with $ is {\"$\": path}, {\"$root\": path}, {\"$atUri\": [authority, collection, rkey]} or {\"$after\": [value, prefix]}", ), Value::Array(items) if top => { if items.len() > MAX_OPEN_ARRAY { p.add(at, format_args!("more than {MAX_OPEN_ARRAY} elements")); } for (i, item) in items.iter().enumerate() { open_param(p, item, &format!("{at}[{i}]"), false); } } Value::Array(_) => p.add(at, "an array of params holds no arrays"), _ => {} } } /// An action's `create`: `{ collection, record }`, the collection an NSID, /// the record an object of at most 4 KiB whose values are checked by /// `record_value`. fn action_create(p: &mut Problems, o: &Map, at: &str) { p.keys(o, at, &["collection", "record"]); p.nsid(o.get("collection"), &format!("{at}.collection")); let rat = format!("{at}.record"); match o.get("record") { Some(r @ Value::Object(_)) if p.binding(r, &rat).is_none() => { let size = serde_json::to_vec(r).map_or(usize::MAX, |b| b.len()); if size > ACTION_RECORD_MAX { p.add( &rat, format_args!( "{size} bytes, more than the {ACTION_RECORD_MAX} a record may take" ), ); } record_fields(p, r, &rat, 1, None); } Some(_) => p.add(&rat, "must be an object, the record"), None => p.add(&rat, "required"), } } /// An action's `interaction`: `{ event }`, asking the feed for more or /// less like the item; a bundled view's alone. fn action_interaction(p: &mut Problems, o: &Map, at: &str) { if !p.1 { p.add(at, "only a bundled view sends interactions"); return; } p.2 = true; p.keys(o, at, &["event"]); let ok = o .get("event") .and_then(Value::as_str) .is_some_and(|e| crate::interactions::ASKED.contains(&e)); if !ok { p.add( &format!("{at}.event"), format_args!("must be one of {}", crate::interactions::ASKED.join(", ")), ); } } /// Covers' `select`: `{ label?, mine?, action, noun, confirm?, tone?, call /// | deletes }`, its words short, `mine` a path in the scope, `noun` the /// singular and plural, `tone` danger, and `call` a procedure as `call_to` /// holds one, or `deletes` a deletion plan's query as `deletes_to` holds /// one, its input or params read from each item picked. fn select_to(p: &mut Problems, o: &Map, at: &str) { p.keys( o, at, &[ "label", "mine", "action", "noun", "confirm", "tone", "call", "deletes", ], ); let words = |v: Option<&Value>, max: usize| { v.and_then(Value::as_str).is_some_and(|s| { (1..=max).contains(&s.chars().count()) && !s.contains(char::is_control) }) }; for (k, max, needed) in [ ("label", 40, false), ("action", 40, true), ("confirm", 300, false), ] { if (needed || o.contains_key(k)) && !words(o.get(k), max) { p.add( &format!("{at}.{k}"), format_args!("text of at most {max} characters"), ); } } let noun = o.get("noun").and_then(Value::as_array); if !noun.is_some_and(|n| n.len() == 2 && n.iter().all(|w| words(Some(w), 40))) { p.add(&format!("{at}.noun"), "must be [singular, plural]"); } if o.get("tone").is_some_and(|t| t != "danger") { p.add(&format!("{at}.tone"), "must be danger"); } if let Some(mine) = o.get("mine") { p.path(mine, &format!("{at}.mine")); } match (o.get("call"), o.get("deletes")) { (Some(Value::Object(c)), None) => call_to(p, c, &format!("{at}.call"), None), (None, Some(Value::Object(d))) => deletes_to(p, d, &format!("{at}.deletes")), _ => p.add( at, "takes one of call ({ method, service, input? }) and deletes ({ method, service, params? })", ), } } /// A procedure a layout calls (`Action`'s `call`; `Messages`' `send`, the /// box's text its `{"$text": true}`; a `Toggle`'s, its new state its /// `{"$on": true}`): `{ method, service, input? }`, the method an NSID, /// the service a DID with its service's #fragment, the input an object of /// at most 4 KiB whose values `record_value` checks, holding `fill`, the /// mark the person's own value takes, where there is one; a bundled view's /// alone, kept for the server's list of the calls bundled views make. fn call_to(p: &mut Problems, o: &Map, at: &str, fill: Option<&str>) { called(p, o, at, fill, false); } /// The query whose deletion plan a layout deletes (`Action`'s and Covers' /// select's `deletes`): `{ method, service, params?, repeat?, collections?, /// then? }`, as `call_to` holds a call, its params the input; a bundled /// view's alone, kept with its calls, marked as deleting, and what it says /// beyond its query kept as its `Plan`: repeat true or false, collections /// at most `PLAN_COLLECTIONS` NSIDs, and then `{ method, service, input? /// }`, its input literal but `{"$did": true}`. fn deletes_to(p: &mut Problems, o: &Map, at: &str) { let before = p.3.len(); called(p, o, at, None, true); if !p.1 { return; } let repeat = match o.get("repeat") { None => false, Some(Value::Bool(b)) => *b, Some(_) => { p.add(&format!("{at}.repeat"), "must be true or false"); false } }; let mut collections = Vec::new(); match o.get("collections") { None => {} Some(Value::Array(a)) if a.len() <= PLAN_COLLECTIONS => { for (i, c) in a.iter().enumerate() { collections.extend(p.nsid(Some(c), &format!("{at}.collections[{i}]"))); } } Some(_) => p.add( &format!("{at}.collections"), format_args!("must be a list of at most {PLAN_COLLECTIONS} collections' NSIDs"), ), } let then = o .get("then") .and_then(|t| plan_then(p, t, &format!("{at}.then"))); if let Some(c) = p.3.get(before) { let (method, service) = (c.method.clone(), c.service.clone()); p.5.push(Plan { method, service, repeat, collections, then, }); } } /// `call_to`'s and `deletes_to`'s checks: a call, or with `deletes` a /// query whose plan is deleted, its template under `params`. fn called(p: &mut Problems, o: &Map, at: &str, fill: Option<&str>, deletes: bool) { let (key, what) = match deletes { true => ("params", "the query's params"), false => ("input", "the procedure's input"), }; if !p.1 { let why = match deletes { true => "only a bundled view deletes what a query plans", false => "only a bundled view calls procedures", }; p.add(at, why); return; } let keys: &[&str] = match deletes { true => &[ "method", "service", "params", "repeat", "collections", "then", ], false => &["method", "service", "input"], }; p.keys(o, at, keys); let method = p.nsid(o.get("method"), &format!("{at}.method")); let service = service_at(p, o, at); let iat = format!("{at}.{key}"); let input = match o.get(key) { None => Value::Object(Map::new()), Some(i @ Value::Object(m)) if !m.contains_key("$") && root(i).is_none() => { let size = serde_json::to_vec(i).map_or(usize::MAX, |b| b.len()); if size > ACTION_RECORD_MAX { p.add( &iat, format_args!( "{size} bytes, more than the {ACTION_RECORD_MAX} an input may take" ), ); } record_fields(p, i, &iat, 1, fill); i.clone() } Some(_) => { p.add(&iat, format_args!("must be an object, {what}")); return; } }; if let Some(mark) = fill && !fills(&input, mark) { let what = if mark == "$text" { "writes" } else { "chooses" }; p.add( &iat, format_args!("holds no {{\"{mark}\": true}}, where what the person {what} goes"), ); } if let (Some(method), Some(service)) = (method, service) { p.3.push(Call { method, service, input, deletes, }); } } /// Whether a template holds `{mark: true}` anywhere. fn fills(v: &Value, mark: &str) -> bool { match v { Value::Object(o) => { o.get(mark) == Some(&Value::Bool(true)) || o.values().any(|c| fills(c, mark)) } Value::Array(items) => items.iter().any(|c| fills(c, mark)), _ => false, } } /// The `service` of `o` at `at`: a DID with its service's #fragment; else /// none, its problem added. fn service_at(p: &mut Problems, o: &Map, at: &str) -> Option { match o.get("service").and_then(Value::as_str) { Some(s) if crate::xrpc::Service::parse(s).is_ok() => Some(s.to_string()), _ => { p.add( &format!("{at}.service"), "required: a DID with its service's #fragment, as did:web:api.bsky.chat#bsky_chat", ); None } } } /// The most collections a `deletes` names. const PLAN_COLLECTIONS: usize = 32; /// A plan's `then`: `{ method, service, input? }`, its input literal JSON /// but `{"$did": true}`, the account's DID; else none, its problems added. fn plan_then(p: &mut Problems, t: &Value, at: &str) -> Option { let o = p.object(t, at, &["method", "service", "input"])?; let method = p.nsid(o.get("method"), &format!("{at}.method")); let service = service_at(p, o, at); let input = o .get("input") .cloned() .unwrap_or_else(|| Value::Object(Map::new())); let iat = format!("{at}.input"); if !input.is_object() { p.add(&iat, "must be an object, the procedure's input"); } else if let Some(wrong) = unbound(&input, &iat) { p.add( &wrong, "is literal JSON but {\"$did\": true}, the account's DID", ); } Some(Call { method: method?, service: service?, input, deletes: false, }) } /// Where in a plan's `then` input a key starting with `$` is not `{"$did": /// true}` alone, if anywhere. fn unbound(v: &Value, at: &str) -> Option { match v { Value::Object(o) if o.keys().any(|k| k.starts_with('$') && k != "$type") => { (o.len() != 1 || o.get("$did") != Some(&Value::Bool(true))).then(|| at.to_string()) } Value::Object(o) => o.iter().find_map(|(k, x)| unbound(x, &format!("{at}.{k}"))), Value::Array(a) => a .iter() .enumerate() .find_map(|(i, x)| unbound(x, &format!("{at}[{i}]"))), _ => None, } } /// An action's `delete`: `{ uri: binding }`. fn action_delete(p: &mut Problems, o: &Map, at: &str) { p.keys(o, at, &["uri"]); let uat = format!("{at}.uri"); match o.get("uri") { Some(u) => { if p.binding(u, &uat).is_none() { p.add(&uat, "must be a binding {\"$\": path} to the record's uri"); } } None => p.add(&uat, "required"), } } /// A template's fields (an action's record, a procedure's input, a /// composer's given values): keys starting with `$` are `$type`, a /// string, alone; each value checked by `record_value`. With `fill` /// (`Messages`' `send`, a `Toggle`'s call), it may take the person's own value. fn record_fields(p: &mut Problems, v: &Value, at: &str, depth: usize, fill: Option<&str>) { for (k, child) in v.as_object().into_iter().flatten() { let kat = format!("{at}.{k}"); match k.as_str() { "$type" if !child.is_string() => p.add(&kat, "must be a string"), "$type" => {} k if k.starts_with('$') => match fill { Some(mark) => p.add( &kat, format_args!( "a key starting with $ is $type, or a binding's, $root's, $now's, $strongRef's, $rkey's, $first's, $join's, $with's, $without's, $move's or {mark}'s alone" ), ), None => p.add( &kat, "a key starting with $ is $type, or a binding's, $root's, $now's, $strongRef's, $rkey's, $first's, $join's, $with's, $without's or $move's alone", ), }, _ => record_value(p, child, &kat, depth + 1, fill), } } } /// A value in a template: a literal; a binding; `{"$now": true}`; /// `{"$strongRef": path}`; `{"$rkey": path}`, the record key of the at-uri /// there; `{"$root": path}`; `{"$first": [values]}`, the /// first of them that resolves; `{"$join": [values]}`, their strings /// joined; an array's edit (`template_edit`); where the person writes or /// chooses, its mark (`{"$text": true}`, `{"$on": true}`); or an object or /// array of those. fn record_value(p: &mut Problems, v: &Value, at: &str, depth: usize, fill: Option<&str>) { if depth > MAX_DEPTH { p.add( at, format_args!("a template nests at most {MAX_DEPTH} deep"), ); return; } match v { Value::Object(o) if o.contains_key("$") => { p.binding(v, at); } Value::Object(o) if o.contains_key("$now") => { if o.len() != 1 || o["$now"] != Value::Bool(true) { p.add(at, "{\"$now\": true} has the key $now alone, true"); } } Value::Object(o) if o.contains_key("$strongRef") => { if o.len() != 1 { p.add(at, "{\"$strongRef\": path} has the key $strongRef alone"); } else { p.path(&o["$strongRef"], &format!("{at}.$strongRef")); } } Value::Object(o) if o.contains_key("$rkey") => { if o.len() != 1 { p.add(at, "{\"$rkey\": path} has the key $rkey alone"); } else { p.path(&o["$rkey"], &format!("{at}.$rkey")); } } Value::Object(o) if o.contains_key("$root") => { p.root(v, at); } Value::Object(o) if o.contains_key("$first") => match o.get("$first") { Some(Value::Array(a)) if o.len() == 1 && (2..=4).contains(&a.len()) => { for (i, item) in a.iter().enumerate() { record_value(p, item, &format!("{at}.$first[{i}]"), depth + 1, fill); } } _ => p.add( at, "{\"$first\": [values]} has the key $first alone, 2 to 4 values", ), }, Value::Object(o) if o.contains_key("$join") => match o.get("$join") { Some(Value::Array(a)) if o.len() == 1 && (2..=8).contains(&a.len()) => { for (i, item) in a.iter().enumerate() { record_value(p, item, &format!("{at}.$join[{i}]"), depth + 1, fill); } } _ => p.add( at, "{\"$join\": [values]} has the key $join alone, 2 to 8 values, each a string once read", ), }, Value::Object(o) if EDITS.iter().any(|e| o.contains_key(*e)) => { template_edit(p, o, at, depth, fill) } Value::Object(o) if fill.is_some_and(|mark| o.contains_key(mark)) => { let mark = fill.unwrap_or_default(); if o.len() != 1 || o[mark] != Value::Bool(true) { p.add( at, format_args!("{{\"{mark}\": true}} has the key {mark} alone, true"), ); } } Value::Object(_) => record_fields(p, v, at, depth, fill), Value::Array(items) => { for (i, item) in items.iter().enumerate() { record_value(p, item, &format!("{at}[{i}]"), depth + 1, fill); } } _ => {} } } /// The edits of an array a template makes (`ui::edit`). const EDITS: [&str; 3] = ["$with", "$without", "$move"]; /// An array's edit in a template: `{"$with": [array, key, element]}`, /// `{"$without": [array, key, value]}` or `{"$move": [array, key, value, /// by]}`, its key alone, `key` a path within each element, `by` a whole /// number but 0, the rest values. fn template_edit( p: &mut Problems, o: &Map, at: &str, depth: usize, fill: Option<&str>, ) { let name = EDITS .into_iter() .find(|e| o.contains_key(*e)) .unwrap_or_default(); let shape = match name { "$move" => "[array, key, value, by]", _ => "[array, key, value]", }; let arity = shape.split(',').count(); let args = o[name] .as_array() .filter(|a| o.len() == 1 && a.len() == arity); let Some(args) = args else { p.add( at, format_args!("{{\"{name}\": {shape}}} has the key {name} alone"), ); return; }; let aat = format!("{at}.{name}"); record_value(p, &args[0], &format!("{aat}[0]"), depth + 1, fill); p.path(&args[1], &format!("{aat}[1]")); record_value(p, &args[2], &format!("{aat}[2]"), depth + 1, fill); if args .get(3) .is_some_and(|by| !by.as_i64().is_some_and(|by| by != 0)) { p.add( &format!("{aat}[3]"), "must be a whole number but 0, the places it moves", ); } } /// A trusted view: a bundled one, or its operator's. Its id, the name of /// its file, and the file as a problem names it (`views/.json`, or the /// operator's file's path). #[derive(Clone, Debug)] pub struct Bundled { pub id: String, pub file: String, pub view: View, } /// The trusted views, in their ids' order, and the service each method /// view names, by its method: what a records view's query join calls when /// it names none; the procedures the operator's app files call to /// register a device for push (`operator::push_calls`); what each app /// file calls to have its server read the account's writes back at once /// (`operator::refresh_of`); the app files' own pages /// (`operator::pages_of`); the domains of the apps whose files draw /// their own sign-in screen (`signIn`); the permission sets their /// files name (`operator::sets_of`); and the lexicons the operator's apps /// ship beside their views (`operator::lexicons_of`). #[derive(Clone, Debug)] pub struct Views { views: Vec, services: Vec<(String, String)>, pushes: Vec, refreshes: Vec, pages: Vec, sign_ins: Vec, sets: Vec, lexicons: Vec>, } /// One of an app file's own pages (`pages`): its app's domain and name, /// its path on the app's site, its title and description, and its text, /// Markdown as written and as blocks (`ui::page_blocks`), its links to the /// app's own paths opening them on the app's site. #[derive(Clone, Debug, PartialEq)] pub struct Page { pub domain: String, pub app: String, pub path: String, pub title: String, pub description: Option, pub markdown: String, pub blocks: Vec, } /// An app file's `refresh`: the procedure of its own server that reads /// records the account just wrote back from the account's PDS, called with /// `{"uris": [...]}` after a write through HUD of one of `collections`, the /// app's own: those its views write. #[derive(Clone, Debug, PartialEq)] pub struct Refresh { pub method: String, pub service: String, pub collections: BTreeSet, } /// The service each method view of `docs` names, by its method, read /// from the documents as they are: what views are checked against before /// they are views. pub(crate) fn services_of<'a>(docs: impl IntoIterator) -> Vec<(String, String)> { docs.into_iter() .filter_map(|doc| { Some(( doc["method"].as_str()?.to_string(), doc["service"].as_str()?.to_string(), )) }) .collect() } /// The service `services` names for the query `method`, if any. fn service_in<'a>(services: &'a [(String, String)], method: &str) -> Option<&'a str> { services .iter() .find(|(m, _)| m == method) .map(|(_, s)| s.as_str()) } impl Views { /// The bundled views, checked: one with a problem, or drawing what /// another draws, is a bug in the build. pub fn built_in() -> Views { let docs: Vec = VIEWS .iter() .filter_map(|(_, text)| serde_json::from_str(text).ok()) .collect(); let services = services_of(&docs); let views: Vec = VIEWS .iter() .map(|&(id, text)| load(format!("views/{id}.json"), id, text, &services)) .map(|loaded| loaded.map(|(b, _)| b)) .collect::>() .expect("a bundled view is broken"); let wrong = conflicts(&views); assert!(wrong.is_empty(), "{}", wrong.join("; ")); Views::of(views) } /// `views` as the trusted views: in their ids' order, with the /// services their method views name, and no app's push calls. fn of(mut views: Vec) -> Views { views.sort_by(|a, b| a.id.cmp(&b.id)); let services = services_of(views.iter().map(|b| &b.view.doc)); Views { views, services, pushes: Vec::new(), refreshes: Vec::new(), pages: Vec::new(), sign_ins: Vec::new(), sets: Vec::new(), lexicons: Vec::new(), } } /// No views: what an instance serving the operator's apps alone adds /// them to (`operator::load`). pub fn none() -> Views { Views::of(Vec::new()) } /// These views with `added`: each replacing the view of its id, and any /// drawing what it draws. pub fn with(&self, added: Vec) -> Views { let mut views: Vec = self .views .iter() .filter(|b| { !added .iter() .any(|a| a.id == b.id || a.view.drawn() == b.view.drawn()) }) .cloned() .collect(); views.extend(added); Views { pushes: self.pushes.clone(), refreshes: self.refreshes.clone(), pages: self.pages.clone(), sign_ins: self.sign_ins.clone(), sets: self.sets.clone(), lexicons: self.lexicons.clone(), ..Views::of(views) } } /// These views with `pushes`, the procedures the operator's app files /// call to register and unregister a device for push. pub fn with_pushes(self, pushes: Vec) -> Views { Views { pushes, ..self } } /// The procedures the operator's app files call for push. pub fn pushes(&self) -> &[Call] { &self.pushes } /// These views with `refreshes`, what the operator's app files call /// after a write. pub fn with_refreshes(self, refreshes: Vec) -> Views { Views { refreshes, ..self } } /// What the operator's app files call after a write. pub fn refreshes(&self) -> &[Refresh] { &self.refreshes } /// These views with `pages`, the app files' own pages. pub fn with_pages(self, pages: Vec) -> Views { Views { pages, ..self } } /// The page of the app of `domain` at `path`, if its app file has one. pub fn page(&self, domain: &str, path: &str) -> Option<&Page> { self.pages .iter() .find(|p| p.domain == domain && p.path == path) } /// These views with `sign_ins`, the domains of the apps whose files /// draw their own sign-in screen. pub fn with_sign_ins(self, sign_ins: Vec) -> Views { Views { sign_ins, ..self } } /// The domains of the apps whose files draw their own sign-in screen /// (`signIn`): their web clients, at those domains, HUD sends straight /// on to the account's server (`oauth::issuer`). pub fn sign_ins(&self) -> &[String] { &self.sign_ins } /// These views with `sets`, the permission sets the operator's app /// files name. pub fn with_sets(self, sets: Vec) -> Views { Views { sets, ..self } } /// The permission sets the operator's app files name: what sign-in /// asks for first (`scope::including`). pub fn sets(&self) -> &[crate::scope::Include] { &self.sets } /// These views with `lexicons`, those the operator's apps ship. pub fn with_lexicons(self, lexicons: Vec>) -> Views { Views { lexicons, ..self } } /// The lexicon of `nsid` an operator's app ships, if one does: what /// HUD reads for it before its bundled one (`Hud::lexicon_doc`). pub fn lexicon(&self, nsid: &str) -> Option> { self.lexicons.iter().find(|d| d["id"] == nsid).cloned() } /// Every lexicon the views are checked against: those the operator's /// apps ship, and the bundled ones they don't replace. pub fn lexicon_docs(&self) -> Vec> { let bundled = crate::lexicon::all_bundled() .filter(|d| !self.lexicons.iter().any(|a| a["id"] == d["id"])) .cloned(); self.lexicons.iter().cloned().chain(bundled).collect() } /// Every view, in their ids' order. pub fn all(&self) -> &[Bundled] { &self.views } /// The service each method view names, by its method. pub(crate) fn services(&self) -> &[(String, String)] { &self.services } /// The service the view of the query `method` names, if any. pub fn service(&self, method: &str) -> Option<&str> { service_in(&self.services, method) } /// Whether a view, or an app file's push, calls the procedure `method` /// of `service`. pub fn calls(&self, method: &str, service: &str) -> bool { self.views .iter() .flat_map(|b| &b.view.calls) .chain(&self.pushes) .any(|c| !c.deletes && c.method == method && c.service == service) } /// Whether a view deletes what the query `method` of `service` plans. pub fn deletes(&self, method: &str, service: &str) -> bool { self.views .iter() .flat_map(|b| &b.view.calls) .any(|c| c.deletes && c.method == method && c.service == service) } /// What a view's `deletes` of the query `method` of `service` says /// beyond it, if a view deletes so: read as `call_procedure` deletes. pub fn plan(&self, method: &str, service: &str) -> Option<&Plan> { self.views .iter() .flat_map(|b| &b.view.plans) .find(|p| p.method == method && p.service == service) } /// Every collection the views' plans name (`Plan::collections`): what /// HUD asks to delete, where it asks to write none of it. pub fn plan_collections(&self) -> BTreeSet { let plans = self.views.iter().flat_map(|b| &b.view.plans); plans.flat_map(|p| p.collections.iter().cloned()).collect() } } /// The trusted views the server uses, set once: with the operator's, as /// the server adds them at startup (`install`), else the bundled ones, on /// first use. static IN_USE: OnceLock = OnceLock::new(); /// The trusted views in use: those the server serves. pub fn views() -> &'static Views { IN_USE.get_or_init(unset) } /// The trusted views where the server installs none: the bundled ones. #[cfg(not(test))] fn unset() -> Views { Views::built_in() } /// An operator's apps the tests serve: Grain's, as its repository has /// them, an app file and its views. #[cfg(test)] pub(crate) const GRAIN_APPS: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/src/testdata/grain/apps"); /// The trusted views where a test installs none: the bundled ones with an /// operator's, Grain's (`GRAIN_APPS`), so the tests hold an operator's /// views to all HUD does with them. #[cfg(test)] fn unset() -> Views { crate::operator::load(std::path::Path::new(GRAIN_APPS), true) .expect("Grain's apps, as an operator's") .views } /// Use `views` as the trusted views, before anything has read them: the /// server does, with its operator's (`operator::load`), as it starts. pub fn install(views: Views) -> Result<(), String> { IN_USE .set(views) .map_err(|_| "the trusted views were read before the operator's were added".to_string()) } /// What the views do as the account, read from their documents: what HUD /// asks an account's server for, as `scope::asked` builds it from the /// views an instance serves. impl Views { /// Every service they name, as `did:…#fragment`: their queries', /// joins', opens' and procedures', and the services the operator's app /// files register devices for push with and refresh writes at. pub fn called(&self) -> BTreeSet { let pushed = self.pushes.iter().map(|c| c.service.clone()); let refreshed = self.refreshes.iter().map(|r| r.service.clone()); let mut out: BTreeSet = pushed.chain(refreshed).collect(); self.each_object(&mut |o| { if let Some(s) = o.get("service").and_then(Value::as_str) && crate::xrpc::Service::parse(s).is_ok() { out.insert(s.to_string()); } }); out } /// Every method they call at `service`: each of their objects naming /// both (a method view, a query join, a procedure's call), and the /// operator's app files' push and refresh calls there. What a /// permission set must list for the views to call it as the account /// (`scope::ungranted`). pub fn methods_of(&self, service: &str) -> BTreeSet { let pushed = self.pushes.iter().map(|c| (&c.method, &c.service)); let refreshed = self.refreshes.iter().map(|r| (&r.method, &r.service)); let mut out: BTreeSet = pushed .chain(refreshed) .filter(|(_, s)| *s == service) .map(|(m, _)| m.clone()) .collect(); self.each_object(&mut |o| { if o.get("service").and_then(Value::as_str) == Some(service) && let Some(m) = o.get("method").and_then(Value::as_str) { out.insert(m.to_string()); } }); out } /// Every collection they write: an `Action`'s `create`, a `Compose`'s /// `writes`. pub fn written(&self) -> BTreeSet { self.written_by(&|_| true) } /// Every collection the views `of` keeps write, as `written` reads them. pub fn written_by(&self, of: &dyn Fn(&Bundled) -> bool) -> BTreeSet { let mut out = BTreeSet::new(); let views = self.views.iter().filter(|b| of(b)); views.for_each(|b| { walk(&b.view.doc, &mut |o| { let props = o.get("props").unwrap_or(&Value::Null); let written: Vec<&Value> = match o.get("component").and_then(Value::as_str) { Some("Action") => vec![&props["create"]["collection"]], Some("Compose") => props["writes"] .as_array() .into_iter() .flatten() .map(|w| &w["collection"]) .collect(), _ => vec![], }; out.extend( written .into_iter() .filter_map(Value::as_str) .map(str::to_string), ); }) }); out } /// Whether a composer of theirs uploads pictures: a `Compose` with a /// `photos` field, or a `card` one, whose link's picture it uploads. pub fn uploads(&self) -> bool { let mut out = false; self.each_object(&mut |o| { let fields = o.get("props").and_then(|p| p.get("fields")); out |= o.get("component").and_then(Value::as_str) == Some("Compose") && fields .and_then(Value::as_array) .into_iter() .flatten() .any(|f| matches!(f["kind"].as_str(), Some("photos" | "card"))); }); out } /// Whether one draws a query that takes feeds by the names a person /// gives them (`feeds::names_feeds`), read from their preferences. pub fn names_feeds(&self) -> bool { self.views.iter().any(|b| { b.view .method .as_deref() .is_some_and(|m| crate::feeds::NAMING.contains(&m)) }) } /// Whether one reports feed interactions (`View::interactions`). pub fn sends_interactions(&self) -> bool { self.views.iter().any(|b| b.view.interactions.is_some()) } /// Call `f` on every object of every view's document. fn each_object(&self, f: &mut impl FnMut(&Map)) { self.views.iter().for_each(|b| walk(&b.view.doc, f)); } } /// Call `f` on every object within `v`, `v` too. fn walk(v: &Value, f: &mut impl FnMut(&Map)) { match v { Value::Object(o) => { f(o); o.values().for_each(|x| walk(x, f)); } Value::Array(a) => a.iter().for_each(|x| walk(x, f)), _ => {} } } /// Whether a trusted view, or an operator's app file registering a device /// for push, calls the procedure `method` of `service`: the only /// procedures `call_procedure` sends. pub fn bundled_call(method: &str, service: &str) -> bool { views().calls(method, service) } /// Whether a trusted view deletes what the query `method` of `service` /// plans (`deletes`): the only plans `call_procedure` deletes. pub fn bundled_deletes(method: &str, service: &str) -> bool { views().deletes(method, service) } /// What is wrong with a call against its procedure's lexicon (`docs`, its /// own among them): a method that is no procedure, or takes no JSON, and /// top-level input fields it does not take or requires and the template /// leaves out. What the template fills in is checked when it is sent. A /// deletion plan's query (`deletes`), as `deletes_problems` checks it. pub fn call_problems(call: &Call, docs: &[std::sync::Arc]) -> Vec { let method = call.method.as_str(); let Some(doc) = docs.iter().find(|d| d["id"] == method) else { return vec![format!("{method}: no lexicon")]; }; if call.deletes { return deletes_problems(call, doc); } let def = match crate::procedures::procedure_def(method, doc) { Ok(def) => def, Err(why) => return vec![why], }; let schema = &def["input"]["schema"]; let schema = match schema["type"].as_str() { Some("ref") => schema["ref"] .as_str() .map(|r| crate::lexicon::full_ref(method, r)) .and_then(|full| { let (nsid, name) = full.split_once('#')?; docs.iter() .find(|d| d["id"] == nsid) .map(|d| d["defs"][name].clone()) }) .unwrap_or(Value::Null), _ => schema.clone(), }; template_problems(method, &call.input, &schema, ("input", "an input")) } /// What is wrong with a deletion plan's query (`deletes`) against its /// lexicon `doc`: a method that is no query, one answering no `writes` /// array (the deletes of one `com.atproto.repo.applyWrites`), and params /// the template gives that it does not take or leaves out that it /// requires. fn deletes_problems(call: &Call, doc: &Value) -> Vec { let method = call.method.as_str(); let def = match crate::xrpc::query_def(method, doc) { Ok(def) => def, Err(why) => return vec![why], }; let params = &def["parameters"]; let mut out = template_problems(method, &call.input, params, ("params", "a param")); if def["output"]["schema"]["properties"]["writes"]["type"] != "array" { out.push(format!( "{method}: answers no writes, the deletes of one com.atproto.repo.applyWrites" )); } out } /// The top-level fields of a template `given` that `schema` (an object's /// or params') does not take, and those it requires that `given` leaves /// out, each named as `what`'s: its name and one of it. fn template_problems( method: &str, given: &Value, schema: &Value, (what, one): (&str, &str), ) -> Vec { let given = given.as_object(); let taken = schema["properties"].as_object(); let mut out: Vec = given .into_iter() .flatten() .filter(|(k, _)| *k != "$type" && taken.is_none_or(|t| !t.contains_key(*k))) .map(|(k, _)| format!("{method}: {what}.{k} is not {one} of it")) .collect(); for k in schema["required"] .as_array() .into_iter() .flatten() .filter_map(Value::as_str) { if !given.is_some_and(|g| g.contains_key(k)) { out.push(format!("{method}: {what}.{k} is required")); } } out } /// A trusted view from its file's `text`, its query joins calling by /// default the services `services` names, and a warning for each of its /// optional keys left out; or every problem with it, each naming its /// `file`. A key written `?key` is optional: read as `key`, and left out /// where HUD finds a problem in it (a prop or a value this HUD doesn't /// know), so a view written for a newer HUD still loads on an older one. pub(crate) fn load( file: String, id: &str, text: &str, services: &[(String, String)], ) -> Result<(Bundled, Vec), Vec> { let doc: Value = serde_json::from_str(text).map_err(|e| vec![format!("{file}: {e}")])?; let mut left_out = BTreeSet::new(); let mut warnings = Vec::new(); loop { let (mut optional, mut problems) = (Vec::new(), Vec::new()); let unmarked = unmark(&doc, "", &left_out, &mut optional, &mut problems); let problems = match problems.is_empty() { true => match parse_in(&unmarked, true, services) { Ok(view) => { let id = id.to_string(); return Ok((Bundled { id, file, view }, warnings)); } Err(problems) => problems, }, false => problems, }; let before = left_out.len(); for problem in &problems { if let Some(key) = optional.iter().find(|k| k.holds(problem)) && left_out.insert(key.at.clone()) { warnings.push(format!( "{file}: {problem} (?{} is optional, so it's left out)", key.key )); } } if left_out.len() == before { return Err(problems.iter().map(|p| format!("{file}: {p}")).collect()); } } } /// An optional key of a view (`?key`): its path and its object's, as the /// view's problems name them less the leading `view.`, and its name. struct Optional { at: String, parent: String, key: String, } impl Optional { /// Whether `problem` lies in this key: at it or within it, or naming it /// as its object's unknown key. fn holds(&self, problem: &str) -> bool { let (at, what) = problem.split_once(": ").unwrap_or((problem, "")); let at = match at { "view" => "", _ => at.strip_prefix("view.").unwrap_or(at), }; let within = at .strip_prefix(self.at.as_str()) .is_some_and(|rest| rest.is_empty() || rest.starts_with(['.', '['])); within || (at == self.parent && what == format!("unknown key \"{}\"", self.key)) } } /// `v`, at `at`, with each key written `?key` read as `key`, those whose /// path is in `left_out` left out; each such key added to `optional`, and /// to `problems` one given both ways. fn unmark( v: &Value, at: &str, left_out: &BTreeSet, optional: &mut Vec, problems: &mut Vec, ) -> Value { match v { Value::Object(o) => { let mut out = Map::new(); for (k, x) in o { let key = k.strip_prefix('?').unwrap_or(k); let kat = match at { "" => key.to_string(), _ => format!("{at}.{key}"), }; if key != k { if o.contains_key(key) { let parent = if at.is_empty() { "view" } else { at }; problems.push(format!("{parent}: \"{k}\" and \"{key}\" both given")); } optional.push(Optional { at: kat.clone(), parent: at.to_string(), key: key.to_string(), }); if left_out.contains(&kat) { continue; } } out.insert( key.to_string(), unmark(x, &kat, left_out, optional, problems), ); } Value::Object(out) } Value::Array(a) => Value::Array( a.iter() .enumerate() .map(|(i, x)| unmark(x, &format!("{at}[{i}]"), left_out, optional, problems)) .collect(), ), _ => v.clone(), } } /// What is wrong among trusted views, each named by its file: one drawing /// what another draws, a collection a method view draws as its query /// (`records`) that another names too or a collection view draws, and a /// plan of a query that another plan of it says otherwise. pub fn conflicts(views: &[Bundled]) -> Vec { let mut out = Vec::new(); for (i, b) in views.iter().enumerate() { let drawn = b.view.drawn(); if let Some(o) = views[..i].iter().find(|o| o.view.drawn() == drawn) { out.push(format!("{}: {} draws {drawn} too", b.file, o.file)); } for c in b.view.records.iter().map(|r| r.collection.as_str()) { let routes = |o: &&Bundled| o.view.records.iter().any(|r| r.collection == c); if let Some(o) = views[..i].iter().find(routes) { out.push(format!("{}: {} draws {c} as its query too", b.file, o.file)); } if let Some(o) = views .iter() .find(|o| o.view.collection.as_deref() == Some(c)) { out.push(format!("{}: {} draws {c} as records", b.file, o.file)); } } for (k, plan) in b.view.plans.iter().enumerate() { let same = |p: &&Plan| p.method == plan.method && p.service == plan.service; let before = views[..i].iter().flat_map(|o| &o.view.plans); if let Some(o) = before.chain(&b.view.plans[..k]).find(same) && o != plan { let method = &plan.method; out.push(format!( "{}: deletes {method} otherwise than before", b.file )); } } } out } /// What is wrong with a trusted view beyond its document, against the /// lexicons `docs` and the other trusted `views`: a collection view that /// does not draw both a page of records and one; and its calls, its query /// joins (an app page's page after too) and their services, and a method /// view's profile section. Every bundled view passes (the tests hold /// them), and an operator's must to be used (`operator`). pub fn trusted_problems(v: &View, docs: &[Arc], views: &Views) -> Vec { let mut out = Vec::new(); if v.collection.is_some() && (v.list.is_none() || v.record.is_none()) { out.push( "view: a trusted collection view draws a page of records (list) and one (record)" .into(), ); } for c in &v.calls { out.extend(call_problems(c, docs)); } let more = v.page.as_ref().and_then(|p| p.more("c")); for j in v.query_joins().chain(more.as_ref()) { out.extend(query_join_problems(j, docs)); if j.service_among(v.service.as_deref(), views).is_none() { out.push(format!( "{}.service: no view of {} names its service", j.at, j.method )); } } if let (Some(s), Some(method)) = (&v.profile, &v.method) { out.extend(section_problems(s, method, docs)); } out } /// What is wrong with a method view's profile section: its params, as an /// account's, against the lexicon of the view's `method` among `docs`. fn section_problems(s: &Section, method: &str, docs: &[Arc]) -> Vec { let Some(doc) = docs.iter().find(|d| d["id"] == method) else { return vec![format!("view.profile: no lexicon for {method}")]; }; let def = match crate::xrpc::query_def(method, doc) { Ok(def) => def, Err(why) => return vec![format!("view.profile: {why}")], }; let lexicons = crate::xrpc::lexicons_for(method, docs); let params = s.params_for("did:plc:abcdefghijklmnopqrstuvwx"); crate::xrpc::params_problems(&lexicons, method, def, ¶ms) .into_iter() .map(|p| format!("view.profile: {p}")) .collect() } impl View { /// What the view draws: its collection, its method, or its app's /// domain. pub fn drawn(&self) -> &str { self.collection .as_deref() .or(self.method.as_deref()) .or(self.domain.as_deref()) .unwrap_or_default() } /// Every query join of its modes. pub fn query_joins(&self) -> impl Iterator { let modes = [&self.list, &self.record].into_iter().flatten(); modes .flat_map(|m| &m.queries) .chain(self.query.iter().flat_map(|q| &q.queries)) .chain(self.page.iter().flat_map(|p| &p.queries)) } } /// The trusted views, in their ids' order: the bundled ones, as the /// operator's replace and add to them. pub fn bundled() -> &'static [Bundled] { views().all() } /// The bundled view of `collection`, if any. pub fn for_collection(collection: &str) -> Option<&'static View> { bundled() .iter() .map(|b| &b.view) .find(|v| v.collection.as_deref() == Some(collection)) } /// The bundled method view that draws a record of `collection` as its /// query, and its `records` entry for it, if any. pub fn route(collection: &str) -> Option<(&'static View, &'static Routed)> { bundled().iter().find_map(|b| { let r = b.view.records.iter().find(|r| r.collection == collection)?; Some((&b.view, r)) }) } /// The `query` tool's arguments a record at `uri` opens as, where a bundled /// view draws its collection and its params need only the at-uri and its /// DID (a post's thread, a gallery): the call to make in place of /// `get_record`, so a record opened from anywhere is the same tile as its /// query opened from a list. None where the params read the record itself. pub fn routed_call(uri: &str) -> Option { let mut parts = uri.strip_prefix("at://")?.split('/'); let (authority, collection) = (parts.next()?, parts.next()?); let (v, routed) = route(collection)?; if routed.reads_record() { return None; } let rec = serde_json::json!({ "uri": uri, "did": authority }); routed.args_for(v.method.as_deref()?, &rec) } /// What making a record of `collection` is, by the bundled view naming it: /// a collection view's `activity` verb and its list's summary, or a method /// view's `records` entry's activity. pub fn activity(collection: &str) -> Option<(&'static str, &'static Summary)> { match for_collection(collection) { Some(v) => Some((v.activity.as_deref()?, v.list.as_ref()?.summary.as_ref()?)), None => { let a = route(collection)?.1.activity.as_ref()?; Some((&a.verb, &a.summary)) } } } /// The bundled view of the query `method`, if any. pub fn for_method(method: &str) -> Option<&'static View> { bundled() .iter() .map(|b| &b.view) .find(|v| v.method.as_deref() == Some(method)) } /// The title a trusted view's launcher gives the call of `method` with /// `params` (Bluesky's "Discover"), if one does: the same /// params, a launcher's `$me` standing for any account (a title says what, /// the header whose: "Galleries" for anyone's). pub fn launch_title(method: &str, params: &Value) -> Option { let empty = Map::new(); let params = params.as_object().unwrap_or(&empty); for_method(method)? .launch .iter() .find(|l| { l.params.len() == params.len() && l.params.iter().all(|(k, v)| match params.get(k) { Some(given) if me(v).is_some() => given.is_string(), Some(given) => given == v, None => false, }) }) .map(|l| l.title.clone()) } /// The bundled app page view of the app at `domain`, if any. pub fn for_domain(domain: &str) -> Option<&'static View> { bundled() .iter() .map(|b| &b.view) .find(|v| v.page.is_some() && v.domain.as_deref() == Some(domain)) } /// The origins the view loads media from, for its CSP's `resourceDomains`: /// the server's own (its blob proxy) and every bundled view's, sorted, each /// once. A `Map`'s tiles are fetched, so its origin is a `connectDomains`' /// (`catalog::MAP_TILES`). pub fn csp_origins(public_origin: &str) -> Vec { origins_of(public_origin, bundled().iter().map(|b| &b.view)) } fn origins_of<'a>(public_origin: &str, views: impl Iterator) -> Vec { let mut all: Vec = std::iter::once(public_origin.to_string()) .chain(views.flat_map(|v| v.origins.iter().cloned())) .collect(); all.sort(); all.dedup(); all } #[cfg(test)] mod tests { use super::*; use serde_json::json; fn problems(doc: &Value, trusted: bool) -> Vec { parse(doc, trusted).err().unwrap_or_default() } /// The problems of an inline view, by where they are. fn wheres(doc: &Value) -> Vec { problems(doc, false) .iter() .map(|p| p.split_once(": ").unwrap().0.to_string()) .collect() } fn posts(layout: Value) -> Value { json!({ "collection": "app.bsky.feed.post", "description": "Posts", "list": { "layout": layout } }) } /// A trusted view's inline copy says nothing at its top that only a /// trusted view may: what is left is in its layout, procedures called, /// plans committed, interactions sent and what the host is asked to do. #[test] fn bundled_views_copied_to_draw_inline() { for b in bundled().iter().filter(|b| b.view.page.is_none()) { let copy = inline_copy(&b.view.doc); for p in problems(©, false) { assert!( p.ends_with("only a bundled view calls procedures") || p.ends_with("only a bundled view deletes what a query plans") || p.ends_with("only a bundled view sends interactions") || p.ends_with("only a bundled view asks the host"), "{}: {p}", b.id ); } } assert_eq!(inline_copy(&json!(1)), json!(1)); } /// Every trusted view the tests use, the bundled ones and Grain's as an /// operator's, parses, draws one thing, and passes what an operator's /// view is held to as HUD starts (`trusted_problems`). #[test] fn every_bundled_view_validates() { assert_eq!(Views::built_in().all().len(), VIEWS.len()); let docs = lexicon_docs(); for b in bundled() { let v = &b.view; let file = if VIEWS.iter().any(|(id, _)| *id == b.id) { format!("views/{}.json", b.id) } else { format!("{GRAIN_APPS}/views/{}.json", b.id) }; assert_eq!(b.file, file); assert_eq!( trusted_problems(v, &docs, views()), Vec::::new(), "{}", b.id ); // Each draws one thing. match (&v.collection, &v.method, &v.domain) { (Some(c), None, None) => { assert_eq!(for_collection(c), Some(v), "{}", b.id); } (None, Some(m), None) => { assert_eq!(for_method(m), Some(v), "{}", b.id); assert!(v.query.is_some() && v.service.is_some(), "{}", b.id); } (c, m, d) => { assert_eq!((c, m), (&None, &None), "{}", b.id); assert_eq!(for_domain(d.as_deref().unwrap()), Some(v), "{}", b.id); assert!(v.page.is_some() && v.service.is_some(), "{}", b.id); } } } // The feeds, from the AppView, their images from its CDN and their // video posters from its video host. for m in [ "app.bsky.feed.getFeed", "app.bsky.feed.getAuthorFeed", "app.bsky.feed.getTimeline", ] { let v = for_method(m).expect("a view of each feed"); assert_eq!( v.service.as_deref(), Some("did:web:api.bsky.app#bsky_appview") ); // Video posters redirect from video.bsky.app to its CDN, and a // CSP holds the redirect's target too. assert_eq!( v.origins, [ "https://cdn.bsky.app", "https://video.bsky.app", "https://video.cdn.bsky.app" ] ); assert_eq!(v.query.as_ref().unwrap().items, Path::parse("feed")); // Each card says first why it is in the feed and what a reply // replies to (FeedContext, above it and under its author), then // the post beside its author's face (a Row of an Image and a // Stack), drawing its embed, whatever it is, as Bluesky does. let card = &v.doc["query"]["layout"]["children"]; fn nodes(n: &Value, out: &mut Vec) { out.push(n.clone()); for c in n["children"].as_array().into_iter().flatten() { nodes(c, out); } } let post = &card[1]; let mut all = Vec::new(); nodes(post, &mut all); assert_eq!(card[0]["component"], "FeedContext", "{m}"); assert_eq!(card[0]["props"]["item"], json!({ "$": "." }), "{m}"); assert_eq!(post["children"][0]["props"]["size"], "face", "{m}"); assert!( all.iter() .any(|n| n["component"] == "FeedContext" && n["props"]["part"] == "replyTo"), "{m}" ); let embed = all.iter().find(|n| n["component"] == "Embed").unwrap(); assert_eq!( embed["props"], json!({ "value": { "$": "post.embed" }, "did": { "$": "post.author.did" }, "uri": { "$": "post.uri" } }), "{m}" ); assert!(!card.to_string().contains("\"Gallery\""), "{m}"); } assert!(for_collection("fm.plyr.list").is_some()); assert!(for_collection("fm.plyr.like").is_some()); // A collection a method view draws as its query is named by one // view alone, and drawn by no collection view. let mut routed: Vec<&str> = Vec::new(); for b in bundled() { for r in &b.view.records { let c = r.collection.as_str(); assert!(!routed.contains(&c), "{}: {c} is routed twice", b.id); assert!(for_collection(c).is_none(), "{}: {c} has a view", b.id); routed.push(c); } } routed.sort_unstable(); assert_eq!( routed, [ "app.bsky.feed.post", "app.bsky.feed.repost", "dev.knotless.check", "dev.knotless.comment", "dev.knotless.issue", "dev.knotless.profile", "dev.knotless.pull", "dev.knotless.repo", "dev.knotless.star", "social.grain.gallery", "social.grain.story", ] ); let (thread, post) = route("app.bsky.feed.post").expect("posts open as threads"); assert_eq!( thread.method.as_deref(), Some("app.bsky.feed.getPostThread") ); assert!(!post.reads_record()); assert!(route("app.bsky.feed.repost").unwrap().1.reads_record()); assert!(route("dev.knotless.release").is_none()); // standard.site's posts and blogs, the post's body drawn by Document. let doc = for_collection("site.standard.document").expect("the document view"); // Its Leaflet blocks embed Bluesky posts, their images and video // posters from the AppView's hosts. assert_eq!( doc.origins, [ "https://cdn.bsky.app", "https://video.bsky.app", "https://video.cdn.bsky.app" ] ); let body = &doc.doc["record"]["layout"]["children"][2]; assert_eq!(body["component"], "Document"); let blog = for_collection("site.standard.publication").expect("the publication view"); assert!(matches!( &blog.record.as_ref().unwrap().join[0].source, Source::Backlinks { collection, .. } if collection == "site.standard.document" )); } #[test] fn actions_checked() { let like = json!({ "component": "Action", "props": { "label": "Like", "icon": "heart", "done": "Liked", "create": { "collection": "app.bsky.feed.like", "record": { "subject": { "$strongRef": "." }, "createdAt": { "$now": true }, "via": { "$": "uri" }, "tags": ["a", { "$": "value.text" }], "nested": { "$type": "app.bsky.embed.record", "record": { "$strongRef": "post" } } } } } }); assert_eq!(problems(&posts(like.clone()), false), Vec::::new()); let unlike = json!({ "component": "Action", "props": { "label": "Unlike", "icon": "trash", "delete": { "uri": { "$": "uri" } } } }); assert_eq!(problems(&posts(unlike), false), Vec::::new()); let action = |props: Value| posts(json!({ "component": "Action", "props": props })); let create = |record: Value| { action(json!({ "label": "Go", "create": { "collection": "app.bsky.feed.like", "record": record } })) }; let at = "list.layout.props.create.record"; for (doc, want) in [ // Exactly one of create and delete. (action(json!({ "label": "Go" })), vec!["list.layout.props"]), ( action(json!({ "label": "Go", "delete": { "uri": { "$": "uri" } }, "create": { "collection": "app.bsky.feed.like", "record": {} } })), vec!["list.layout.props"], ), // The label, the collection and delete's uri as they must be. ( action(json!({ "label": { "$": "uri" }, "delete": { "uri": { "$": "uri" } } })), vec!["list.layout.props.label"], ), ( action(json!({ "label": "Go", "delete": { "uri": "at://did:plc:x/a.b.c/1" } })), vec!["list.layout.props.delete.uri"], ), ( action( json!({ "label": "Go", "icon": "bomb", "delete": { "uri": { "$": "uri" } } }), ), vec!["list.layout.props.icon"], ), ( action(json!({ "label": "Go", "create": { "collection": { "$": "c" }, "record": {} } })), vec!["list.layout.props.create.collection"], ), ( action(json!({ "label": "Go", "create": { "collection": "app.bsky.feed.like", "record": { "$": "value" } } })), vec![at], ), // The record's special values, each alone and well formed. ( create(json!({ "t": { "$now": false } })), vec!["list.layout.props.create.record.t"], ), ( create(json!({ "s": { "$strongRef": "a b" } })), vec!["list.layout.props.create.record.s.$strongRef"], ), ( create(json!({ "s": { "$strongRef": ".", "x": 1 } })), vec!["list.layout.props.create.record.s"], ), ( create(json!({ "s": { "$rkey": "a b" } })), vec!["list.layout.props.create.record.s.$rkey"], ), ( create(json!({ "s": { "$rkey": "uri", "x": 1 } })), vec!["list.layout.props.create.record.s"], ), ( create(json!({ "$link": "x" })), vec!["list.layout.props.create.record.$link"], ), ( create(json!({ "a": [{ "b": { "$": "no spaces" } }] })), vec!["list.layout.props.create.record.a[0].b.$"], ), ( create(json!({ "big": "x".repeat(5000) })), vec!["list.layout.props.create.record"], ), ] { assert_eq!(wheres(&doc), want, "{doc}"); } // The bundled thread and feeds like and repost each card. for (v, mode, strong) in [ ( for_method("app.bsky.feed.getPostThread").unwrap(), "query", "post", ), ( for_method("app.bsky.feed.getFeed").unwrap(), "query", "post", ), ( for_method("app.bsky.feed.getAuthorFeed").unwrap(), "query", "post", ), ] { let text = v.doc[mode].to_string(); for c in ["app.bsky.feed.like", "app.bsky.feed.repost"] { assert!( text.contains(&format!("\"collection\":\"{c}\"")), "{mode} {c}" ); } assert!( text.contains(&format!("{{\"$strongRef\":\"{strong}\"}}")), "{mode}" ); } } #[test] fn document_props() { let doc = |props: Value| { json!({ "collection": "site.standard.document", "description": "Posts", "record": { "layout": { "component": "Document", "props": props } } }) }; assert!(parse(&doc(json!({ "value": { "$": "value.content" } })), false).is_ok()); assert!( parse( &doc(json!({ "value": { "$": "value.content" }, "fallback": { "$": "value.textContent" } })), false ) .is_ok() ); let found = |props: Value| problems(&doc(props), false); assert_eq!( found(json!({ "fallback": { "$": "value.textContent" } })), ["record.layout.props.value: Document requires it"] ); assert_eq!( found(json!({ "value": { "$type": "pub.leaflet.content", "pages": [] } })), ["record.layout.props.value: must be a binding {\"$\": path}"] ); assert_eq!( found(json!({ "value": { "$": "value.content" }, "fallback": "no text" })), ["record.layout.props.fallback: must be a binding {\"$\": path}"] ); assert_eq!( found(json!({ "value": { "$": "value.content" }, "markdown": true })), ["record.layout.props.markdown: Document has no prop \"markdown\""] ); let mut with_children = doc(json!({ "value": { "$": "value.content" } })); with_children["record"]["layout"]["children"] = json!([]); assert_eq!( problems(&with_children, false), ["record.layout.children: Document takes no children"] ); } #[test] fn an_inline_view() { let doc = posts( json!({ "component": "List", "props": { "items": { "$": "records" }, "style": "cards" }, "children": [ { "component": "Text", "props": { "value": { "$": "value.text" } } }, { "component": "Time", "props": { "value": { "$": ["value.createdAt", "value.indexedAt"] } } }, ] }), ); let v = parse(&doc, false).unwrap(); assert_eq!(v.collection.as_deref(), Some("app.bsky.feed.post")); assert!(v.record.is_none() && v.origins.is_empty()); } #[test] fn components_and_props() { let doc = posts( json!({ "component": "Stack", "props": { "gap": { "$": "value.gap" } }, "children": [ { "component": "Marquee", "props": {} }, { "component": "Text", "props": { "value": "hi", "color": "red" } }, { "component": "Text", "props": {} }, { "component": "Text", "props": { "value": { "$": "value..text" } } }, { "component": "Text", "props": { "value": { "$": "value.text", "x": 1 } } }, { "component": "Text", "props": { "value": "a", "style": "loud" } }, { "component": "Image", "props": { "src": { "$": "a-b" } } }, { "component": "Badge", "props": { "value": "a" }, "children": [] }, { "component": "List", "props": { "items": [1, 2] } }, { "component": "Header", "props": { "meta": ["a", { "$": "handle" }, 3] } }, { "component": "Covers", "props": { "items": { "$": "records" }, "src": { "$": "value.a" }, "countNoun": { "$": "x" } } }, ] }), ); assert_eq!( wheres(&doc), [ "list.layout.props.gap", "list.layout.children[0].component", "list.layout.children[1].props.color", "list.layout.children[2].props.value", "list.layout.children[3].props.value.$", "list.layout.children[4].props.value", "list.layout.children[5].props.style", "list.layout.children[6].props.src.$", "list.layout.children[7].children", "list.layout.children[8].props.items", "list.layout.children[9].props.meta[2]", "list.layout.children[10].props.countNoun", ] ); let text = error_text(&problems(&doc, false)); assert!(text.starts_with("Invalid view: 12 problems: list.layout.props.gap: must be a literal value, not a binding; "), "{text}"); // Past 20 problems, how many more. let many: Vec = (0..25) .map(|i| format!("view: unknown key \"k{i}\"")) .collect(); let text = error_text(&many); assert!( text.starts_with("Invalid view: 25 problems: view: unknown key \"k0\"; "), "{text}" ); assert!( text.ends_with("view: unknown key \"k19\"; and 5 more"), "{text}" ); let text = error_text(&problems(&doc, false)); assert!( text.contains( "list.layout.children[3].props.value.$: unknown path syntax \"value..text\"" ), "{text}" ); assert!(text.contains("unknown component \"Marquee\""), "{text}"); } /// A Stack's, Pane's or Bar's when and unless is a path or a gate on /// the kit, `{"$kit": semver requirement}`; any node's fallback is a /// node, checked as one. #[test] fn gates_on_the_kit_and_fallbacks_are_checked() { let text = json!({ "component": "Text", "props": { "value": "x" } }); let gated = |key: &str, gate: Value| json!({ "component": "Stack", "props": { key: gate }, "children": [text.clone()] }); let good = posts(json!({ "component": "Stack", "children": [ gated("when", json!({ "$kit": ">=0.3.0" })), gated("unless", json!({ "$kit": ">=0.3.0, <1" })), gated("when", json!("record.value.text")), { "component": "Pane", "props": { "label": "A", "when": { "$kit": "^0.3" } }, "children": [text.clone()] }, { "component": "Text", "props": { "value": "x" }, "fallback": text.clone() }, ] })); assert_eq!(problems(&good, false), Vec::::new()); let bad = posts(json!({ "component": "Stack", "children": [ gated("when", json!({ "$kit": "soon" })), gated("unless", json!({ "$kit": ">=0.3.0", "or": 1 })), gated("when", json!("a..b")), { "component": "Text", "props": { "value": "x" }, "fallback": { "component": "Marquee" } }, { "component": "Text", "props": { "value": "x" }, "fallback": { "component": "Text", "props": { "value": "x" }, "extra": 1 } }, ] })); assert_eq!( wheres(&bad), [ "list.layout.children[0].props.when", "list.layout.children[1].props.unless", "list.layout.children[2].props.when", "list.layout.children[3].fallback.component", "list.layout.children[4].fallback", ] ); assert!( problems(&bad, false)[0].ends_with("must be a path, keys joined by dots, or {\"$kit\": a semver requirement, as \">=0.3.0\"}"), "{:?}", problems(&bad, false) ); } #[test] fn tabs_icons_and_literal_items() { let issues = |state: &str| json!({ "query": "com.example.listIssues", "params": { "state": state, "repo": { "$root": "did" } } }); let good = posts(json!({ "component": "Stack", "children": [ { "component": "Tabs", "props": { "items": [ { "label": "Open", "open": issues("open") }, { "label": "Closed", "open": issues("closed") }, ] } }, { "component": "List", "props": { "items": [ { "label": "Issues", "icon": "issue", "tint": "green", "count": { "$root": "records" }, "more": { "$root": "cursor" }, "deeper": [{ "x": { "$root": "did" } }], "open": issues("open") }, { "label": "Stars", "icon": "star", "tint": "yellow", "$type": "a.b.c" }, ] }, "children": [{ "component": "Row", "children": [ { "component": "Icon", "props": { "name": { "$": "icon" }, "tint": { "$": "tint" } } }, { "component": "Icon", "props": { "name": "pull", "tint": "purple" } }, { "component": "Text", "props": { "value": { "$": "label" } } }, { "component": "Stat", "props": { "value": { "$": "count" }, "more": { "$": "more" } } }, ] }] }, ] })); assert_eq!(problems(&good, false), Vec::::new()); let many: Vec = (0..21).map(|i| json!({ "n": i })).collect(); let bad = posts(json!({ "component": "Stack", "children": [ { "component": "Tabs", "props": { "items": [{ "label": "One", "open": issues("open") }] } }, { "component": "Tabs", "props": { "items": [ { "label": "No open" }, { "label": "", "open": issues("closed") }, { "label": "Bad", "open": { "query": "no" }, "extra": 1 }, { "label": "Nothing", "open": false }, ] } }, { "component": "Tabs", "props": { "items": { "$": "tabs" } } }, { "component": "List", "props": { "items": many } }, { "component": "List", "props": { "items": [ { "a": { "$": "x" } }, { "b": [{ "c": { "$root": "a b" } }] }, { "open": { "query": "no" } }, "row", ] } }, { "component": "Icon", "props": { "name": "rocket", "tint": "teal" } }, { "component": "Stat", "props": { "value": 1, "more": "yes" } }, ] })); assert_eq!( wheres(&bad), [ "list.layout.children[0].props.items", "list.layout.children[1].props.items[0].open", "list.layout.children[1].props.items[1].label", "list.layout.children[1].props.items[2]", "list.layout.children[1].props.items[2].open.query", "list.layout.children[1].props.items[3].open", "list.layout.children[2].props.items", "list.layout.children[3].props.items", "list.layout.children[4].props.items", "list.layout.children[5].props.name", "list.layout.children[5].props.tint", "list.layout.children[6].props.more", ] ); let rows = posts(json!({ "component": "List", "props": { "items": [ { "a": { "$": "x" } }, { "b": [{ "c": { "$root": "a b" } }] }, { "open": { "query": "no" } }, ] } })); assert_eq!( wheres(&rows), [ "list.layout.props.items[0].a", "list.layout.props.items[1].b[0].c.$root", "list.layout.props.items[2].open.query", ] ); } #[test] fn documents() { // Unknown keys, a missing collection and modes, a bad type, an // origin no bundled view names. let doc = json!({ "$type": "x.y.z", "description": "d", "extra": 1, "origins": ["https://a.example"] }); assert_eq!( wheres(&doc), ["view", "view.$type", "view", "view.origins[0]", "view"] ); // Trusted, origins are checked and kept. let mut trusted = posts(json!({ "component": "Badge", "props": { "value": "x" } })); trusted["origins"] = json!(["https://a.example", "https://b.example:8443/"]); assert_eq!( parse(&trusted, true).unwrap().origins, ["https://a.example", "https://b.example:8443"] ); for bad in [ "http://a.example", "https://a.example/path", "https://u@a.example", "https://A.example", ] { trusted["origins"] = json!([bad]); assert_eq!(problems(&trusted, true).len(), 1, "{bad}"); } // Inline, only the origins a bundled view names: those load where // any view is drawn already. trusted["origins"] = json!(["https://a.example"]); let refused = problems(&trusted, false); assert_eq!(refused.len(), 1); assert!( refused[0].starts_with( "view.origins[0]: a view given inline names only origins a bundled view names: " ) && refused[0].contains("https://cdn.bsky.app"), "{refused:?}" ); // Thumbnails: a trusted view's, on its origins. trusted["thumbnails"] = json!([{ "from": "https://a.example/big/", "to": "https://a.example/small/" }]); assert!(problems(&trusted, true).is_empty()); for bad in [ json!([{ "from": "https://b.example/big/", "to": "https://a.example/small/" }]), json!([{ "from": "https://a.example/big", "to": "https://a.example/small/" }]), json!([{ "from": "https://a.example/big/" }]), json!([]), ] { trusted["thumbnails"] = bad.clone(); assert_eq!(problems(&trusted, true).len(), 1, "{bad}"); } // Inline, thumbnails on origins a bundled view names are kept. let mut inline = trusted.clone(); inline["origins"] = json!(["https://cdn.bsky.app"]); inline["thumbnails"] = json!([{ "from": "https://cdn.bsky.app/img/avatar/plain/", "to": "https://cdn.bsky.app/img/avatar_thumbnail/plain/" }]); assert_eq!( parse(&inline, false).unwrap().origins, ["https://cdn.bsky.app"] ); // A mode needs its layout. let doc = json!({ "collection": "a.b.c", "description": "d", "record": {} }); assert_eq!(problems(&doc, false), ["record: layout required"]); } #[test] fn limits() { let mut deep = json!({ "component": "Badge", "props": { "value": "x" } }); for _ in 0..12 { deep = json!({ "component": "Stack", "children": [deep] }); } assert!( problems(&posts(deep), false) .iter() .any(|p| p.contains("nest at most 12")) ); let many: Vec = (0..200) .map(|_| json!({ "component": "Badge", "props": { "value": "x" } })) .collect(); let wide = posts(json!({ "component": "Stack", "children": many })); assert!( problems(&wide, false) .iter() .any(|p| p.contains("201 nodes")) ); let big = posts(json!({ "component": "Text", "props": { "value": "x".repeat(70_000) } })); assert!(problems(&big, false)[0].contains("more than the 65536")); let mut path = String::from("value"); for _ in 0..10 { path.push_str(".k"); } let long = posts(json!({ "component": "Text", "props": { "value": { "$": path } } })); assert_eq!(problems(&long, false).len(), 1); // Joins: depth 3 at most, 8 to a level. let mut joins = json!([{ "as": "a", "ref": "value.x" }]); for _ in 0..3 { joins = json!([{ "as": "a", "ref": "value.x", "join": joins }]); } let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); doc["list"]["join"] = joins; assert_eq!(wheres(&doc), ["list.join[0].join[0].join[0].join"]); doc["list"]["join"] = Value::Array( (0..9) .map(|i| json!({ "as": format!("j{i}"), "ref": "value.x" })) .collect(), ); assert_eq!(wheres(&doc), ["list.join"]); } #[test] fn joins_checked() { let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); doc["list"]["join"] = json!([ { "as": "a" }, { "as": "b", "each": "value.items" }, { "as": "c", "backlinks": { "collection": "a.b.c" } }, { "as": "d", "ref": "value.x", "limit": 3, "orderBy": ["value.y"] }, { "as": "e", "backlinks": { "collection": "a.b.c", "path": "value.p" }, "ref": "value.x", "collection": "a.b.c" }, { "as": "f", "ref": "value x" }, { "as": "a.b", "ref": "value.x" }, { "as": "g", "each": "value.items", "ref": "subject", "limit": 501 }, { "as": "h", "ref": "value.x", "collection": "nope" }, { "as": "d", "ref": "value.x" }, ]); let found = problems(&doc, false); assert_eq!( found .iter() .map(|p| p.split_once(": ").unwrap().0) .collect::>(), [ "list.join", "list.join[0]", "list.join[1]", "list.join[2].backlinks.path", "list.join[3].orderBy", "list.join[3].limit", "list.join[4]", "list.join[4].collection", "list.join[5].ref", "list.join[6].as", "list.join[7].limit", "list.join[8].collection", "list.join[9].as", ] ); assert_eq!(found[0], "list.join: 10 joins, more than 8"); assert_eq!( found[1], "list.join[0]: \"ref\", \"backlinks\" or \"counts\" required" ); let good = json!([{ "as": "items", "backlinks": { "collection": "social.grain.gallery.item", "path": "value.gallery" }, "orderBy": ["value.position", "value.createdAt"], "limit": 1, "join": [{ "as": "photo", "ref": "value.item", "collection": "social.grain.photo" }] }]); doc["list"]["join"] = good; let v = parse(&doc, false).unwrap(); let j = &v.list.unwrap().join[0]; assert_eq!((j.limit, j.order_by.len(), j.join.len()), (1, 2, 1)); } #[test] fn counts_checked() { let mut doc = posts( json!({ "component": "Stat", "props": { "value": { "$": "joins.c.likes" }, "icon": "heart", "label": "likes" } }), ); doc["list"]["join"] = json!([{ "as": "c", "counts": { "likes": { "collection": "app.bsky.feed.like", "path": ".subject.uri" }, "quotes": { "collection": "app.bsky.feed.post", "path": ".embed[app.bsky.embed.record].record.uri" } } }]); let v = parse(&doc, false).unwrap(); assert_eq!( v.list.unwrap().join[0].source, Source::Counts(vec![ ( "likes".into(), "app.bsky.feed.like".into(), ".subject.uri".into() ), ( "quotes".into(), "app.bsky.feed.post".into(), ".embed[app.bsky.embed.record].record.uri".into() ), ]) ); let nine: Map = (0..9) .map(|i| { ( format!("n{i}"), json!({ "collection": "a.b.c", "path": ".x" }), ) }) .collect(); doc["list"]["join"] = json!([ { "as": "a", "counts": {} }, { "as": "b", "counts": nine }, { "as": "c", "counts": { "bad-name": { "collection": "a.b.c", "path": ".x" } } }, { "as": "d", "counts": { "n": { "collection": "nope", "path": "subject" } } }, { "as": "e", "counts": { "n": { "collection": "a.b.c", "path": ".x", "extra": 1 } } }, { "as": "f", "counts": { "n": { "collection": "a.b.c", "path": ".x" } }, "limit": 3, "join": [] }, { "as": "g", "counts": [] }, ]); assert_eq!( wheres(&doc), [ "list.join[0].counts", "list.join[1].counts", "list.join[2].counts.bad-name", "list.join[3].counts.n.collection", "list.join[3].counts.n.path", "list.join[4].counts.n", "list.join[5].limit", "list.join[5].join", "list.join[6].counts", ] ); // Stat's icon and label are literals, its value a number or binding. let stat = |props: Value| { problems( &posts(json!({ "component": "Stat", "props": props })), false, ) }; assert_eq!( stat(json!({ "value": 3, "icon": "heart" })), Vec::::new() ); assert_eq!( stat(json!({ "value": "many", "icon": "fire", "label": { "$": "x" } })).len(), 3 ); } #[test] fn summaries_checked() { let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); doc["list"]["summary"] = json!({ "label": 3, "detail": { "noun": ["a"] } }); assert_eq!( wheres(&doc), [ "list.summary.noun", "list.summary.label", "list.summary.detail.count", "list.summary.detail.noun" ] ); } /// A mode's head: its known keys, a type of Open Graph's, templates as /// an action's record's, structured data an object, its `$each` an /// array and a template, nested at most so deep. #[test] fn heads_checked() { let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); let fine = json!({ "title": { "$join": [{ "$": "handle" }, " on Grain"] }, "type": "profile", "noindex": true, "canonical": "/x", "jsonLd": { "@type": "ItemList", "itemListElement": [ { "$each": [{ "$": "records" }, { "url": { "$": "uri" }, "n": [1] }] }] } }); doc["list"]["head"] = fine; assert_eq!(problems(&doc, false), Vec::::new()); let mut odd = doc.clone(); odd["list"]["head"] = json!("Coast"); assert_eq!(wheres(&odd), ["list.head"]); let mut deep = json!(1); for _ in 0..MAX_DEPTH { deep = json!([deep]); } doc["list"]["head"] = json!({ "type": "page", "jsonLd": [], "title": { "$first": [1] }, "image": { "$": "a..b" }, "x": { "$": "a" } }); doc["record"] = json!({ "head": { "jsonLd": { "a": { "$each": [1] }, "b": { "$join": ["a"] }, "c": deep } }, "layout": { "component": "Badge", "props": { "value": "x" } } }); assert_eq!( wheres(&doc), [ "list.head", "list.head.type", "list.head.jsonLd", "list.head.title", "list.head.image.$", "record.head.jsonLd.a", "record.head.jsonLd.b", "record.head.jsonLd.c[0][0][0][0][0][0][0][0][0][0][0]", ] ); } #[test] fn paths() { let v = json!({ "records": [{ "value": { "t": "x" } }], "a": { "b$_1": 2 } }); assert_eq!( Path::parse("records.0.value.t").unwrap().get(&v), Some(&json!("x")) ); assert_eq!(Path::parse("a.b$_1").unwrap().get(&v), Some(&json!(2))); assert_eq!(Path::parse("records.1.value").unwrap().get(&v), None); assert_eq!(Path::parse("records.x").unwrap().get(&v), None); // `.` is the scope itself, as for an array of strings' items. assert_eq!( Path::parse(".").unwrap().get(&json!("tag")), Some(&json!("tag")) ); for bad in [ "", "..", "a.", ".a", "a..b", "a-b", "a b", "a.b.c.d.e.f.g.h.i.j.k", ] { assert_eq!(Path::parse(bad), None, "{bad}"); } let b = Binding(vec![ Path::parse("a.missing").unwrap(), Path::parse("a.b$_1").unwrap(), ]); assert_eq!(b.get(&v), Some(&json!(2))); } #[test] fn csp() { let view = |origins: &[&str]| { let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); doc["origins"] = json!(origins); parse(&doc, true).unwrap() }; let views = [ view(&["https://images.plyr.fm", "https://audio.plyr.fm"]), view(&["https://cdn.example", "https://audio.plyr.fm"]), ]; assert_eq!( origins_of("http://localhost:8787", views.iter()), [ "http://localhost:8787", "https://audio.plyr.fm", "https://cdn.example", "https://images.plyr.fm", ] ); let bundled = csp_origins("https://hud.example"); assert!(!bundled.iter().any(|b| b == catalog::MAP_TILES)); for o in [ "https://hud.example", "https://audio.plyr.fm", "https://images.plyr.fm", "https://cdn.bsky.app", ] { assert!(bundled.iter().any(|b| b == o), "{o} in {bundled:?}"); } assert!(bundled.windows(2).all(|w| w[0] < w[1]), "{bundled:?}"); } /// A feed's view, as a model might write one. fn feed_view() -> Value { json!({ "method": "app.bsky.feed.getFeed", "service": "did:web:api.bsky.app#bsky_appview", "description": "A feed", "query": { "items": "feed", "cursor": "cursor", "summary": { "noun": ["post", "posts"], "label": { "$": "post.record.text" } }, "layout": { "component": "List", "props": { "items": { "$": "items" }, "open": { "$": "post.uri" } }, "children": [{ "component": "Text", "props": { "value": { "$": "post.record.text" } } }, { "component": "Image", "props": { "src": { "$": "post.author.avatar" }, "size": "avatar" } }] } } }) } #[test] fn method_views() { let v = parse(&feed_view(), false).unwrap(); assert_eq!( ( v.method.as_deref(), v.service.as_deref(), v.collection.as_deref() ), ( Some("app.bsky.feed.getFeed"), Some("did:web:api.bsky.app#bsky_appview"), None ) ); let q = v.query.unwrap(); assert_eq!( (q.items, q.cursor), (Path::parse("feed"), Path::parse("cursor")) ); // Both a collection and a method: refused. let mut both = feed_view(); both["collection"] = json!("app.bsky.feed.post"); assert_eq!( problems(&both, false), ["view: a view draws a collection or a method, not both"] ); // A method view: a service, the query mode alone. let mut v = feed_view(); v["service"] = json!("did:web:api.bsky.app"); v["list"] = json!({ "layout": { "component": "Badge", "props": { "value": "x" } } }); v["pageSize"] = json!(10); v["query"]["nope"] = json!([]); // Without items, a cursor pages nothing. v["query"].as_object_mut().unwrap().remove("items"); assert_eq!( wheres(&v), [ "view.service", "view.pageSize", "view.list", "query", "query.cursor" ] ); let mut no_query = feed_view(); no_query.as_object_mut().unwrap().remove("query"); no_query.as_object_mut().unwrap().remove("service"); assert_eq!(wheres(&no_query), ["view.service", "view"]); // A collection view takes no service or query. let mut c = posts(json!({ "component": "Badge", "props": { "value": "x" } })); c["service"] = json!("did:web:api.bsky.app#bsky_appview"); c["query"] = feed_view()["query"].clone(); assert_eq!(wheres(&c), ["view.service", "view.query"]); // Image's size is a literal. let mut sized = feed_view(); sized["query"]["layout"]["children"][1]["props"]["size"] = json!("huge"); assert_eq!(wheres(&sized), ["query.layout.children[1].props.size"]); // Text's prefix: a literal of 40 characters at most. let text = |prefix: Value| { problems( &posts( json!({ "component": "Text", "props": { "value": { "$": "value.text" }, "prefix": prefix } }), ), false, ) }; assert!(text(json!("Reposted by ")).is_empty()); assert_eq!(text(json!("x".repeat(41))).len(), 1); assert_eq!(text(json!("two\nlines")).len(), 1); assert_eq!(text(json!({ "$": "value.by" })).len(), 1); } #[test] fn opens_checked() { let list = |open: Value| { posts(json!({ "component": "List", "props": { "items": { "$": "records" }, "open": open }, "children": [{ "component": "Badge", "props": { "value": "x" } }] })) }; let at = "list.layout.props.open"; for ok in [ json!({ "$": "uri" }), json!({ "query": "app.bsky.feed.getPostThread", "params": { "uri": { "$": "uri" } } }), json!({ "query": "app.bsky.feed.getPostThread" }), json!({ "query": "app.bsky.feed.getAuthorFeed", "service": "did:web:api.bsky.app#bsky_appview", "params": { "actor": { "$": ["did", "handle"] }, "limit": 5, "filter": ["a"], "x": { "k": 1 } } }), // The view's own scope, an at-uri built of parts, an array of params. json!({ "query": "a.b.c", "params": { "repo": { "$root": "params.repo" }, "made": { "$atUri": [{ "$": "repo.did" }, "a.b.repo", { "$root": "params.name" }] }, "paths": [{ "$": "old" }, { "$": "new" }, "README.md"] } }), // What it shows first, and its screen over what is open. json!({ "query": "a.b.c", "at": { "$": "uri" }, "sheet": true }), // Its screen opened at a form: its first, or one by label. json!({ "query": "a.b.c", "compose": true }), json!({ "query": "a.b.c", "compose": "New gallery" }), // At its form only while nothing is at a path (one's own stories). json!({ "query": "a.b.c", "compose": true, "unless": "stories" }), json!(false), ] { assert_eq!( problems(&list(ok.clone()), false), Vec::::new(), "{ok}" ); } let nine: serde_json::Map = (0..9).map(|i| (format!("p{i}"), json!(i))).collect(); for (open, want) in [ (json!("at://did:plc:x/a.b.c/1"), at.to_string()), (json!({ "params": {} }), format!("{at}.query")), (json!({ "query": "not an nsid" }), format!("{at}.query")), ( json!({ "query": "a.b.c", "params": [] }), format!("{at}.params"), ), ( json!({ "query": "a.b.c", "params": nine }), format!("{at}.params"), ), ( json!({ "query": "a.b.c", "params": { "a-b": 1 } }), format!("{at}.params.a-b"), ), ( json!({ "query": "a.b.c", "params": { "uri": { "$": "no spaces" } } }), format!("{at}.params.uri.$"), ), ( json!({ "query": "a.b.c", "service": "did:web:x" }), format!("{at}.service"), ), (json!({ "query": "a.b.c", "method": "x" }), at.to_string()), ( json!({ "query": "a.b.c", "params": { "repo": { "$root": "no spaces" } } }), format!("{at}.params.repo.$root"), ), ( json!({ "query": "a.b.c", "params": { "repo": { "$root": "a", "x": 1 } } }), format!("{at}.params.repo"), ), ( json!({ "query": "a.b.c", "params": { "repo": { "$atUri": ["a", "b"] } } }), format!("{at}.params.repo.$atUri"), ), ( json!({ "query": "a.b.c", "params": { "repo": { "$atUri": ["a", 1, "c"] } } }), format!("{at}.params.repo.$atUri[1]"), ), ( json!({ "query": "a.b.c", "params": { "repo": { "$now": true } } }), format!("{at}.params.repo"), ), ( json!({ "query": "a.b.c", "params": { "paths": [["a"]] } }), format!("{at}.params.paths[0]"), ), ( json!({ "query": "a.b.c", "sheet": "yes" }), format!("{at}.sheet"), ), ( json!({ "query": "a.b.c", "compose": "" }), format!("{at}.compose"), ), ( json!({ "query": "a.b.c", "compose": 1 }), format!("{at}.compose"), ), ( json!({ "query": "a.b.c", "compose": true, "unless": "no spaces" }), format!("{at}.unless"), ), ( json!({ "query": "a.b.c", "unless": "stories" }), format!("{at}.unless"), ), ( json!({ "query": "a.b.c", "at": { "$": "no spaces" } }), format!("{at}.at.$"), ), (json!(true), at.to_string()), ] { assert_eq!(wheres(&list(open.clone())), [want], "{open}"); } // Covers takes either form too. let covers = posts(json!({ "component": "Covers", "props": { "items": { "$": "records" }, "src": { "$": "value.a" }, "open": { "query": "app.bsky.feed.getPostThread", "params": { "uri": { "$": "uri" } } } } })); assert_eq!(problems(&covers, false), Vec::::new()); } /// The host's accounts are drawn where a bundled view places them, never /// an inline one. #[test] fn accounts_are_a_bundled_views_alone() { let accounts = posts(json!({ "component": "Accounts", "children": [ { "component": "Badge", "props": { "value": "x" } }] })); assert_eq!(problems(&accounts, true), Vec::::new()); assert_eq!(wheres(&accounts), ["list.layout.component"]); } /// An open may be an https page, or, in a bundled view alone, what the /// host does itself; never both, and never a tab's. #[test] fn opens_a_page_or_the_hosts_own() { let list = |open: Value| { posts(json!({ "component": "List", "props": { "items": { "$": "records" }, "open": open }, "children": [{ "component": "Badge", "props": { "value": "x" } }] })) }; let at = "list.layout.props.open"; let page = json!({ "url": "https://grain.social/settings/import" }); let host = json!({ "host": "signOut" }); assert_eq!(problems(&list(page.clone()), false), Vec::::new()); assert_eq!(problems(&list(host.clone()), true), Vec::::new()); for (open, trusted, want) in [ (host, false, at.to_string()), ( json!({ "host": "deleteAccount" }), true, format!("{at}.host"), ), (json!({ "host": 1 }), true, format!("{at}.host")), ( json!({ "url": "http://grain.social" }), false, format!("{at}.url"), ), (json!({ "url": "grain.social" }), false, format!("{at}.url")), (json!({ "url": 5 }), false, format!("{at}.url")), ( json!({ "url": "https://a.example", "host": "signOut" }), true, at.to_string(), ), ] { let found: Vec = problems(&list(open.clone()), trusted) .iter() .map(|p| p.split_once(": ").unwrap().0.to_string()) .collect(); assert_eq!(found, [want], "{open}"); } // A tab draws a query. let tabs = posts(json!({ "component": "Tabs", "props": { "items": [ { "label": "A", "open": { "query": "a.b.c" } }, { "label": "B", "open": page } ] } })); assert_eq!(wheres(&tabs), ["list.layout.props.items[1].open"]); // So does a List's more chip, which opens something. let more = |more: Value| { posts( json!({ "component": "List", "props": { "items": { "$": "records" }, "style": "chips", "more": more }, "children": [] }), ) }; assert!( wheres(&more( json!({ "label": "See all", "open": { "query": "a.b.c" } }) )) .is_empty() ); assert_eq!( wheres(&more(json!({ "label": "See all", "open": page }))), ["list.layout.props.more.open"] ); assert_eq!( wheres(&more(json!({ "label": "See all" }))), ["list.layout.props.more.open"] ); assert_eq!(wheres(&more(json!("See all"))), ["list.layout.props.more"]); } #[test] fn states_labels_diffs_and_links() { let layout = |node: Value| { posts(json!({ "component": "List", "props": { "items": { "$": "records" } }, "children": [node] })) }; let node = |component: &str, props: Value| json!({ "component": component, "props": props }); for ok in [ node( "State", json!({ "value": { "$": "value.state" }, "of": { "$": "value.kind" }, "roles": { "pull/open": "open-pull", "merged": "merged" }, "words": { "open": "Open" }, "style": "pill" }), ), node( "Label", json!({ "name": { "$": "value.name" }, "color": "#d73a4a" }), ), node( "Diff", json!({ "path": { "$": "value.new" }, "rows": { "$": "value.rows" }, "kinds": { "added": "a", "removed": "d" } }), ), node("Diff", json!({ "path": "README.md", "added": 3 })), node( "Link", json!({ "label": "Repo", "open": { "query": "a.b.c", "params": { "repo": { "$": "uri" } } } }), ), node( "Text", json!({ "value": { "$": "value.sha" }, "chars": 7, "style": "strong", "words": { "a": "b" } }), ), json!({ "component": "List", "props": { "items": { "$root": "records" }, "where": { "commit": "value.sha" }, "title": "Checks", "empty": "No checks" }, "children": [node("Badge", json!({ "value": "x" }))] }), ] { assert_eq!( problems(&layout(ok.clone()), false), Vec::::new(), "{ok}" ); } let at = "list.layout.children[0]"; for (bad, want) in [ // A state's roles are literal, and only roles there are. ( node("State", json!({ "value": "x" })), format!("{at}.props.roles"), ), ( node( "State", json!({ "value": "x", "roles": { "open": "green" } }), ), format!("{at}.props.roles"), ), ( node( "State", json!({ "value": "x", "roles": { "$": "value.roles" } }), ), format!("{at}.props.roles"), ), ( node("Text", json!({ "value": "x", "words": {} })), format!("{at}.props.words"), ), // A diff's kinds are those four alone. ( node("Diff", json!({ "path": "a", "kinds": { "context": " " } })), format!("{at}.props.kinds"), ), ( node("Diff", json!({ "path": "a", "rows": [] })), format!("{at}.props.rows"), ), // A link takes href or open, one of them. (node("Link", json!({ "label": "x" })), format!("{at}.props")), ( node( "Link", json!({ "href": "https://x.example", "open": { "query": "a.b.c" } }), ), format!("{at}.props"), ), // Only a list's items read the view's own scope, by a path. ( node( "Gallery", json!({ "items": { "$root": "records" }, "src": { "$": "a" } }), ), format!("{at}.props.items"), ), ( json!({ "component": "List", "props": { "items": { "$root": "no spaces" } } }), format!("{at}.props.items.$root"), ), ( json!({ "component": "List", "props": { "items": { "$": "records" }, "where": { "a b": "c" } } }), format!("{at}.props.where.a b"), ), ] { assert_eq!(wheres(&layout(bad.clone())), [want], "{bad}"); } } /// `app`, `profile` and `activity` are a bundled view's alone, checked /// as the rest: a method view's section its query's params, `$actor` /// the account's DID; a collection view's how many records, 3 by /// default; activity a short verb, of a collection view whose list has /// a summary to name its records by. #[test] fn apps_and_profile_sections() { let method = |trusted: bool, extra: Value| { let mut doc = json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "layout": { "component": "Badge", "props": { "value": "x" } } } }); doc.as_object_mut() .unwrap() .extend(extra.as_object().unwrap().clone()); parse(&doc, trusted) }; let records = |trusted: bool, extra: Value| { let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); doc.as_object_mut() .unwrap() .extend(extra.as_object().unwrap().clone()); parse(&doc, trusted) }; let v = method( true, json!({ "app": "Example", "profile": { "title": "Example", "params": { "actor": { "$actor": "did" }, "limit": 3, "on": true } } }), ) .unwrap(); assert_eq!(v.app.as_deref(), Some("Example")); let s = v.profile.unwrap(); assert_eq!( Value::Object(s.params_for("did:plc:a")), json!({ "actor": "did:plc:a", "limit": 3, "on": true }) ); let s = records(true, json!({ "profile": { "title": "Posts" } })) .unwrap() .profile .unwrap(); assert_eq!((s.title.as_str(), s.limit), ("Posts", 3)); let summed = json!({ "layout": { "component": "Badge", "props": { "value": "x" } }, "summary": { "noun": ["post", "posts"], "label": { "$": "value.text" } } }); let v = records(true, json!({ "list": summed, "activity": "Posted" })).unwrap(); assert_eq!(v.activity.as_deref(), Some("Posted")); for (bad, want) in [ ( method(true, json!({ "profile": { "title": "x", "limit": 3 } })), "view.profile: unknown key \"limit\"", ), ( records(true, json!({ "profile": { "title": "x", "params": {} } })), "view.profile: unknown key \"params\"", ), ( records(true, json!({ "profile": { "title": "x", "limit": 11 } })), "view.profile.limit: must be an integer from 1 to 10", ), ( method( true, json!({ "profile": { "title": "x", "params": { "a": { "$actor": "handle" } } } }), ), "view.profile.params.a: must be a string, number, boolean or {\"$actor\": \"did\"}, or an array of those", ), ( records(true, json!({ "profile": {} })), "view.profile.title: must be one line of text, 40 characters at most", ), ( records(true, json!({ "app": "two\nlines" })), "view.app: must be one line of text, 40 characters at most", ), ( records(false, json!({ "app": "Example" })), "view.app: only a bundled view names its app", ), ( records(false, json!({ "profile": { "title": "x" } })), "view.profile: only a bundled view adds a section to profiles", ), ( records(false, json!({ "list": summed, "activity": "Posted" })), "view.activity: only a bundled view names its activity", ), ( method(true, json!({ "activity": "Posted" })), "view.activity: only a collection view's records are activity", ), ( records(true, json!({ "activity": "Posted" })), "view.activity: activity names its records by the list's summary, which it needs", ), ( records( true, json!({ "list": summed, "activity": "Posted and then posted again" }), ), "view.activity: must be one line of text, 20 characters at most", ), ] { assert_eq!(bad.unwrap_err(), [want]); } // A method view's records: each collection's params read from the // record, and what making one is. let routes = json!([ { "collection": "a.b.post", "params": { "uri": { "$": "uri" }, "depth": 2 }, "activity": { "verb": "Posted", "noun": ["post", "posts"], "label": { "$": "value.text" } } }, { "collection": "a.b.repost", "params": { "uri": { "$": ["value.subject.uri", "uri"] } } } ]); let v = method(true, json!({ "records": routes })).unwrap(); assert_eq!(v.records.len(), 2); let (post, repost) = (&v.records[0], &v.records[1]); assert!(!post.reads_record() && repost.reads_record()); let rec = json!({ "uri": "at://did:plc:a/a.b.repost/1", "did": "did:plc:a", "value": { "subject": { "uri": "at://did:plc:b/a.b.post/2" } } }); assert_eq!( Value::Object(post.params_for(&rec).unwrap()), json!({ "uri": "at://did:plc:a/a.b.repost/1", "depth": 2 }) ); assert_eq!( repost.params_for(&rec).unwrap()["uri"], "at://did:plc:b/a.b.post/2" ); // A binding that finds nothing, or no scalar, gives no params. let bare = json!({ "uri": "", "did": "did:plc:a", "value": { "subject": {} } }); assert!(post.params_for(&bare).is_none()); assert!(post.params_for(&json!({ "uri": { "x": 1 } })).is_none()); let a = post.activity.as_ref().unwrap(); assert_eq!( (a.verb.as_str(), &a.summary.noun), ("Posted", &["post".to_string(), "posts".to_string()]) ); // What the answer shows first, read from the record too: a value of // the record's read makes it read; one finding no string, none. let at = json!([{ "collection": "a.b.story", "params": { "actor": { "$": "did" } }, "at": { "$": "value.first" } }]); let story = &method(true, json!({ "records": at })).unwrap().records[0]; assert!(story.reads_record()); assert_eq!( story.args_for( "a.b.getArchive", &json!({ "did": "did:plc:a", "value": { "first": "s1" } }) ), Some( json!({ "method": "a.b.getArchive", "params": { "actor": "did:plc:a" }, "at": "s1" }) ) ); assert_eq!( story.args_for( "a.b.getArchive", &json!({ "did": "did:plc:a", "value": { "first": 2 } }) ), Some(json!({ "method": "a.b.getArchive", "params": { "actor": "did:plc:a" } })) ); assert_eq!(story.args_for("a.b.getArchive", &json!({})), None); for (bad, want) in [ ( method(false, json!({ "records": routes })), "view.records: only a bundled view names the records it draws", ), ( records(true, json!({ "records": routes })), "view.records: only a method view draws records, as its query", ), ( method(true, json!({ "records": [] })), "view.records: must be 1 to 8 collections", ), ( method( true, json!({ "records": [{ "collection": "a.b.c", "params": { "x": { "$": "joins.y" } } }] }), ), "view.records[0].params.x: reads the record: a path from uri, cid, did, value", ), ( method( true, json!({ "records": [{ "collection": "a.b.c", "params": { "x": [1] } }] }), ), "view.records[0].params.x: must be a binding {\"$\": path} into the record", ), ( method( true, json!({ "records": [{ "collection": "a.b.c", "params": { "x": 1 }, "at": "uri" }] }), ), "view.records[0].at: must be a binding {\"$\": path} into the record", ), ( method(true, json!({ "records": [{ "collection": "a.b.c" }] })), "view.records[0].params: must be an object of 1 to 8 params", ), ( method( true, json!({ "records": [{ "collection": "a.b.c", "params": { "x": 1 } }, { "collection": "a.b.c", "params": { "x": 1 } }] }), ), "view.records[1].collection: a.b.c is named twice", ), ( method( true, json!({ "records": [{ "collection": "a.b.c", "params": { "x": 1 }, "activity": { "verb": "Posted and then posted again", "noun": ["a", "b"] } }] }), ), "view.records[0].activity.verb: must be one line of text, 20 characters at most", ), ] { assert_eq!(bad.unwrap_err(), [want]); } } /// Every bundled method view's profile section's params are its /// method's, as its lexicon takes them (a bundled one, or knotless's /// as it publishes them); Bluesky's posts are the author feed's. #[test] fn bundled_profile_sections_are_their_methods() { let docs = lexicon_docs(); let mut seen = 0; for b in bundled() { let (Some(s), Some(method)) = (&b.view.profile, &b.view.method) else { continue; }; assert_eq!( section_problems(s, method, &docs), Vec::::new(), "{}", b.id ); seen += 1; } assert_eq!(seen, 4); let message = for_method("chat.bsky.convo.getConvoAvailability").unwrap(); let head = message.profile.as_ref().unwrap(); assert!(head.head); assert_eq!( head.params_for("did:plc:a")["members"], json!(["did:plc:a"]) ); assert_eq!( for_method("dev.knotless.getActorRepos") .and_then(|v| v.profile.as_ref()) .map(|s| s.title.as_str()), Some("knotless") ); let feed = for_method("app.bsky.feed.getAuthorFeed").unwrap(); assert_eq!(feed.profile.as_ref().unwrap().title, "Bluesky"); } #[test] fn sign_in_words_are_a_bundled_method_views() { let view = |trusted: bool, extra: Value| { let mut doc = json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "layout": { "component": "Badge", "props": { "value": "x" } } } }); doc.as_object_mut() .unwrap() .extend(extra.as_object().unwrap().clone()); parse(&doc, trusted) }; let signed = view(true, json!({ "signIn": "Sign in to see your things" })).unwrap(); assert_eq!( signed.sign_in.as_deref(), Some("Sign in to see your things") ); assert_eq!( view(false, json!({ "signIn": "x" })).unwrap_err(), ["view.signIn: only a bundled view says what signing in shows"] ); assert!(view(true, json!({ "signIn": "two\nlines" })).is_err()); assert_eq!( for_method("dev.knotless.listNotifications").and_then(|v| v.sign_in.as_deref()), Some("Sign in to see your notifications") ); } /// The paths a layout binds (`{"$": …}`), each binding's alternatives /// together, but `$root`'s. fn bound_paths(v: &Value, out: &mut Vec>) { match v { // A binding's path, or its paths. Value::Object(o) => match o.get("$") { Some(p @ (Value::String(_) | Value::Array(_))) => out.push( std::iter::once(p) .chain(p.as_array().into_iter().flatten()) .filter_map(Value::as_str) .map(str::to_string) .collect(), ), _ => o.values().for_each(|v| bound_paths(v, out)), }, Value::Array(items) => items.iter().for_each(|v| bound_paths(v, out)), _ => {} } } /// knotless's notifications are an asker's alone, so no live call draws /// them signed out: an answer as their lexicon says one is (as knotless /// has it, in testdata/knotless), held to it as the query tool holds /// the service's, summarized and drawn by the bundled view, every path /// its rows bind there in one of them. #[test] fn the_knotless_notifications_view_draws_an_answer_its_lexicon_allows() { let method = "dev.knotless.listNotifications"; let docs: Vec> = [ include_str!("testdata/knotless/dev.knotless.listNotifications.json"), include_str!("testdata/knotless/dev.knotless.defs.json"), include_str!("testdata/knotless/dev.knotless.queue.json"), ] .iter() .map(|t| std::sync::Arc::new(serde_json::from_str(t).unwrap())) .collect(); let answer: Value = serde_json::from_str(include_str!( "testdata/knotless/listNotifications.answer.json" )) .unwrap(); let lexicons = crate::xrpc::lexicons_for(method, &docs); let def = crate::xrpc::query_def(method, &docs[0]).unwrap(); assert_eq!( crate::xrpc::output_problem(&lexicons, method, def, &answer), None ); let mut wrong = answer.clone(); wrong["notifications"][0]["isRead"] = json!("no"); assert!(crate::xrpc::output_problem(&lexicons, method, def, &wrong).is_some()); let view = for_method(method).expect("the knotless notifications view"); assert_eq!( view.service.as_deref(), Some("did:web:knotless.dev#knotless_appview") ); let q = view.query.as_ref().unwrap(); let items = q .items .as_ref() .unwrap() .get(&answer) .and_then(Value::as_array) .unwrap(); let cursor = q .cursor .as_ref() .and_then(|c| c.get(&answer)) .and_then(Value::as_str); assert_eq!( crate::summary::query( q.summary.as_ref().unwrap(), method, "knotless.dev", items, cursor ), "3 notifications from dev.knotless.listNotifications on knotless.dev: Draw a pull's checks beside its files, A space's background sync leaves no line its tests may miss, star; more with cursor 2026-11-14T08:00:00.000Z" ); let rows = &view.doc["query"]["layout"]; assert_eq!(rows["props"]["items"], json!({ "$": "items" })); let mut paths = Vec::new(); bound_paths(&rows["children"], &mut paths); assert!(paths.len() > 10); for alternatives in paths { let found = alternatives.iter().any(|p| { let path = Path::parse(p).unwrap(); items.iter().any(|i| path.get(i).is_some()) }); assert!(found, "no notification has {alternatives:?}"); } } /// Every bundled launcher's params are its method's, as the lexicon /// knotless publishes takes them (copies in testdata/knotless), and /// Bluesky's bundled ones. #[test] fn launchers_are_their_methods() { let mut docs: Vec> = [ include_str!("testdata/knotless/dev.knotless.listNotifications.json"), include_str!("testdata/knotless/dev.knotless.getActorRepos.json"), include_str!("testdata/knotless/dev.knotless.getFeed.json"), include_str!("testdata/knotless/dev.knotless.defs.json"), include_str!("testdata/knotless/dev.knotless.queue.json"), ] .iter() .map(|t| std::sync::Arc::new(serde_json::from_str(t).unwrap())) .chain(crate::lexicon::all_bundled().cloned()) .collect(); docs.extend( [ "app.bsky.feed.getFeed", "app.bsky.feed.getTimeline", "app.bsky.feed.getAuthorFeed", "app.bsky.feed.getFeedGenerators", "social.grain.feed.getFeed", ] .iter() .map(|m| crate::lexicon::bundled(m).unwrap()), ); let mut seen = 0; for b in bundled().iter().filter(|b| !b.view.launch.is_empty()) { let method = b.view.method.as_deref().unwrap(); let doc = docs.iter().find(|d| d["id"] == method); let doc = doc.ok_or(format!("no lexicon for {method}")).unwrap(); let def = crate::xrpc::query_def(method, doc).unwrap(); let lexicons = crate::xrpc::lexicons_for(method, &docs); for l in &b.view.launch { assert_eq!( launch_problems(l, &lexicons, method, def), Vec::::new(), "{}", b.id ); seen += 1; } } assert_eq!(seen, 20); // Those naming the person need one signed in. let names_me: Vec = for_method("dev.knotless.getFeed") .unwrap() .launch .iter() .map(Launch::names_me) .collect(); assert_eq!(names_me, [true, false]); // Chats are launched as any other app's are. let chats = launchers("did:plc:me", None) .into_iter() .find(|l| l["method"] == "chat.bsky.convo.listConvos"); assert_eq!( chats, Some( json!({ "app": "Bluesky Chat", "domain": "bsky.chat", "title": "Chats", "method": "chat.bsky.convo.listConvos", "service": "did:web:api.bsky.chat#bsky_chat", "params": {} }) ) ); // A param the method lacks, or a value it refuses, is found. let feed = docs .iter() .find(|d| d["id"] == "dev.knotless.getFeed") .unwrap(); let def = crate::xrpc::query_def("dev.knotless.getFeed", feed).unwrap(); let lexicons = crate::xrpc::lexicons_for("dev.knotless.getFeed", &docs); let bad = Launch { title: "x".into(), params: json!({ "nope": 1, "actor": { "$me": "handle" } }) .as_object() .unwrap() .clone(), }; assert_eq!( launch_problems(&bad, &lexicons, "dev.knotless.getFeed", def).len(), 2 ); // $me is the person. let mine = for_method("dev.knotless.getActorRepos").unwrap().launch[0] .params_for("did:plc:me", None) .unwrap(); assert_eq!(mine["actor"], "did:plc:me"); assert!(bad.params_for("did:plc:me", None).is_none()); // The shape, and bundled method views alone. let view = |trusted: bool, launch: Value| { parse( &json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "launch": launch, "query": { "layout": { "component": "Badge", "props": { "value": "x" } } } }), trusted, ) }; assert!( view( true, json!([{ "title": "Mine", "params": { "a": { "$me": "did" }, "b": 2 } }]) ) .is_ok() ); for bad in [ json!([]), json!([{ "params": {} }]), json!([{ "title": "x", "params": { "a": { "$me": "email" } } }]), json!([{ "title": "x", "params": { "a": [1] } }]), json!([{ "title": "x", "y": 1 }]), ] { assert!(view(true, bad.clone()).is_err(), "{bad}"); } assert_eq!( view(false, json!([{ "title": "x" }])).unwrap_err(), ["view.launch: only a bundled view offers launchers"] ); } /// A composer's writes read only its fields' and given values' names; /// what is written is never an `Action`'s, whose record takes no /// `$text` or `$facets`. #[test] fn compose_checked() { let doc = |c: &str, props: Value| { json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "layout": { "component": "Stack", "children": [{ "component": c, "props": props }] } } }) }; let reply = json!({ "label": "Reply", "icon": "reply", "given": { "root": { "$first": [{ "$": "post.record.reply.root" }, { "$strongRef": "post" }] }, "parent": { "$strongRef": "post" } }, "fields": [{ "name": "text", "kind": "text", "max": 300, "facets": true, "required": true }], "writes": [{ "collection": "app.bsky.feed.post", "record": { "text": { "$value": "text" }, "facets": { "$facets": "text" }, "createdAt": { "$now": true }, "reply": { "root": { "$value": "root" }, "parent": { "$value": "parent" } } } }] }); assert!(parse(&doc("Compose", reply.clone()), true).is_ok()); let problems = |change: &dyn Fn(&mut Value)| { let mut a = reply.clone(); change(&mut a); parse(&doc("Compose", a), true).unwrap_err() }; // A write reading a value no field or given names. let unnamed = problems(&|a| a["writes"][0]["record"]["text"] = json!({ "$value": "body" })); assert_eq!(unnamed.len(), 1); assert!(unnamed[0].contains("writes[0].record.text"), "{unnamed:?}"); // A field of a kind there is none of. let kind = problems(&|a| a["fields"][0]["kind"] = json!("poem")); assert_eq!(kind.len(), 1); assert!(kind[0].contains("fields[0]"), "{kind:?}"); // A given value's name is lowercase letters and digits. let named = problems(&|a| a["given"]["Root"] = json!("x")); assert_eq!( named, ["query.layout.children[0].props.given.Root: a name, lowercase letters and digits"] ); // A text's max only narrows its record's lexicon's, which it may leave to it. assert_eq!( problems(&|a| a["fields"][0]["max"] = json!(301)), [ "query.layout.children[0].props: fields[0].max: at most its record's lexicon's 300 graphemes" ] ); let mut unlimited = reply.clone(); unlimited["fields"][0] .as_object_mut() .unwrap() .remove("max"); assert!(parse(&doc("Compose", unlimited), true).is_ok()); // A photo's review is of one photo, its first field. let review = problems(&|a| a["form"] = json!("photo")); assert_eq!( review, ["query.layout.children[0].props.form: photo: its first field is photos of max 1"] ); let mut story = reply.clone(); story["form"] = json!("photo"); story["fields"].as_array_mut().unwrap().insert( 0, json!({ "name": "photo", "kind": "photos", "max": 1, "maxBytes": 900000, "maxSide": 1000, "camera": true }), ); assert!(parse(&doc("Compose", story), true).is_ok()); // A Compose for another names one drawn in its scope, and takes its form. let both = |prompt: Value| { json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "layout": { "component": "Stack", "children": [ { "component": "Compose", "props": reply.clone() }, { "component": "Compose", "props": prompt }] } } }) }; let field = json!({ "for": "Reply", "label": "Add a reply…", "style": "field", "signIn": "Sign in to reply" }); assert!(parse(&both(field), true).is_ok()); assert_eq!( parse( &both(json!({ "for": "Quote", "label": "Quote it", "send": "Quote" })), true ) .unwrap_err(), [ "query.layout.children[1].props.send: a Compose for another takes that one's", "query.layout.children[1].props.for: no Compose labelled \"Quote\" is drawn in its scope" ] ); assert_eq!( parse(&doc("Compose", json!({ "label": "Write", "writes": [{ "collection": "a.b.c", "record": { "n": 1 } }] })), true).unwrap_err(), ["query.layout.children[0].props.fields: Compose requires it"] ); // An Action writes no text a person wrote. let texted = json!({ "label": "Reply", "create": { "collection": "app.bsky.feed.post", "record": { "text": { "$text": true }, "createdAt": { "$now": true } } } }); let refused = parse(&doc("Action", texted), true).unwrap_err(); assert_eq!(refused.len(), 1); assert!( refused[0].contains("create.record.text.$text"), "{refused:?}" ); } /// A conversation is a layout's top node; a procedure call, in it or an /// `Action`, a bundled view's alone, its input a template. #[test] fn messages_and_calls_checked() { let send = json!({ "method": "chat.bsky.convo.sendMessage", "service": "did:web:api.bsky.chat#bsky_chat", "input": { "convoId": { "$root": "params.convoId" }, "message": { "text": { "$text": true } } } }); let messages = json!({ "component": "Messages", "props": { "items": { "$": "items" }, "from": "sender.did", "sent": "sentAt", "people": { "$root": "response.relatedProfiles" }, "order": "newest-first", "send": send }, "children": [{ "component": "Text", "props": { "value": { "$": "text" } } }] }); let doc = |layout: Value| { json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "items": "messages", "layout": layout } }) }; let v = parse(&doc(messages.clone()), true).unwrap(); assert_eq!( v.calls, [Call { method: "chat.bsky.convo.sendMessage".into(), service: "did:web:api.bsky.chat#bsky_chat".into(), input: send["input"].clone(), deletes: false, }] ); assert_eq!( parse(&doc(messages.clone()), false).unwrap_err(), ["query.layout.props.send: only a bundled view calls procedures"] ); let mut read_only = messages.clone(); read_only["props"].as_object_mut().unwrap().remove("send"); assert!(parse(&doc(read_only.clone()), false).is_ok()); assert_eq!( parse( &doc(json!({ "component": "Stack", "children": [read_only] })), false ) .unwrap_err(), ["query.layout.children[0].component: Messages is a layout's top node alone"] ); let mut bad = messages; bad["props"]["send"] = json!({ "method": "a.b.c", "input": { "text": "fixed", "x": { "$nope": 1 } } }); bad["props"]["from"] = json!({ "$": "sender.did" }); assert_eq!( parse(&doc(bad), true).unwrap_err(), [ "query.layout.props.from: must be a literal value, not a binding", "query.layout.props.send.service: required: a DID with its service's #fragment, as did:web:api.bsky.chat#bsky_chat", "query.layout.props.send.input.x.$nope: a key starting with $ is $type, or a binding's, $root's, $now's, $strongRef's, $rkey's, $first's, $join's, $with's, $without's, $move's or $text's alone", "query.layout.props.send.input: holds no {\"$text\": true}, where what the person writes goes" ] ); // An Action's call takes no text, and is one of its four. let action = |props: Value| doc(json!({ "component": "Action", "props": props })); let call = json!({ "method": "chat.bsky.convo.updateRead", "service": "did:web:api.bsky.chat#bsky_chat", "input": { "convoId": { "$": "id" } } }); assert!(parse(&action(json!({ "label": "Read", "call": call })), true).is_ok()); let mut texted = call.clone(); texted["input"]["convoId"] = json!({ "$text": true }); assert_eq!( parse(&action(json!({ "label": "Read", "call": texted })), true).unwrap_err(), [ "query.layout.props.call.input.convoId.$text: a key starting with $ is $type, or a binding's, $root's, $now's, $strongRef's, $rkey's, $first's, $join's, $with's, $without's or $move's alone" ] ); assert_eq!( parse( &action( json!({ "label": "Read", "call": call, "delete": { "uri": { "$": "uri" } } }) ), true ) .unwrap_err(), [ "query.layout.props: Action takes exactly one of create, delete, interaction, call, deletes" ] ); } /// An Action's `deletes`: a bundled view's alone, its query and params /// held as a call's, kept with the view's calls, and what it says beyond /// its query (repeat, collections, then) kept as its plan. #[test] fn deletes_checked() { let doc = |deletes: Value| { json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "layout": { "component": "Action", "props": { "label": "Delete", "deletes": deletes } } } }) }; let then = json!({ "method": "a.b.forget", "service": "did:web:x.example#svc", "input": { "did": { "$did": true }, "n": [1, { "$type": "t" }] } }); let full = json!({ "method": "a.b.getPlan", "service": "did:web:x.example#svc", "params": { "of": { "$": "uri" } }, "repeat": true, "collections": ["a.b.thing"], "then": then }); let v = parse(&doc(full.clone()), true).unwrap(); assert_eq!( v.calls, [Call { method: "a.b.getPlan".into(), service: "did:web:x.example#svc".into(), input: json!({ "of": { "$": "uri" } }), deletes: true, }] ); assert_eq!( v.plans, [Plan { method: "a.b.getPlan".into(), service: "did:web:x.example#svc".into(), repeat: true, collections: vec!["a.b.thing".into()], then: Some(Call { method: "a.b.forget".into(), service: "did:web:x.example#svc".into(), input: then["input"].clone(), deletes: false, }), }] ); // A then with no input takes an empty one. let mut bare = full.clone(); bare["then"] = json!({ "method": "a.b.forget", "service": "did:web:x.example#svc" }); let v = parse(&doc(bare), true).unwrap(); assert_eq!(v.plans[0].then.as_ref().unwrap().input, json!({})); // Of a gallery: no more than its query. let once = json!({ "method": "a.b.getPlan", "service": "did:web:x.example#svc" }); let v = parse(&doc(once.clone()), true).unwrap(); assert_eq!( ( v.plans[0].repeat, v.plans[0].collections.len(), &v.plans[0].then ), (false, 0, &None) ); let at = "query.layout.props.deletes"; assert_eq!( parse(&doc(once), false).unwrap_err(), [format!( "{at}: only a bundled view deletes what a query plans" )] ); let bad = json!({ "method": "a.b.getPlan", "service": "nope", "params": [], "repeat": 1, "collections": "a.b.c", "input": {}, "then": { "method": "a.b.forget", "service": "did:web:x.example#svc", "input": { "did": { "$": "did" } } } }); assert_eq!( parse(&doc(bad), true).unwrap_err(), [ format!("{at}: unknown key \"input\""), format!( "{at}.service: required: a DID with its service's #fragment, as did:web:api.bsky.chat#bsky_chat" ), format!("{at}.params: must be an object, the query's params"), format!("{at}.repeat: must be true or false"), format!("{at}.collections: must be a list of at most 32 collections' NSIDs"), format!( "{at}.then.input.did: is literal JSON but {{\"$did\": true}}, the account's DID" ), ] ); let mut odd = full; odd["collections"] = json!(["not an nsid"]); odd["then"] = json!({ "method": "a.b.forget", "input": [] }); let errs = parse(&doc(odd.clone()), true).unwrap_err(); assert!( errs.iter() .any(|e| e.starts_with(&format!("{at}.collections[0]"))), "{errs:?}" ); assert!( errs.contains(&format!( "{at}.then.input: must be an object, the procedure's input" )), "{errs:?}" ); odd["then"] = json!({ "method": "a.b.forget", "service": "did:web:x.example#svc", "input": { "a": { "$did": true, "b": 1 } } }); odd["collections"] = json!([]); assert_eq!( parse(&doc(odd), true).unwrap_err(), [format!( "{at}.then.input.a: is literal JSON but {{\"$did\": true}}, the account's DID" )] ); } /// A Choice's options are 2 to 6 `{ label, value }`, their values /// different strings; its default one of them; its call holds `$on`. #[test] fn a_choices_options_and_default_are_checked() { let doc = |options: Value, default: Value| { json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "layout": { "component": "Choice", "props": { "label": "Theme", "options": options, "default": default, "on": { "$": "response.prefs.@themePref.theme" }, "call": { "method": "a.b.setPreference", "service": "did:web:x.example#svc", "input": { "preference": { "$type": "a.b.defs#themePref", "theme": { "$on": true } } } } } } } }) }; let two = json!([{ "label": "Light", "value": "light" }, { "label": "Dark", "value": "dark" }]); assert!(parse(&doc(two.clone(), json!("light")), true).is_ok()); let options = "query.layout.props.options: must be an array of 2 to 6 options, each { label, value }, label short text, the values different strings"; for bad in [ json!([{ "label": "Light", "value": "light" }]), json!([{ "label": "Light", "value": "light" }, { "label": "Also", "value": "light" }]), json!([{ "label": "Light", "value": "light" }, { "label": "Dark", "value": 2 }]), json!([{ "label": "Light", "value": "light", "x": 1 }, { "label": "Dark", "value": "dark" }]), ] { assert_eq!( parse(&doc(bad, json!("light")), true).unwrap_err(), [options] ); } assert_eq!( parse(&doc(two, json!("sepia")), true).unwrap_err(), ["query.layout.props.default: must be one of its options' values"] ); } /// A Map's pins are at a cell, or at lat and lng; one giving neither, /// or lat alone, is the view's error. #[test] fn a_map_says_where_its_pins_are() { let doc = |place: Value| { let mut props = json!({ "items": { "$": "response.locations" }, "height": "tall", "zoom": 3 }); props .as_object_mut() .unwrap() .extend(place.as_object().unwrap().clone()); json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "layout": { "component": "Map", "props": props } } }) }; for place in [ json!({ "cell": { "$": "h3Index" } }), json!({ "lat": { "$": "lat" }, "lng": { "$": "lng" } }), ] { assert!(parse(&doc(place), true).is_ok()); } for place in [json!({}), json!({ "lat": { "$": "lat" } })] { assert_eq!( parse(&doc(place), true).unwrap_err(), ["query.layout.props: Map takes cell, or lat and lng"] ); } } /// A Toggle's call carries `{"$on": true}` where the new state goes, and /// takes no `$text`; one without it is the view's error. #[test] fn a_toggles_call_holds_where_its_state_goes() { let doc = |input: Value| { json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "layout": { "component": "Toggle", "props": { "label": "Photos keep EXIF", "on": { "$": "response.prefs.@photoPref.exif" }, "default": true, "call": { "method": "a.b.setPreference", "service": "did:web:x.example#svc", "input": input } } } } }) }; let set = json!({ "preference": { "$type": "a.b.defs#photoPref", "exif": { "$on": true } } }); assert!(parse(&doc(set), true).is_ok()); assert_eq!( parse( &doc(json!({ "preference": { "exif": { "$text": true } } })), true ) .unwrap_err(), [ "query.layout.props.call.input.preference.exif.$text: a key starting with $ is $type, or a binding's, $root's, $now's, $strongRef's, $rkey's, $first's, $join's, $with's, $without's, $move's or $on's alone", "query.layout.props.call.input: holds no {\"$on\": true}, where what the person chooses goes" ] ); assert_eq!( parse(&doc(json!({ "x": { "$on": true, "y": 1 } })), true).unwrap_err(), ["query.layout.props.call.input.x: {\"$on\": true} has the key $on alone, true"] ); } /// A template's edits of an array and its strings joined, a param's /// `$after` and a Tabs' opens by case, each malformed one named where it /// is. #[test] fn template_edits_after_and_cases_checked() { let list = |layout: Value| listing(layout, json!({})); let creating = |record: Value| { list(json!({ "component": "Action", "props": { "label": "Go", "create": { "collection": "a.b.pref", "record": record } } })) }; let opening = |param: Value| { list( json!({ "component": "Link", "props": { "open": { "query": "a.b.getThing", "params": { "x": param } } } }), ) }; let tabs = |opens: Value| { let tab = |label: &str| json!({ "label": label, "open": { "query": "a.b.getFeed" } }); list( json!({ "component": "Tabs", "props": { "items": [tab("A"), tab("B")], "label": "label", "from": { "$": "records" }, "opens": opens } }), ) }; let items = json!({ "$": "items" }); let good = creating(json!({ "added": { "$with": [items, "id", { "id": { "$join": ["camera:", { "$": "camera" }] } }] }, "taken": { "$without": [{ "$root": "records" }, "id", "a"] }, "moved": { "$move": [{ "$first": [items, []] }, "id", { "$": "id" }, -1] } })); assert_eq!(problems(&good, false), Vec::::new()); let after = opening(json!({ "$after": [{ "$": "id" }, "camera:"] })); assert_eq!(problems(&after, false), Vec::::new()); let open = json!({ "query": "a.b.getFeed", "params": { "camera": { "$after": [{ "$": "id" }, "camera:"] } } }); let cased = tabs( json!([{ "where": { "type": { "$is": "camera" } }, "open": open }, { "open": open }]), ); assert_eq!(problems(&cased, false), Vec::::new()); let r = "list.layout.props.create.record.x"; let o = "list.layout.props.open.params.x"; let c = "list.layout.props.opens"; for (doc, want) in [ ( creating(json!({ "x": { "$join": ["a"] } })), format!( "{r}: {{\"$join\": [values]}} has the key $join alone, 2 to 8 values, each a string once read" ), ), ( creating(json!({ "x": { "$with": [items, "id"] } })), format!("{r}: {{\"$with\": [array, key, value]}} has the key $with alone"), ), ( creating(json!({ "x": { "$move": [items, "id", "a", 0] } })), format!("{r}.$move[3]: must be a whole number but 0, the places it moves"), ), ( creating(json!({ "x": { "$without": [items, 5, "a"] } })), format!("{r}.$without[1]: must be a path, keys joined by dots"), ), ( creating(json!({ "x": { "$without": [items, "id", { "$nope": 1 }] } })), format!( "{r}.$without[2].$nope: a key starting with $ is $type, or a binding's, $root's, $now's, $strongRef's, $rkey's, $first's, $join's, $with's, $without's or $move's alone" ), ), ( opening(json!({ "$after": [{ "$": "id" }, ""] })), format!( "{o}: {{\"$after\": [value, prefix]}} has the key $after alone, the prefix a string" ), ), ( opening(json!({ "$nope": 1 })), format!( "{o}: an object with a key starting with $ is {{\"$\": path}}, {{\"$root\": path}}, {{\"$atUri\": [authority, collection, rkey]}} or {{\"$after\": [value, prefix]}}" ), ), ( tabs(json!({ "open": open })), format!("{c}: must be an array of 1 to 4 cases, each {{ where?, open }}"), ), ( tabs(json!([{ "where": { "type": 5 }, "open": open }])), format!( "{c}[0].where: must be an object of 1 to 4 paths within each element to paths in the scope or {{\"$is\": a string, number or boolean}}, either in {{\"$not\": …}}" ), ), ( tabs(json!([{ "where": { "type": "kind" } }])), format!("{c}[0].open: required: what the element opens"), ), ] { assert_eq!(problems(&doc, false), [want], "{doc}"); } } /// A path takes a union array's element by its type's name. #[test] fn a_path_takes_an_element_by_its_type() { let prefs = json!({ "prefs": [ { "$type": "a.b.defs#themePref", "theme": "dark" }, { "$type": "a.b.defs#photoPref", "exif": false }, { "$type": "plain", "x": 1 }, ] }); let at = |s: &str| Path::parse(s).unwrap().get(&prefs).cloned(); assert_eq!(at("prefs.@photoPref.exif"), Some(json!(false))); assert_eq!(at("prefs.@plain.x"), Some(json!(1))); assert_eq!(at("prefs.@feedPref"), None); assert_eq!(at("prefs.0.theme"), Some(json!("dark"))); assert!(Path::parse("prefs.@").is_none()); } /// A `where` pairs an element's path with a scope path or a literal, /// either in `{"$not": …}`, whose path is checked as one outside it. #[test] fn a_where_compares_with_a_path_or_a_literal() { let doc = |w: Value| { json!({ "collection": "a.b.c", "description": "d", "list": { "layout": { "component": "List", "props": { "items": { "$": "records" }, "where": w } } } }) }; assert!( parse( &doc(json!({ "type": { "$is": "feed" }, "did": "did", "uri": { "$not": "params.uri" }, "kind": { "$not": { "$is": 3 } } })), false ) .is_ok() ); let off = "list.layout.props.where: must be an object of 1 to 4 paths within each element to paths in the scope or {\"$is\": a string, number or boolean}, either in {\"$not\": …}"; for w in [ json!({ "type": { "$is": ["feed"] } }), json!({ "type": { "$not": ["feed"] } }), json!({ "type": { "$not": { "$not": "did" } } }), ] { assert_eq!(parse(&doc(w.clone()), false).unwrap_err(), [off], "{w}"); } assert_eq!( parse(&doc(json!({ "uri": { "$not": "a b" } })), false).unwrap_err(), ["list.layout.props.where.uri: unknown path syntax \"a b\""] ); } /// Covers' select: its words, its `mine` a path, and its call a bundled /// view's procedure, as an Action's. #[test] fn covers_select_checked() { let call = json!({ "method": "social.grain.unspecced.deleteGallery", "service": "did:web:grain.social#grain_appview", "input": { "rkey": { "$rkey": "uri" } } }); let doc = |select: Value| { json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": { "items": "items", "layout": { "component": "Covers", "props": { "items": { "$": "items" }, "src": { "$": "thumb" }, "select": select } } } }) }; let good = json!({ "mine": "params.actor", "action": "Delete", "noun": ["gallery", "galleries"], "confirm": "Gone for good.", "tone": "danger", "call": call }); let v = parse(&doc(good.clone()), true).unwrap(); assert_eq!(v.calls[0].method, "social.grain.unspecced.deleteGallery"); // Without mine, offered to anyone signed in. let mut anyone = good.clone(); anyone.as_object_mut().unwrap().remove("mine"); assert!(parse(&doc(anyone), true).is_ok()); assert_eq!( parse(&doc(good), false).unwrap_err(), ["query.layout.props.select.call: only a bundled view calls procedures"] ); let at = "query.layout.props.select"; assert_eq!( parse(&doc(json!({ "label": "", "noun": ["gallery"], "tone": "loud", "mine": "a..b", "extra": 1 })), true) .unwrap_err(), [ format!("{at}: unknown key \"extra\""), format!("{at}.label: text of at most 40 characters"), format!("{at}.action: text of at most 40 characters"), format!("{at}.noun: must be [singular, plural]"), format!("{at}.tone: must be danger"), format!("{at}.mine: unknown path syntax \"a..b\""), format!("{at}: takes one of call ({{ method, service, input? }}) and deletes ({{ method, service, params? }})"), ] ); assert_eq!( parse(&doc(json!(1)), true).unwrap_err(), [format!( "{at}: must be {{ label?, mine?, action, noun, confirm?, tone?, call or deletes }}" )] ); // Or what a query plans deleted, its params read from each item; // not both, and a bundled view's alone. let deletes = json!({ "method": "social.grain.gallery.getDeletionPlan", "service": "did:web:grain.social#grain_appview", "params": { "gallery": { "$": "uri" } } }); let planned = json!({ "action": "Delete", "noun": ["gallery", "galleries"], "deletes": deletes }); let v = parse(&doc(planned.clone()), true).unwrap(); assert_eq!( ( v.calls[0].method.as_str(), v.calls[0].deletes, &v.calls[0].input ), ( "social.grain.gallery.getDeletionPlan", true, &json!({ "gallery": { "$": "uri" } }) ) ); assert_eq!( parse(&doc(planned.clone()), false).unwrap_err(), [format!( "{at}.deletes: only a bundled view deletes what a query plans" )] ); let mut both = planned.clone(); both["call"] = call; assert_eq!( parse(&doc(both), true).unwrap_err(), [format!( "{at}: takes one of call ({{ method, service, input? }}) and deletes ({{ method, service, params? }})" )] ); let mut odd = planned; odd["deletes"]["params"] = json!([]); odd["deletes"]["input"] = json!({}); assert_eq!( parse(&doc(odd), true).unwrap_err(), [ format!("{at}.deletes: unknown key \"input\""), format!("{at}.deletes.params: must be an object, the query's params"), ] ); } /// Every procedure a bundled view calls is one, and its template's /// fields are the input's, as the bundled lexicons have them; and the /// server sends those alone. #[test] fn bundled_calls_are_their_procedures() { let docs = lexicon_docs(); let mut seen = Vec::new(); for b in Views::built_in().all() { for c in &b.view.calls { assert_eq!(call_problems(c, &docs), Vec::::new(), "{}", b.id); assert!(bundled_call(&c.method, &c.service)); seen.push(format!("{} {}", b.id, c.method)); } } // A view's switches each call the same procedure: once is enough here. seen.dedup(); assert_eq!( seen, [ "bsky-chat chat.bsky.convo.sendMessage", "bsky-chat-with chat.bsky.convo.sendMessage", "bsky-chats chat.bsky.convo.updateRead", ] ); assert!(!bundled_call( "chat.bsky.convo.sendMessage", "did:web:api.bsky.app#bsky_appview" )); // A template the lexicon would refuse is found. let bad = Call { method: "chat.bsky.convo.sendMessage".into(), service: "did:web:api.bsky.chat#bsky_chat".into(), input: json!({ "message": { "text": "x" }, "nope": 1 }), deletes: false, }; assert_eq!( call_problems(&bad, &docs), [ "chat.bsky.convo.sendMessage: input.nope is not an input of it", "chat.bsky.convo.sendMessage: input.convoId is required" ] ); } #[test] fn trees_and_chains() { let card = json!([{ "component": "Text", "props": { "value": { "$": "post.record.text" } } }]); let node = |component: &str, props: Value| { posts(json!({ "component": component, "props": props, "children": card })) }; let ok = [ node( "Tree", json!({ "items": { "$": "records" }, "children": "replies" }), ), node( "Tree", json!({ "items": { "$": "records" }, "children": "value.replies", "depth": 8, "open": { "query": "app.bsky.feed.getPostThread", "params": { "uri": { "$": "post.uri" } } } }), ), node( "Chain", json!({ "from": { "$": "value.parent" }, "next": "parent" }), ), node( "Chain", json!({ "from": { "$": "." }, "next": "parent", "depth": 20, "open": { "$": "uri" } }), ), ]; for doc in ok { assert_eq!(problems(&doc, false), Vec::::new(), "{doc}"); } let at = "list.layout.props"; for (doc, want) in [ ( node("Tree", json!({ "children": "replies" })), vec![format!("{at}.items")], ), ( node("Tree", json!({ "items": [] , "children": "replies" })), vec![format!("{at}.items")], ), ( node("Tree", json!({ "items": { "$": "records" } })), vec![format!("{at}.children")], ), ( node( "Tree", json!({ "items": { "$": "records" }, "children": { "$": "replies" } }), ), vec![format!("{at}.children")], ), ( node( "Tree", json!({ "items": { "$": "records" }, "children": "two words" }), ), vec![format!("{at}.children")], ), ( node( "Tree", json!({ "items": { "$": "records" }, "children": "replies", "depth": 9 }), ), vec![format!("{at}.depth")], ), ( node("Chain", json!({ "from": "x", "next": "parent" })), vec![format!("{at}.from")], ), ( node("Chain", json!({ "from": { "$": "value" } })), vec![format!("{at}.next")], ), ( node( "Chain", json!({ "from": { "$": "value" }, "next": "parent", "depth": 21 }), ), vec![format!("{at}.depth")], ), ( node( "Chain", json!({ "from": { "$": "value" }, "next": "parent", "items": { "$": "x" } }), ), vec![format!("{at}.items")], ), ] { assert_eq!(wheres(&doc), want, "{doc}"); } } #[test] fn query_mode_without_items() { let mut v = feed_view(); let q = v["query"].as_object_mut().unwrap(); q.remove("items"); q.remove("cursor"); q["summary"] = json!({ "noun": ["thread", "threads"], "label": { "$": "response.thread.post.record.text" }, "by": { "$": "response.thread.post.author.handle" }, "detail": { "count": { "$": "response.thread.replies" }, "noun": ["reply", "replies"], "children": "replies" } }); let parsed = parse(&v, false).unwrap(); let q = parsed.query.unwrap(); assert_eq!((q.items, q.cursor), (None, None)); let s = q.summary.unwrap(); let scope = json!({ "method": "m", "response": { "thread": { "post": { "record": { "text": "hello\nworld" }, "author": { "handle": "alice.test" } }, "replies": [ { "post": {}, "replies": [{ "post": {} }, { "post": {}, "replies": [{ "post": {} }] }] }, { "$type": "app.bsky.feed.defs#notFoundPost" }, ] } } }); assert_eq!( crate::summary::query_one(&s, "app.bsky.feed.getPostThread", "api.bsky.app", &scope), "Thread: \"hello world\" by alice.test: 5 replies." ); // With nothing that resolves: the noun, the method and the host. assert_eq!( crate::summary::query_one( &s, "app.bsky.feed.getPostThread", "api.bsky.app", &json!({}) ), "Thread from app.bsky.feed.getPostThread on api.bsky.app." ); // by and children are a query mode's alone, children a path. let mut bad = posts(json!({ "component": "Badge", "props": { "value": "x" } })); bad["list"]["summary"] = json!({ "noun": ["a", "b"], "by": { "$": "handle" }, "detail": { "count": { "$": "x" }, "noun": ["c", "d"], "children": "no spaces" } }); assert_eq!( wheres(&bad), ["list.summary.by", "list.summary.detail.children"] ); } #[test] fn the_thread_view() { let v = for_method("app.bsky.feed.getPostThread").expect("the thread view"); assert_eq!( v.service.as_deref(), Some("did:web:api.bsky.app#bsky_appview") ); assert_eq!( v.origins, [ "https://cdn.bsky.app", "https://video.bsky.app", "https://video.cdn.bsky.app" ] ); assert_eq!(v.doc.to_string().matches("\"Embed\"").count(), 3); let q = v.query.as_ref().unwrap(); assert!(q.items.is_none() && q.cursor.is_none()); let s = q.summary.as_ref().unwrap(); assert!(s.by.is_some() && s.detail.as_ref().is_some_and(|d| d.2.is_some())); let layout = &v.doc["query"]["layout"]["children"]; let components: Vec<&str> = layout .as_array() .unwrap() .iter() .filter_map(|n| n["component"].as_str()) .collect(); assert_eq!(components, ["Chain", "Stack", "Tree"]); assert_eq!(layout[0]["props"]["next"], "parent"); assert_eq!(layout[2]["props"]["children"], "replies"); assert_eq!(layout[2]["props"]["depth"], 6); // Every Bluesky card opens its post's thread. let thread = json!({ "query": "app.bsky.feed.getPostThread", "params": { "uri": { "$": "post.uri" } } }); assert_eq!(layout[0]["props"]["open"], thread); assert_eq!(layout[2]["props"]["open"], thread); for m in [ "app.bsky.feed.getFeed", "app.bsky.feed.getAuthorFeed", "app.bsky.feed.getTimeline", ] { let feed = for_method(m).unwrap(); assert_eq!(feed.doc["query"]["layout"]["props"]["open"], thread, "{m}"); } // A post opened by its at-uri is its thread. let (view, post) = route("app.bsky.feed.post").unwrap(); assert_eq!(view.method.as_deref(), Some("app.bsky.feed.getPostThread")); assert_eq!( Value::Object( post.params_for(&json!({ "uri": "at://did:plc:a/app.bsky.feed.post/1" })) .unwrap() ), json!({ "uri": "at://did:plc:a/app.bsky.feed.post/1" }) ); } #[test] fn interactions_are_trusted_views_alone() { let reporting = || { let mut v = feed_view(); v["interactions"] = json!({ "feed": { "$": "params.feed" }, "item": "post.uri", "feedContext": "feedContext", "reqId": "reqId" }); v["query"]["layout"]["children"][0] = json!({ "component": "Action", "props": { "label": "More like this", "icon": "plus", "interaction": { "event": "app.bsky.feed.defs#requestMore" } } }); v }; // Bundled: taken, the paths checked. let v = parse(&reporting(), true).unwrap(); let i = v.interactions.unwrap(); assert_eq!( (i.item, i.feed_context, i.req_id), ( Path::parse("post.uri").unwrap(), Path::parse("feedContext"), Path::parse("reqId") ) ); // Inline: both refused. assert_eq!( wheres(&reporting()), [ "query.layout.children[0].props.interaction", "view.interactions" ] ); // Even bundled, an interaction needs the view's interactions, an // event of the two asked for, and no create beside it. let mut lone = reporting(); lone.as_object_mut().unwrap().remove("interactions"); assert_eq!(problems(&lone, true).len(), 1); assert!(problems(&lone, true)[0].starts_with("view: an Action with interaction")); let mut seen = reporting(); seen["query"]["layout"]["children"][0]["props"]["interaction"]["event"] = json!("app.bsky.feed.defs#interactionSeen"); let trusted = |v: &Value| -> Vec { problems(v, true) .iter() .map(|p| p.split_once(": ").unwrap().0.to_string()) .collect() }; assert_eq!( trusted(&seen), ["query.layout.children[0].props.interaction.event"] ); let mut both = reporting(); both["query"]["layout"]["children"][0]["props"]["delete"] = json!({ "uri": { "$": "post.uri" } }); assert_eq!(trusted(&both), ["query.layout.children[0].props"]); // The interactions' own shape: feed a binding, item a path; for a // method view with items. let mut bad = reporting(); bad["interactions"] = json!({ "feed": "at://x", "item": "a b", "other": 1 }); assert_eq!( trusted(&bad), [ "view.interactions", "view.interactions.feed", "view.interactions.item" ] ); let mut thread = reporting(); thread["query"].as_object_mut().unwrap().remove("items"); thread["query"].as_object_mut().unwrap().remove("cursor"); assert_eq!(trusted(&thread), ["view.interactions"]); // The bundled feed reports, and asks for more and less. let feed = for_method("app.bsky.feed.getFeed").unwrap(); assert!(feed.interactions.is_some()); let text = feed.doc.to_string(); for e in ["requestMore", "requestLess"] { assert!(text.contains(&format!("app.bsky.feed.defs#{e}")), "{e}"); } } #[test] fn the_feed_generators_view() { let v = for_method("app.bsky.feed.getFeedGenerators").expect("the feed generators view"); assert_eq!( v.service.as_deref(), Some("did:web:api.bsky.app#bsky_appview") ); assert_eq!(v.origins, ["https://cdn.bsky.app"]); assert_eq!(v.query.as_ref().unwrap().items, Path::parse("feeds")); // A feed opens as its posts. assert_eq!( v.doc["query"]["layout"]["props"]["open"], json!({ "query": "app.bsky.feed.getFeed", "params": { "feed": { "$": "uri" } } }) ); } #[test] fn embeds_checked() { let embed = |props: Value| posts(json!({ "component": "Embed", "props": props })); for ok in [ json!({ "value": { "$": "value.embed" } }), json!({ "value": { "$": "value.embed" }, "did": { "$": "did" }, "uri": { "$": "uri" }, "depth": 0 }), json!({ "value": { "$": "value.embed" }, "depth": 1 }), ] { assert_eq!( problems(&embed(ok.clone()), false), Vec::::new(), "{ok}" ); } let at = "list.layout"; for (props, want) in [ (json!({}), format!("{at}.props.value")), ( json!({ "value": { "$type": "app.bsky.embed.images" } }), format!("{at}.props.value"), ), ( json!({ "value": { "$": "x" }, "depth": 2 }), format!("{at}.props.depth"), ), ( json!({ "value": { "$": "x" }, "depth": { "$": "d" } }), format!("{at}.props.depth"), ), ( json!({ "value": { "$": "x" }, "did": "did:plc:x" }), format!("{at}.props.did"), ), ( json!({ "value": { "$": "x" }, "uri": "at://did:plc:x/a.b.c/1" }), format!("{at}.props.uri"), ), ] { assert_eq!(wheres(&embed(props.clone())), [want], "{props}"); } let mut kids = posts( json!({ "component": "Embed", "props": { "value": { "$": "x" } }, "children": [{ "component": "Badge", "props": { "value": "x" } }] }), ); assert_eq!(wheres(&kids), [format!("{at}.children")]); kids["list"]["layout"] .as_object_mut() .unwrap() .remove("children"); assert!(problems(&kids, false).is_empty()); } /// A query view of `a.b.getThing` joining `join`. fn joining(join: Value) -> Value { json!({ "method": "a.b.getThing", "service": "did:web:x.example#svc", "description": "d", "query": { "join": join, "layout": { "component": "Badge", "props": { "value": "x" } } } }) } fn query_join(n: usize) -> Value { json!({ "as": format!("q{n}"), "query": "a.b.getOther", "params": { "actor": { "$": "response.did" } } }) } #[test] fn query_joins_parsed() { let doc = joining(json!([ { "as": "posts", "query": "app.bsky.feed.getAuthorFeed", "params": { "actor": { "$": ["response.did", "params.actor"] }, "filter": "posts_no_replies", "limit": 5, "tags": ["a", "b"], "me": { "$root": "params.actor" } }, "say": { "noun": ["recent post", "recent posts"], "count": "feed" } }, { "as": "feeds", "query": "app.bsky.feed.getActorFeeds", "service": "did:web:other.example#svc" } ])); let v = parse(&doc, false).unwrap(); let q = &v.query.as_ref().unwrap().queries; assert_eq!(q.len(), 2); assert_eq!( (q[0].name.as_str(), q[0].at.as_str(), q[0].params.len()), ("posts", "query.join[0]", 5) ); assert_eq!(q[0].params[1].1, Param::Literal(json!("posts_no_replies"))); assert_eq!(q[0].say.as_ref().unwrap().count, Path::parse("feed")); // A query view's joins call its own service, unless they name one. assert_eq!( q[0].service_or(v.service.as_deref()).as_deref(), Some("did:web:x.example#svc") ); assert_eq!( q[1].service_or(v.service.as_deref()).as_deref(), Some("did:web:other.example#svc") ); assert_eq!(v.query_joins().count(), 2); } /// At most six a mode, and record joins' limit across both; untrusted /// views alike. #[test] fn query_joins_capped() { let six: Vec = (0..6).map(query_join).collect(); for trusted in [false, true] { assert!(parse(&joining(json!(six)), trusted).is_ok()); let seven: Vec = (0..7).map(query_join).collect(); assert_eq!( problems(&joining(json!(seven)), trusted), ["query.join: 7 query joins, more than 6"] ); } // In a records mode, beside record joins: eight joins of both. let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); let mut join: Vec = (0..4) .map(|n| json!({ "as": format!("q{n}"), "query": "app.bsky.feed.getPostThread", "params": { "uri": { "$": "records.0.uri" } } })) .collect(); join.extend((0..5).map(|n| json!({ "as": format!("r{n}"), "ref": "value.x" }))); doc["list"]["join"] = json!(join); assert_eq!(problems(&doc, false), ["list.join: 9 joins, more than 8"]); } /// Depth 1: a query join sits at a mode's top level, never in a record /// join, and joins nothing further; a query view joins queries alone. #[test] fn query_joins_never_nest() { let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); doc["list"]["join"] = json!([{ "as": "a", "ref": "value.x", "join": [{ "as": "q", "query": "app.bsky.feed.getPostThread", "params": { "uri": { "$": "uri" } } }] }]); assert_eq!( wheres(&doc), ["list.join[0].join[0]"], "{:?}", problems(&doc, false) ); let mut deeper = query_join(0); deeper["join"] = json!([query_join(1)]); assert_eq!( problems(&joining(json!([deeper])), false), ["query.join[0]: unknown key \"join\""] ); assert_eq!( wheres(&joining(json!([{ "as": "r", "ref": "response.uri" }]))), ["query.join[0]"] ); } /// A query view and each of its query joins may say `fresh`: asked /// each time, never an answer HUD keeps. Kept, unless they say so. #[test] fn query_views_and_joins_say_fresh() { let mut doc = joining(json!([ { "as": "a", "query": "a.b.getOther", "fresh": true }, { "as": "b", "query": "a.b.getOther" } ])); let q = parse(&doc, false).unwrap().query.unwrap(); assert_eq!( (q.fresh, q.queries[0].fresh, q.queries[1].fresh), (false, true, false) ); doc["query"]["fresh"] = json!(true); assert!(parse(&doc, false).unwrap().query.unwrap().fresh); doc["query"]["fresh"] = json!("yes"); doc["query"]["join"][1]["fresh"] = json!(1); assert_eq!( problems(&doc, false), [ "query.join[1].fresh: must be true or false", "query.fresh: must be true or false" ] ); } /// A query join's `when` is a path in the main scope, `at` too. #[test] fn query_joins_when_reads_the_main_scope() { let ok = joining( json!([{ "as": "a", "query": "a.b.getOther", "when": "response.mine", "unless": "me" }, { "as": "b", "query": "a.b.getOther", "params": { "x": { "$": "at" } }, "when": "at" }]), ); assert!(parse(&ok, false).is_ok(), "{:?}", problems(&ok, false)); let bad = joining( json!([{ "as": "a", "query": "a.b.getOther", "when": "items.0", "unless": "items.1" }]), ); assert_eq!( problems(&bad, false), [ "query.join[0].when: a path in the main scope: from method, service, params, response, me, at", "query.join[0].unless: a path in the main scope: from method, service, params, response, me, at" ] ); } /// A query join's params read the main scope, never another join's /// result; the names are its own beside the record joins'. #[test] fn query_joins_read_the_main_scope() { let bad = joining(json!([ { "as": "a", "query": "a.b.getOther", "params": { "x": { "$": "joins.readme.text" } } }, { "as": "b", "query": "a.b.getOther", "params": { "x": { "$root": "joins.a.uri" } } }, { "as": "c", "query": "a.b.getOther", "params": { "x": { "$": "items.0.uri" } } }, { "as": "d", "query": "a.b.getOther", "params": { "x": { "$": ["response.uri", "joins.a"] } } }, { "as": "e", "query": "a.b.getOther", "params": { "x": { "y": 1 }, "z": [[1]], "w": { "$atUri": ["a", "b", "c"] } } }, { "as": "e", "query": "nope", "service": "nope", "say": { "noun": ["x", "xs"] } } ])); let found = problems(&bad, false); assert_eq!( found .iter() .map(|p| p.split_once(": ").unwrap().0) .collect::>(), [ "query.join[0].params.x", "query.join[1].params.x", "query.join[2].params.x", "query.join[3].params.x", "query.join[4].params.x", "query.join[4].params.z", "query.join[4].params.w", "query.join[5].query", "query.join[5].service", "query.join[5].say", ], "{found:?}" ); assert!(found[0].ends_with("never a join's result"), "{}", found[0]); assert!(found[1].ends_with("never a join's result"), "{}", found[1]); assert!(found[2].ends_with("a path from method, service, params, response, me, at")); assert!(found[3].ends_with("never a join's result"), "{}", found[3]); // A list reads its page, a record its record too; never a record // join's result. let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); doc["record"] = doc["list"].clone(); let thread = |at: &str| { json!([{ "as": "pub", "ref": "value.reply.root" }, { "as": "t", "query": "app.bsky.feed.getPostThread", "params": { "uri": { "$": at } } }]) }; doc["list"]["join"] = thread("records.0.uri"); doc["record"]["join"] = thread("value.reply.root.uri"); assert!(parse(&doc, false).is_ok(), "{:?}", problems(&doc, false)); doc["list"]["join"] = thread("value.reply.root.uri"); doc["record"]["join"] = thread("records.0.joins.pub.uri"); assert_eq!( wheres(&doc), ["list.join[1].params.uri", "record.join[1].params.uri"] ); doc["record"]["join"] = json!([{ "as": "t", "ref": "value.x" }, { "as": "t", "query": "app.bsky.feed.getPostThread", "params": { "uri": { "$": "uri" } } }]); doc["list"]["join"] = json!([]); assert_eq!( problems(&doc, false), ["record.join[1].as: \"t\" is used twice"] ); } /// In a records mode, a query join's service is its own, or its /// method's bundled view's. #[test] fn query_joins_in_records_modes_name_a_service() { let mut doc = posts(json!({ "component": "Badge", "props": { "value": "x" } })); doc["list"]["join"] = json!([ { "as": "a", "query": "app.bsky.feed.getPostThread", "params": { "uri": { "$": "records.0.uri" } } }, { "as": "b", "query": "a.b.getOther", "service": "did:web:x.example#svc" }, { "as": "c", "query": "a.b.getOther" } ]); assert_eq!( problems(&doc, false), ["list.join[2].service: required: no bundled view of a.b.getOther names its service"] ); doc["list"]["join"].as_array_mut().unwrap().pop(); let v = parse(&doc, false).unwrap(); let q = &v.list.as_ref().unwrap().queries; assert_eq!( q[0].service_or(None).as_deref(), Some("did:web:api.bsky.app#bsky_appview") ); } #[test] fn says_checked() { for (say, ok) in [ (json!({ "noun": ["post", "posts"], "count": "feed" }), true), ( json!({ "noun": ["README", "READMEs"], "label": "text" }), true, ), (json!({ "noun": ["post", "posts"] }), false), ( json!({ "noun": ["post", "posts"], "count": "feed", "label": "text" }), false, ), (json!({ "noun": ["post"], "count": "feed" }), false), (json!({ "noun": ["post", "posts"], "count": "a b" }), false), (json!("5 posts"), false), ] { let mut j = query_join(0); j["say"] = say.clone(); assert_eq!(parse(&joining(json!([j])), false).is_ok(), ok, "{say}"); } } /// The lexicon documents tests check query joins and routed records /// against: the bundled ones, and knotless's as it publishes them /// (testdata/knotless), its queries and records. fn lexicon_docs() -> Vec> { let knotless = [ include_str!("testdata/knotless/dev.knotless.getFile.json"), include_str!("testdata/knotless/dev.knotless.listReleases.json"), include_str!("testdata/knotless/dev.knotless.defs.json"), include_str!("testdata/knotless/dev.knotless.listActorItems.json"), include_str!("testdata/knotless/dev.knotless.getActorStars.json"), include_str!("testdata/knotless/dev.knotless.getActorRepos.json"), include_str!("testdata/knotless/dev.knotless.getFeed.json"), include_str!("testdata/knotless/dev.knotless.getRepo.json"), include_str!("testdata/knotless/dev.knotless.getThread.json"), include_str!("testdata/knotless/dev.knotless.getProfile.json"), include_str!("testdata/knotless/dev.knotless.repo.json"), include_str!("testdata/knotless/dev.knotless.check.json"), include_str!("testdata/knotless/dev.knotless.issue.json"), include_str!("testdata/knotless/dev.knotless.pull.json"), include_str!("testdata/knotless/dev.knotless.comment.json"), include_str!("testdata/knotless/dev.knotless.star.json"), include_str!("testdata/knotless/dev.knotless.profile.json"), include_str!("testdata/knotless/dev.knotless.release.json"), ]; crate::lexicon::all_bundled() .cloned() .chain( knotless .iter() .map(|t| std::sync::Arc::new(serde_json::from_str(t).unwrap())), ) .collect() } /// A query join's literal params are checked against its method's /// lexicon as the view loads: names it does not take, required ones /// left out, values it refuses; a bound one only when it runs. #[test] fn query_join_params_are_their_methods() { let docs = lexicon_docs(); let check = |j: Value| { let v = parse(&joining(json!([j])), false).unwrap(); query_join_problems(&v.query.unwrap().queries[0], &docs) }; let feed = |params: Value| json!({ "as": "a", "query": "app.bsky.feed.getAuthorFeed", "params": params }); assert_eq!( check(feed( json!({ "actor": { "$": "response.did" }, "limit": 5 }) )), Vec::::new() ); assert_eq!( check(feed( json!({ "actor": { "$": "response.did" }, "nope": 1, "limit": 500 }) )), [ "query.join[0].params.nope: not a parameter of app.bsky.feed.getAuthorFeed", "query.join[0].params.limit can not be greater than 100" ] ); assert_eq!( check(feed(json!({ "limit": 5 }))), ["query.join[0].params.actor: app.bsky.feed.getAuthorFeed requires it"] ); assert_eq!( check(json!({ "as": "a", "query": "app.bsky.feed.post" })), ["query.join[0].query: app.bsky.feed.post is a record, not a query: HUD only reads"] ); assert_eq!( check(json!({ "as": "a", "query": "com.example.unknown" })), ["query.join[0].query: no lexicon for com.example.unknown"] ); } /// Every bundled query join's params are its method's. #[test] fn bundled_query_joins_are_their_methods() { let docs = lexicon_docs(); let mut seen = Vec::new(); for b in Views::built_in().all() { for j in b.view.query_joins() { assert_eq!( query_join_problems(j, &docs), Vec::::new(), "{}", b.id ); assert!( j.service_or(b.view.service.as_deref()).is_some(), "{}", b.id ); seen.push(format!("{} {}", b.id, j.name)); } } assert_eq!( seen, [ "bsky-app profile", "bsky-app posts", "bsky-app feeds", "bsky-chat-with messages", "bsky-notifications subjects", "bsky-profile posts", "bsky-profile feeds", "knotless-app issues", "knotless-app pulls", "knotless-app stars", "knotless-app recent", "knotless-repo readme", "knotless-repo release", "standard-document replies", ] ); } /// The format of the string a routed record's binding reads, by the /// record's lexicon (`docs`): its `uri` an at-uri, its `did` a DID, its /// `cid` a CID, a path in its `value` the string's own format, through /// refs; none for what is no string with a format. fn read_format( docs: &[std::sync::Arc], collection: &str, path: &Path, ) -> Option { let (first, rest) = path.split_first()?; let known = [("uri", "at-uri"), ("did", "did"), ("cid", "cid")]; if let Some((_, format)) = known.iter().find(|(k, _)| *k == first) { return Some(format.to_string()); } let doc = |id: &str| docs.iter().find(|d| d["id"] == id).cloned(); let (mut at, mut def) = ( collection.to_string(), doc(collection)?["defs"]["main"]["record"].clone(), ); for key in rest { def = def["properties"][key].clone(); while def["type"] == "ref" { let r = def["ref"].as_str()?; let full = crate::lexicon::full_ref(&at, r); let (id, name) = full.split_once('#').unwrap_or((full.as_str(), "main")); def = doc(id)?["defs"][name].clone(); at = id.to_string(); } } (def["type"] == "string").then(|| def["format"].as_str().map(str::to_string))? } /// Every bundled routed record's params are its method's, as its /// lexicon takes them, the record's lexicon read for what each binding /// gives (an at-uri where the method takes one, a DID for an account), /// as launchers' are checked. #[test] fn routed_records_are_their_methods() { let docs = lexicon_docs(); let mut seen = 0; for b in bundled() { let Some(method) = b.view.method.as_deref() else { continue; }; for r in &b.view.records { let at = format!("{} {}", b.id, r.collection); let doc = docs.iter().find(|d| d["id"] == method); let doc = doc.ok_or(format!("no lexicon for {method}")).unwrap(); let def = crate::xrpc::query_def(method, doc).unwrap(); assert_eq!( docs.iter() .find(|d| d["id"] == r.collection.as_str()) .map(|d| d["defs"]["main"]["type"].clone()), Some(json!("record")), "{at}: no record lexicon" ); // A record whose every binding finds a string of the // format it reads, which the method takes. let mut record = json!({}); for (k, p) in &r.params { let Prop::Bound(binding) = p else { continue }; let path = &binding.0[0]; let read = read_format(&docs, &r.collection, path); let read = read.ok_or(format!("{at}: {k} reads no string")).unwrap(); let takes = def["parameters"]["properties"][k]["format"] .as_str() .unwrap_or_default(); let fits = read == takes || (read == "did" && takes == "at-identifier"); assert!(fits, "{at}: {k} reads {read}, the method takes {takes}"); let given = match read.as_str() { "did" => json!("did:plc:abcdefghijklmnopqrstuvwx"), _ => json!(format!( "at://did:plc:abcdefghijklmnopqrstuvwx/{}/3abc", r.collection )), }; let slot = path .0 .iter() .fold(&mut record, |v, key| &mut v[key.as_str()]); *slot = given; } let params = r.params_for(&record).unwrap(); let lexicons = crate::xrpc::lexicons_for(method, &docs); assert_eq!( crate::xrpc::params_problems(&lexicons, method, def, ¶ms), Vec::::new(), "{at}" ); seen += 1; } } assert_eq!(seen, 11); // A release's repo is a TID, no at-uri: nothing routes one. let repo = Path::parse("value.repo").unwrap(); assert_eq!( read_format(&docs, "dev.knotless.release", &repo).as_deref(), Some("tid") ); assert!(route("dev.knotless.release").is_none()); } fn app_page(join: Value) -> Value { json!({ "domain": "knotless.dev", "app": "knotless", "description": "Home", "service": "did:web:knotless.dev#knotless_appview", "page": { "join": join, "layout": { "component": "Text", "props": { "value": { "$": "name" } } } } }) } /// An app page view draws its domain's page, its joins reading the /// page's main scope alone; only a bundled view is one. #[test] fn app_pages_are_bundled_alone() { let feed = json!([{ "as": "recent", "query": "dev.knotless.getFeed", "params": { "actor": { "$": "actor.did" }, "limit": 5 } }]); let v = parse(&app_page(feed.clone()), true).unwrap(); assert_eq!(v.domain.as_deref(), Some("knotless.dev")); assert_eq!(v.drawn(), "knotless.dev"); assert_eq!(v.query_joins().count(), 1); assert!(v.collection.is_none() && v.method.is_none() && v.query.is_none()); // Inline, refused. let mut inline = app_page(feed); inline.as_object_mut().unwrap().remove("app"); assert_eq!( parse(&inline, false).unwrap_err(), ["view.page: only a bundled view is an app's page"] ); // A join reads the page's scope, never another's. let response = json!([{ "as": "a", "query": "dev.knotless.getFeed", "params": { "actor": { "$": "response.did" } } }]); assert!(parse(&app_page(response), true).is_err()); // A domain in place of a collection or method, never beside one. let mut both = app_page(json!([])); both["method"] = json!("dev.knotless.getFeed"); assert_eq!( parse(&both, true).unwrap_err(), ["view: an app page draws its domain, not a collection or a method"] ); let mut upper = app_page(json!([])); upper["domain"] = json!("Knotless.dev"); assert!(parse(&upper, true).is_err()); // The bundled ones: knotless's, Bluesky's; none for Bluesky Chat. assert_eq!( for_domain("knotless.dev").map(View::drawn), Some("knotless.dev") ); assert_eq!(for_domain("bsky.app").map(View::drawn), Some("bsky.app")); assert!(for_domain("bsky.chat").is_none()); } fn badge() -> Value { json!({ "component": "Badge", "props": { "value": "x" } }) } /// A collection view drawing its list with `layout`, and `extra` keys. fn listing(layout: Value, extra: Value) -> Value { let mut doc = json!({ "collection": "a.b.c", "description": "d", "list": { "layout": layout } }); doc.as_object_mut() .unwrap() .extend(extra.as_object().unwrap().clone()); doc } /// A method view drawing its query with `query`, and `extra` keys. fn querying(query: Value, extra: Value) -> Value { let mut doc = json!({ "method": "a.b.c", "service": "did:web:x.example#svc", "description": "d", "query": query }); doc.as_object_mut() .unwrap() .extend(extra.as_object().unwrap().clone()); doc } /// What a bundled view alone names, each where only some views may. #[test] fn trusted_keys_belong_to_their_views() { let mut no_service = app_page(json!([])); no_service.as_object_mut().unwrap().remove("service"); let mut no_domain = app_page(json!([])); no_domain.as_object_mut().unwrap().remove("domain"); let mut no_page = app_page(json!([])); no_page.as_object_mut().unwrap().remove("page"); let mut profiled = app_page(json!([])); profiled["profile"] = json!({ "title": "Home" }); let mut tagged = app_page(json!([])); tagged["page"]["tag"] = json!("x"); let mut paged = app_page(json!([])); paged["page"]["pages"] = json!("recent"); let method = |extra: Value| querying(json!({ "layout": badge() }), extra); let items = |extra: Value| querying(json!({ "items": "items", "layout": badge() }), extra); for (doc, want) in [ ( listing(badge(), json!({ "signIn": "Sign in" })), "view.signIn: only a method view's query asks for sign-in", ), ( listing(badge(), json!({ "launch": [{ "title": "All" }] })), "view.launch: only a method view's query is launched", ), ( no_service, "view.service: an app page names the service its joins call", ), ( profiled, "view.profile: an app page adds no section to profiles", ), (no_domain, "view.domain: an app page names its app's domain"), ( no_page, "view.page: an app page needs its page: { join?, layout, tag?, pages? }", ), (tagged, "page.tag: must be an object"), (paged, "page.pages: the as of one of the page's joins"), ( querying(json!({ "latest": "uri", "layout": badge() }), json!({})), "query.latest: the path to each item's key: name query.items too, or no latest", ), ( method(json!({ "profile": { "title": "Things", "head": "yes" } })), "view.profile.head: must be true or false", ), ( method(json!({ "profile": { "title": "Things", "params": { "a": null } } })), "view.profile.params.a: must be a string, number, boolean or {\"$actor\": \"did\"}, or an array of those", ), ( method(json!({ "profile": { "title": "Things", "params": [1] } })), "view.profile.params: must be an object of at most 8 params", ), ( method(json!({ "launch": [5] })), "view.launch[0]: must be an object", ), ( method(json!({ "launch": [{ "title": "Mine", "params": [1] }] })), "view.launch[0].params: must be an object of params", ), ( method(json!({ "records": [5] })), "view.records[0]: must be an object", ), ( method( json!({ "records": [{ "collection": "nope", "params": { "uri": { "$": "uri" } } }] }), ), "view.records[0].collection: must be an NSID", ), ( items(json!({ "interactions": { "item": "uri" } })), "view.interactions.feed: required", ), ( items(json!({ "interactions": { "feed": { "$": "params.feed" } } })), "view.interactions.item: required: the path to an item's uri", ), ( listing(badge(), json!({ "origins": "https://cdn.example" })), "view.origins: must be 1 to 8 origins, as https://host[:port]", ), ( listing( badge(), json!({ "origins": ["https://cdn.example"], "thumbnails": [5] }), ), "view.thumbnails[0]: must be an object", ), ] { assert_eq!(problems(&doc, true), [want], "{doc}"); } // A page whose joins are left out joins nothing. let mut plain = app_page(json!([])); plain["page"].as_object_mut().unwrap().remove("join"); assert_eq!(parse(&plain, true).unwrap().query_joins().count(), 0); } /// A view's measure is one of three widths, any of them allowed, and a /// split is two sides. #[test] fn measure_and_split_checked() { for m in MEASURES { assert_eq!( problems(&listing(badge(), json!({ "measure": m })), true), Vec::::new() ); } assert_eq!( problems(&listing(badge(), json!({ "measure": "huge" })), false), ["view.measure: must be one of content, wide, full"] ); let when = |cases: Value| listing(badge(), json!({ "measureWhen": cases })); assert_eq!( problems( &when(json!([{ "when": "params.camera", "measure": "wide" }])), true ), Vec::::new() ); for bad in [ json!([]), json!({}), json!(vec![json!({ "when": "a", "measure": "wide" }); 9]), ] { assert_eq!( problems(&when(bad.clone()), true), ["view.measureWhen: must be 1 to 8 cases"], "{bad}" ); } assert_eq!( problems( &when(json!([5, { "when": "a..b", "measure": "huge", "x": 1 }, {}])), true ), [ "view.measureWhen[0]: must be an object", "view.measureWhen[1]: unknown key \"x\"", "view.measureWhen[1].when: unknown path syntax \"a..b\"", "view.measureWhen[1].measure: must be one of content, wide, full", "view.measureWhen[2].when: must be a path, keys joined by dots", "view.measureWhen[2].measure: must be one of content, wide, full", ] ); let split = |k: Vec| json!({ "component": "Split", "props": { "side": "start", "aside": "sm" }, "children": k }); assert_eq!( problems(&listing(split(vec![badge(), badge()]), json!({})), false), Vec::::new() ); for k in [vec![badge()], vec![badge(), badge(), badge()]] { assert_eq!( problems(&listing(split(k), json!({})), false), ["list.layout.children: Split takes two children"] ); } assert_eq!( problems(&listing(json!({ "component": "Split" }), json!({})), false), ["list.layout.children: Split takes two children"] ); } /// Each malformed join, query join, layout node, open and template /// named where it is. #[test] fn malformed_parts_named() { let list = |layout: Value| listing(layout, json!({})); let joined = |join: Value| { listing( badge(), json!({ "list": { "join": join, "layout": badge() } }), ) }; let one_param = |params: Value| { joining(json!([{ "as": "o", "query": "a.b.getOther", "params": params }])) }; let backlinks = |order: Value| { joined(json!([{ "as": "items", "orderBy": order, "backlinks": { "collection": "a.b.item", "path": "value.of" } }])) }; let opening = |params: Value| { list( json!({ "component": "Link", "props": { "open": { "query": "a.b.getThing", "params": params } } }), ) }; let action = |props: Value| list(json!({ "component": "Action", "props": props })); let creating = |record: Value| { action( json!({ "label": "Go", "create": { "collection": "a.b.like", "record": record } }), ) }; let mut deep = json!(1); for _ in 0..12 { deep = json!({ "a": deep }); } let nine: Map = (0..9).map(|i| (format!("p{i}"), json!(i))).collect(); for (doc, want) in [ ( joined(json!({})), vec!["list.join: must be an array of joins"], ), ( joining(json!([{ "as": "a.b", "query": "a.b.getOther" }])), vec!["query.join[0].as: must be a key, [A-Za-z0-9_$]+"], ), ( one_param(Value::Object(nine)), vec!["query.join[0].params: 9 params, more than 8"], ), ( one_param(json!({ "a-b": 1 })), vec!["query.join[0].params.a-b: a param's name is [A-Za-z0-9_]+"], ), ( one_param(json!([1])), vec!["query.join[0].params: must be an object of params"], ), ( one_param(json!({ "x": { "$each": "response.items" } })), vec![ "query.join[0].params.x: $each is [array, path] alone: the path within each element of the array", ], ), ( one_param(json!({ "x": { "$root": "response.did", "y": 1 } })), vec!["query.join[0].params.x: a root binding has the key \"$root\" alone"], ), ( backlinks(json!("value.at")), vec!["list.join[0].orderBy: must be at most 4 paths"], ), ( backlinks(json!([5])), vec!["list.join[0].orderBy[0]: must be a path, keys joined by dots"], ), ( joined(json!([{ "as": "n", "counts": { "likes": 5 } }])), vec!["list.join[0].counts.likes: must be an object"], ), (list(json!(5)), vec!["list.layout: must be an object"]), ( list(json!({ "props": {} })), vec!["list.layout.component: required: a component's name"], ), ( list(json!({ "component": "Badge", "props": [1] })), vec![ "list.layout.props: must be an object", "list.layout.props.value: Badge requires it", ], ), ( list(json!({ "component": "Stack", "children": {} })), vec!["list.layout.children: must be an array of nodes"], ), ( list(json!({ "component": "Badge", "props": { "value": { "$": [] } } })), vec!["list.layout.props.value: a binding tries 1 to 4 paths"], ), ( opening(json!({ "uri": { "$atUri": ["a", "b", "c"], "x": 1 } })), vec![ "list.layout.props.open.params.uri: an at-uri to build has the key \"$atUri\" alone", ], ), ( opening(json!({ "uris": vec!["x"; 51] })), vec!["list.layout.props.open.params.uris: more than 50 elements"], ), ( action(json!({ "label": "Go", "create": { "collection": "a.b.like" } })), vec!["list.layout.props.create.record: required"], ), ( action(json!({ "label": "Go", "delete": {} })), vec!["list.layout.props.delete.uri: required"], ), ( creating(deep), vec![ "list.layout.props.create.record.a.a.a.a.a.a.a.a.a.a.a.a: a template nests at most 12 deep", ], ), ( creating(json!({ "x": { "$first": [1] } })), vec![ "list.layout.props.create.record.x: {\"$first\": [values]} has the key $first alone, 2 to 4 values", ], ), ] { assert_eq!(problems(&doc, false), want, "{doc}"); } } /// A bundled view's procedure calls: an input left out is an empty /// one; one too big or no object refused; a conversation's text may go /// anywhere in its input, alone. #[test] fn call_inputs_checked() { let action = |call: Value| { querying( json!({ "layout": { "component": "Action", "props": { "label": "Go", "call": call } } }), json!({}), ) }; let service = "did:web:x.example#svc"; let v = parse( &action(json!({ "method": "a.b.doIt", "service": service })), true, ) .unwrap(); assert_eq!( v.calls, [Call { method: "a.b.doIt".into(), service: service.into(), input: json!({}), deletes: false, }] ); assert_eq!( problems( &action(json!({ "method": "a.b.doIt", "service": service, "input": { "big": "x".repeat(5000) } })), true ), ["query.layout.props.call.input: 5010 bytes, more than the 4096 an input may take"] ); assert_eq!( problems( &action(json!({ "method": "a.b.doIt", "service": service, "input": { "$": "x" } })), true ), ["query.layout.props.call.input: must be an object, the procedure's input"] ); let messages = |input: Value| { querying( json!({ "items": "messages", "layout": { "component": "Messages", "props": { "items": { "$": "items" }, "from": "sender.did", "send": { "method": "a.b.send", "service": service, "input": input } } } }), json!({}), ) }; let parts = parse(&messages(json!({ "parts": [{ "$text": true }] })), true).unwrap(); assert_eq!( parts.calls[0].input, json!({ "parts": [{ "$text": true }] }) ); assert_eq!( problems(&messages(json!({ "t": { "$text": true, "x": 1 } })), true), ["query.layout.props.send.input.t: {\"$text\": true} has the key $text alone, true"] ); } /// A literal refused by its prop's type says what the type takes. #[test] fn literals_checked_by_their_types() { let node = |component: &str, props: Value| { listing(json!({ "component": component, "props": props }), json!({})) }; let at = |prop: &str, why: &str| format!("list.layout.props.{prop}: {why}"); for (doc, want) in [ ( node("Text", json!({ "value": "x", "clamp": 0 })), at("clamp", "must be an integer from 1 to 100"), ), ( node("Stack", json!({ "when": 5 })), at( "when", "must be a path, keys joined by dots, or {\"$kit\": a semver requirement, as \">=0.3.0\"}", ), ), ( node("Stack", json!({ "mine": 5 })), at("mine", "must be a path, keys joined by dots"), ), ( node( "Covers", json!({ "items": { "$": "records" }, "src": { "$": "value.src" }, "countNoun": ["a"] }), ), at("countNoun", "must be [singular, plural]"), ), ( node( "Image", json!({ "src": { "$": "value.src" }, "aspectRatio": { "width": 1, "height": 0 } }), ), at("aspectRatio", "must be {width, height}, positive integers"), ), ( node("ContentWarning", json!({ "labels": [1] })), at("labels", "must be labels or a binding"), ), ( node("Action", json!({ "label": "Go", "create": "x" })), at("create", "must be { collection, record }"), ), ( node("Action", json!({ "label": "Go", "delete": "x" })), at("delete", "must be { uri: binding }"), ), ( node("Action", json!({ "label": "Go", "interaction": "x" })), at("interaction", "must be { event }"), ), ( node("Action", json!({ "label": "Go", "call": "x" })), at("call", "must be { method, service, input? }"), ), ( node("Action", json!({ "label": "Go", "deletes": "x" })), at( "deletes", "must be { method, service, params?, repeat?, collections?, then? }", ), ), ( node("List", json!({ "items": { "$": "records" }, "where": 5 })), at( "where", "must be an object of 1 to 4 paths within each element to paths in the scope or {\"$is\": a string, number or boolean}, either in {\"$not\": …}", ), ), ( node( "State", json!({ "value": "x", "roles": { "open": "nope" } }), ), at( "roles", &format!( "must be an object of 1 to 32 values to one of {}, each at most 64 characters", catalog::ROLES.join(", ") ), ), ), ( node("Diff", json!({ "path": "x", "kinds": { "x": "y" } })), at( "kinds", "must be an object of 1 to 32 of added, removed, hunk, note to words, each at most 64 characters", ), ), ] { assert_eq!(problems(&doc, false), [want], "{doc}"); } // A composer's parts each as their types take them. let compose = problems( &node( "Compose", json!({ "label": "Go", "fields": "x", "writes": "x", "given": [1] }), ), false, ); for want in [ at("fields", "must be an array of fields"), at("writes", "must be an array of writes"), at("given", "must be an object of at most 8 values"), ] { assert!(compose.contains(&want), "{compose:?}"); } // Any JSON is any. assert!( parse( &node("RichText", json!({ "value": "x", "facets": [1] })), false ) .is_ok() ); } /// A trusted view that does not load names its file in each of its /// problems, every one of them. #[test] fn a_broken_bundled_view_names_its_file() { let load = |id: &str, text: &str| load(format!("views/{id}.json"), id, text, &[]); let err = load("broken", "{").err().unwrap(); assert!(err[0].starts_with("views/broken.json: EOF"), "{err:?}"); let mut doc = listing(badge(), json!({})); for i in 0..25 { doc[format!("k{i}")] = json!(1); } let err = load("broken", &doc.to_string()).err().unwrap(); assert_eq!(err.len(), 25); assert_eq!(err[0], "views/broken.json: view: unknown key \"k0\""); assert_eq!(err[24], "views/broken.json: view: unknown key \"k24\""); let fine = listing(badge(), json!({})).to_string(); let (fine, warnings) = load("fine", &fine).unwrap(); assert_eq!(fine.id, "fine"); assert!(warnings.is_empty()); } /// A key written `?key` is optional: read as `key` where this HUD knows /// it, and left out, with a warning naming the problem, where it finds /// one in it (a prop, a value or a key it doesn't know), at any depth. /// What dropping it leaves wrong, or a problem anywhere else, still /// refuses the view, as does a key given both ways. #[test] fn an_optional_key_is_left_out_where_it_falls_short() { let load = |doc: Value| load("views/v.json".into(), "v", &doc.to_string(), &[]); let stack = |props: Value| { json!({ "component": "Stack", "props": props, "children": [{ "component": "Badge", "props": { "value": "x", "?shiny": true } }] }) }; // Known: read as its key, with no warning. let (b, warnings) = load(listing( stack(json!({ "?gap": "sm" })), json!({ "?$type": TYPE }), )) .unwrap(); assert_eq!( b.view.doc["list"]["layout"]["props"], json!({ "gap": "sm" }) ); assert_eq!(b.view.doc["$type"], TYPE); assert_eq!( warnings, [ "views/v.json: list.layout.children[0].props.shiny: Badge has no prop \"shiny\" (?shiny is optional, so it's left out)" ] ); assert_eq!( b.view.doc["list"]["layout"]["children"][0]["props"], json!({ "value": "x" }) ); // A value this HUD doesn't know, and a key at the top it doesn't. let (b, warnings) = load(listing( stack(json!({ "?gap": "huge" })), json!({ "?newer": { "a": 1 } }), )) .unwrap(); assert_eq!(b.view.doc["list"]["layout"]["props"], json!({})); assert!(b.view.doc.get("newer").is_none()); assert_eq!( warnings, [ "views/v.json: view: unknown key \"newer\" (?newer is optional, so it's left out)", "views/v.json: list.layout.props.gap: must be one of none, xs, sm, md, lg (?gap is optional, so it's left out)", "views/v.json: list.layout.children[0].props.shiny: Badge has no prop \"shiny\" (?shiny is optional, so it's left out)", ] ); // Left out, a required key is missing: refused. let mut doc = listing(badge(), json!({ "?description": "a\nb" })); doc.as_object_mut().unwrap().remove("description"); assert_eq!( load(doc).unwrap_err(), ["views/v.json: view.description: must be one line of text, 200 characters at most"] ); // A problem elsewhere refuses it, the optional one left out. assert_eq!( load(listing(stack(json!({ "nope": 1 })), json!({}))).unwrap_err(), ["views/v.json: list.layout.props.nope: Stack has no prop \"nope\""] ); // Both ways at once. assert_eq!( load(listing( stack(json!({ "gap": "sm", "?gap": "md" })), json!({}) )) .unwrap_err(), ["views/v.json: list.layout.props: \"?gap\" and \"gap\" both given"] ); assert_eq!( load(listing(badge(), json!({ "?description": "e" }))).unwrap_err(), ["views/v.json: view: \"?description\" and \"description\" both given"] ); } /// The trusted views are read once: the operator's are added before. #[test] fn the_operators_views_come_before_any_are_read() { assert!(!bundled().is_empty()); assert_eq!( install(Views::built_in()).unwrap_err(), "the trusted views were read before the operator's were added" ); } /// Two trusted views drawing one thing, or a collection as their /// query, are named by their files. #[test] fn conflicts_name_their_files() { let thread = for_method("app.bsky.feed.getPostThread").unwrap(); let as_file = |id: &str| Bundled { id: id.into(), file: format!("{id}.json"), view: thread.clone(), }; assert_eq!(conflicts(&[as_file("a")]), Vec::::new()); assert_eq!( conflicts(&[as_file("a"), as_file("b")]), [ "b.json: a.json draws app.bsky.feed.getPostThread too", "b.json: a.json draws app.bsky.feed.post as its query too", "b.json: a.json draws app.bsky.feed.repost as its query too" ] ); // Two views deleting by one query, saying otherwise beyond it. let plan = Plan { method: "a.b.getPlan".into(), service: "did:web:x.example#svc".into(), repeat: true, collections: Vec::new(), then: None, }; let planning = |id: &str, repeat: bool| { let mut b = as_file(id); b.view.records.clear(); b.view.method = Some(format!("a.b.{id}")); b.view.plans = vec![Plan { repeat, ..plan.clone() }]; b }; let same = [planning("a", true), planning("b", true)]; assert_eq!(conflicts(&same), Vec::::new()); assert_eq!( conflicts(&[planning("a", true), planning("b", false)]), ["b.json: deletes a.b.getPlan otherwise than before"] ); } /// What a trusted view is held to beyond its document, as the bundled /// ones are by the tests and the operator's as HUD starts. #[test] fn trusted_views_checked() { let docs = lexicon_docs(); let section = |method: &str, params: Value| { let doc = json!({ "method": method, "service": "did:web:x.example#svc", "description": "d", "profile": { "title": "T", "params": params }, "query": { "layout": badge() } }); trusted_problems(&parse(&doc, true).unwrap(), &docs, views()) }; assert_eq!( section("a.b.c", json!({})), ["view.profile: no lexicon for a.b.c"] ); assert_eq!( section("social.grain.graph.muteActor", json!({})), [ "view.profile: social.grain.graph.muteActor is a procedure, not a query: HUD only reads" ] ); assert_eq!( section( "app.bsky.feed.getAuthorFeed", json!({ "actor": { "$actor": "did" }, "nope": 1 }) ), ["view.profile: params.nope is not a parameter of app.bsky.feed.getAuthorFeed"] ); // A records view's join names its service, or its method's view // does: checked against the views as they are used. let join = listing( badge(), json!({ "list": { "layout": badge(), "join": [ { "as": "x", "query": "a.b.getX", "params": {} }] } }), ); let services = [("a.b.getX".to_string(), "did:web:x.example#svc".to_string())]; let v = parse_in(&join, true, &services).unwrap(); assert_eq!( trusted_problems(&v, &docs, views()), [ "view: a trusted collection view draws a page of records (list) and one (record)", "list.join[0].query: no lexicon for a.b.getX", "list.join[0].service: no view of a.b.getX names its service" ] ); } /// A records view's query join that reads its page's records runs on /// every page; one of literals alone, on the first. #[test] fn query_joins_reading_records_run_per_page() { let doc = listing( badge(), json!({ "list": { "layout": badge(), "join": [ { "as": "top", "query": "a.b.getTop", "service": "did:web:x.example#svc", "params": { "limit": 5 } }, { "as": "each", "query": "a.b.getMany", "service": "did:web:x.example#svc", "params": { "uris": { "$each": ["records", "uri"] } } }] } }), ); let v = parse(&doc, false).unwrap(); let per_page: Vec = v.query_joins().map(QueryJoin::per_page).collect(); assert_eq!(per_page, [false, true]); } /// A query join of a method taking no params at all, given none, is /// fine. #[test] fn a_query_join_of_a_method_without_params() { let doc = joining(json!([{ "as": "o", "query": "a.b.getOther" }])); let v = parse(&doc, false).unwrap(); let docs = [std::sync::Arc::new( json!({ "lexicon": 1, "id": "a.b.getOther", "defs": { "main": { "type": "query", "output": { "encoding": "application/json", "schema": { "type": "object", "properties": {} } } } } }), )]; let j = v.query_joins().next().unwrap(); assert_eq!(query_join_problems(j, &docs), Vec::::new()); } /// A call's procedure must have a lexicon, be a procedure, and take /// the template's fields, its input a ref to a def of its own. #[test] fn call_problems_read_the_procedure() { let call = |method: &str, input: Value| Call { method: method.into(), service: "did:web:x.example#svc".into(), input, deletes: false, }; let docs = [ std::sync::Arc::new(json!({ "lexicon": 1, "id": "a.b.getIt", "defs": { "main": { "type": "query" } } })), std::sync::Arc::new(json!({ "lexicon": 1, "id": "a.b.doIt", "defs": { "main": { "type": "procedure", "input": { "encoding": "application/json", "schema": { "type": "ref", "ref": "#input" } } }, "input": { "type": "object", "required": ["text"], "properties": { "text": { "type": "string" } } } } })), ]; assert_eq!( call_problems(&call("a.b.none", json!({})), &docs), ["a.b.none: no lexicon"] ); assert_eq!( call_problems(&call("a.b.getIt", json!({})), &docs), ["a.b.getIt is a query, not a procedure"] ); assert_eq!( call_problems(&call("a.b.doIt", json!({ "nope": 1 })), &docs), [ "a.b.doIt: input.nope is not an input of it", "a.b.doIt: input.text is required" ] ); assert_eq!( call_problems(&call("a.b.doIt", json!({ "text": "hi" })), &docs), Vec::::new() ); } /// A deletion plan's query must have a lexicon, be a query answering /// `writes`, and take the template's params, as it requires them. #[test] fn deletes_problems_read_the_query() { let deletes = |method: &str, input: Value| Call { method: method.into(), service: "did:web:x.example#svc".into(), input, deletes: true, }; let writes = json!({ "encoding": "application/json", "schema": { "type": "object", "properties": { "writes": { "type": "array", "items": { "type": "unknown" } } } } }); let docs = [ std::sync::Arc::new(json!({ "lexicon": 1, "id": "a.b.getPlan", "defs": { "main": { "type": "query", "output": writes, "parameters": { "type": "params", "required": ["of"], "properties": { "of": { "type": "string" } } } } } })), std::sync::Arc::new(json!({ "lexicon": 1, "id": "a.b.getOther", "defs": { "main": { "type": "query" } } })), std::sync::Arc::new(json!({ "lexicon": 1, "id": "a.b.doIt", "defs": { "main": { "type": "procedure" } } })), ]; assert_eq!( call_problems(&deletes("a.b.none", json!({})), &docs), ["a.b.none: no lexicon"] ); assert_eq!( call_problems(&deletes("a.b.doIt", json!({})), &docs), ["a.b.doIt is a procedure, not a query: HUD only reads"] ); assert_eq!( call_problems(&deletes("a.b.getOther", json!({})), &docs), ["a.b.getOther: answers no writes, the deletes of one com.atproto.repo.applyWrites"] ); assert_eq!( call_problems(&deletes("a.b.getPlan", json!({ "nope": 1 })), &docs), [ "a.b.getPlan: params.nope is not a param of it", "a.b.getPlan: params.of is required" ] ); assert_eq!( call_problems( &deletes("a.b.getPlan", json!({ "of": { "$": "uri" } })), &docs ), Vec::::new() ); } /// A record whose query's params read the record itself is not opened /// as the query by its at-uri alone; a launcher's title is had only /// for its own params. #[test] fn routed_calls_and_launch_titles() { assert_eq!(routed_call("at://did:plc:a/app.bsky.feed.repost/r1"), None); // A story opens as its account's archive, at itself. let story = "at://did:plc:a/social.grain.story/3s"; assert_eq!( routed_call(story), Some(json!({ "method": "social.grain.story.getArchive", "params": { "actor": "did:plc:a" }, "at": story })) ); let repos = "dev.knotless.getActorRepos"; assert_eq!( launch_title(repos, &json!({ "actor": "did:plc:a" })).as_deref(), Some("Repositories") ); assert_eq!(launch_title(repos, &json!({ "cursor": "x" })), None); } }