From 709f8cbf3c5146a0f53a6e9b6a4aeb3abd1aff35 Mon Sep 17 00:00:00 2001 From: Vika Date: Sat, 1 Jul 2023 20:40:38 +0300 Subject: micropub: use the new, better typed updates internally --- kittybox-rs/src/database/memory.rs | 111 +------------------------------------ 1 file changed, 2 insertions(+), 109 deletions(-) (limited to 'kittybox-rs/src/database/memory.rs') diff --git a/kittybox-rs/src/database/memory.rs b/kittybox-rs/src/database/memory.rs index 36e924f..ce98d05 100644 --- a/kittybox-rs/src/database/memory.rs +++ b/kittybox-rs/src/database/memory.rs @@ -88,115 +88,8 @@ impl Storage for MemoryStorage { Ok(()) } - async fn update_post(&self, url: &'_ str, update: serde_json::Value) -> Result<()> { - let mut add_keys: HashMap = HashMap::new(); - let mut remove_keys: Vec = vec![]; - let mut remove_values: HashMap> = HashMap::new(); - - if let Some(delete) = update["delete"].as_array() { - remove_keys.extend( - delete - .iter() - .filter_map(|v| v.as_str()) - .map(|v| v.to_string()), - ); - } else if let Some(delete) = update["delete"].as_object() { - for (k, v) in delete { - if let Some(v) = v.as_array() { - remove_values - .entry(k.to_string()) - .or_default() - .extend(v.clone()); - } else { - return Err(StorageError::from_static( - ErrorKind::BadRequest, - "Malformed update object", - )); - } - } - } - if let Some(add) = update["add"].as_object() { - for (k, v) in add { - if v.is_array() { - add_keys.insert(k.to_string(), v.clone()); - } else { - return Err(StorageError::from_static( - ErrorKind::BadRequest, - "Malformed update object", - )); - } - } - } - if let Some(replace) = update["replace"].as_object() { - for (k, v) in replace { - remove_keys.push(k.to_string()); - add_keys.insert(k.to_string(), v.clone()); - } - } - let mut mapping = self.mapping.write().await; - if let Some(mut post) = mapping.get(url) { - if let Some(url) = post["see_other"].as_str() { - if let Some(new_post) = mapping.get(url) { - post = new_post - } else { - return Err(StorageError::from_static( - ErrorKind::NotFound, - "The post you have requested is not found in the database.", - )); - } - } - let mut post = post.clone(); - for k in remove_keys { - post["properties"].as_object_mut().unwrap().remove(&k); - } - for (k, v) in remove_values { - let k = &k; - let props = if k == "children" { - &mut post - } else { - &mut post["properties"] - }; - v.iter().for_each(|v| { - if let Some(vec) = props[k].as_array_mut() { - if let Some(index) = vec.iter().position(|w| w == v) { - vec.remove(index); - } - } - }); - } - for (k, v) in add_keys { - let props = if k == "children" { - &mut post - } else { - &mut post["properties"] - }; - let k = &k; - if let Some(prop) = props[k].as_array_mut() { - if k == "children" { - v.as_array() - .unwrap() - .iter() - .cloned() - .rev() - .for_each(|v| prop.insert(0, v)); - } else { - prop.extend(v.as_array().unwrap().iter().cloned()); - } - } else { - post["properties"][k] = v - } - } - mapping.insert( - post["properties"]["uid"][0].as_str().unwrap().to_string(), - post, - ); - } else { - return Err(StorageError::from_static( - ErrorKind::NotFound, - "The designated post wasn't found in the database.", - )); - } - Ok(()) + async fn update_post(&self, url: &'_ str, update: crate::micropub::MicropubUpdate) -> Result<()> { + todo!() } async fn get_channels(&self, user: &'_ str) -> Result> { -- cgit 1.4.1