diff --git a/giterated-daemon/src/backend/mod.rs b/giterated-daemon/src/backend/mod.rs index 62275f4..93381c8 100644 --- a/giterated-daemon/src/backend/mod.rs +++ b/giterated-daemon/src/backend/mod.rs @@ -105,6 +105,6 @@ pub trait UserBackend: AuthBackend { async fn write_settings( &mut self, user: &User, - settings: &[(String, Value)], + settings: &[(String, String)], ) -> Result<(), Error>; } diff --git a/giterated-daemon/src/backend/user.rs b/giterated-daemon/src/backend/user.rs index 4010d86..ea5569a 100644 --- a/giterated-daemon/src/backend/user.rs +++ b/giterated-daemon/src/backend/user.rs @@ -17,7 +17,7 @@ use giterated_models::{ UserDisplayNameRequest, UserDisplayNameResponse, }, }, - model::{instance::Instance, user::User}, + model::{instance::Instance, user::User, settings::{UserBio, Setting, UserDisplayName}}, }; use rsa::{ pkcs8::{EncodePrivateKey, EncodePublicKey}, @@ -58,18 +58,17 @@ impl UserBackend for UserAuth { &mut self, request: UserDisplayNameRequest, ) -> Result { - let db_row = sqlx::query_as!( - UserRow, - r#"SELECT * FROM users WHERE username = $1"#, - request.user.username - ) - .fetch_one(&self.pg_pool.clone()) - .await - .unwrap(); + let settings = self.settings(&request.user).await?; - Ok(UserDisplayNameResponse { - display_name: db_row.display_name, - }) + let name = settings.iter().find(|setting| &setting.0 == UserDisplayName::name()); + + if let Some((_, name)) = name { + let name: UserDisplayName = serde_json::from_value(name.clone()).unwrap(); + + Ok(UserDisplayNameResponse { display_name: Some(name.0) }) + } else { + Ok(UserDisplayNameResponse { display_name: None }) + } } async fn display_image( @@ -91,15 +90,17 @@ impl UserBackend for UserAuth { } async fn bio(&mut self, request: UserBioRequest) -> Result { - let db_row = sqlx::query_as!( - UserRow, - r#"SELECT * FROM users WHERE username = $1"#, - request.user.username - ) - .fetch_one(&self.pg_pool.clone()) - .await?; + let settings = self.settings(&request.user).await?; + + let bio = settings.iter().find(|setting| &setting.0 == UserBio::name()); + + if let Some((_, bio)) = bio { + let bio: UserBio = serde_json::from_value(bio.clone()).unwrap(); - Ok(UserBioResponse { bio: db_row.bio }) + Ok(UserBioResponse { bio: Some(bio.0)}) + } else { + Ok(UserBioResponse { bio: None }) + } } async fn exists(&mut self, user: &User) -> Result { @@ -128,7 +129,7 @@ impl UserBackend for UserAuth { } }) .filter_map(|row| async move { - if let Ok(value) = serde_json::from_str(&row.value) { + if let Ok(value) = serde_json::from_str(&format!("\"{}\"", row.value)) { Some((row.name, value)) } else { None @@ -143,13 +144,11 @@ impl UserBackend for UserAuth { async fn write_settings( &mut self, user: &User, - settings: &[(String, Value)], + settings: &[(String, String)], ) -> Result<(), Error> { for (name, value) in settings { - let serialized = serde_json::to_string(value)?; - sqlx::query!("INSERT INTO user_settings VALUES ($1, $2, $3) ON CONFLICT (username, name) DO UPDATE SET value = $3", - user.username, name, serialized) + user.username, name, value) .execute(&self.pg_pool).await?; } diff --git a/giterated-models/src/messages/user.rs b/giterated-models/src/messages/user.rs index 070d936..5a5fe42 100644 --- a/giterated-models/src/messages/user.rs +++ b/giterated-models/src/messages/user.rs @@ -69,7 +69,7 @@ impl UserSettingsResponse { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserWriteSettingsRequest { pub user: User, - pub settings: Vec<(String, serde_json::Value)>, + pub settings: Vec<(String, String)>, } impl UserWriteSettingsRequest { @@ -83,7 +83,7 @@ impl UserWriteSettingsRequest { pub fn set(&mut self, setting: S) { self.settings.push(( S::name().to_string(), - serde_json::to_value(setting).unwrap(), + serde_json::to_string(&setting).unwrap(), )); } pub fn try_get(&self) -> Option { @@ -93,7 +93,7 @@ impl UserWriteSettingsRequest { .filter(|(key, _)| key.as_str() == S::name()) .next()?; - serde_json::from_value(setting_member.1.clone()).ok() + serde_json::from_str(&setting_member.1).ok() } }