about summary refs log tree commit diff
path: root/src/micropub/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/micropub/util.rs')
-rw-r--r--src/micropub/util.rs93
1 files changed, 46 insertions, 47 deletions
diff --git a/src/micropub/util.rs b/src/micropub/util.rs
index 19f4953..8c5d5e9 100644
--- a/src/micropub/util.rs
+++ b/src/micropub/util.rs
@@ -1,7 +1,7 @@
 use crate::database::Storage;
-use kittybox_indieauth::TokenData;
 use chrono::prelude::*;
 use core::iter::Iterator;
+use kittybox_indieauth::TokenData;
 use newbase60::num_to_sxg;
 use serde_json::json;
 use std::convert::TryInto;
@@ -33,7 +33,12 @@ fn reset_dt(post: &mut serde_json::Value) -> DateTime<FixedOffset> {
     chrono::DateTime::from(curtime)
 }
 
-pub fn normalize_mf2(mut body: serde_json::Value, user: &TokenData) -> (String, serde_json::Value) {
+pub struct NormalizedPost {
+    pub id: String,
+    pub post: serde_json::Value,
+}
+
+pub fn normalize_mf2(mut body: serde_json::Value, user: &TokenData) -> NormalizedPost {
     // Normalize the MF2 object here.
     let me = &user.me;
     let folder = get_folder_from_type(body["type"][0].as_str().unwrap());
@@ -137,12 +142,12 @@ pub fn normalize_mf2(mut body: serde_json::Value, user: &TokenData) -> (String,
     }
     // 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)
-    ) {
+    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...
@@ -176,10 +181,10 @@ pub fn normalize_mf2(mut body: serde_json::Value, user: &TokenData) -> (String,
     }
     // TODO: maybe highlight #hashtags?
     // Find other processing to do and insert it here
-    return (
-        body["properties"]["uid"][0].as_str().unwrap().to_string(),
-        body,
-    );
+    NormalizedPost {
+        id: body["properties"]["uid"][0].as_str().unwrap().to_string(),
+        post: body,
+    }
 }
 
 pub(crate) fn form_to_mf2_json(form: Vec<(String, String)>) -> serde_json::Value {
@@ -219,7 +224,7 @@ pub(crate) async fn create_feed(
         _ => panic!("Tried to create an unknown default feed!"),
     };
 
-    let (_, feed) = normalize_mf2(
+    let NormalizedPost { id: _, post: feed } = normalize_mf2(
         json!({
             "type": ["h-feed"],
             "properties": {
@@ -244,7 +249,7 @@ mod tests {
             client_id: "https://quill.p3k.io/".parse().unwrap(),
             scope: kittybox_indieauth::Scopes::new(vec![kittybox_indieauth::Scope::Create]),
             exp: Some(u64::MAX),
-            iat: Some(0)
+            iat: Some(0),
         }
     }
 
@@ -274,12 +279,15 @@ mod tests {
             }
         });
 
-        let (uid, normalized) = normalize_mf2(
-            mf2.clone(),
-            &token_data()
-        );
+        let NormalizedPost {
+            id: _,
+            post: normalized,
+        } = normalize_mf2(mf2.clone(), &token_data());
         assert!(
-            normalized["properties"]["channel"].as_array().unwrap_or(&vec![]).is_empty(),
+            normalized["properties"]["channel"]
+                .as_array()
+                .unwrap_or(&vec![])
+                .is_empty(),
             "Returned post was added to a channel despite the `unlisted` visibility"
         );
     }
@@ -295,16 +303,16 @@ mod tests {
             }
         });
 
-        let (uid, normalized) = normalize_mf2(
-            mf2.clone(),
-            &token_data(),
-        );
+        let NormalizedPost {
+            id,
+            post: normalized,
+        } = normalize_mf2(mf2.clone(), &token_data());
         assert_eq!(
             normalized["properties"]["uid"][0], mf2["properties"]["uid"][0],
             "UID was replaced"
         );
         assert_eq!(
-            normalized["properties"]["uid"][0], uid,
+            normalized["properties"]["uid"][0], id,
             "Returned post location doesn't match UID"
         );
     }
@@ -320,10 +328,10 @@ mod tests {
             }
         });
 
-        let (_, normalized) = normalize_mf2(
-            mf2.clone(),
-            &token_data(),
-        );
+        let NormalizedPost {
+            id: _,
+            post: normalized,
+        } = normalize_mf2(mf2.clone(), &token_data());
 
         assert_eq!(
             normalized["properties"]["channel"],
@@ -342,10 +350,10 @@ mod tests {
             }
         });
 
-        let (_, normalized) = normalize_mf2(
-            mf2.clone(),
-            &token_data(),
-        );
+        let NormalizedPost {
+            id: _,
+            post: normalized,
+        } = normalize_mf2(mf2.clone(), &token_data());
 
         assert_eq!(
             normalized["properties"]["channel"][0],
@@ -362,10 +370,7 @@ mod tests {
             }
         });
 
-        let (uid, post) = normalize_mf2(
-            mf2,
-            &token_data(),
-        );
+        let NormalizedPost { id, post } = normalize_mf2(mf2, &token_data());
         assert_eq!(
             post["properties"]["published"]
                 .as_array()
@@ -392,11 +397,11 @@ mod tests {
             "Post doesn't have a single UID"
         );
         assert_eq!(
-            post["properties"]["uid"][0], uid,
+            post["properties"]["uid"][0], id,
             "UID of a post and its supposed location don't match"
         );
         assert!(
-            uid.starts_with("https://fireburn.ru/posts/"),
+            id.starts_with("https://fireburn.ru/posts/"),
             "The post namespace is incorrect"
         );
         assert_eq!(
@@ -427,10 +432,7 @@ mod tests {
             },
         });
 
-        let (_, post) = normalize_mf2(
-            mf2,
-            &token_data(),
-        );
+        let NormalizedPost { id: _, post } = normalize_mf2(mf2, &token_data());
         assert!(
             post["properties"]["url"]
                 .as_array()
@@ -456,12 +458,9 @@ mod tests {
             }
         });
 
-        let (uid, post) = normalize_mf2(
-            mf2,
-            &token_data(),
-        );
+        let NormalizedPost { id, post } = normalize_mf2(mf2, &token_data());
         assert_eq!(
-            post["properties"]["uid"][0], uid,
+            post["properties"]["uid"][0], id,
             "UID of a post and its supposed location don't match"
         );
         assert_eq!(post["properties"]["author"][0], "https://fireburn.ru/");