···
1
+
use tinyjson::JsonValue;
3
+
use crate::parse_any_link;
5
+
pub fn walk_record(path: &str, v: &JsonValue, found: &mut Vec<(String, String)>) {
7
+
JsonValue::Object(o) => {
8
+
for (key, child) in o {
9
+
walk_record(&format!("{path}.{key}"), child, found)
12
+
JsonValue::Array(a) => {
13
+
let p = format!("{path}[]");
15
+
walk_record(&p, child, found)
18
+
JsonValue::String(s) => {
19
+
if let Some(link) = parse_any_link(s) {
20
+
found.push((path.to_string(), link.into_string()));
27
+
pub fn collect_links(v: JsonValue) -> Vec<(String, String)> {
28
+
let mut found = vec![];
29
+
walk_record("", &v, &mut found);
38
+
fn test_collect_links() {
39
+
let rec = r#"{"a": "https://example.com", "b": "not a link"}"#;
40
+
let json = collect_links(rec.parse().unwrap());
41
+
assert_eq!(json, vec![(".a".into(), "https://example.com".into())]);
45
+
fn test_bsky_feed_post_record_reply() {
47
+
"$type": "app.bsky.feed.post",
48
+
"createdAt": "2025-01-08T20:52:43.041Z",
54
+
"cid": "bafyreifk3bwnmulk37ezrarg4ouheqnhgucypynftqafl4limssogvzk6i",
55
+
"uri": "at://did:plc:b3rzzkblqsxhr3dgcueymkqe/app.bsky.feed.post/3lf6yc4drhk2f"
58
+
"cid": "bafyreifk3bwnmulk37ezrarg4ouheqnhgucypynftqafl4limssogvzk6i",
59
+
"uri": "at://did:plc:b3rzzkblqsxhr3dgcueymkqe/app.bsky.feed.post/3lf6yc4drhk2f"
64
+
let mut json = collect_links(rec.parse().unwrap());
70
+
".reply.parent.uri".into(),
71
+
"at://did:plc:b3rzzkblqsxhr3dgcueymkqe/app.bsky.feed.post/3lf6yc4drhk2f".into()
74
+
".reply.root.uri".into(),
75
+
"at://did:plc:b3rzzkblqsxhr3dgcueymkqe/app.bsky.feed.post/3lf6yc4drhk2f".into()
82
+
fn test_bsky_feed_post_record_embed() {
84
+
"$type": "app.bsky.feed.post",
85
+
"createdAt": "2025-01-08T20:52:39.539Z",
87
+
"$type": "app.bsky.embed.external",
89
+
"description": "YouTube video by More Perfect Union",
93
+
"$link": "bafkreifxuvkbqksq5usi4cryex37o4absjexuouvgenlb62ojsx443b2tm"
95
+
"mimeType": "image/jpeg",
98
+
"title": "Corporations & Wealthy Elites Are Coopting Our Government. Who Can Stop Them?",
99
+
"uri": "https://youtu.be/oKXm4szEP1Q?si=_0n_uPu4qNKokMnq"
106
+
"$type": "app.bsky.richtext.facet#link",
107
+
"uri": "https://youtu.be/oKXm4szEP1Q?si=_0n_uPu4qNKokMnq"
119
+
"text": "youtu.be/oKXm4szEP1Q?..."
121
+
let mut json = collect_links(rec.parse().unwrap());
127
+
".embed.external.uri".into(),
128
+
"https://youtu.be/oKXm4szEP1Q?si=_0n_uPu4qNKokMnq".into()
131
+
".facets[].features[].uri".into(),
132
+
"https://youtu.be/oKXm4szEP1Q?si=_0n_uPu4qNKokMnq".into()