Translation support + french.
This commit is contained in:
parent
e9873c1943
commit
f059d3c61f
16 changed files with 380 additions and 169 deletions
|
|
@ -9,7 +9,7 @@ use crate::{
|
|||
consts,
|
||||
data::{db, model},
|
||||
html_templates::*,
|
||||
translation,
|
||||
translation::{self, Sentence},
|
||||
};
|
||||
|
||||
#[debug_handler]
|
||||
|
|
@ -22,7 +22,7 @@ pub async fn create(
|
|||
let recipe_id = connection.create_recipe(user.id).await?;
|
||||
Ok(Redirect::to(&format!("/recipe/edit/{}", recipe_id)).into_response())
|
||||
} else {
|
||||
Ok(MessageTemplate::new("Not logged in", tr).into_response())
|
||||
Ok(MessageTemplate::new(tr.t(Sentence::NotLoggedIn), tr).into_response())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -53,13 +53,16 @@ pub async fn edit_recipe(
|
|||
}
|
||||
.into_response())
|
||||
} else {
|
||||
Ok(MessageTemplate::new("Not allowed to edit this recipe", tr).into_response())
|
||||
Ok(
|
||||
MessageTemplate::new(tr.t(Sentence::RecipeNotAllowedToEdit), tr)
|
||||
.into_response(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
Ok(MessageTemplate::new("Recipe not found", tr).into_response())
|
||||
Ok(MessageTemplate::new(tr.t(Sentence::RecipeNotFound), tr).into_response())
|
||||
}
|
||||
} else {
|
||||
Ok(MessageTemplate::new("Not logged in", tr).into_response())
|
||||
Ok(MessageTemplate::new(tr.t(Sentence::NotLoggedIn), tr).into_response())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -76,7 +79,7 @@ pub async fn view(
|
|||
&& (user.is_none() || recipe.user_id != user.as_ref().unwrap().id)
|
||||
{
|
||||
return Ok(MessageTemplate::new_with_user(
|
||||
&format!("Not allowed the view the recipe {}", recipe_id),
|
||||
tr.tp(Sentence::RecipeNotAllowedToView, &[Box::new(recipe_id)]),
|
||||
tr,
|
||||
user,
|
||||
)
|
||||
|
|
@ -103,11 +106,9 @@ pub async fn view(
|
|||
}
|
||||
.into_response())
|
||||
}
|
||||
None => Ok(MessageTemplate::new_with_user(
|
||||
&format!("Cannot find the recipe {}", recipe_id),
|
||||
tr,
|
||||
user,
|
||||
)
|
||||
.into_response()),
|
||||
None => Ok(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::RecipeNotFound), tr, user)
|
||||
.into_response(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,15 @@
|
|||
use axum::{
|
||||
debug_handler,
|
||||
extract::{Extension, Query, State},
|
||||
http::StatusCode,
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::{ErrorResponse, IntoResponse, Result},
|
||||
};
|
||||
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
||||
use serde::Deserialize;
|
||||
// use tracing::{event, Level};
|
||||
|
||||
use crate::{
|
||||
consts,
|
||||
data::db,
|
||||
model,
|
||||
ron_extractor::ExtractRon,
|
||||
|
|
@ -22,29 +24,46 @@ pub struct RecipeId {
|
|||
id: i64,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
// #[allow(dead_code)]
|
||||
// #[debug_handler]
|
||||
// pub async fn update_user(
|
||||
// State(connection): State<db::Connection>,
|
||||
// Extension(user): Extension<Option<model::User>>,
|
||||
// ExtractRon(ron): ExtractRon<common::ron_api::UpdateProfile>,
|
||||
// ) -> Result<StatusCode> {
|
||||
// if let Some(user) = user {
|
||||
// connection
|
||||
// .update_user(
|
||||
// user.id,
|
||||
// ron.email.as_deref().map(str::trim),
|
||||
// ron.name.as_deref(),
|
||||
// ron.password.as_deref(),
|
||||
// )
|
||||
// .await?;
|
||||
// } else {
|
||||
// return Err(ErrorResponse::from(ron_error(
|
||||
// StatusCode::UNAUTHORIZED,
|
||||
// NOT_AUTHORIZED_MESSAGE,
|
||||
// )));
|
||||
// }
|
||||
// Ok(StatusCode::OK)
|
||||
// }
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn update_user(
|
||||
pub async fn set_lang(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::UpdateProfile>,
|
||||
) -> Result<StatusCode> {
|
||||
headers: HeaderMap,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetLang>,
|
||||
) -> Result<(CookieJar, StatusCode)> {
|
||||
let mut jar = CookieJar::from_headers(&headers);
|
||||
if let Some(user) = user {
|
||||
connection
|
||||
.update_user(
|
||||
user.id,
|
||||
ron.email.as_deref().map(str::trim),
|
||||
ron.name.as_deref(),
|
||||
ron.password.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
connection.set_user_lang(user.id, &ron.lang).await?;
|
||||
} else {
|
||||
return Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
NOT_AUTHORIZED_MESSAGE,
|
||||
)));
|
||||
let cookie = Cookie::build((consts::COOKIE_LANG_NAME, ron.lang)).path("/");
|
||||
jar = jar.add(cookie);
|
||||
}
|
||||
Ok(StatusCode::OK)
|
||||
Ok((jar, StatusCode::OK))
|
||||
}
|
||||
|
||||
async fn check_user_rights_recipe(
|
||||
|
|
|
|||
|
|
@ -126,9 +126,12 @@ pub async fn sign_up_post(
|
|||
let email = form_data.email.clone();
|
||||
match email::send_email(
|
||||
&email,
|
||||
&format!(
|
||||
"Follow this link to confirm your inscription: {}/validation?validation_token={}",
|
||||
url, token
|
||||
&tr.tp(
|
||||
Sentence::SignUpFollowEmailLink,
|
||||
&[Box::new(format!(
|
||||
"{}/validation?validation_token={}",
|
||||
url, token
|
||||
))],
|
||||
),
|
||||
&config.smtp_relay_address,
|
||||
&config.smtp_login,
|
||||
|
|
@ -136,10 +139,12 @@ pub async fn sign_up_post(
|
|||
)
|
||||
.await
|
||||
{
|
||||
Ok(()) => Ok(
|
||||
MessageTemplate::new_with_user(
|
||||
"An email has been sent, follow the link to validate your account",
|
||||
tr, user).into_response()),
|
||||
Ok(()) => {
|
||||
Ok(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::SignUpEmailSent), tr, user)
|
||||
.into_response(),
|
||||
)
|
||||
}
|
||||
Err(_) => {
|
||||
// error!("Email validation error: {}", error); // TODO: log
|
||||
error_response(SignUpError::UnableSendEmail, &form_data, user, tr)
|
||||
|
|
@ -166,7 +171,7 @@ pub async fn sign_up_validation(
|
|||
if user.is_some() {
|
||||
return Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user("User already exists", tr, user),
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::ValidationUserAlreadyExists), tr, user),
|
||||
));
|
||||
}
|
||||
let (client_ip, client_user_agent) = utils::get_ip_and_user_agent(&headers, addr);
|
||||
|
|
@ -189,7 +194,7 @@ pub async fn sign_up_validation(
|
|||
Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user(
|
||||
"Email validation successful, your account has been created",
|
||||
tr.t(Sentence::SignUpEmailValidationSuccess),
|
||||
tr,
|
||||
user,
|
||||
),
|
||||
|
|
@ -198,7 +203,7 @@ pub async fn sign_up_validation(
|
|||
db::user::ValidationResult::ValidationExpired => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user(
|
||||
"The validation has expired. Try to sign up again",
|
||||
tr.t(Sentence::SignUpValidationExpired),
|
||||
tr,
|
||||
user,
|
||||
),
|
||||
|
|
@ -206,7 +211,7 @@ pub async fn sign_up_validation(
|
|||
db::user::ValidationResult::UnknownUser => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user(
|
||||
"Validation error. Try to sign up again",
|
||||
tr.t(Sentence::SignUpValidationErrorTryAgain),
|
||||
tr,
|
||||
user,
|
||||
),
|
||||
|
|
@ -215,7 +220,7 @@ pub async fn sign_up_validation(
|
|||
}
|
||||
None => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user("Validation error", tr, user),
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::ValidationError), tr, user),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
|
@ -313,12 +318,10 @@ pub async fn ask_reset_password_get(
|
|||
Extension(tr): Extension<translation::Tr>,
|
||||
) -> Result<Response> {
|
||||
if user.is_some() {
|
||||
Ok(MessageTemplate::new_with_user(
|
||||
"Can't ask to reset password when already logged in",
|
||||
tr,
|
||||
user,
|
||||
Ok(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::AskResetAlreadyLoggedInError), tr, user)
|
||||
.into_response(),
|
||||
)
|
||||
.into_response())
|
||||
} else {
|
||||
Ok(AskResetPasswordTemplate {
|
||||
user,
|
||||
|
|
@ -361,23 +364,21 @@ pub async fn ask_reset_password_post(
|
|||
) -> Result<Response> {
|
||||
Ok(AskResetPasswordTemplate {
|
||||
user,
|
||||
tr,
|
||||
email: email.to_string(),
|
||||
message_email: match error {
|
||||
AskResetPasswordError::InvalidEmail => "Invalid email",
|
||||
_ => "",
|
||||
}
|
||||
.to_string(),
|
||||
AskResetPasswordError::InvalidEmail => tr.t(Sentence::InvalidEmail),
|
||||
_ => String::new(),
|
||||
},
|
||||
message: match error {
|
||||
AskResetPasswordError::EmailAlreadyReset => {
|
||||
"The password has already been reset for this email"
|
||||
tr.t(Sentence::AskResetEmailAlreadyResetError)
|
||||
}
|
||||
AskResetPasswordError::EmailUnknown => "Email unknown",
|
||||
AskResetPasswordError::UnableSendEmail => "Unable to send the reset password email",
|
||||
AskResetPasswordError::DatabaseError => "Database error",
|
||||
_ => "",
|
||||
}
|
||||
.to_string(),
|
||||
AskResetPasswordError::EmailUnknown => tr.t(Sentence::EmailUnknown),
|
||||
AskResetPasswordError::UnableSendEmail => tr.t(Sentence::UnableToSendResetEmail),
|
||||
AskResetPasswordError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
_ => String::new(),
|
||||
},
|
||||
tr,
|
||||
}
|
||||
.into_response())
|
||||
}
|
||||
|
|
@ -417,9 +418,12 @@ pub async fn ask_reset_password_post(
|
|||
let url = utils::get_url_from_host(&host);
|
||||
match email::send_email(
|
||||
&form_data.email,
|
||||
&format!(
|
||||
"Follow this link to reset your password: {}/reset_password?reset_token={}",
|
||||
url, token
|
||||
&tr.tp(
|
||||
Sentence::AskResetFollowEmailLink,
|
||||
&[Box::new(format!(
|
||||
"{}/reset_password?reset_token={}",
|
||||
url, token
|
||||
))],
|
||||
),
|
||||
&config.smtp_relay_address,
|
||||
&config.smtp_login,
|
||||
|
|
@ -427,12 +431,12 @@ pub async fn ask_reset_password_post(
|
|||
)
|
||||
.await
|
||||
{
|
||||
Ok(()) => Ok(MessageTemplate::new_with_user(
|
||||
"An email has been sent, follow the link to reset your password.",
|
||||
tr,
|
||||
user,
|
||||
)
|
||||
.into_response()),
|
||||
Ok(()) => {
|
||||
Ok(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::AskResetEmailSent), tr, user)
|
||||
.into_response(),
|
||||
)
|
||||
}
|
||||
Err(_) => {
|
||||
// error!("Email validation error: {}", error); // TODO: log
|
||||
error_response(
|
||||
|
|
@ -472,7 +476,10 @@ pub async fn reset_password_get(
|
|||
}
|
||||
.into_response())
|
||||
} else {
|
||||
Ok(MessageTemplate::new_with_user("Reset token missing", tr, user).into_response())
|
||||
Ok(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::AskResetTokenMissing), tr, user)
|
||||
.into_response(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -505,22 +512,21 @@ pub async fn reset_password_post(
|
|||
) -> Result<Response> {
|
||||
Ok(ResetPasswordTemplate {
|
||||
user,
|
||||
tr,
|
||||
reset_token: form_data.reset_token.clone(),
|
||||
message_password: match error {
|
||||
ResetPasswordError::PasswordsNotEqual => "Passwords don't match",
|
||||
ResetPasswordError::InvalidPassword => {
|
||||
"Password must have at least eight characters"
|
||||
}
|
||||
_ => "",
|
||||
}
|
||||
.to_string(),
|
||||
ResetPasswordError::PasswordsNotEqual => tr.t(Sentence::PasswordDontMatch),
|
||||
ResetPasswordError::InvalidPassword => tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
),
|
||||
_ => String::new(),
|
||||
},
|
||||
message: match error {
|
||||
ResetPasswordError::TokenExpired => "Token expired, try to reset password again",
|
||||
ResetPasswordError::DatabaseError => "Database error",
|
||||
_ => "",
|
||||
}
|
||||
.to_string(),
|
||||
ResetPasswordError::TokenExpired => tr.t(Sentence::AskResetTokenExpired),
|
||||
ResetPasswordError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
_ => String::new(),
|
||||
},
|
||||
tr,
|
||||
}
|
||||
.into_response())
|
||||
}
|
||||
|
|
@ -545,7 +551,7 @@ pub async fn reset_password_post(
|
|||
{
|
||||
Ok(db::user::ResetPasswordResult::Ok) => {
|
||||
Ok(
|
||||
MessageTemplate::new_with_user("Your password has been reset", tr, user)
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::PasswordReset), tr, user)
|
||||
.into_response(),
|
||||
)
|
||||
}
|
||||
|
|
@ -575,7 +581,7 @@ pub async fn edit_user_get(
|
|||
}
|
||||
.into_response()
|
||||
} else {
|
||||
MessageTemplate::new("Not logged in", tr).into_response()
|
||||
MessageTemplate::new(tr.t(Sentence::NotLoggedIn), tr).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -617,25 +623,23 @@ pub async fn edit_user_post(
|
|||
username: form_data.name.clone(),
|
||||
email: form_data.email.clone(),
|
||||
message_email: match error {
|
||||
ProfileUpdateError::InvalidEmail => "Invalid email",
|
||||
ProfileUpdateError::EmailAlreadyTaken => "Email already taken",
|
||||
_ => "",
|
||||
}
|
||||
.to_string(),
|
||||
ProfileUpdateError::InvalidEmail => tr.t(Sentence::InvalidEmail),
|
||||
ProfileUpdateError::EmailAlreadyTaken => tr.t(Sentence::EmailAlreadyTaken),
|
||||
_ => String::new(),
|
||||
},
|
||||
message_password: match error {
|
||||
ProfileUpdateError::PasswordsNotEqual => "Passwords don't match",
|
||||
ProfileUpdateError::InvalidPassword => {
|
||||
"Password must have at least eight characters"
|
||||
}
|
||||
_ => "",
|
||||
}
|
||||
.to_string(),
|
||||
ProfileUpdateError::PasswordsNotEqual => tr.t(Sentence::PasswordDontMatch),
|
||||
ProfileUpdateError::InvalidPassword => tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
),
|
||||
_ => String::new(),
|
||||
},
|
||||
message: match error {
|
||||
ProfileUpdateError::DatabaseError => "Database error",
|
||||
ProfileUpdateError::UnableSendEmail => "Unable to send the validation email",
|
||||
_ => "",
|
||||
}
|
||||
.to_string(),
|
||||
ProfileUpdateError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
ProfileUpdateError::UnableSendEmail => tr.t(Sentence::UnableToSendEmail),
|
||||
_ => String::new(),
|
||||
},
|
||||
tr,
|
||||
}
|
||||
.into_response())
|
||||
|
|
@ -662,7 +666,7 @@ pub async fn edit_user_post(
|
|||
};
|
||||
|
||||
let email_trimmed = form_data.email.trim();
|
||||
let message: &str;
|
||||
let message: String;
|
||||
|
||||
match connection
|
||||
.update_user(
|
||||
|
|
@ -681,9 +685,12 @@ pub async fn edit_user_post(
|
|||
let email = form_data.email.clone();
|
||||
match email::send_email(
|
||||
&email,
|
||||
&format!(
|
||||
"Follow this link to validate this email address: {}/revalidation?validation_token={}",
|
||||
url, token
|
||||
&tr.tp(
|
||||
Sentence::ProfileFollowEmailLink,
|
||||
&[Box::new(format!(
|
||||
"{}/revalidation?validation_token={}",
|
||||
url, token
|
||||
))],
|
||||
),
|
||||
&config.smtp_relay_address,
|
||||
&config.smtp_login,
|
||||
|
|
@ -692,18 +699,21 @@ pub async fn edit_user_post(
|
|||
.await
|
||||
{
|
||||
Ok(()) => {
|
||||
message =
|
||||
"An email has been sent, follow the link to validate your new email";
|
||||
message = tr.t(Sentence::ProfileEmailSent);
|
||||
}
|
||||
Err(_) => {
|
||||
// error!("Email validation error: {}", error); // TODO: log
|
||||
return error_response(
|
||||
ProfileUpdateError::UnableSendEmail, &form_data, user, tr);
|
||||
ProfileUpdateError::UnableSendEmail,
|
||||
&form_data,
|
||||
user,
|
||||
tr,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(db::user::UpdateUserResult::Ok) => {
|
||||
message = "Profile saved";
|
||||
message = tr.t(Sentence::ProfileSaved);
|
||||
}
|
||||
Err(_) => {
|
||||
return error_response(ProfileUpdateError::DatabaseError, &form_data, user, tr)
|
||||
|
|
@ -717,14 +727,14 @@ pub async fn edit_user_post(
|
|||
user,
|
||||
username: form_data.name,
|
||||
email: form_data.email,
|
||||
message: message.to_string(),
|
||||
message,
|
||||
message_email: String::new(),
|
||||
message_password: String::new(),
|
||||
tr,
|
||||
}
|
||||
.into_response())
|
||||
} else {
|
||||
Ok(MessageTemplate::new("Not logged in", tr).into_response())
|
||||
Ok(MessageTemplate::new(tr.t(Sentence::NotLoggedIn), tr).into_response())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -741,7 +751,7 @@ pub async fn email_revalidation(
|
|||
if user.is_some() {
|
||||
return Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user("User already exists", tr, user),
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::ValidationUserAlreadyExists), tr, user),
|
||||
));
|
||||
}
|
||||
let (client_ip, client_user_agent) = utils::get_ip_and_user_agent(&headers, addr);
|
||||
|
|
@ -763,21 +773,21 @@ pub async fn email_revalidation(
|
|||
let user = connection.load_user(user_id).await?;
|
||||
Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user("Email validation successful", tr, user),
|
||||
MessageTemplate::new_with_user(
|
||||
tr.t(Sentence::ValidationSuccessful),
|
||||
tr,
|
||||
user,
|
||||
),
|
||||
))
|
||||
}
|
||||
db::user::ValidationResult::ValidationExpired => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user(
|
||||
"The validation has expired. Try to sign up again with the same email",
|
||||
tr,
|
||||
user,
|
||||
),
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::ValidationExpired), tr, user),
|
||||
)),
|
||||
db::user::ValidationResult::UnknownUser => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user(
|
||||
"Validation error. Try to sign up again with the same email",
|
||||
tr.t(Sentence::ValidationErrorTryToSignUpAgain),
|
||||
tr,
|
||||
user,
|
||||
),
|
||||
|
|
@ -786,7 +796,7 @@ pub async fn email_revalidation(
|
|||
}
|
||||
None => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user("Validation error", tr, user),
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::ValidationError), tr, user),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue