~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
604 lines · 23.7 KB Raw History
//! A Bluesky post's embed, as the web renderer's embed components read it:
//! pictures, a link card, a video, a quoted post or card, or media with a quote.

use super::*;

pub(super) fn embed_node(p: &Value, scope: &Value, ctx: &Ctx) -> R<Option<Value>> {
    let value = required("value", &p["value"], scope, present)?;
    let uri = text_of(&p["uri"], scope)?;
    let did = text_of(&p["did"], scope)?.or_else(|| {
        uri.as_deref()
            .and_then(|u| u.strip_prefix("at://"))
            .and_then(|r| r.split('/').next())
            .map(str::to_string)
    });
    let depth = if p["depth"].as_u64() == Some(0) { 0 } else { 1 };
    let Some(shown) = embed(value, did.as_deref(), uri.as_deref(), depth, ctx) else {
        return Err(Fail::Missing);
    };
    Ok(Some(json!({ "c": "Embed", "p": shown })))
}

fn page_url(v: &Value) -> Option<&str> {
    v.as_str().filter(|s| http_url(s))
}

/// An embed by its `$type` (`#view` or not): `{kind, …}` where kind is
/// images, external, video, quote, post (a raw quote of a post, see
/// `fill_posts`), gone, card or recordWithMedia; None for
/// a kind not drawn (a quote past `depth`).
fn embed(v: &Value, did: Option<&str>, uri: Option<&str>, depth: u8, ctx: &Ctx) -> Option<Value> {
    let kind = v["$type"]
        .as_str()
        .map(|t| t.strip_suffix("#view").unwrap_or(t));
    match kind {
        Some("app.bsky.embed.images" | "app.bsky.embed.gallery") => images(v, ctx),
        Some("app.bsky.embed.external") => external(v, ctx),
        Some("app.bsky.embed.video") => video(v, did, uri, ctx),
        Some("app.bsky.embed.record") => (depth >= 1)
            .then(|| quoted(&v["record"], depth, ctx))
            .flatten(),
        Some("app.bsky.embed.recordWithMedia") => {
            let media = embed(&v["media"], did, uri, depth, ctx);
            let record = (depth >= 1)
                .then(|| quoted(&v["record"]["record"], depth, ctx))
                .flatten();
            match (media, record) {
                (None, None) => None,
                (m, r) => Some(json!({ "kind": "recordWithMedia", "media": m, "record": r })),
            }
        }
        None if str_at(v, "uri").is_some() => quoted(v, depth, ctx),
        _ => None,
    }
}

fn images(v: &Value, ctx: &Ctx) -> Option<Value> {
    let list = v["images"].as_array().or_else(|| v["items"].as_array())?;
    let drawn: Vec<Value> = list
        .iter()
        .map(|i| {
            let thumb = i.get("thumb").or_else(|| i.get("thumbnail"));
            let mut src = source_of(thumb, ctx, true);
            if src.is_null() {
                src = source_of(i.get("image"), ctx, true);
            }
            let full = page_url(&i["fullsize"])
                .map(str::to_string)
                .or_else(|| url_of(&i["image"], ctx).filter(|u| http_url(u)));
            let ratio = ratio_of(&i["aspectRatio"]).unwrap_or(1.0).clamp(0.25, 4.0);
            json!({ "src": src, "full": full, "alt": i["alt"].as_str().unwrap_or(""), "ratio": ratio })
        })
        .collect();
    (!drawn.is_empty()).then(|| json!({ "kind": "images", "images": drawn }))
}

fn external(v: &Value, ctx: &Ctx) -> Option<Value> {
    let e = &v["external"];
    let href = page_url(&e["uri"]);
    let host = e["uri"]
        .as_str()
        .and_then(|u| url::Url::parse(u).ok())
        .and_then(|u| {
            u.host_str()
                .map(|h| h.trim_start_matches("www.").to_string())
        });
    // A card needs a page or a title; a page (http or https) always has a
    // host to stand for its title.
    let title = str_at(e, "title")
        .map(str::to_string)
        .or_else(|| href.and(host.clone()))?;
    let found = url_of(&e["thumb"], ctx);
    let gif = found.is_some()
        && href.and_then(|h| url::Url::parse(h).ok()).is_some_and(|u| {
            u.host_str() == Some("media.tenor.com")
                || u.path().to_ascii_lowercase().ends_with(".gif")
        });
    let thumb = source_of(Some(&e["thumb"]), ctx, !gif);
    Some(json!({
        "kind": "external", "url": href, "title": title,
        "description": str_at(e, "description"), "host": host,
        "thumb": thumb, "gif": gif,
    }))
}

