JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Fix settings serialization

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨a70c63a

Showing ⁨⁨3⁩ changed files⁩ with ⁨⁨28⁩ insertions⁩ and ⁨⁨29⁩ deletions⁩

giterated-daemon/src/backend/mod.rs

View file
@@ -105,6 +105,6 @@ pub trait UserBackend: AuthBackend {
105 105 async fn write_settings(
106 106 &mut self,
107 107 user: &User,
108 settings: &[(String, Value)],
108 settings: &[(String, String)],
109 109 ) -> Result<(), Error>;
110 110 }

giterated-daemon/src/backend/user.rs

View file
@@ -17,7 +17,7 @@ use giterated_models::{
17 17 UserDisplayNameRequest, UserDisplayNameResponse,
18 18 },
19 19 },
20 model::{instance::Instance, user::User},
20 model::{instance::Instance, user::User, settings::{UserBio, Setting, UserDisplayName}},
21 21 };
22 22 use rsa::{
23 23 pkcs8::{EncodePrivateKey, EncodePublicKey},
@@ -58,18 +58,17 @@ impl UserBackend for UserAuth {
58 58 &mut self,
59 59 request: UserDisplayNameRequest,
60 60 ) -> Result<UserDisplayNameResponse, Error> {
61 let db_row = sqlx::query_as!(
62 UserRow,
63 r#"SELECT * FROM users WHERE username = $1"#,
64 request.user.username
65 )
66 .fetch_one(&self.pg_pool.clone())
67 .await
68 .unwrap();
61 let settings = self.settings(&request.user).await?;
69 62
70 Ok(UserDisplayNameResponse {
71 display_name: db_row.display_name,
72 })
63 let name = settings.iter().find(|setting| &setting.0 == UserDisplayName::name());
64
65 if let Some((_, name)) = name {
66 let name: UserDisplayName = serde_json::from_value(name.clone()).unwrap();
67
68 Ok(UserDisplayNameResponse { display_name: Some(name.0) })
69 } else {
70 Ok(UserDisplayNameResponse { display_name: None })
71 }
73 72 }
74 73
75 74 async fn display_image(
@@ -91,15 +90,17 @@ impl UserBackend for UserAuth {
91 90 }
92 91
93 92 async fn bio(&mut self, request: UserBioRequest) -> Result<UserBioResponse, Error> {
94 let db_row = sqlx::query_as!(
95 UserRow,
96 r#"SELECT * FROM users WHERE username = $1"#,
97 request.user.username
98 )
99 .fetch_one(&self.pg_pool.clone())
100 .await?;
93 let settings = self.settings(&request.user).await?;
94
95 let bio = settings.iter().find(|setting| &setting.0 == UserBio::name());
96
97 if let Some((_, bio)) = bio {
98 let bio: UserBio = serde_json::from_value(bio.clone()).unwrap();
101 99
102 Ok(UserBioResponse { bio: db_row.bio })
100 Ok(UserBioResponse { bio: Some(bio.0)})
101 } else {
102 Ok(UserBioResponse { bio: None })
103 }
103 104 }
104 105
105 106 async fn exists(&mut self, user: &User) -> Result<bool, Error> {
@@ -128,7 +129,7 @@ impl UserBackend for UserAuth {
128 129 }
129 130 })
130 131 .filter_map(|row| async move {
131 if let Ok(value) = serde_json::from_str(&row.value) {
132 if let Ok(value) = serde_json::from_str(&format!("\"{}\"", row.value)) {
132 133 Some((row.name, value))
133 134 } else {
134 135 None
@@ -143,13 +144,11 @@ impl UserBackend for UserAuth {
143 144 async fn write_settings(
144 145 &mut self,
145 146 user: &User,
146 settings: &[(String, Value)],
147 settings: &[(String, String)],
147 148 ) -> Result<(), Error> {
148 149 for (name, value) in settings {
149 let serialized = serde_json::to_string(value)?;
150
151 150 sqlx::query!("INSERT INTO user_settings VALUES ($1, $2, $3) ON CONFLICT (username, name) DO UPDATE SET value = $3",
152 user.username, name, serialized)
151 user.username, name, value)
153 152 .execute(&self.pg_pool).await?;
154 153 }
155 154

giterated-models/src/messages/user.rs

View file
@@ -69,7 +69,7 @@ impl UserSettingsResponse {
69 69 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
70 70 pub struct UserWriteSettingsRequest {
71 71 pub user: User,
72 pub settings: Vec<(String, serde_json::Value)>,
72 pub settings: Vec<(String, String)>,
73 73 }
74 74
75 75 impl UserWriteSettingsRequest {
@@ -83,7 +83,7 @@ impl UserWriteSettingsRequest {
83 83 pub fn set<S: Setting>(&mut self, setting: S) {
84 84 self.settings.push((
85 85 S::name().to_string(),
86 serde_json::to_value(setting).unwrap(),
86 serde_json::to_string(&setting).unwrap(),
87 87 ));
88 88 }
89 89 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
@@ -93,7 +93,7 @@ impl UserWriteSettingsRequest {
93 93 .filter(|(key, _)| key.as_str() == S::name())
94 94 .next()?;
95 95
96 serde_json::from_value(setting_member.1.clone()).ok()
96 serde_json::from_str(&setting_member.1).ok()
97 97 }
98 98 }
99 99