From 0ede2093c52c9b62c91bd3fbd2c0f8a0c6eb09b8 Mon Sep 17 00:00:00 2001 From: silverpill Date: Wed, 11 Jan 2023 20:04:47 +0000 Subject: [PATCH] Ensure get_profile_by_remote_actor_id returns profile with actor data --- src/models/profiles/queries.rs | 9 ++++----- src/models/profiles/types.rs | 8 ++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/models/profiles/queries.rs b/src/models/profiles/queries.rs index ea52a65..97acac2 100644 --- a/src/models/profiles/queries.rs +++ b/src/models/profiles/queries.rs @@ -130,7 +130,7 @@ pub async fn get_profile_by_remote_actor_id( db_client: &impl GenericClient, actor_id: &str, ) -> Result { - let result = db_client.query_opt( + let maybe_row = db_client.query_opt( " SELECT actor_profile FROM actor_profile @@ -138,10 +138,9 @@ pub async fn get_profile_by_remote_actor_id( ", &[&actor_id], ).await?; - let profile = match result { - Some(row) => row.try_get("actor_profile")?, - None => return Err(DatabaseError::NotFound("profile")), - }; + let row = maybe_row.ok_or(DatabaseError::NotFound("profile"))?; + let profile: DbActorProfile = row.try_get("actor_profile")?; + profile.check_remote()?; Ok(profile) } diff --git a/src/models/profiles/types.rs b/src/models/profiles/types.rs index 3ad601b..52b54a5 100644 --- a/src/models/profiles/types.rs +++ b/src/models/profiles/types.rs @@ -332,6 +332,14 @@ pub struct DbActorProfile { // identity proofs: TBD (likely will do "Trust on first use" (TOFU)) impl DbActorProfile { + pub fn check_remote(&self) -> Result<(), DatabaseTypeError> { + // Consistency checks + if self.hostname.is_none() || self.actor_json.is_none() { + return Err(DatabaseTypeError); + }; + Ok(()) + } + pub fn is_local(&self) -> bool { self.actor_json.is_none() }