From 4aa7f01da39ab55b4f6346e7565d8bb29566de39 Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 5 Dec 2021 23:00:01 +0300 Subject: Code cleanup and small bugfixing in templates --- src/database/mod.rs | 58 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'src/database/mod.rs') 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 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 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) -> Self { + fn with_source( + kind: ErrorKind, + msg: &str, + source: Box, + ) -> 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 = std::result::Result; -pub fn filter_post(mut post: serde_json::Value, user: &'_ Option) -> Option { +/// 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, +) -> Option { 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: 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); -- cgit 1.4.1