From 653088376a61a5cfe7508620225dc38e83dc5208 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 4 Jun 2022 20:57:48 +0530 Subject: [PATCH] feat: def interface to get user data --- db/db-core/src/lib.rs | 17 +++++++++++++++++ db/db-core/src/tests.rs | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index 3147684..bdf79dd 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -73,6 +73,20 @@ pub fn get_hostname(url: &Url) -> String { url.host().as_ref().unwrap().to_string() } +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +/// user data +pub struct User { + /// hostname of the forge instance: with scheme but remove trailing slash + /// hostname can be derived from html_link also, but used to link to user's forge instance + pub hostname: String, + /// username of the user + pub username: String, + /// html link to the user profile + pub html_link: String, + /// OPTIONAL: html link to the user's profile photo + pub profile_photo: Option, +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] /// add new user to database pub struct AddUser<'a> { @@ -186,6 +200,9 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { /// add new user to database async fn add_user(&self, u: &AddUser) -> DBResult<()>; + /// get user data + async fn get_user(&self, username: &str, hostname: &str) -> DBResult; + /// check if an user exists. When hostname of a forge instace is provided, username search is /// done only on that forge async fn user_exists(&self, username: &str, hostname: Option<&str>) -> DBResult; diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index 6258770..c347374 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -42,6 +42,19 @@ pub async fn adding_forge_works<'a, T: SCDatabase>( // add user db.add_user(&add_user_msg).await.unwrap(); db.add_user(&add_user_msg2).await.unwrap(); + { + let db_user = db + .get_user(add_user_msg.username, add_user_msg.hostname) + .await + .unwrap(); + assert_eq!(db_user.hostname, add_user_msg.hostname); + assert_eq!(db_user.username, add_user_msg.username); + assert_eq!(db_user.html_link, add_user_msg.html_link); + assert_eq!( + db_user.profile_photo, + add_user_msg.profile_photo.map(|s| s.to_owned()) + ); + } // verify user exists assert!(db.user_exists(add_user_msg.username, None).await.unwrap()); assert!(db