]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/reset_password.rs
Merge pull request #2593 from LemmyNet/refactor-notifications
[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   context::LemmyContext,
5   person::{PasswordReset, PasswordResetResponse},
6   utils::send_password_reset_email,
7 };
8 use lemmy_db_views::structs::LocalUserView;
9 use lemmy_utils::{error::LemmyError, ConnectionId};
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 = LocalUserView::find_by_email(context.pool(), &email)
26       .await
27       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?;
28
29     // Email the pure token to the user.
30     send_password_reset_email(&local_user_view, context.pool(), context.settings()).await?;
31     Ok(PasswordResetResponse {})
32   }
33 }