about summary refs log tree commit diff
path: root/src/micropub
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-08-23 22:50:36 +0300
committerVika <vika@fireburn.ru>2024-08-23 22:50:36 +0300
commiteb30f3bd0790fbf78aeb8cc8f33f76a0a114cc9d (patch)
tree9621d952381c19017db762f64107161e55a54ca6 /src/micropub
parent22217993d1a0cba4d419ddf2867e1751df9248bc (diff)
downloadkittybox-eb30f3bd0790fbf78aeb8cc8f33f76a0a114cc9d.tar.zst
micropub: Suppress adding to default feeds on visibility="unlisted"
If the post has no explicit channels, normally Kittybox attempts to
post to a channel that makes sense for the post type. Adding
`unlisted` to `visibility` now suppresses this behavior.
Diffstat (limited to 'src/micropub')
-rw-r--r--src/micropub/util.rs32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/micropub/util.rs b/src/micropub/util.rs
index 0633ce9..9bbdcf6 100644
--- a/src/micropub/util.rs
+++ b/src/micropub/util.rs
@@ -83,7 +83,7 @@ pub fn normalize_mf2(mut body: serde_json::Value, user: &TokenData) -> (String,
             .iter()
             .map(|i| i.as_str().unwrap_or(""))
             .filter(|i| i != &"")
-            .map(|i| me.join(&((&folder).clone() + i)).unwrap().to_string())
+            .map(|i| me.join(&(folder.clone() + i)).unwrap().to_string())
             .collect::<Vec<String>>();
         let urls = body["properties"]["url"].as_array_mut().unwrap();
         new_urls.iter().for_each(|i| urls.push(json!(i)));
@@ -125,7 +125,14 @@ pub fn normalize_mf2(mut body: serde_json::Value, user: &TokenData) -> (String,
             .unwrap()
             .remove("mp-channel");
     }
-    if body["properties"]["channel"][0].as_str().is_none() {
+    // If there is no explicit channels, and the post is not marked as "unlisted",
+    // post it to one of the default channels that makes sense for the post type.
+    if body["properties"]["channel"][0].as_str().is_none() && (!body["properties"]["visibility"]
+        .as_array()
+        .map(|v| v.contains(
+            &serde_json::Value::String("unlisted".to_owned())
+        )).unwrap_or(false)
+    ) {
         match body["type"][0].as_str() {
             Some("h-entry") => {
                 // Set the channel to the main channel...
@@ -247,6 +254,27 @@ mod tests {
     }
 
     #[test]
+    fn test_unlisted_post() {
+        let mf2 = json!({
+            "type": ["h-entry"],
+            "properties": {
+                "uid": ["https://fireburn.ru/posts/test"],
+                "content": [{"html": "<p>Hello world!</p>"}],
+                "visibility": ["public", "unlisted"]
+            }
+        });
+
+        let (uid, normalized) = normalize_mf2(
+            mf2.clone(),
+            &token_data()
+        );
+        assert!(
+            normalized["properties"]["channel"].as_array().unwrap_or(&vec![]).is_empty(),
+            "Returned post was added to a channel despite the `unlisted` visibility"
+        );
+    }
+
+    #[test]
     fn test_no_replace_uid() {
         let mf2 = json!({
             "type": ["h-card"],