Add utility function for including relationship on User.

This commit is contained in:
Paul Makles
2021-01-26 22:33:14 +00:00
parent 3b85dcce14
commit 222a417fff
3 changed files with 32 additions and 29 deletions

View File

@@ -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)
))
}