about summary refs log tree commit diff
path: root/src/database/mod.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2021-12-05 23:00:01 +0300
committerVika <vika@fireburn.ru>2021-12-05 23:00:01 +0300
commit4aa7f01da39ab55b4f6346e7565d8bb29566de39 (patch)
tree125c147e1a39dfebc4be1a3ac3a6dd8fadb3855e /src/database/mod.rs
parentc0b552318cb08872c7deae82a10122b0e16b7ae8 (diff)
Code cleanup and small bugfixing in templates
Diffstat (limited to 'src/database/mod.rs')
-rw-r--r--src/database/mod.rs58
1 files changed, 37 insertions, 21 deletions
diff --git a/src/database/mod.rs b/src/database/mod.rs
index a57e243..6a874ed 100644
--- a/src/database/mod.rs
+++ b/src/database/mod.rs
@@ -1,5 +1,4 @@
 #![warn(missing_docs)]
-use crate::indieauth::User;
 use async_trait::async_trait;
 use serde::{Deserialize, Serialize};
 
@@ -70,7 +69,9 @@ impl From<StorageError> for tide::Response {
 }
 impl std::error::Error for StorageError {
     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
-        self.source.as_ref().map(|e| e.as_ref() as &dyn std::error::Error)
+        self.source
+            .as_ref()
+            .map(|e| e.as_ref() as &dyn std::error::Error)
     }
 }
 impl From<serde_json::Error> for StorageError {
@@ -115,11 +116,15 @@ impl StorageError {
         }
     }
     /// Create a StorageError using another arbitrary Error as a source.
-    fn with_source(kind: ErrorKind, msg: &str, source: Box<dyn std::error::Error + Send + Sync>) -> Self {
+    fn with_source(
+        kind: ErrorKind,
+        msg: &str,
+        source: Box<dyn std::error::Error + Send + Sync>,
+    ) -> Self {
         Self {
             msg: msg.to_string(),
             source: Some(source),
-            kind
+            kind,
         }
     }
     /// Get the kind of an error.
@@ -135,7 +140,15 @@ impl StorageError {
 /// A special Result type for the Micropub backing storage.
 pub type Result<T> = std::result::Result<T, StorageError>;
 
-pub fn filter_post(mut post: serde_json::Value, user: &'_ Option<String>) -> Option<serde_json::Value> {
+/// Filter the post according to the value of `user`.
+///
+/// Anonymous users cannot view private posts and protected locations;
+/// Logged-in users can only view private posts targeted at them;
+/// Logged-in users can't view private location data
+pub fn filter_post(
+    mut post: serde_json::Value,
+    user: &'_ Option<String>,
+) -> Option<serde_json::Value> {
     if post["properties"]["deleted"][0].is_string() {
         return Some(serde_json::json!({
             "type": post["type"],
@@ -253,8 +266,8 @@ mod tests {
     //#[cfg(feature="redis")]
     //use super::redis::tests::get_redis_instance;
     use super::{MicropubChannel, Storage};
-    use serde_json::json;
     use paste::paste;
+    use serde_json::json;
 
     async fn test_backend_basic_operations<Backend: Storage>(backend: Backend) {
         let post: serde_json::Value = json!({
@@ -338,15 +351,21 @@ mod tests {
             .await
             .unwrap();
 
-        backend.update_post(&key, json!({
-            "url": &key,
-            "add": {
-                "category": ["testing"],
-            },
-            "replace": {
-                "content": ["Different test content"]
-            }
-        })).await.unwrap();
+        backend
+            .update_post(
+                &key,
+                json!({
+                    "url": &key,
+                    "add": {
+                        "category": ["testing"],
+                    },
+                    "replace": {
+                        "content": ["Different test content"]
+                    }
+                }),
+            )
+            .await
+            .unwrap();
 
         if let Some(returned_post) = backend.get_post(&key).await.unwrap() {
             assert!(returned_post.is_object());
@@ -385,10 +404,7 @@ mod tests {
             .put_post(&feed, "https://fireburn.ru/")
             .await
             .unwrap();
-        let chans = backend
-            .get_channels("https://fireburn.ru/")
-            .await
-            .unwrap();
+        let chans = backend.get_channels("https://fireburn.ru/").await.unwrap();
         assert_eq!(chans.len(), 1);
         assert_eq!(
             chans[0],
@@ -412,7 +428,7 @@ mod tests {
             "Vika's Hideout"
         );
     }
-    
+
     /*macro_rules! redis_test {
         ($func_name:expr) => {
             paste! {
@@ -441,7 +457,7 @@ mod tests {
                     $func_name(backend).await
                 }
             }
-        }
+        };
     }
 
     /*redis_test!(test_backend_basic_operations);