diff --git a/migrations/mysql/2023-01-11-205851_add_avatar_color/down.sql b/migrations/mysql/2023-01-11-205851_add_avatar_color/down.sql new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/migrations/mysql/2023-01-11-205851_add_avatar_color/up.sql b/migrations/mysql/2023-01-11-205851_add_avatar_color/up.sql new file mode 100644 index 0000000000000000000000000000000000000000..8f8e72051238288707024d67489d162f1a8188da --- /dev/null +++ b/migrations/mysql/2023-01-11-205851_add_avatar_color/up.sql @@ -0,0 +1,2 @@ +ALTER TABLE users +ADD COLUMN avatar_color VARCHAR(7); diff --git a/migrations/postgresql/2023-01-11-205851_add_avatar_color/down.sql b/migrations/postgresql/2023-01-11-205851_add_avatar_color/down.sql new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/migrations/postgresql/2023-01-11-205851_add_avatar_color/up.sql b/migrations/postgresql/2023-01-11-205851_add_avatar_color/up.sql new file mode 100644 index 0000000000000000000000000000000000000000..cf3ef9f77ddb6ce65d708275b8764f254b8a03da --- /dev/null +++ b/migrations/postgresql/2023-01-11-205851_add_avatar_color/up.sql @@ -0,0 +1,2 @@ +ALTER TABLE users +ADD COLUMN avatar_color TEXT; diff --git a/migrations/sqlite/2023-01-11-205851_add_avatar_color/down.sql b/migrations/sqlite/2023-01-11-205851_add_avatar_color/down.sql new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/migrations/sqlite/2023-01-11-205851_add_avatar_color/up.sql b/migrations/sqlite/2023-01-11-205851_add_avatar_color/up.sql new file mode 100644 index 0000000000000000000000000000000000000000..cf3ef9f77ddb6ce65d708275b8764f254b8a03da --- /dev/null +++ b/migrations/sqlite/2023-01-11-205851_add_avatar_color/up.sql @@ -0,0 +1,2 @@ +ALTER TABLE users +ADD COLUMN avatar_color TEXT; diff --git a/src/api/core/accounts.rs b/src/api/core/accounts.rs index 1f932f699b1d90f89a654502f0b8583a317361ad..758d90289a882c0a2f773f59fff278234961d61e 100644 --- a/src/api/core/accounts.rs +++ b/src/api/core/accounts.rs @@ -39,6 +39,7 @@ pub fn routes() -> Vec<rocket::Route> { api_key, rotate_api_key, get_known_device, + put_avatar, ] } @@ -228,6 +229,32 @@ async fn post_profile(data: JsonUpcase<ProfileData>, headers: Headers, mut conn: Ok(Json(user.to_json(&mut conn).await)) } +#[derive(Deserialize)] +#[allow(non_snake_case)] +struct AvatarData { + AvatarColor: Option<String>, +} + +#[put("/accounts/avatar", data = "<data>")] +async fn put_avatar(data: JsonUpcase<AvatarData>, headers: Headers, mut conn: DbConn) -> JsonResult { + let data: AvatarData = data.into_inner().data; + + // It looks like it only supports the 6 hex color format. + // If you try to add the short value it will not show that color. + // Check and force 7 chars, including the #. + if let Some(color) = &data.AvatarColor { + if color.len() != 7 { + err!("The field AvatarColor must be a HTML/Hex color code with a length of 7 characters") + } + } + + let mut user = headers.user; + user.avatar_color = data.AvatarColor; + + user.save(&mut conn).await?; + Ok(Json(user.to_json(&mut conn).await)) +} + #[get("/users/<uuid>/public-key")] async fn get_public_keys(uuid: String, _headers: Headers, mut conn: DbConn) -> JsonResult { let user = match User::find_by_uuid(&uuid, &mut conn).await { diff --git a/src/db/models/user.rs b/src/db/models/user.rs index b59f10b8cc48977bf937466aad3881fafcdb951b..611c4ebb851781ba52b2e67e6ee9f1497186d894 100644 --- a/src/db/models/user.rs +++ b/src/db/models/user.rs @@ -46,6 +46,8 @@ db_object! { pub client_kdf_iter: i32, pub api_key: Option<String>, + + pub avatar_color: Option<String>, } #[derive(Identifiable, Queryable, Insertable)] @@ -113,6 +115,8 @@ impl User { client_kdf_iter: Self::CLIENT_KDF_ITER_DEFAULT, api_key: None, + + avatar_color: None, } } @@ -226,6 +230,7 @@ impl User { "Providers": [], "ProviderOrganizations": [], "ForcePasswordReset": false, + "AvatarColor": self.avatar_color, "Object": "profile", }) } diff --git a/src/db/schemas/mysql/schema.rs b/src/db/schemas/mysql/schema.rs index 0073a9d5de832b29f2c4cd6c369cda2aee5b0174..27cd24c35f4dfa2f2d197f5929a330218db039d4 100644 --- a/src/db/schemas/mysql/schema.rs +++ b/src/db/schemas/mysql/schema.rs @@ -200,6 +200,7 @@ table! { client_kdf_type -> Integer, client_kdf_iter -> Integer, api_key -> Nullable<Text>, + avatar_color -> Nullable<Text>, } } diff --git a/src/db/schemas/postgresql/schema.rs b/src/db/schemas/postgresql/schema.rs index 1421513cf33dc20868e1a6bf31e8f22d8e223a5e..0233e0c9752c75c18b968fccb021b3df45c97f18 100644 --- a/src/db/schemas/postgresql/schema.rs +++ b/src/db/schemas/postgresql/schema.rs @@ -200,6 +200,7 @@ table! { client_kdf_type -> Integer, client_kdf_iter -> Integer, api_key -> Nullable<Text>, + avatar_color -> Nullable<Text>, } } diff --git a/src/db/schemas/sqlite/schema.rs b/src/db/schemas/sqlite/schema.rs index 0fedcf1dc609e5da657d8a8b21962033bc0fe245..391e67001898b8483facd81cf6d741bd82e50715 100644 --- a/src/db/schemas/sqlite/schema.rs +++ b/src/db/schemas/sqlite/schema.rs @@ -200,6 +200,7 @@ table! { client_kdf_type -> Integer, client_kdf_iter -> Integer, api_key -> Nullable<Text>, + avatar_color -> Nullable<Text>, } } diff --git a/src/main.rs b/src/main.rs index 8a5f53dac706387e25a2f32b839378ccc4435061..3c648231577fbdb9b873b9d7cfc36e8a64edbfe2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ // The more key/value pairs there are the more recursion occurs. // We want to keep this as low as possible, but not higher then 128. // If you go above 128 it will cause rust-analyzer to fail, -#![recursion_limit = "94"] +#![recursion_limit = "97"] // When enabled use MiMalloc as malloc instead of the default malloc #[cfg(feature = "enable_mimalloc")]