From f894d1746b94d60cd22260b933948f4169ece9ae Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 27 Sep 2021 17:10:54 +0300 Subject: Implemented support for channels --- src/database/mod.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/database/mod.rs') diff --git a/src/database/mod.rs b/src/database/mod.rs index a1f5861..0b97c24 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -122,6 +122,55 @@ 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 { + if post["properties"]["deleted"][0].is_string() { + return Some(serde_json::json!({ + "type": post["type"], + "properties": { + "deleted": post["properties"]["deleted"] + } + })); + } + let empty_vec: Vec = vec![]; + let author = post["properties"]["author"] + .as_array() + .unwrap_or(&empty_vec) + .iter() + .map(|i| i.as_str().unwrap().to_string()); + let visibility = post["properties"]["visibility"][0] + .as_str() + .unwrap_or("public"); + let mut audience = author.chain( + post["properties"]["audience"] + .as_array() + .unwrap_or(&empty_vec) + .iter() + .map(|i| i.as_str().unwrap().to_string()), + ); + if (visibility == "private" && !audience.any(|i| Some(i) == *user)) + || (visibility == "protected" && user.is_none()) + { + return None; + } + if post["properties"]["location"].is_array() { + let location_visibility = post["properties"]["location-visibility"][0] + .as_str() + .unwrap_or("private"); + let mut author = post["properties"]["author"] + .as_array() + .unwrap_or(&empty_vec) + .iter() + .map(|i| i.as_str().unwrap().to_string()); + if location_visibility == "private" && !author.any(|i| Some(i) == *user) { + post["properties"] + .as_object_mut() + .unwrap() + .remove("location"); + } + } + Some(post) +} + /// A storage backend for the Micropub server. /// /// Implementations should note that all methods listed on this trait MUST be fully atomic -- cgit 1.4.1