about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2023-10-15 20:54:30 +0300
committerVika <vika@fireburn.ru>2023-10-15 20:54:30 +0300
commitdd74adb75960ceac8970f5a0b38f3d720733b63f (patch)
treed3de1fa7c807413bf4c6be88496cf6d56452da43 /src
parent27b5bcc274675eab956e70bc59ba3e137e5aa676 (diff)
downloadkittybox-dd74adb75960ceac8970f5a0b38f3d720733b63f.tar.zst
Fix bug with likes/bookmarks on h-entries lacking URLs in markup
Diffstat (limited to 'src')
-rw-r--r--src/micropub/mod.rs28
1 files changed, 21 insertions, 7 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::<Vec<serde_json::Value>>()
     })
 }
@@ -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");
     }