From dd74adb75960ceac8970f5a0b38f3d720733b63f Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 15 Oct 2023 20:54:30 +0300 Subject: Fix bug with likes/bookmarks on h-entries lacking URLs in markup --- src/micropub/mod.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/micropub/mod.rs') diff --git a/src/micropub/mod.rs b/src/micropub/mod.rs index 02eee6e..c3e78fe 100644 --- a/src/micropub/mod.rs +++ b/src/micropub/mod.rs @@ -66,12 +66,24 @@ fn populate_reply_context( // TODO: This seems to be O(n^2) and I don't like it. // Switching `ctxs` to a hashmap might speed it up to O(n) // The key would be the URL/UID - .map(|i| ctxs - .iter() - .find(|ctx| Some(ctx.url.as_str()) == i.as_str()) - .and_then(|ctx| ctx.mf2["items"].get(0)) - .unwrap_or(i)) - .cloned() + .map(|i| { + let mut item = ctxs + .iter() + .find(|ctx| Some(ctx.url.as_str()) == i.as_str()) + .and_then(|ctx| ctx.mf2["items"].get(0)) + .unwrap_or(i) + .clone(); + + if item.is_object() && (i != &item) { + if let Some(props) = item["properties"].as_object_mut() { + // Fixup the item: if it lacks a URL, add one. + if !props.get("url").and_then(serde_json::Value::as_array).map(|a| a.len() > 0).unwrap_or(false) { + props.insert("url".to_owned(), json!([i.as_str()])); + } + } + } + item + }) .collect::>() }) } @@ -725,7 +737,9 @@ mod tests { let like_of = super::populate_reply_context(&mf2, "like-of", &reply_contexts).unwrap(); - assert_eq!(like_of[0], test_ctx); + assert_eq!(like_of[0]["properties"]["content"], test_ctx["properties"]["content"]); + assert_eq!(like_of[0]["properties"]["url"][0].as_str().unwrap(), reply_contexts[0].url.as_str()); + assert_eq!(like_of[1], already_expanded_reply_ctx); assert_eq!(like_of[2], "https://fireburn.ru/posts/non-existent"); } -- cgit 1.4.1