From ee8211a3216a5f0f2aeb8f2154eec78ba9027aa3 Mon Sep 17 00:00:00 2001 From: Radical Date: Sun, 1 Jun 2025 15:58:07 +0200 Subject: [PATCH] feat: add pronouns to users --- .../down.sql | 2 ++ .../up.sql | 2 ++ src/api/v1/me/mod.rs | 5 +++++ src/schema.rs | 2 ++ src/structs.rs | 21 +++++++++++++++++++ 5 files changed, 32 insertions(+) create mode 100644 migrations/2025-06-01-134036_add_pronouns_to_users/down.sql create mode 100644 migrations/2025-06-01-134036_add_pronouns_to_users/up.sql diff --git a/migrations/2025-06-01-134036_add_pronouns_to_users/down.sql b/migrations/2025-06-01-134036_add_pronouns_to_users/down.sql new file mode 100644 index 0000000..32d891f --- /dev/null +++ b/migrations/2025-06-01-134036_add_pronouns_to_users/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE users DROP COLUMN pronouns; diff --git a/migrations/2025-06-01-134036_add_pronouns_to_users/up.sql b/migrations/2025-06-01-134036_add_pronouns_to_users/up.sql new file mode 100644 index 0000000..90807bb --- /dev/null +++ b/migrations/2025-06-01-134036_add_pronouns_to_users/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE users ADD COLUMN pronouns VARCHAR(32) DEFAULT NULL; diff --git a/src/api/v1/me/mod.rs b/src/api/v1/me/mod.rs index e7c8167..fc9e61b 100644 --- a/src/api/v1/me/mod.rs +++ b/src/api/v1/me/mod.rs @@ -40,6 +40,7 @@ struct NewInfo { display_name: Option, //password: Option, will probably be handled through a reset password link email: Option, + pronouns: Option, } #[derive(Debug, MultipartForm)] @@ -97,5 +98,9 @@ pub async fn update( me.set_email(&data, email.clone()).await?; } + if let Some(pronouns) = &form.json.pronouns { + me.set_pronouns(&data, pronouns.clone()).await?; + } + Ok(HttpResponse::Ok().finish()) } diff --git a/src/schema.rs b/src/schema.rs index 1b34400..3be885a 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -144,6 +144,8 @@ diesel::table! { deleted_at -> Nullable, #[max_length = 100] avatar -> Nullable, + #[max_length = 32] + pronouns -> Nullable, } } diff --git a/src/structs.rs b/src/structs.rs index 0dada1a..5e19dad 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -865,6 +865,7 @@ pub struct User { username: String, display_name: Option, avatar: Option, + pronouns: Option, } impl User { @@ -915,6 +916,7 @@ pub struct Me { username: String, display_name: Option, avatar: Option, + pronouns: Option, email: String, pub email_verified: bool, } @@ -1090,6 +1092,25 @@ impl Me { Ok(()) } + + pub async fn set_pronouns(&mut self, data: &Data, new_pronouns: String) -> Result<(), Error> { + let mut conn = data.pool.get().await?; + + use users::dsl; + update(users::table) + .filter(dsl::uuid.eq(self.uuid)) + .set(( + dsl::pronouns.eq(new_pronouns.as_str()), + )) + .execute(&mut conn) + .await?; + + if data.get_cache_key(self.uuid.to_string()).await.is_ok() { + data.del_cache_key(self.uuid.to_string()).await? + } + + Ok(()) + } } #[derive(Deserialize)]