Add check that people can message others in guild.

This commit is contained in:
Paul Makles
2020-04-12 12:16:00 +01:00
parent 2063eeac3d
commit 87e7df02f2
5 changed files with 57 additions and 21 deletions

View File

@@ -1,7 +1,7 @@
use super::get_collection;
use super::{get_collection, MemberPermissions};
use bson::doc;
use mongodb::options::{FindOneOptions, FindOptions};
use mongodb::options::FindOptions;
pub fn find_mutual_guilds(user_id: &str, target_id: &str) -> Vec<String> {
let col = get_collection("members");
@@ -79,23 +79,51 @@ pub fn find_mutual_groups(user_id: &str, target_id: &str) -> Vec<String> {
}
}
pub fn has_mutual_connection(user_id: &str, target_id: &str) -> bool {
let col = get_collection("guilds");
if let Ok(result) = col.find_one(
pub fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bool) -> bool {
let mut doc = doc! { "_id": 1 };
if with_permission {
doc.insert("default_permissions", 1);
}
let opt = FindOptions::builder().projection(doc);
if let Ok(result) = get_collection("guilds").find(
doc! {
"$and": [
{ "members": { "$elemMatch": { "id": user_id } } },
{ "members": { "$elemMatch": { "id": target_id } } },
]
},
FindOneOptions::builder()
.projection(doc! { "_id": 1 }) // ? TODO: fetch permissions
.build(),
) {
if result.is_some() {
true
if with_permission {
opt.build()
} else {
opt.limit(1).build()
},
) {
if with_permission {
for item in result {
// ? logic should match permissions.rs#calculate
if let Ok(guild) = item {
if guild.get_str("owner").unwrap() == user_id {
return true;
}
let permissions = guild.get_i32("default_permissions").unwrap() as u32;
if MemberPermissions([ permissions ]).get_send_direct_messages() {
return true;
}
}
}
false
} else {
if result.count() > 0 {
true
} else {
false
}
}
} else {
false