From eb30f3bd0790fbf78aeb8cc8f33f76a0a114cc9d Mon Sep 17 00:00:00 2001 From: Vika Date: Fri, 23 Aug 2024 22:50:36 +0300 Subject: 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. --- src/micropub/util.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/micropub/util.rs') 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::>(); 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... @@ -246,6 +253,27 @@ mod tests { ) } + #[test] + fn test_unlisted_post() { + let mf2 = json!({ + "type": ["h-entry"], + "properties": { + "uid": ["https://fireburn.ru/posts/test"], + "content": [{"html": "

Hello world!

"}], + "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!({ -- cgit 1.4.1