forked from jmug/stoatchat
Display mutual connections when fetching user.
This commit is contained in:
@@ -1,17 +1,91 @@
|
|||||||
use super::get_collection;
|
use super::get_collection;
|
||||||
|
|
||||||
use bson::doc;
|
use bson::doc;
|
||||||
use mongodb::options::FindOneOptions;
|
use mongodb::options::{FindOptions, FindOneOptions};
|
||||||
|
|
||||||
/*pub struct MutualGuild {
|
pub fn find_mutual_guilds(user_id: &str, target_id: &str) -> Vec<String> {
|
||||||
|
let col = get_collection("guilds");
|
||||||
|
if let Ok(result) = col.find(
|
||||||
|
doc! {
|
||||||
|
"$and": [
|
||||||
|
{ "members": { "$elemMatch": { "id": user_id } } },
|
||||||
|
{ "members": { "$elemMatch": { "id": target_id } } },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
FindOptions::builder()
|
||||||
|
.projection(doc! { "_id": 1 })
|
||||||
|
.build(),
|
||||||
|
) {
|
||||||
|
let mut results = vec![];
|
||||||
|
|
||||||
|
for doc in result {
|
||||||
|
if let Ok(guild) = doc {
|
||||||
|
results.push(guild.get_str("_id").unwrap().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
results
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_mutual_guilds(user_id: String, target_id: String) -> Vec<> {
|
pub fn find_mutual_friends(user_id: &str, target_id: &str) -> Vec<String> {
|
||||||
|
let col = get_collection("users");
|
||||||
|
if let Ok(result) = col.find(
|
||||||
|
doc! {
|
||||||
|
"$and": [
|
||||||
|
{ "relations": { "$elemMatch": { "id": user_id, "status": 0 } } },
|
||||||
|
{ "relations": { "$elemMatch": { "id": target_id, "status": 0 } } },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
FindOptions::builder()
|
||||||
|
.projection(doc! { "_id": 1 })
|
||||||
|
.build(),
|
||||||
|
) {
|
||||||
|
let mut results = vec![];
|
||||||
|
|
||||||
}*/
|
for doc in result {
|
||||||
|
if let Ok(user) = doc {
|
||||||
|
results.push(user.get_str("_id").unwrap().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn has_mutual_connection(user_id: String, target_id: String) -> bool {
|
results
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find_mutual_groups(user_id: &str, target_id: &str) -> Vec<String> {
|
||||||
|
let col = get_collection("channels");
|
||||||
|
if let Ok(result) = col.find(
|
||||||
|
doc! {
|
||||||
|
"type": 1,
|
||||||
|
"$and": [
|
||||||
|
{ "recipients": user_id },
|
||||||
|
{ "recipients": target_id },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
FindOptions::builder()
|
||||||
|
.projection(doc! { "_id": 1 })
|
||||||
|
.build(),
|
||||||
|
) {
|
||||||
|
let mut results = vec![];
|
||||||
|
|
||||||
|
for doc in result {
|
||||||
|
if let Ok(group) = doc {
|
||||||
|
results.push(group.get_str("_id").unwrap().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
results
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_mutual_connection(user_id: &str, target_id: &str) -> bool {
|
||||||
let col = get_collection("guilds");
|
let col = get_collection("guilds");
|
||||||
if let Ok(result) = col.find_one(
|
if let Ok(result) = col.find_one(
|
||||||
doc! {
|
doc! {
|
||||||
@@ -21,7 +95,7 @@ pub fn has_mutual_connection(user_id: String, target_id: String) -> bool {
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
FindOneOptions::builder()
|
FindOneOptions::builder()
|
||||||
.projection(doc! { "_id": 1 })
|
.projection(doc! { "_id": 1 }) // ? TODO: fetch permissions
|
||||||
.build(),
|
.build(),
|
||||||
) {
|
) {
|
||||||
if result.is_some() {
|
if result.is_some() {
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ impl PermissionCalculator {
|
|||||||
|| relationship == Relationship::BlockedOther
|
|| relationship == Relationship::BlockedOther
|
||||||
{
|
{
|
||||||
permissions = 1;
|
permissions = 1;
|
||||||
} else if has_mutual_connection(self.user.id, other_user.to_string()) {
|
} else if has_mutual_connection(&self.user.id, other_user) {
|
||||||
permissions = 177;
|
permissions = 177;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use super::Response;
|
use super::Response;
|
||||||
use crate::database::{self, get_relationship, get_relationship_internal, Relationship};
|
use crate::database::{self, get_relationship, get_relationship_internal, Relationship, mutual};
|
||||||
use crate::guards::auth::UserRef;
|
use crate::guards::auth::UserRef;
|
||||||
use crate::routes::channel;
|
use crate::routes::channel;
|
||||||
|
|
||||||
@@ -32,7 +32,12 @@ pub fn user(user: UserRef, target: UserRef) -> Response {
|
|||||||
Response::Success(json!({
|
Response::Success(json!({
|
||||||
"id": target.id,
|
"id": target.id,
|
||||||
"username": target.username,
|
"username": target.username,
|
||||||
"relationship": get_relationship(&user, &target) as u8
|
"relationship": get_relationship(&user, &target) as u8,
|
||||||
|
"mutual": {
|
||||||
|
"guilds": mutual::find_mutual_guilds(&user.id, &target.id),
|
||||||
|
"friends": mutual::find_mutual_friends(&user.id, &target.id),
|
||||||
|
"groups": mutual::find_mutual_groups(&user.id, &target.id),
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user