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/redis/mod.rs | 71 ++++++----------------------------------------- 1 file changed, 9 insertions(+), 62 deletions(-) (limited to 'src/database/redis/mod.rs') diff --git a/src/database/redis/mod.rs b/src/database/redis/mod.rs index 5b3506a..f1724b7 100644 --- a/src/database/redis/mod.rs +++ b/src/database/redis/mod.rs @@ -12,7 +12,7 @@ use mobc_redis::RedisConnectionManager; use serde_json::json; use std::time::Duration; -use crate::database::{ErrorKind, MicropubChannel, Result, Storage, StorageError}; +use crate::database::{ErrorKind, MicropubChannel, Result, Storage, StorageError, filter_post}; use crate::indieauth::User; struct RedisScripts { @@ -56,55 +56,6 @@ pub struct RedisStorage { redis: mobc::Pool, } -fn filter_post(mut post: serde_json::Value, user: &'_ Option) -> Option { - if post["properties"]["deleted"][0].is_string() { - return Some(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) -} - #[async_trait] impl Storage for RedisStorage { async fn get_setting<'a>(&self, setting: &'a str, user: &'a str) -> Result { @@ -265,18 +216,14 @@ impl Storage for RedisStorage { } if feed["children"].is_array() { let children = feed["children"].as_array().unwrap(); - let posts_iter: Box + Send>; - // TODO: refactor this to apply the skip on the &mut iterator - if let Some(after) = after { - posts_iter = Box::new( - children - .iter() - .map(|i| i.as_str().unwrap().to_string()) - .skip_while(move |i| i != after) - .skip(1), - ); - } else { - posts_iter = Box::new(children.iter().map(|i| i.as_str().unwrap().to_string())); + let mut posts_iter = children.iter().map(|i| i.as_str().unwrap().to_string()); + if after.is_some() { + loop { + let i = posts_iter.next(); + if &i == after { + break; + } + } } let posts = stream::iter(posts_iter) .map(|url| async move { -- cgit 1.4.1