about summary refs log tree commit diff
path: root/src/micropub
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2025-04-09 23:06:25 +0300
committerVika <vika@fireburn.ru>2025-04-09 23:31:57 +0300
commitfa6e3fef17de18f80b5148b443d5f79f35d2dde2 (patch)
tree30d71a5fc640f4e5e24b57e6a78956feeb02d4d9 /src/micropub
parent6f47cd5b5ed220f3fcfe6566166f774ccd26d0c3 (diff)
downloadkittybox-fa6e3fef17de18f80b5148b443d5f79f35d2dde2.tar.zst
Replace tuple from `normalize_mf2` with `NormalizedPost` struct
This is a little bit more idiomatic.

Perhaps I should consider doing the same with other tuples I return?

Change-Id: I85f0e5dc76b8212ab6d192376d8c38ce2048ae85
Diffstat (limited to 'src/micropub')
-rw-r--r--src/micropub/mod.rs21
-rw-r--r--src/micropub/util.rs39
2 files changed, 33 insertions, 27 deletions
diff --git a/src/micropub/mod.rs b/src/micropub/mod.rs
index 719fbf0..8505ae5 100644
--- a/src/micropub/mod.rs
+++ b/src/micropub/mod.rs
@@ -1,5 +1,6 @@
 use std::collections::HashMap;
 use url::Url;
+use util::NormalizedPost;
 use std::sync::Arc;
 
 use crate::database::{MicropubChannel, Storage, StorageError};
@@ -39,7 +40,7 @@ impl From<StorageError> for MicropubError {
     }
 }
 
-mod util;
+pub(crate) mod util;
 pub(crate) use util::normalize_mf2;
 
 #[derive(Debug)]
@@ -591,8 +592,8 @@ pub(crate) async fn post<D: Storage + 'static, A: AuthBackend>(
             Err(err) => err.into_response(),
         },
         Ok(PostBody::MF2(mf2)) => {
-            let (uid, mf2) = normalize_mf2(mf2, &user);
-            match _post(&user, uid, mf2, db, http, jobset).await {
+            let NormalizedPost { id, post } = normalize_mf2(mf2, &user);
+            match _post(&user, id, post, db, http, jobset).await {
                 Ok(response) => response,
                 Err(err) => err.into_response(),
             }
@@ -764,7 +765,7 @@ impl MicropubQuery {
 mod tests {
     use std::sync::Arc;
 
-    use crate::{database::Storage, micropub::MicropubError};
+    use crate::{database::Storage, micropub::{util::NormalizedPost, MicropubError}};
     use bytes::Bytes;
     use futures::StreamExt;
     use serde_json::json;
@@ -831,10 +832,10 @@ mod tests {
             scope: Scopes::new(vec![Scope::Profile]),
             iat: None, exp: None
         };
-        let (uid, mf2) = super::normalize_mf2(post, &user);
+        let NormalizedPost { id, post } = super::normalize_mf2(post, &user);
 
         let err = super::_post(
-            &user, uid, mf2, db.clone(),
+            &user, id, post, db.clone(),
             reqwest_middleware::ClientWithMiddleware::new(
                 reqwest::Client::new(),
                 Box::default()
@@ -868,10 +869,10 @@ mod tests {
             scope: Scopes::new(vec![Scope::Profile, Scope::Create, Scope::Update, Scope::Media]),
             iat: None, exp: None
         };
-        let (uid, mf2) = super::normalize_mf2(post, &user);
+        let NormalizedPost { id, post } = super::normalize_mf2(post, &user);
 
         let err = super::_post(
-            &user, uid, mf2, db.clone(),
+            &user, id, post, db.clone(),
             reqwest_middleware::ClientWithMiddleware::new(
                 reqwest::Client::new(),
                 Box::default()
@@ -903,10 +904,10 @@ mod tests {
             scope: Scopes::new(vec![Scope::Profile, Scope::Create]),
             iat: None, exp: None
         };
-        let (uid, mf2) = super::normalize_mf2(post, &user);
+        let NormalizedPost { id, post } = super::normalize_mf2(post, &user);
 
         let res = super::_post(
-            &user, uid, mf2, db.clone(),
+            &user, id, post, db.clone(),
             reqwest_middleware::ClientWithMiddleware::new(
                 reqwest::Client::new(),
                 Box::default()
diff --git a/src/micropub/util.rs b/src/micropub/util.rs
index 19f4953..99aec8e 100644
--- a/src/micropub/util.rs
+++ b/src/micropub/util.rs
@@ -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());
@@ -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": {
@@ -274,7 +279,7 @@ mod tests {
             }
         });
 
-        let (uid, normalized) = normalize_mf2(
+        let NormalizedPost { id: _, post: normalized } = normalize_mf2(
             mf2.clone(),
             &token_data()
         );
@@ -295,7 +300,7 @@ mod tests {
             }
         });
 
-        let (uid, normalized) = normalize_mf2(
+        let NormalizedPost { id, post: normalized } = normalize_mf2(
             mf2.clone(),
             &token_data(),
         );
@@ -304,7 +309,7 @@ mod tests {
             "UID was replaced"
         );
         assert_eq!(
-            normalized["properties"]["uid"][0], uid,
+            normalized["properties"]["uid"][0], id,
             "Returned post location doesn't match UID"
         );
     }
@@ -320,7 +325,7 @@ mod tests {
             }
         });
 
-        let (_, normalized) = normalize_mf2(
+        let NormalizedPost { id: _, post: normalized } = normalize_mf2(
             mf2.clone(),
             &token_data(),
         );
@@ -342,7 +347,7 @@ mod tests {
             }
         });
 
-        let (_, normalized) = normalize_mf2(
+        let NormalizedPost { id: _, post: normalized } = normalize_mf2(
             mf2.clone(),
             &token_data(),
         );
@@ -362,7 +367,7 @@ mod tests {
             }
         });
 
-        let (uid, post) = normalize_mf2(
+        let NormalizedPost { id, post } = normalize_mf2(
             mf2,
             &token_data(),
         );
@@ -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,7 +432,7 @@ mod tests {
             },
         });
 
-        let (_, post) = normalize_mf2(
+        let NormalizedPost { id: _, post } = normalize_mf2(
             mf2,
             &token_data(),
         );
@@ -456,12 +461,12 @@ mod tests {
             }
         });
 
-        let (uid, post) = normalize_mf2(
+        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/");