about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-03-03 00:31:18 +0300
committerVika <vika@fireburn.ru>2022-03-03 00:31:18 +0300
commit11a39e85f2673029c7e317c02ae56bd8813773ad (patch)
tree440472668a981b1c38826c42873d94f84905ea4d /src
parentb64f3add27bac6914ea1f8b2a5e801c7f73bcc76 (diff)
micropub: parse mp-channel as channel
Diffstat (limited to 'src')
-rw-r--r--src/micropub/post.rs74
1 files changed, 72 insertions, 2 deletions
diff --git a/src/micropub/post.rs b/src/micropub/post.rs
index de2b162..30304e5 100644
--- a/src/micropub/post.rs
+++ b/src/micropub/post.rs
@@ -10,8 +10,7 @@ use log::{error, info, warn};
 use newbase60::num_to_sxg;
 use std::convert::TryInto;
 use std::str::FromStr;
-use tide::prelude::json;
-use tide::{Request, Response, Result};
+use serde_json::json;
 
 static DEFAULT_CHANNEL_PATH: &str = "/feeds/main";
 static DEFAULT_CHANNEL_NAME: &str = "Main feed";
@@ -126,6 +125,24 @@ pub fn normalize_mf2(mut body: serde_json::Value, user: &User) -> (String, serde
             "value": body["properties"]["content"][0]
         }])
     }
+    // TODO: apply this normalization to editing too
+    if body["properties"]["mp-channel"].is_array() {
+        let mut additional_channels = body["properties"]["mp-channel"].as_array().unwrap().clone();
+        if let Some(array) = body["properties"]["channel"].as_array_mut() {
+            array.append(&mut additional_channels);
+        } else {
+            body["properties"]["channel"] = json!(additional_channels)
+        }
+        body["properties"].as_object_mut().unwrap().remove("mp-channel");
+    } else if body["properties"]["mp-channel"].is_string() {
+        let chan = body["properties"]["mp-channel"].as_str().unwrap().to_owned();
+        if let Some(array) = body["properties"]["channel"].as_array_mut() {
+            array.push(json!(chan))
+        } else {
+            body["properties"]["channel"] = json!([chan]);
+        }
+        body["properties"].as_object_mut().unwrap().remove("mp-channel");
+    }
     if body["properties"]["channel"][0].as_str().is_none() {
         match body["type"][0].as_str() {
             Some("h-entry") => {
@@ -785,6 +802,59 @@ mod tests {
     }
 
     #[test]
+    fn test_mp_channel() {
+        let mf2 = json!({
+            "type": ["h-entry"],
+            "properties": {
+                "uid": ["https://fireburn.ru/posts/test"],
+                "content": [{"html": "<p>Hello world!</p>"}],
+                "mp-channel": ["https://fireburn.ru/feeds/test"]
+            }
+        });
+
+        let (_, normalized) = normalize_mf2(
+            mf2.clone(),
+            &User::new(
+                "https://fireburn.ru/",
+                "https://quill.p3k.io/",
+                "create update media",
+            )
+        );
+
+        assert_eq!(
+            normalized["properties"]["channel"],
+            mf2["properties"]["mp-channel"]
+        );
+    }
+
+    #[test]
+    fn test_mp_channel_as_string() {
+        let mf2 = json!({
+            "type": ["h-entry"],
+            "properties": {
+                "uid": ["https://fireburn.ru/posts/test"],
+                "content": [{"html": "<p>Hello world!</p>"}],
+                "mp-channel": "https://fireburn.ru/feeds/test"
+            }
+        });
+
+        let (_, normalized) = normalize_mf2(
+            mf2.clone(),
+            &User::new(
+                "https://fireburn.ru/",
+                "https://quill.p3k.io/",
+                "create update media",
+            )
+        );
+
+        assert_eq!(
+            normalized["properties"]["channel"][0],
+            mf2["properties"]["mp-channel"]
+        );
+    }
+
+    
+    #[test]
     fn test_form_to_mf2() {
         use serde_urlencoded::from_str;