]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/reset_password.rs
Convert emails to lowercase (fixes #2462) (#2463)
[lemmy.git] / crates / api / src / local_user / reset_password.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   person::{PasswordReset, PasswordResetResponse},
5   utils::{blocking, send_password_reset_email},
6 };
7 use lemmy_db_views::structs::LocalUserView;
8 use lemmy_utils::{error::LemmyError, ConnectionId};
9 use lemmy_websocket::LemmyContext;
10
11 #[async_trait::async_trait(?Send)]
12 impl Perform for PasswordReset {
13   type Response = PasswordResetResponse;
14
15   #[tracing::instrument(skip(self, context, _websocket_id))]
16   async fn perform(
17     &self,
18     context: &Data<LemmyContext>,
19     _websocket_id: Option<ConnectionId>,
20   ) -> Result<PasswordResetResponse, LemmyError> {
21     let data: &PasswordReset = self;
22
23     // Fetch that email
24     let email = data.email.to_lowercase();
25     let local_user_view = blocking(context.pool(), move |conn| {
26       LocalUserView::find_by_email(conn, &email)
27     })
28     .await?
29     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?;
30
31     // Email the pure token to the user.
32     send_password_reset_email(&local_user_view, context.pool(), context.settings()).await?;
33     Ok(PasswordResetResponse {})
34   }
35 }