From 77d9284e1debcd1db3ac1604cb2f0552f6609c91 Mon Sep 17 00:00:00 2001 From: Vika Date: Fri, 6 Aug 2021 09:09:15 +0300 Subject: Added a default food channel for foodstuffs --- src/micropub/post.rs | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/micropub/post.rs b/src/micropub/post.rs index 260b9aa..6906dc0 100644 --- a/src/micropub/post.rs +++ b/src/micropub/post.rs @@ -17,6 +17,8 @@ static DEFAULT_CHANNEL_PATH: &str = "/feeds/main"; static DEFAULT_CHANNEL_NAME: &str = "Main feed"; static CONTACTS_CHANNEL_PATH: &str = "/feeds/vcards"; static CONTACTS_CHANNEL_NAME: &str = "My address book"; +static FOOD_CHANNEL_PATH: &str = "/feeds/food"; +static FOOD_CHANNEL_NAME: &str = "My recipe book"; macro_rules! response { ($($code:expr, $json:tt)+) => { @@ -125,19 +127,31 @@ pub fn normalize_mf2(mut body: serde_json::Value, user: &User) -> (String, serde }]) } if body["properties"]["channel"][0].as_str().is_none() { - if body["type"][0] == "h-entry" { - // Set the channel to the main channel... - let default_channel = me.join(DEFAULT_CHANNEL_PATH).unwrap().to_string(); + match body["type"][0].as_str() { + Some("h-entry") => { + // Set the channel to the main channel... + let default_channel = me.join(DEFAULT_CHANNEL_PATH).unwrap().to_string(); - body["properties"]["channel"] = json!([default_channel]); - } else if body["type"][0] == "h-card" { - let default_channel = me.join(CONTACTS_CHANNEL_PATH).unwrap().to_string(); + body["properties"]["channel"] = json!([default_channel]); + }, + Some("h-card") => { + let default_channel = me.join(CONTACTS_CHANNEL_PATH).unwrap().to_string(); - body["properties"]["channel"] = json!([default_channel]); - } else { - body["properties"]["channel"] = json!([]); + body["properties"]["channel"] = json!([default_channel]); + }, + Some("h-food") => { + let default_channel = me.join(FOOD_CHANNEL_PATH).unwrap().to_string(); + + body["properties"]["channel"] = json!([default_channel]); + }, + // TODO h-event + /*"h-event" => { + let default_channel + },*/ + _ => { + body["properties"]["channel"] = json!([]); + } } - // TODO: Sort other types of posts into channels too } body["properties"]["posted-with"] = json!([user.client_id]); if body["properties"]["author"][0].as_str().is_none() { @@ -220,6 +234,7 @@ pub async fn new_post( { let default_channel = user.me.join(DEFAULT_CHANNEL_PATH).unwrap().to_string(); let vcards_channel = user.me.join(CONTACTS_CHANNEL_PATH).unwrap().to_string(); + let food_channel = user.me.join(FOOD_CHANNEL_PATH).unwrap().to_string(); match storage.post_exists(&channel).await { Ok(exists) => { if exists { @@ -243,7 +258,7 @@ pub async fn new_post( ) ); } - } else if channel == default_channel || channel == vcards_channel { + } else if channel == default_channel || channel == vcards_channel || channel == food_channel { if let Err(err) = create_feed(storage, &uid, &channel, &user).await { return error_json!( 500, @@ -279,12 +294,18 @@ async fn create_feed( user: &User, ) -> crate::database::Result<()> { let path = url::Url::parse(channel).unwrap().path().to_string(); + + // Note to Future Vika: DO NOT CONVERT THIS TO A MATCH BLOCK + // It will get treated as a binding instead of a const + // See `rustc --explain E0530` for more info let (name, slug) = if path == DEFAULT_CHANNEL_PATH { (DEFAULT_CHANNEL_NAME, "main") } else if path == CONTACTS_CHANNEL_PATH { (CONTACTS_CHANNEL_NAME, "vcards") + } else if path == FOOD_CHANNEL_PATH { + (FOOD_CHANNEL_NAME, "food") } else { - panic!("Tried to create an unknown default feed!"); + panic!("Tried to create an unknown default feed!") }; let (_, feed) = normalize_mf2( @@ -437,6 +458,8 @@ async fn post_process_new_post( if let Some(values) = response.header("Link") { let iter = values.iter().flat_map(|i| i.as_str().split(',')); + // Honestly I don't like this parser. It's very crude. + // But it should do the job. But I don't like it. for link in iter { let mut split = link.split(';'); -- cgit 1.4.1