diff options
Diffstat (limited to 'src/micropub/util.rs')
-rw-r--r-- | src/micropub/util.rs | 39 |
1 files changed, 22 insertions, 17 deletions
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/"); |