From 1b711a88ef567db8b93edbf9e2b73489df4f4b41 Mon Sep 17 00:00:00 2001
From: Paul Makles <paulmakles@gmail.com>
Date: Sun, 3 Jan 2021 20:27:23 +0000
Subject: [PATCH] Include relationship when fetching a user.

---
 src/routes/users/fetch_user.rs | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/routes/users/fetch_user.rs b/src/routes/users/fetch_user.rs
index acbbfde..5979ed9 100644
--- a/src/routes/users/fetch_user.rs
+++ b/src/routes/users/fetch_user.rs
@@ -1,4 +1,4 @@
-use crate::database::entities::User;
+use crate::database::entities::{RelationshipStatus, User};
 use crate::database::guards::reference::Ref;
 use crate::util::result::{Error, Result};
 use rocket_contrib::json::JsonValue;
@@ -16,6 +16,19 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
 
         // Only return user relationships if the target is the caller.
         target.relations = None;
+
+        // Add relevant relationship
+        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))
-- 
GitLab