about summary refs log tree commit diff
path: root/kittybox-rs/src/database/file/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'kittybox-rs/src/database/file/mod.rs')
-rw-r--r--kittybox-rs/src/database/file/mod.rs58
1 files changed, 17 insertions, 41 deletions
diff --git a/kittybox-rs/src/database/file/mod.rs b/kittybox-rs/src/database/file/mod.rs
index 3b373d8..0f63c9d 100644
--- a/kittybox-rs/src/database/file/mod.rs
+++ b/kittybox-rs/src/database/file/mod.rs
@@ -1,5 +1,6 @@
 //#![warn(clippy::unwrap_used)]
 use crate::database::{filter_post, ErrorKind, Result, settings, Storage, StorageError};
+use crate::micropub::{MicropubUpdate, MicropubPropertyDeletion};
 use async_trait::async_trait;
 use futures::{stream, StreamExt, TryStreamExt};
 use serde_json::json;
@@ -122,57 +123,32 @@ fn url_to_relative_path(url: &str) -> relative_path::RelativePathBuf {
     path
 }
 
-fn modify_post(post: &serde_json::Value, update: &serde_json::Value) -> Result<serde_json::Value> {
+fn modify_post(post: &serde_json::Value, update: MicropubUpdate) -> Result<serde_json::Value> {
+    let mut post = post.clone();
+
     let mut add_keys: HashMap<String, Vec<serde_json::Value>> = HashMap::new();
     let mut remove_keys: Vec<String> = vec![];
     let mut remove_values: HashMap<String, Vec<serde_json::Value>> = HashMap::new();
-    let mut post = post.clone();
 
-    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() {
+    if let Some(MicropubPropertyDeletion::Properties(delete)) = update.delete {
+        remove_keys.extend(delete.iter().cloned());
+    } else if let Some(MicropubPropertyDeletion::Values(delete)) = update.delete {
         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",
-                ));
-            }
+            remove_values
+                .entry(k.to_string())
+                .or_default()
+                .extend(v.clone());
         }
     }
-    if let Some(add) = update["add"].as_object() {
+    if let Some(add) = update.add {
         for (k, v) in add {
-            if let Some(v) = v.as_array() {
-                add_keys.insert(k.to_string(), v.clone());
-            } else {
-                return Err(StorageError::from_static(
-                    ErrorKind::BadRequest,
-                    "Malformed update object",
-                ));
-            }
+            add_keys.insert(k.to_string(), v.clone());
         }
     }
-    if let Some(replace) = update["replace"].as_object() {
+    if let Some(replace) = update.replace {
         for (k, v) in replace {
             remove_keys.push(k.to_string());
-            if let Some(v) = v.as_array() {
-                add_keys.insert(k.to_string(), v.clone());
-            } else {
-                return Err(StorageError::from_static(
-                    ErrorKind::BadRequest,
-                    "Malformed update object",
-                ));
-            }
+            add_keys.insert(k.to_string(), v.clone());
         }
     }
 
@@ -446,7 +422,7 @@ impl Storage for FileStorage {
     }
 
     #[tracing::instrument(skip(self))]
-    async fn update_post(&self, url: &'_ str, update: serde_json::Value) -> Result<()> {
+    async fn update_post(&self, url: &str, update: MicropubUpdate) -> Result<()> {
         let path = url_to_path(&self.root_dir, url);
         let tempfilename = path.with_extension("tmp");
         #[allow(unused_variables)]
@@ -463,7 +439,7 @@ impl Storage for FileStorage {
             let json: serde_json::Value = serde_json::from_str(&content)?;
             drop(file);
             // Apply the editing algorithms
-            let new_json = modify_post(&json, &update)?;
+            let new_json = modify_post(&json, update)?;
 
             temp.write_all(new_json.to_string().as_bytes()).await?;
             temp.flush().await?;