about summary refs log tree commit diff
path: root/src/database/redis
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2021-09-27 17:10:54 +0300
committerVika <vika@fireburn.ru>2021-09-27 17:10:54 +0300
commitf894d1746b94d60cd22260b933948f4169ece9ae (patch)
treeba6343dade34182d81f1483a56685d17f4fe3ba4 /src/database/redis
parent5d635d9e9ae466ca52d1923fafdc74487030e975 (diff)
downloadkittybox-f894d1746b94d60cd22260b933948f4169ece9ae.tar.zst
Implemented support for channels
Diffstat (limited to 'src/database/redis')
-rw-r--r--src/database/redis/mod.rs71
1 files changed, 9 insertions, 62 deletions
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<RedisConnectionManager>,
 }
 
-fn filter_post(mut post: serde_json::Value, user: &'_ Option<String>) -> Option<serde_json::Value> {
-    if post["properties"]["deleted"][0].is_string() {
-        return Some(json!({
-            "type": post["type"],
-            "properties": {
-                "deleted": post["properties"]["deleted"]
-            }
-        }));
-    }
-    let empty_vec: Vec<serde_json::Value> = 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<String> {
@@ -265,18 +216,14 @@ impl Storage for RedisStorage {
         }
         if feed["children"].is_array() {
             let children = feed["children"].as_array().unwrap();
-            let posts_iter: Box<dyn std::iter::Iterator<Item = String> + 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 {