feat: support for changing user name and avatar color
This commit is contained in:
parent
3408ccb555
commit
84e35ec032
6 changed files with 128 additions and 1 deletions
2
migrations/0004_add_avatar_color.sql
Normal file
2
migrations/0004_add_avatar_color.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE users ADD COLUMN avatar_color TEXT;
|
||||
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY NOT NULL,
|
||||
name TEXT,
|
||||
avatar_color TEXT,
|
||||
email TEXT NOT NULL UNIQUE,
|
||||
email_verified BOOLEAN NOT NULL DEFAULT 0,
|
||||
master_password_hash TEXT NOT NULL,
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ use crate::{
|
|||
sync::Profile,
|
||||
user::{
|
||||
ChangeKdfRequest, ChangePasswordRequest, PasswordOrOtpData, PreloginResponse,
|
||||
RegisterRequest, RotateKeyRequest, User,
|
||||
RegisterRequest, RotateKeyRequest, User, ProfileData, AvatarData,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
@ -212,6 +212,7 @@ pub async fn register(
|
|||
let user = User {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
name: payload.name,
|
||||
avatar_color: None,
|
||||
email: payload.email.to_lowercase(),
|
||||
email_verified: false,
|
||||
master_password_hash: hashed_password,
|
||||
|
|
@ -312,6 +313,110 @@ pub async fn get_profile(
|
|||
Ok(Json(profile))
|
||||
}
|
||||
|
||||
#[worker::send]
|
||||
pub async fn post_profile(
|
||||
claims: Claims,
|
||||
State(env): State<Arc<Env>>,
|
||||
Json(payload): Json<ProfileData>,
|
||||
) -> Result<Json<Profile>, AppError> {
|
||||
if payload.name.len() > 50 {
|
||||
return Err(AppError::BadRequest(
|
||||
"The field Name must be a string with a maximum length of 50.".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let db = db::get_db(&env)?;
|
||||
let user_id = &claims.sub;
|
||||
|
||||
let user_value: Value = db
|
||||
.prepare("SELECT * FROM users WHERE id = ?1")
|
||||
.bind(&[user_id.clone().into()])?
|
||||
.first(None)
|
||||
.await
|
||||
.map_err(|_| AppError::Database)?
|
||||
.ok_or_else(|| AppError::NotFound("User not found".to_string()))?;
|
||||
|
||||
let mut user: User = serde_json::from_value(user_value).map_err(|_| AppError::Internal)?;
|
||||
let now = Utc::now().to_rfc3339();
|
||||
|
||||
user.name = Some(payload.name);
|
||||
user.updated_at = now.clone();
|
||||
|
||||
query!(
|
||||
&db,
|
||||
"UPDATE users SET name = ?1, updated_at = ?2 WHERE id = ?3",
|
||||
user.name,
|
||||
now,
|
||||
user_id
|
||||
)
|
||||
.map_err(|_| AppError::Database)?
|
||||
.run()
|
||||
.await
|
||||
.map_err(|_| AppError::Database)?;
|
||||
|
||||
let profile = Profile::from_user(user)?;
|
||||
|
||||
Ok(Json(profile))
|
||||
}
|
||||
|
||||
#[worker::send]
|
||||
pub async fn put_profile(
|
||||
claims: Claims,
|
||||
state: State<Arc<Env>>,
|
||||
json: Json<ProfileData>,
|
||||
) -> Result<Json<Profile>, AppError> {
|
||||
post_profile(claims, state, json).await
|
||||
}
|
||||
|
||||
#[worker::send]
|
||||
pub async fn put_avatar(
|
||||
claims: Claims,
|
||||
State(env): State<Arc<Env>>,
|
||||
Json(payload): Json<AvatarData>,
|
||||
) -> Result<Json<Profile>, AppError> {
|
||||
if let Some(color) = &payload.avatar_color {
|
||||
if color.len() != 7 {
|
||||
return Err(AppError::BadRequest(
|
||||
"The field AvatarColor must be a HTML/Hex color code with a length of 7 characters"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let db = db::get_db(&env)?;
|
||||
let user_id = &claims.sub;
|
||||
|
||||
let user_value: Value = db
|
||||
.prepare("SELECT * FROM users WHERE id = ?1")
|
||||
.bind(&[user_id.clone().into()])?
|
||||
.first(None)
|
||||
.await
|
||||
.map_err(|_| AppError::Database)?
|
||||
.ok_or_else(|| AppError::NotFound("User not found".to_string()))?;
|
||||
|
||||
let mut user: User = serde_json::from_value(user_value).map_err(|_| AppError::Internal)?;
|
||||
let now = Utc::now().to_rfc3339();
|
||||
|
||||
user.avatar_color = payload.avatar_color;
|
||||
user.updated_at = now.clone();
|
||||
|
||||
query!(
|
||||
&db,
|
||||
"UPDATE users SET avatar_color = ?1, updated_at = ?2 WHERE id = ?3",
|
||||
user.avatar_color,
|
||||
now,
|
||||
user_id
|
||||
)
|
||||
.map_err(|_| AppError::Database)?
|
||||
.run()
|
||||
.await
|
||||
.map_err(|_| AppError::Database)?;
|
||||
|
||||
let profile = Profile::from_user(user)?;
|
||||
|
||||
Ok(Json(profile))
|
||||
}
|
||||
|
||||
#[worker::send]
|
||||
pub async fn delete_account(
|
||||
claims: Claims,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ use serde_json::Value;
|
|||
pub struct Profile {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub name: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub avatar_color: Option<String>,
|
||||
pub email: String,
|
||||
pub id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
|
|
@ -35,6 +37,7 @@ impl Profile {
|
|||
Ok(Self {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
avatar_color: user.avatar_color,
|
||||
email: user.email,
|
||||
master_password_hint: user.master_password_hint,
|
||||
security_stamp: user.security_stamp,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use crate::{crypto::verify_password, error::AppError};
|
|||
pub struct User {
|
||||
pub id: String,
|
||||
pub name: Option<String>,
|
||||
pub avatar_color: Option<String>,
|
||||
pub email: String,
|
||||
#[serde(with = "bool_from_int")]
|
||||
pub email_verified: bool,
|
||||
|
|
@ -321,3 +322,15 @@ pub struct RotateFolderData {
|
|||
pub id: Option<String>,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ProfileData {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AvatarData {
|
||||
pub avatar_color: Option<String>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ pub fn api_router(env: Env) -> Router {
|
|||
// For on-demand sync checks
|
||||
.route("/api/accounts/revision-date", get(accounts::revision_date))
|
||||
.route("/api/accounts/profile", get(accounts::get_profile))
|
||||
.route("/api/accounts/profile", post(accounts::post_profile))
|
||||
.route("/api/accounts/profile", put(accounts::put_profile))
|
||||
.route("/api/accounts/avatar", put(accounts::put_avatar))
|
||||
// Delete account
|
||||
.route("/api/accounts", delete(accounts::delete_account))
|
||||
.route("/api/accounts/delete", post(accounts::delete_account))
|
||||
|
|
|
|||
Loading…
Reference in a new issue