]> Untitled Git - lemmy.git/blobdiff - crates/api/src/local_user.rs
Moving settings and secrets to context.
[lemmy.git] / crates / api / src / local_user.rs
index df03e30205d967283e138e60170a78feaee2e77e..c69ea48d3ee77710ca924639040fdb74e22611d2 100644 (file)
@@ -25,7 +25,6 @@ use lemmy_db_queries::{
     person_mention::PersonMention_,
     post::Post_,
     private_message::PrivateMessage_,
-    secret::SecretSingleton,
   },
   Blockable,
   Crud,
@@ -44,7 +43,6 @@ use lemmy_db_schema::{
     person_mention::*,
     post::Post,
     private_message::PrivateMessage,
-    secret::Secret,
     site::*,
   },
 };
@@ -63,7 +61,6 @@ use lemmy_utils::{
   claims::Claims,
   email::send_email,
   location_info,
-  settings::structs::Settings,
   utils::{generate_random_string, is_valid_display_name, is_valid_matrix_id, naive_from_unix},
   ApiError,
   ConnectionId,
@@ -105,9 +102,12 @@ impl Perform for Login {
     }
 
     // Return the jwt
-    let jwt_secret = Secret::get().jwt_secret;
     Ok(LoginResponse {
-      jwt: Claims::jwt(local_user_view.local_user.id.0, &jwt_secret)?,
+      jwt: Claims::jwt(
+        local_user_view.local_user.id.0,
+        &context.secret().jwt_secret,
+        &context.settings().hostname,
+      )?,
     })
   }
 }
@@ -121,7 +121,7 @@ impl Perform for GetCaptcha {
     context: &Data<LemmyContext>,
     _websocket_id: Option<ConnectionId>,
   ) -> Result<Self::Response, LemmyError> {
-    let captcha_settings = Settings::get().captcha;
+    let captcha_settings = context.settings().captcha;
 
     if !captcha_settings.enabled {
       return Ok(GetCaptchaResponse { ok: None });
@@ -167,7 +167,8 @@ impl Perform for SaveUserSettings {
     _websocket_id: Option<ConnectionId>,
   ) -> Result<LoginResponse, LemmyError> {
     let data: &SaveUserSettings = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
     let banner = diesel_option_overwrite_to_url(&data.banner)?;
@@ -184,7 +185,10 @@ impl Perform for SaveUserSettings {
     }
 
     if let Some(Some(display_name)) = &display_name {
-      if !is_valid_display_name(display_name.trim()) {
+      if !is_valid_display_name(
+        display_name.trim(),
+        context.settings().actor_name_max_length,
+      ) {
         return Err(ApiError::err("invalid_username").into());
       }
     }
@@ -271,9 +275,12 @@ impl Perform for SaveUserSettings {
     };
 
     // Return the jwt
-    let jwt_secret = Secret::get().jwt_secret;
     Ok(LoginResponse {
-      jwt: Claims::jwt(updated_local_user.id.0, &jwt_secret)?,
+      jwt: Claims::jwt(
+        updated_local_user.id.0,
+        &context.secret().jwt_secret,
+        &context.settings().hostname,
+      )?,
     })
   }
 }
@@ -288,7 +295,8 @@ impl Perform for ChangePassword {
     _websocket_id: Option<ConnectionId>,
   ) -> Result<LoginResponse, LemmyError> {
     let data: &ChangePassword = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     password_length_check(&data.new_password)?;
 
@@ -315,9 +323,12 @@ impl Perform for ChangePassword {
     .await??;
 
     // Return the jwt
-    let jwt_secret = Secret::get().jwt_secret;
     Ok(LoginResponse {
-      jwt: Claims::jwt(updated_local_user.id.0, &jwt_secret)?,
+      jwt: Claims::jwt(
+        updated_local_user.id.0,
+        &context.secret().jwt_secret,
+        &context.settings().hostname,
+      )?,
     })
   }
 }
@@ -332,7 +343,8 @@ impl Perform for AddAdmin {
     websocket_id: Option<ConnectionId>,
   ) -> Result<AddAdminResponse, LemmyError> {
     let data: &AddAdmin = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     // Make sure user is an admin
     is_admin(&local_user_view)?;
@@ -394,7 +406,8 @@ impl Perform for BanPerson {
     websocket_id: Option<ConnectionId>,
   ) -> Result<BanPersonResponse, LemmyError> {
     let data: &BanPerson = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     // Make sure user is an admin
     is_admin(&local_user_view)?;
@@ -486,7 +499,8 @@ impl Perform for BlockPerson {
     _websocket_id: Option<ConnectionId>,
   ) -> Result<BlockPersonResponse, LemmyError> {
     let data: &BlockPerson = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     let target_id = data.person_id;
     let person_id = local_user_view.person.id;
@@ -539,7 +553,8 @@ impl Perform for GetReplies {
     _websocket_id: Option<ConnectionId>,
   ) -> Result<GetRepliesResponse, LemmyError> {
     let data: &GetReplies = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
 
@@ -576,7 +591,8 @@ impl Perform for GetPersonMentions {
     _websocket_id: Option<ConnectionId>,
   ) -> Result<GetPersonMentionsResponse, LemmyError> {
     let data: &GetPersonMentions = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
 
@@ -610,7 +626,8 @@ impl Perform for MarkPersonMentionAsRead {
     _websocket_id: Option<ConnectionId>,
   ) -> Result<PersonMentionResponse, LemmyError> {
     let data: &MarkPersonMentionAsRead = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     let person_mention_id = data.person_mention_id;
     let read_person_mention = blocking(context.pool(), move |conn| {
@@ -653,7 +670,8 @@ impl Perform for MarkAllAsRead {
     _websocket_id: Option<ConnectionId>,
   ) -> Result<GetRepliesResponse, LemmyError> {
     let data: &MarkAllAsRead = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     let person_id = local_user_view.person.id;
     let replies = blocking(context.pool(), move |conn| {
@@ -732,10 +750,16 @@ impl Perform for PasswordReset {
     // TODO no i18n support here.
     let email = &local_user_view.local_user.email.expect("email");
     let subject = &format!("Password reset for {}", local_user_view.person.name);
-    let hostname = &Settings::get().get_protocol_and_hostname();
-    let html = &format!("<h1>Password Reset Request for {}</h1><br><a href={}/password_change/{}>Click here to reset your password</a>", local_user_view.person.name, hostname, &token);
-    send_email(subject, email, &local_user_view.person.name, html)
-      .map_err(|e| ApiError::err(&e))?;
+    let protocol_and_hostname = &context.settings().get_protocol_and_hostname();
+    let html = &format!("<h1>Password Reset Request for {}</h1><br><a href={}/password_change/{}>Click here to reset your password</a>", local_user_view.person.name, protocol_and_hostname, &token);
+    send_email(
+      subject,
+      email,
+      &local_user_view.person.name,
+      html,
+      &context.settings(),
+    )
+    .map_err(|e| ApiError::err(&e))?;
 
     Ok(PasswordResetResponse {})
   }
@@ -775,9 +799,12 @@ impl Perform for PasswordChange {
     .map_err(|_| ApiError::err("couldnt_update_user"))?;
 
     // Return the jwt
-    let jwt_secret = Secret::get().jwt_secret;
     Ok(LoginResponse {
-      jwt: Claims::jwt(updated_local_user.id.0, &jwt_secret)?,
+      jwt: Claims::jwt(
+        updated_local_user.id.0,
+        &context.secret().jwt_secret,
+        &context.settings().hostname,
+      )?,
     })
   }
 }
@@ -792,7 +819,8 @@ impl Perform for GetReportCount {
     websocket_id: Option<ConnectionId>,
   ) -> Result<GetReportCountResponse, LemmyError> {
     let data: &GetReportCount = self;
-    let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
+    let local_user_view =
+      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     let person_id = local_user_view.person.id;
     let community_id = data.community;