Switch to async Rust and break all logic.

This commit is contained in:
Paul Makles
2020-12-27 13:28:37 +00:00
parent 5711986768
commit 6cfec0ee08
26 changed files with 1141 additions and 1256 deletions

View File

@@ -1,11 +1,12 @@
use super::{get_collection, MemberPermissions};
use mongodb::bson::doc;
use mongodb::bson::{doc, Document};
use mongodb::options::FindOptions;
use rocket::futures::StreamExt;
pub fn find_mutual_guilds(user_id: &str, target_id: &str) -> Vec<String> {
pub async fn find_mutual_guilds(user_id: &str, target_id: &str) -> Vec<String> {
let col = get_collection("members");
if let Ok(result) = col.find(
if let Ok(mut result) = col.find(
doc! {
"$and": [
{ "id": user_id },
@@ -13,10 +14,10 @@ pub fn find_mutual_guilds(user_id: &str, target_id: &str) -> Vec<String> {
]
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
) {
).await {
let mut results = vec![];
for doc in result {
while let Some(doc) = result.next().await {
if let Ok(guild) = doc {
results.push(guild.get_str("_id").unwrap().to_string());
}
@@ -28,9 +29,9 @@ pub fn find_mutual_guilds(user_id: &str, target_id: &str) -> Vec<String> {
}
}
pub fn find_mutual_friends(user_id: &str, target_id: &str) -> Vec<String> {
pub async fn find_mutual_friends(user_id: &str, target_id: &str) -> Vec<String> {
let col = get_collection("users");
if let Ok(result) = col.find(
if let Ok(mut result) = col.find(
doc! {
"$and": [
{ "relations": { "$elemMatch": { "id": user_id, "status": 0 } } },
@@ -38,10 +39,10 @@ pub fn find_mutual_friends(user_id: &str, target_id: &str) -> Vec<String> {
]
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
) {
).await {
let mut results = vec![];
for doc in result {
while let Some(doc) = result.next().await {
if let Ok(user) = doc {
results.push(user.get_str("_id").unwrap().to_string());
}
@@ -53,9 +54,9 @@ pub fn find_mutual_friends(user_id: &str, target_id: &str) -> Vec<String> {
}
}
pub fn find_mutual_groups(user_id: &str, target_id: &str) -> Vec<String> {
pub async fn find_mutual_groups(user_id: &str, target_id: &str) -> Vec<String> {
let col = get_collection("channels");
if let Ok(result) = col.find(
if let Ok(mut result) = col.find(
doc! {
"type": 1,
"$and": [
@@ -64,10 +65,10 @@ pub fn find_mutual_groups(user_id: &str, target_id: &str) -> Vec<String> {
]
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
) {
).await {
let mut results = vec![];
for doc in result {
while let Some(doc) = result.next().await {
if let Ok(group) = doc {
results.push(group.get_str("_id").unwrap().to_string());
}
@@ -79,7 +80,7 @@ pub fn find_mutual_groups(user_id: &str, target_id: &str) -> Vec<String> {
}
}
pub fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bool) -> bool {
pub async fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bool) -> bool {
let mut doc = doc! { "_id": 1 };
if with_permission {
@@ -88,7 +89,7 @@ pub fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bo
let opt = FindOptions::builder().projection(doc);
if let Ok(result) = get_collection("guilds").find(
if let Ok(mut result) = get_collection("guilds").find(
doc! {
"$and": [
{ "members": { "$elemMatch": { "id": user_id } } },
@@ -100,9 +101,9 @@ pub fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bo
} else {
opt.limit(1).build()
},
) {
).await {
if with_permission {
for item in result {
while let Some(item) = result.next().await {
// ? logic should match permissions.rs#calculate
if let Ok(guild) = item {
if guild.get_str("owner").unwrap() == user_id {
@@ -118,7 +119,7 @@ pub fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bo
}
false
} else if result.count() > 0 {
} else if result.collect::<Vec<Result<Document, _>>>().await.len() > 0 {
true
} else {
false