chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.4.0 Latest 5 packages
0
0
//! The heads of pages, as the web renderer's HeaderNode and ProfileNode
//! read them: a set's or a page's `Header` (a picture beside its words)
//! and an account's `Profile` (a banner, an avatar, its names and words).
use super::*;
/// A text prop, or None where it is absent or empty: what draws nothing.
fn words(prop: &Value, scope: &Value) -> R<Option<String>> {
Ok(text_of(prop, scope)?.filter(|s| !s.is_empty()))
}
/// `Header`: `p: { title?, subtitle?, kicker?, image?, meta? }`. `image` is
/// there only where its value is (a picture drawn small, or null for a
/// placeholder); `meta` the line's non-empty words, from a literal list of
/// props or one bound list of strings and numbers. Every prop is optional,
/// so it always draws.
pub(super) fn header(p: &Value, scope: &Value, ctx: &Ctx) -> R<Option<Value>> {
let mut o = Map::new();
for key in ["title", "subtitle", "kicker"] {
if let Some(s) = words(&p[key], scope)? {
o.insert(key.into(), s.into());
}
}
if value(&p["image"], scope, present)?.is_some() {
o.insert("image".into(), source(&p["image"], scope, ctx, true)?);
}
let meta: Vec<Value> = match &p["meta"] {
Value::Array(list) => {
let mut found = Vec::new();
for item in list {
if let Some(s) = words(item, scope)? {
found.push(s.into());
}
}
found
}
bound => value(bound, scope, Value::is_array)?
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter(|v| v.is_string() || v.is_number())
.map(write)
.filter(|s| !s.is_empty())
.map(Value::from)
.collect(),
};
if !meta.is_empty() {
o.insert("meta".into(), Value::Array(meta));
}
Ok(Some(make("Header", o, vec![])))
}
/// `Profile`: `p: { name?, handle?, open?, descriptionRuns?, avatar, banner }`;
/// `handle` without its leading `@`, `open` the account it names (as given),
/// `descriptionRuns` its description with its links, @mentions and #tags
/// found as Bluesky's app finds them (a profile's record has no facets),
/// `avatar` drawn small and `banner` large, each null for none. It always
/// draws.
pub(super) fn profile(p: &Value, scope: &Value, ctx: &Ctx) -> R<Option<Value>> {
let mut o = Map::new();
if let Some(s) = words(&p["name"], scope)? {
o.insert("name".into(), s.into());
}
if let Some(s) = words(&p["description"], scope)? {
o.insert("descriptionRuns".into(), detected_runs(&s, ctx).into());
}
if let Some(handle) = words(&p["handle"], scope)? {
o.insert(
"open".into(),
call("describe_repo", json!({ "actor": handle })),
);
let shown = handle.strip_prefix('@').unwrap_or(&handle).to_string();
o.insert("handle".into(), shown.into());
}
o.insert("avatar".into(), source(&p["avatar"], scope, ctx, true)?);
o.insert("banner".into(), source(&p["banner"], scope, ctx, false)?);
Ok(Some(make("Profile", o, vec![])))
}
#[cfg(test)]
mod tests {
use super::*;
fn ctx<'a>(root: &'a Value, origins: &'a [String]) -> Ctx<'a> {
Ctx {
origins,
thumbnails: vec![(
"https://cdn.bsky.app/img/avatar/plain/".into(),
"https://cdn.bsky.app/img/avatar_thumbnail/plain/".into(),
)],
trusted: true,
root,
report: None,
inline: None,
lexicon: None,
handles: None,
paged: Vec::new(),
more: More::default(),
me: None,
people: None,
}
}
#[test]
fn a_header_draws_an_image_box_only_where_its_value_is() {
let root = json!({});
let c = ctx(&root, &[]);
let p = json!({ "title": { "$": "name" }, "image": { "$": "icon" }, "subtitle": { "$": "about" },
"meta": [{ "$": ["handle", "did"] }, "", 3, { "$": "none" }] });
let scope = json!({ "name": "Blog", "icon": "https://evil.example/i.png", "about": "", "did": "did:plc:a" });
let with = header(&p, &scope, &c).ok().flatten().unwrap();
check::checked(&with);
assert_eq!(
with["p"]["image"],
Value::Null,
"there but unusable: a placeholder"
);
assert!(
with["p"].get("subtitle").is_none(),
"empty text draws nothing"
);
assert_eq!(with["p"]["meta"], json!(["did:plc:a", "3"]));
let scope = json!({ "name": "Blog" });
let without = header(&p, &scope, &c).ok().flatten().unwrap();
check::checked(&without);
assert!(
without["p"].get("image").is_none(),
"no value: no image box"
);
}
#[test]
fn a_bound_meta_list_keeps_its_strings_and_numbers() {
let root = json!({});
let c = ctx(&root, &[]);
let p = json!({ "meta": { "$": "tags" } });
let scope = json!({ "tags": ["rust", 2, "", { "a": 1 }, null] });
let drawn = header(&p, &scope, &c).ok().flatten().unwrap();
check::checked(&drawn);
assert_eq!(drawn["p"]["meta"], json!(["rust", "2"]));
}
#[test]
fn a_header_with_no_meta_words_draws_no_meta_line() {
let root = json!({});
let c = ctx(&root, &[]);
let p = json!({ "title": "Blog", "meta": [{ "$": "none" }, ""] });
let drawn = header(&p, &json!({}), &c).ok().flatten().unwrap();
check::checked(&drawn);
assert_eq!(drawn, json!({ "c": "Header", "p": { "title": "Blog" } }));
}
#[test]
fn a_profile_opens_its_handle_and_draws_its_avatar_small() {
let root = json!({});
let origins = vec!["https://cdn.bsky.app".to_string()];
let c = ctx(&root, &origins);
let p = json!({ "name": { "$": "displayName" }, "handle": { "$": "handle" },
"avatar": { "$": "avatar" }, "banner": { "$": "banner" } });
let scope = json!({ "handle": "@alice.test", "displayName": "",
"avatar": "https://cdn.bsky.app/img/avatar/plain/did:plc:a/x@jpeg",
"banner": "https://cdn.bsky.app/img/avatar/plain/did:plc:a/y@jpeg" });
let drawn = profile(&p, &scope, &c).ok().flatten().unwrap();
check::checked(&drawn);
let p = &drawn["p"];
assert_eq!(p["handle"], "alice.test");
assert_eq!(p["open"]["call"]["args"]["actor"], "@alice.test");
assert!(p.get("name").is_none());
assert_eq!(
p["avatar"]["src"],
"https://cdn.bsky.app/img/avatar_thumbnail/plain/did:plc:a/x@jpeg"
);
assert_eq!(
p["banner"]["src"], "https://cdn.bsky.app/img/avatar/plain/did:plc:a/y@jpeg",
"a banner is drawn large"
);
}
#[test]
fn a_profiles_description_marks_its_links_mentions_and_tags() {
let root = json!({});
let c = ctx(&root, &[]);
let p = json!({ "description": { "$": "description" }, "avatar": null, "banner": null });
let scope =
json!({ "description": "building @Grain.social, https://a.example/x. #atproto" });
let drawn = profile(&p, &scope, &c).ok().flatten().unwrap();
check::checked(&drawn);
assert_eq!(
drawn["p"]["descriptionRuns"],
json!([
{ "text": "building " },
{ "text": "@Grain.social", "actor": "grain.social" },
{ "text": ", " },
{ "text": "https://a.example/x", "link": "https://a.example/x" },
{ "text": ". " },
{ "text": "#atproto", "tag": "atproto" },
]),
"a mention opens the account by its handle, as the text names no DID"
);
assert!(drawn["p"].get("description").is_none());
}
}