fn video(v: &Value, did: Option<&str>, uri: Option<&str>, ctx: &Ctx) -> Option<Value> {
    let src = page_url(&v["url"]).or_else(|| page_url(&v["video"]["url"]));
    // Bluesky's own HLS, adaptive and from its CDN, where its playlist is
    // on Bluesky's video service: played before the stream from the server.
    let hls = str_at(v, "playlist").filter(|p| {
        url::Url::parse(p).is_ok_and(|u| {
            u.origin().ascii_serialization() == crate::blobs::VIDEO_ORIGINS[0]
                && u.path().starts_with("/watch/")
        })
    });
    let poster = url_of(&v["thumbnail"], ctx);
    let ratio = ratio_of(&v["aspectRatio"]).unwrap_or(16.0 / 9.0);
    let author = did.map(str::to_string).or_else(|| {
        let playlist = str_at(v, "playlist")?;
        let rest = &playlist[playlist.find("/watch/")? + 7..];
        Some(rest.split('/').next()?.to_string())
    });
    let watch = author.map(|a| {
        let key = uri
            .and_then(|u| u.rsplit('/').next())
            .filter(|_| uri.is_some_and(|u| u.matches('/').count() == 4));
        match key {
            Some(k) => format!("https://bsky.app/profile/{a}/post/{k}"),
            None => format!("https://bsky.app/profile/{a}"),
        }
    });
    let playable = src.is_some() || hls.is_some();
    Some(json!({
        "kind": "video", "src": src, "hls": hls, "poster": poster, "ratio": ratio,
        "alt": str_at(v, "alt").unwrap_or("Video"), "watch": if playable { None } else { watch },
    }))
}

fn count(n: &Value, noun: &str) -> Option<String> {
    let n = n.as_u64()?;
    Some(format!("{n} {noun}{}", if n == 1 { "" } else { "s" }))
}

/// A post as the AppView answers it (a postView), drawn as a quoted post
/// is: read as a `#viewRecord`, its `record` the value, its `embed` the
/// first of its embeds.
pub(super) fn post_quote(post: &Value, ctx: &Ctx) -> Option<Value> {
    let embeds: Vec<Value> = post.get("embed").into_iter().cloned().collect();
    let r = json!({ "$type": "app.bsky.embed.record#viewRecord", "uri": post["uri"],
                    "cid": post["cid"], "author": post["author"], "value": post["record"],
                    "embeds": embeds, "indexedAt": post["indexedAt"] });
    quoted(&r, 1, ctx)
}

