Translation support + french.
This commit is contained in:
parent
e9873c1943
commit
f059d3c61f
16 changed files with 380 additions and 169 deletions
|
|
@ -6,7 +6,9 @@ pub const DB_DIRECTORY: &str = "data";
|
|||
pub const DB_FILENAME: &str = "recipes.sqlite";
|
||||
pub const SQL_FILENAME: &str = "sql/version_{VERSION}.sql";
|
||||
pub const VALIDATION_TOKEN_DURATION: i64 = 60 * 60; // [s]. (1 jour).
|
||||
|
||||
pub const COOKIE_AUTH_TOKEN_NAME: &str = "auth_token";
|
||||
pub const COOKIE_LANG_NAME: &str = "lang";
|
||||
|
||||
pub const VALIDATION_PASSWORD_RESET_TOKEN_DURATION: i64 = 60 * 60; // [s]. (1 jour).
|
||||
|
||||
|
|
@ -22,6 +24,7 @@ pub const REVERSE_PROXY_IP_HTTP_FIELD: &str = "x-real-ip"; // Set by the reverse
|
|||
|
||||
pub const MAX_DB_CONNECTION: u32 = 1; // To avoid database lock.
|
||||
|
||||
// TODO: remove, should be replaced by the translation module.
|
||||
pub static LANGUAGES: LazyLock<[(&str, &str); 2]> = LazyLock::new(|| {
|
||||
let mut langs = [("Français", "fr"), ("English", "en")];
|
||||
langs.sort();
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use crate::{
|
|||
consts,
|
||||
data::model,
|
||||
hash::{hash, verify_password},
|
||||
services::user,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -162,6 +163,16 @@ WHERE [id] = $1
|
|||
})
|
||||
}
|
||||
|
||||
pub async fn set_user_lang(&self, user_id: i64, lang: &str) -> Result<()> {
|
||||
sqlx::query("UPDATE [User] SET [lang] = $2 WHERE [id] = $1")
|
||||
.bind(user_id)
|
||||
.bind(lang)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn sign_up(&self, email: &str, password: &str) -> Result<SignUpResult> {
|
||||
self.sign_up_with_given_time(email, password, Utc::now())
|
||||
.await
|
||||
|
|
|
|||
|
|
@ -37,20 +37,20 @@ pub struct MessageTemplate {
|
|||
}
|
||||
|
||||
impl MessageTemplate {
|
||||
pub fn new(message: &str, tr: Tr) -> MessageTemplate {
|
||||
pub fn new(message: String, tr: Tr) -> MessageTemplate {
|
||||
MessageTemplate {
|
||||
user: None,
|
||||
tr,
|
||||
message: message.to_string(),
|
||||
message,
|
||||
as_code: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_user(message: &str, tr: Tr, user: Option<model::User>) -> MessageTemplate {
|
||||
pub fn new_with_user(message: String, tr: Tr, user: Option<model::User>) -> MessageTemplate {
|
||||
MessageTemplate {
|
||||
user,
|
||||
tr,
|
||||
message: message.to_string(),
|
||||
message,
|
||||
as_code: false,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,6 +89,7 @@ async fn main() {
|
|||
let ron_api_routes = Router::new()
|
||||
// Disabled: update user profile is now made with a post data ('edit_user_post').
|
||||
// .route("/user/update", put(services::ron::update_user))
|
||||
.route("/set_lang", put(services::ron::set_lang))
|
||||
.route("/recipe/set_title", put(services::ron::set_recipe_title))
|
||||
.route(
|
||||
"/recipe/set_description",
|
||||
|
|
@ -231,26 +232,26 @@ async fn translation(
|
|||
user.lang
|
||||
} else {
|
||||
let available_codes = Tr::available_codes();
|
||||
let jar = CookieJar::from_headers(req.headers());
|
||||
match jar.get(consts::COOKIE_LANG_NAME) {
|
||||
Some(lang) if available_codes.contains(&lang.value()) => lang.value().to_string(),
|
||||
_ => {
|
||||
let accept_language = req
|
||||
.headers()
|
||||
.get(axum::http::header::ACCEPT_LANGUAGE)
|
||||
.map(|v| v.to_str().unwrap_or_default())
|
||||
.unwrap_or_default()
|
||||
.split(',')
|
||||
.map(|l| l.split('-').next().unwrap_or_default())
|
||||
.find_or_first(|l| available_codes.contains(l));
|
||||
|
||||
// TODO: Check cookies before http headers.
|
||||
|
||||
let accept_language = req
|
||||
.headers()
|
||||
.get(axum::http::header::ACCEPT_LANGUAGE)
|
||||
.map(|v| v.to_str().unwrap_or_default())
|
||||
.unwrap_or_default()
|
||||
.split(',')
|
||||
.map(|l| l.split('-').next().unwrap_or_default())
|
||||
.find_or_first(|l| available_codes.contains(l));
|
||||
|
||||
// TODO: Save to cookies.
|
||||
|
||||
accept_language.unwrap_or("en").to_string()
|
||||
accept_language.unwrap_or("en").to_string()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let tr = Tr::new(&language);
|
||||
|
||||
// let jar = CookieJar::from_headers(req.headers());
|
||||
req.extensions_mut().insert(tr);
|
||||
Ok(next.run(req).await)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,6 +77,6 @@ pub async fn not_found(
|
|||
) -> impl IntoResponse {
|
||||
(
|
||||
StatusCode::NOT_FOUND,
|
||||
MessageTemplate::new_with_user("404: Not found", tr, user),
|
||||
MessageTemplate::new_with_user("404: Not found".to_string(), tr, user),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,15 +9,21 @@ use crate::consts;
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Clone)]
|
||||
pub enum Sentence {
|
||||
ProfileTitle,
|
||||
MainTitle,
|
||||
CreateNewRecipe,
|
||||
UnpublishedRecipes,
|
||||
UntitledRecipe,
|
||||
|
||||
Name,
|
||||
EmailAddress,
|
||||
Password,
|
||||
|
||||
SignOut,
|
||||
Save,
|
||||
NotLoggedIn,
|
||||
|
||||
DatabaseError,
|
||||
|
||||
// Sign in page.
|
||||
SignInMenu,
|
||||
SignInTitle,
|
||||
|
|
@ -28,6 +34,11 @@ pub enum Sentence {
|
|||
SignUpMenu,
|
||||
SignUpTitle,
|
||||
SignUpButton,
|
||||
SignUpEmailSent,
|
||||
SignUpFollowEmailLink,
|
||||
SignUpEmailValidationSuccess,
|
||||
SignUpValidationExpired,
|
||||
SignUpValidationErrorTryAgain,
|
||||
ChooseAPassword,
|
||||
ReEnterPassword,
|
||||
AccountMustBeValidatedFirst,
|
||||
|
|
@ -37,9 +48,38 @@ pub enum Sentence {
|
|||
EmailAlreadyTaken,
|
||||
UnableToSendEmail,
|
||||
|
||||
// Validation.
|
||||
ValidationSuccessful,
|
||||
ValidationExpired,
|
||||
ValidationErrorTryToSignUpAgain,
|
||||
ValidationError,
|
||||
ValidationUserAlreadyExists,
|
||||
|
||||
// Reset password page.
|
||||
LostPassword,
|
||||
AskResetButton,
|
||||
AskResetAlreadyLoggedInError,
|
||||
AskResetEmailAlreadyResetError,
|
||||
AskResetFollowEmailLink,
|
||||
AskResetEmailSent,
|
||||
AskResetTokenMissing,
|
||||
AskResetTokenExpired,
|
||||
PasswordReset,
|
||||
EmailUnknown,
|
||||
UnableToSendResetEmail,
|
||||
|
||||
// Profile
|
||||
ProfileTitle,
|
||||
ProfileEmail,
|
||||
ProfileNewPassword,
|
||||
ProfileFollowEmailLink,
|
||||
ProfileEmailSent,
|
||||
ProfileSaved,
|
||||
|
||||
// Recipe.
|
||||
RecipeNotAllowedToEdit,
|
||||
RecipeNotAllowedToView,
|
||||
RecipeNotFound,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
@ -74,7 +114,7 @@ impl Tr {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn tp(&self, sentence: Sentence, params: &[Box<dyn ToString>]) -> String {
|
||||
pub fn tp(&self, sentence: Sentence, params: &[Box<dyn ToString + Send>]) -> String {
|
||||
match self.lang.translation.get(&sentence) {
|
||||
Some(str) => {
|
||||
let mut result = str.clone();
|
||||
|
|
@ -90,6 +130,10 @@ impl Tr {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn current_lang_code(&self) -> &str {
|
||||
&self.lang.code
|
||||
}
|
||||
|
||||
pub fn available_languages() -> Vec<(&'static str, &'static str)> {
|
||||
TRANSLATIONS
|
||||
.iter()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue