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 +++++++++++++++++++++------- templates/src/mf2.rs | 22 +++++++++++++++++++++- 2 files changed, 42 insertions(+), 8 deletions(-) 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"); } diff --git a/templates/src/mf2.rs b/templates/src/mf2.rs index 33cd84a..2c41de9 100644 --- a/templates/src/mf2.rs +++ b/templates/src/mf2.rs @@ -56,6 +56,13 @@ markup::define! { " " a."u-like-of"[href=likeof] { @likeof } } else if let Some(likeof) = post["properties"]["like-of"][0].as_object() { + " " + span."like-icon"["aria-label"="liked"] { + span."like-icon-label"["aria-hidden"="true"] { + "❤️" + } + } + " " a."u-like-of"[href=likeof["properties"]["url"][0].as_str().unwrap()] { @likeof["properties"]["name"][0] .as_str() @@ -63,9 +70,22 @@ markup::define! { } } @if let Some(bookmarkof) = post["properties"]["bookmark-of"][0].as_str() { - " 🔖 " + " " + span."like-icon"["aria-label"="bookmarked"] { + span."bookmark-icon-label"["aria-hidden"="true"] { + "🔖" + } + } + " " a."u-bookmark-of"[href=bookmarkof] { @bookmarkof } } else if let Some(bookmarkof) = post["properties"]["bookmark-of"][0].as_object() { + " " + span."like-icon"["aria-label"="bookmarked"] { + span."bookmark-icon-label"["aria-hidden"="true"] { + "🔖" + } + } + " " a."u-bookmark-of"[href=bookmarkof["properties"]["url"][0].as_str().unwrap()] { @bookmarkof["properties"]["name"][0] .as_str() -- cgit 1.4.1