/// What a post quotes, by the kind the AppView resolved it to; in a
/// record's raw form, only its at-uri: a card that opens it.
fn quoted(r: &Value, depth: u8, ctx: &Ctx) -> Option<Value> {
    let uri = str_at(r, "uri");
    let open_record = uri.map(|u| ctx.record_call(u));
    let creator = r["creator"]["handle"].as_str().map(|h| format!("@{h}"));
    let by = |what: &str| match &creator {
        Some(c) => format!("{what} by {c}"),
        None => what.to_string(),
    };
    let card = |avatar: &Value,
                title: &str,
                byline: String,
                detail: Option<String>,
                open: Option<Value>| {
        json!({ "kind": "card", "avatar": source_of(Some(avatar), ctx, true), "title": title,
                "byline": byline, "detail": detail, "open": open })
    };
    let gone = |text: &str| Some(json!({ "kind": "gone", "text": text }));
    match r["$type"].as_str() {
        Some("app.bsky.embed.record#viewRecord") => {
            let author = &r["author"];
            let name = name_of(author)
                .or_else(|| str_at(author, "did"))
                .unwrap_or("Someone");
            let value = &r["value"];
            let inner = r["embeds"]
                .get(0)
                .and_then(|e| embed(e, str_at(author, "did"), uri, depth - 1, ctx));
            Some(json!({
                "kind": "quote",
                "author": { "name": name, "handle": str_at(author, "handle"), "avatar": source_of(Some(&author["avatar"]), ctx, true) },
                "when": str_at(value, "createdAt").or_else(|| str_at(r, "indexedAt")),
                "runs": str_at(value, "text").map(|t| facet_runs(t, value.get("facets"), ctx)),
                "open": uri.map(thread_call),
                "embed": inner,
            }))
        }
        Some("app.bsky.embed.record#viewNotFound") => gone("Post not found"),
        Some("app.bsky.embed.record#viewBlocked") => gone("Blocked post"),
        Some("app.bsky.embed.record#viewDetached") => gone("Removed by its author"),
        Some("app.bsky.feed.defs#generatorView") => Some(card(
            &r["avatar"],
            str_at(r, "displayName").unwrap_or("Feed"),
            if creator.is_some() {
                format!("Feed by {}", creator.as_deref().unwrap_or(""))
            } else {
                "Feed".into()
            },
            count(&r["likeCount"], "like"),
            uri.map(|f| {
                call(
                    "query",
                    json!({ "method": "app.bsky.feed.getFeed", "params": { "feed": f } }),
                )
            }),
        )),
        Some("app.bsky.graph.defs#listView") => Some(card(
            &r["avatar"],
            str_at(r, "name").unwrap_or("List"),
            by(if r["purpose"] == "app.bsky.graph.defs#modlist" {
                "Moderation list"
            } else {
                "List"
            }),
            count(&r["listItemCount"], "member"),
            open_record,
        )),
        Some("app.bsky.graph.defs#starterPackViewBasic") => Some(card(
            &Value::Null,
            str_at(&r["record"], "name").unwrap_or("Starter pack"),
            by("Starter pack"),
            count(&r["listItemCount"], "account")
                .or_else(|| count(&r["joinedAllTimeCount"], "join")),
            open_record,
        )),
        Some("app.bsky.labeler.defs#labelerView") => {
            let c = &r["creator"];
            Some(card(
                &c["avatar"],
                name_of(c).unwrap_or("Labeler"),
                by("Labeler"),
                count(&r["likeCount"], "like"),
                open_record,
            ))
        }
        None | Some("com.atproto.repo.strongRef") => {
            let uri = uri?;
            let collection = uri.strip_prefix("at://")?.split('/').nth(1)?;
            let title = if collection == "app.bsky.feed.post" {
                "Quoted post".to_string()
            } else {
                format!("Quoted {collection}")
            };
            let mut c = card(&Value::Null, &title, uri.to_string(), None, open_record);
            c["compact"] = true.into();
            // A post the tool fetches and fills in as `quote` (`fill_posts`).
            if collection == "app.bsky.feed.post" {
                return Some(json!({ "kind": "post", "uri": uri, "fallback": c }));
            }
            Some(c)
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ctx<'a>(root: &'a Value, origins: &'a [String]) -> Ctx<'a> {
        Ctx {
            origins,
            thumbnails: vec![],
            trusted: true,
            root,
            report: None,
            inline: None,
            lexicon: None,
            handles: None,
            paged: Vec::new(),
            more: More::default(),
            me: None,
            people: None,
        }
    }

    /// An Embed node's drawing of `value`, held to the lexicon; None where
    /// it draws nothing.
    fn shown(value: Value, props: Value, c: &Ctx) -> Option<Value> {
        let mut props = props;
        props["value"] = json!({ "$": "value" });
        let layout = json!({ "component": "Embed", "props": props });
        let tree = resolve(&layout, &json!({ "value": value }), c)?;
        check::checked(&tree);
        Some(tree["p"].clone())
    }

    fn quoting(record: Value) -> Value {
        json!({ "$type": "app.bsky.embed.record#view", "record": record })
    }

    #[test]
    fn feeds_lists_starter_packs_and_labelers_are_cards() {
        let root = json!({});
        let origins = vec!["https://cdn.test".to_string()];
        let c = ctx(&root, &origins);
        let avatar = "https://cdn.test/a.jpg";
        let pic = json!({ "src": avatar, "fallback": null });
        let creator = json!({ "did": "did:plc:c", "handle": "c.test" });
        let card = |v: Value| shown(quoting(v), json!({}), &c).unwrap();
        let expect = |avatar: &Value, title: &str, byline: &str, detail: Value, open: Value| json!({ "kind": "card", "avatar": avatar, "title": title, "byline": byline, "detail": detail, "open": open });

        let feed = "at://did:plc:c/app.bsky.feed.generator/f";
        assert_eq!(
            card(
                json!({ "$type": "app.bsky.feed.defs#generatorView", "uri": feed, "creator": creator,
                         "displayName": "Cats", "avatar": avatar, "likeCount": 1 })
            ),
            expect(
                &pic,
                "Cats",
                "Feed by @c.test",
                "1 like".into(),
                call(
                    "query",
                    json!({ "method": "app.bsky.feed.getFeed", "params": { "feed": feed } })
                )
            )
        );
        assert_eq!(
            card(json!({ "$type": "app.bsky.feed.defs#generatorView", "likeCount": 2 })),
            expect(&Value::Null, "Feed", "Feed", "2 likes".into(), Value::Null)
        );

        let list = "at://did:plc:c/app.bsky.graph.list/l";
        assert_eq!(
            card(
                json!({ "$type": "app.bsky.graph.defs#listView", "uri": list, "creator": creator, "name": "Mods",
                         "purpose": "app.bsky.graph.defs#modlist", "listItemCount": 3 })
            ),
            expect(
                &Value::Null,
                "Mods",
                "Moderation list by @c.test",
                "3 members".into(),
                c.record_call(list)
            )
        );
        assert_eq!(
            card(
                json!({ "$type": "app.bsky.graph.defs#listView", "purpose": "app.bsky.graph.defs#curatelist" })
            ),
            expect(&Value::Null, "List", "List", Value::Null, Value::Null)
        );

        let pack = "at://did:plc:c/app.bsky.graph.starterpack/s";
        assert_eq!(
            card(
                json!({ "$type": "app.bsky.graph.defs#starterPackViewBasic", "uri": pack, "creator": creator,
                         "record": { "name": "Start" }, "listItemCount": 1, "joinedAllTimeCount": 9 })
            ),
            expect(
                &Value::Null,
                "Start",
                "Starter pack by @c.test",
                "1 account".into(),
                c.record_call(pack)
            )
        );
        assert_eq!(
            card(
                json!({ "$type": "app.bsky.graph.defs#starterPackViewBasic", "joinedAllTimeCount": 5 })
            ),
            expect(
                &Value::Null,
                "Starter pack",
                "Starter pack",
                "5 joins".into(),
                Value::Null
            )
        );

        let labeler = "at://did:plc:c/app.bsky.labeler.service/self";
        let mut by = creator.clone();
        by["displayName"] = "Labels Inc".into();
        by["avatar"] = avatar.into();
        assert_eq!(
            card(
                json!({ "$type": "app.bsky.labeler.defs#labelerView", "uri": labeler, "creator": by, "likeCount": 4 })
            ),
            expect(
                &pic,
                "Labels Inc",
                "Labeler by @c.test",
                "4 likes".into(),
                c.record_call(labeler)
            )
        );
        assert_eq!(
            card(json!({ "$type": "app.bsky.labeler.defs#labelerView" })),
            expect(&Value::Null, "Labeler", "Labeler", Value::Null, Value::Null)
        );
    }

    #[test]
    fn a_quote_gone_unknown_or_not_a_post_says_so_or_draws_nothing() {
        let root = json!({});
        let c = ctx(&root, &[]);
        let gone = |t: &str| {
            shown(
                quoting(json!({ "$type": format!("app.bsky.embed.record#{t}"), "uri": "at://x" })),
                json!({}),
                &c,
            )
            .unwrap()
        };
        assert_eq!(
            gone("viewNotFound"),
            json!({ "kind": "gone", "text": "Post not found" })
        );
        assert_eq!(
            gone("viewBlocked"),
            json!({ "kind": "gone", "text": "Blocked post" })
        );
        assert_eq!(
            gone("viewDetached"),
            json!({ "kind": "gone", "text": "Removed by its author" })
        );
        assert_eq!(
            shown(
                quoting(json!({ "$type": "app.bsky.embed.record#viewSomethingNew" })),
                json!({}),
                &c
            ),
            None
        );

        // A strongRef to a record that is no post: a card that opens it.
        let feed = "at://did:plc:c/app.bsky.feed.generator/f";
        assert_eq!(
            shown(json!({ "$type": "app.bsky.embed.record", "record": { "$type": "com.atproto.repo.strongRef", "uri": feed } }), json!({}), &c)
                .unwrap(),
            json!({ "kind": "card", "avatar": null, "title": "Quoted app.bsky.feed.generator", "byline": feed,
                    "detail": null, "open": c.record_call(feed), "compact": true })
        );
        // One with no uri, or an at-uri naming no record, draws nothing.
        for record in [json!({ "cid": "bafy" }), json!({ "uri": "at://did:plc:c" })] {
            assert_eq!(
                shown(
                    json!({ "$type": "app.bsky.embed.record", "record": record }),
                    json!({}),
                    &c
                ),
                None
            );
        }
        // Nor does an embed of a kind not drawn.
        assert_eq!(
            shown(json!({ "$type": "app.bsky.embed.poll" }), json!({}), &c),
            None
        );
    }

    #[test]
    fn a_quoted_author_is_named_by_did_else_someone_and_dated_by_its_indexing() {
        let root = json!({});
        let c = ctx(&root, &[]);
        let quote = |author: Value| {
            shown(
                quoting(
                    json!({ "$type": "app.bsky.embed.record#viewRecord", "author": author,
                                  "value": {}, "indexedAt": "2026-01-02T00:00:00Z" }),
                ),
                json!({}),
                &c,
            )
            .unwrap()
        };
        let q = quote(json!({ "did": "did:plc:q" }));
        assert_eq!(q["author"]["name"], "did:plc:q");
        assert_eq!(q["when"], "2026-01-02T00:00:00Z");
        assert_eq!(q["runs"], Value::Null);
        assert_eq!(q["open"], Value::Null);
        assert_eq!(quote(json!({}))["author"]["name"], "Someone");
    }

    #[test]
    fn at_depth_zero_no_quote_is_drawn_and_media_alone_stays() {
        let root = json!({});
        let origins = vec!["https://cdn.test".to_string()];
        let c = ctx(&root, &origins);
        let depth0 = json!({ "depth": 0 });
        let post = json!({ "$type": "app.bsky.embed.record#viewRecord", "uri": "at://did:plc:q/app.bsky.feed.post/1",
                           "author": { "did": "did:plc:q" }, "value": { "text": "hi" } });
        assert_eq!(shown(quoting(post.clone()), depth0.clone(), &c), None);
        let with_media = |media: Value| json!({ "$type": "app.bsky.embed.recordWithMedia#view", "media": media, "record": { "record": post } });
        assert_eq!(
            shown(
                with_media(json!({ "$type": "app.bsky.embed.poll" })),
                depth0.clone(),
                &c
            ),
            None
        );
        let images = json!({ "$type": "app.bsky.embed.images#view", "images": [{ "thumb": "https://cdn.test/t.jpg", "alt": "t" }] });
        let shown = shown(with_media(images), depth0, &c).unwrap();
        assert_eq!(shown["kind"], "recordWithMedia");
        assert_eq!(shown["media"]["images"][0]["alt"], "t");
        assert_eq!(shown["record"], Value::Null);
    }

    #[test]
    fn a_link_card_needs_a_page_or_a_title_and_a_page_is_titled_by_its_host() {
        let root = json!({});
        let c = ctx(&root, &[]);
        let external = |e: Value| {
            shown(
                json!({ "$type": "app.bsky.embed.external", "external": e }),
                json!({}),
                &c,
            )
        };
        assert_eq!(external(json!({ "uri": "javascript:alert(1)" })), None);
        let titled = external(json!({ "title": "Just a title" })).unwrap();
        assert_eq!(titled["title"], "Just a title");
        assert_eq!(titled["url"], Value::Null);
        let untitled = external(json!({ "uri": "https://www.example.test/a" })).unwrap();
        assert_eq!(untitled["title"], "example.test");
        assert_eq!(untitled["host"], "example.test");
        assert_eq!(untitled["url"], "https://www.example.test/a");
    }

    #[test]
    fn a_gallery_draws_its_items_as_pictures() {
        let root = json!({});
        let origins = vec!["https://cdn.test".to_string()];
        let c = ctx(&root, &origins);
        let gallery = json!({ "$type": "app.bsky.embed.gallery#view", "items": [
            { "thumbnail": "https://cdn.test/t.jpg", "fullsize": "https://cdn.test/f.jpg", "alt": "one",
              "aspectRatio": { "width": 10, "height": 1 } },
        ] });
        assert_eq!(
            shown(gallery, json!({}), &c).unwrap(),
            json!({ "kind": "images", "images": [{ "src": { "src": "https://cdn.test/t.jpg", "fallback": null },
                    "full": "https://cdn.test/f.jpg", "alt": "one", "ratio": 4.0 }] })
        );
    }

    #[test]
    fn a_video_without_its_source_links_to_its_post_or_its_author() {
        let root = json!({});
        let c = ctx(&root, &[]);
        let video = json!({ "$type": "app.bsky.embed.video#view",
                            "playlist": "http://video.bsky.app/watch/did%3Aplc%3Av/bafy/playlist.m3u8" });
        // Its author from its playlist, with no post to link to; a playlist
        // over http is never played.
        let v = shown(video.clone(), json!({}), &c).unwrap();
        assert_eq!(v["watch"], "https://bsky.app/profile/did%3Aplc%3Av");
        assert_eq!(v["hls"], Value::Null);
        assert_eq!(v["alt"], "Video");
        assert_eq!(v["ratio"], 16.0 / 9.0);
        // The post it is in, by its uri.
        let layout = json!({ "component": "Embed", "props": { "value": { "$": "value" }, "uri": { "$": "uri" } } });
        let scope = json!({ "value": { "$type": "app.bsky.embed.video" }, "uri": "at://did:plc:v/app.bsky.feed.post/k" });
        let tree = resolve(&layout, &scope, &c).unwrap();
        check::checked(&tree);
        assert_eq!(
            tree["p"]["watch"],
            "https://bsky.app/profile/did:plc:v/post/k"
        );
        // No author anywhere: nowhere to watch it.
        let v = shown(
            json!({ "$type": "app.bsky.embed.video", "playlist": "https://video.test/p.m3u8" }),
            json!({}),
            &c,
        )
        .unwrap();
        assert_eq!(v["watch"], Value::Null);
    }

    #[test]
    fn a_video_plays_bluesky_s_hls_and_needs_no_link_then() {
        let root = json!({});
        let c = ctx(&root, &[]);
        let playlist = "https://video.bsky.app/watch/did%3Aplc%3Av/bafy/playlist.m3u8";
        let v = shown(
            json!({ "$type": "app.bsky.embed.video#view", "playlist": playlist }),
            json!({}),
            &c,
        )
        .unwrap();
        assert_eq!(v["hls"], playlist);
        assert_eq!(v["src"], Value::Null);
        assert_eq!(v["watch"], Value::Null);
        // Anywhere else, or not under /watch/: none.
        for other in [
            "https://video.example/watch/x/playlist.m3u8",
            "https://video.bsky.app/other/playlist.m3u8",
            "not a url",
        ] {
            let v = shown(
                json!({ "$type": "app.bsky.embed.video#view", "playlist": other }),
                json!({}),
                &c,
            )
            .unwrap();
            assert_eq!(v["hls"], Value::Null, "{other}");
        }
    }
}