mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Add utility function for including relationship on User.
This commit is contained in:
@@ -36,6 +36,24 @@ pub struct User {
|
||||
}
|
||||
|
||||
impl User {
|
||||
/// Mutate the user object to include relationship as seen by user.
|
||||
pub fn from(mut self, user: &User) -> User {
|
||||
if self.id == user.id {
|
||||
self.relationship = Some(RelationshipStatus::User);
|
||||
return self;
|
||||
}
|
||||
|
||||
if let Some(relations) = &user.relations {
|
||||
if let Some(relationship) = relations.iter().find(|x| self.id == x.id) {
|
||||
self.relationship = Some(relationship.status.clone());
|
||||
return self;
|
||||
}
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
/// Mutate the user object to appear as seen by user.
|
||||
pub fn with(mut self, permissions: UserPermissions<[u32; 1]>) -> User {
|
||||
if !permissions.get_view_all() {
|
||||
self.relations = None;
|
||||
|
||||
@@ -65,6 +65,7 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
|
||||
}
|
||||
}
|
||||
|
||||
user_ids.remove(&user.id);
|
||||
if user_ids.len() > 0 {
|
||||
let mut cursor = get_collection("users")
|
||||
.find(
|
||||
@@ -85,33 +86,27 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
|
||||
|
||||
while let Some(result) = cursor.next().await {
|
||||
if let Ok(doc) = result {
|
||||
let mut other: User = from_document(doc).map_err(|_| Error::DatabaseError {
|
||||
let other: User = from_document(doc).map_err(|_| Error::DatabaseError {
|
||||
operation: "from_document",
|
||||
with: "user",
|
||||
})?;
|
||||
|
||||
if let Some(relationships) = &user.relations {
|
||||
other.relationship = Some(
|
||||
if let Some(relationship) = relationships.iter().find(|x| other.id == x.id)
|
||||
{
|
||||
relationship.status.clone()
|
||||
} else {
|
||||
RelationshipStatus::None
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
let permissions = PermissionCalculator::new(&user)
|
||||
.with_mutual_connection()
|
||||
.with_user(&other)
|
||||
.for_user_given()
|
||||
.await?;
|
||||
|
||||
users.push(other.with(permissions));
|
||||
users.push(
|
||||
other
|
||||
.from(&user)
|
||||
.with(permissions)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
user.relationship = Some(RelationshipStatus::User);
|
||||
user.online = Some(true);
|
||||
users.push(user);
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[get("/<target>")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
let mut target = target.fetch_user().await?;
|
||||
let target = target.fetch_user().await?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_user(&target)
|
||||
@@ -16,19 +16,9 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
Err(Error::LabelMe)?
|
||||
}
|
||||
|
||||
if user.id != target.id {
|
||||
if let Some(relationships) = &user.relations {
|
||||
target.relationship = relationships
|
||||
.iter()
|
||||
.find(|x| x.id == user.id)
|
||||
.map(|x| x.status.clone())
|
||||
.or_else(|| Some(RelationshipStatus::None));
|
||||
} else {
|
||||
target.relationship = Some(RelationshipStatus::None);
|
||||
}
|
||||
} else {
|
||||
target.relationship = Some(RelationshipStatus::User);
|
||||
}
|
||||
|
||||
Ok(json!(target.with(perm)))
|
||||
Ok(json!(
|
||||
target
|
||||
.from(&user)
|
||||
.with(perm)
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user