about summary refs log tree commit diff
path: root/src/database/redis
diff options
context:
space:
mode:
Diffstat (limited to 'src/database/redis')
-rw-r--r--src/database/redis/mod.rs30
1 files changed, 12 insertions, 18 deletions
diff --git a/src/database/redis/mod.rs b/src/database/redis/mod.rs
index faf9c13..5ab93af 100644
--- a/src/database/redis/mod.rs
+++ b/src/database/redis/mod.rs
@@ -4,7 +4,6 @@ use futures_util::StreamExt;
 use futures::stream;
 use lazy_static::lazy_static;
 use log::error;
-use redis;
 use redis::AsyncCommands;
 use serde_json::json;
 
@@ -37,7 +36,7 @@ pub struct RedisStorage {
     redis: redis::Client,
 }
 
-fn filter_post<'a>(mut post: serde_json::Value, user: &'a Option<String>) -> Option<serde_json::Value> {
+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"],
@@ -50,10 +49,8 @@ fn filter_post<'a>(mut post: serde_json::Value, user: &'a Option<String>) -> Opt
     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" {
-        if !audience.any(|i| Some(i) == *user) {
-            return None
-        }
+    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");
@@ -147,27 +144,24 @@ impl Storage for RedisStorage {
                 let key: &str;
                 match post["properties"]["uid"][0].as_str() {
                     Some(uid) => key = uid,
-                    None => return Err(StorageError::new(ErrorKind::Other, "post doesn't have a UID"))
+                    None => return Err(StorageError::new(ErrorKind::BadRequest, "post doesn't have a UID"))
                 }        
-                match conn.hset::<&str, &str, String, ()>(&"posts", key, post.to_string()).await {
-                    Err(err) => return Err(err.into()),
-                    _ => {}
+                if let Err(err) = conn.hset::<&str, &str, String, ()>(&"posts", key, post.to_string()).await {
+                    return Err(err.into())
                 }
                 if post["properties"]["url"].is_array() {
                     for url in post["properties"]["url"].as_array().unwrap().iter().map(|i| i.as_str().unwrap().to_string()) {
-                        if &url != key {
-                            match conn.hset::<&str, &str, String, ()>(&"posts", &url, json!({"see_other": key}).to_string()).await {
-                                Err(err) => return Err(err.into()),
-                                _ => {}
+                        if url != key {
+                            if let Err(err) = conn.hset::<&str, &str, String, ()>(&"posts", &url, json!({"see_other": key}).to_string()).await {
+                                return Err(err.into())
                             }
                         }
                     }
                 }
                 if post["type"].as_array().unwrap().iter().any(|i| i == "h-feed") {
                     // This is a feed. Add it to the channels array if it's not already there.
-                    match conn.sadd::<String, &str, ()>("channels_".to_string() + post["properties"]["author"][0].as_str().unwrap(), key).await {
-                        Err(err) => return Err(err.into()),
-                        _ => {},
+                    if let Err(err) = conn.sadd::<String, &str, ()>("channels_".to_string() + post["properties"]["author"][0].as_str().unwrap(), key).await {
+                        return Err(err.into())
                     }
                 }
                 Ok(())
@@ -258,7 +252,7 @@ impl Storage for RedisStorage {
                         // Broken links return None, and Stream::filter_map skips all Nones.
                         .filter_map(|post: Option<serde_json::Value>| async move { post })
                         .filter_map(|post| async move {
-                            return filter_post(post, user)
+                            filter_post(post, user)
                         })
                         .take(limit);
                     match std::panic::AssertUnwindSafe(posts.collect::<Vec<serde_json::Value>>()).catch_unwind().await {