]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/verify_email.rs
Remove chatserver (#2919)
[lemmy.git] / crates / api / src / local_user / verify_email.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   context::LemmyContext,
5   person::{VerifyEmail, VerifyEmailResponse},
6   utils::send_email_verification_success,
7 };
8 use lemmy_db_schema::{
9   source::{
10     email_verification::EmailVerification,
11     local_user::{LocalUser, LocalUserUpdateForm},
12   },
13   traits::Crud,
14 };
15 use lemmy_db_views::structs::LocalUserView;
16 use lemmy_utils::error::LemmyError;
17
18 #[async_trait::async_trait(?Send)]
19 impl Perform for VerifyEmail {
20   type Response = VerifyEmailResponse;
21
22   async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError> {
23     let token = self.token.clone();
24     let verification = EmailVerification::read_for_token(context.pool(), &token)
25       .await
26       .map_err(|e| LemmyError::from_error_message(e, "token_not_found"))?;
27
28     let form = LocalUserUpdateForm::builder()
29       // necessary in case this is a new signup
30       .email_verified(Some(true))
31       // necessary in case email of an existing user was changed
32       .email(Some(Some(verification.email)))
33       .build();
34     let local_user_id = verification.local_user_id;
35
36     LocalUser::update(context.pool(), local_user_id, &form).await?;
37
38     let local_user_view = LocalUserView::read(context.pool(), local_user_id).await?;
39
40     send_email_verification_success(&local_user_view, context.settings())?;
41
42     EmailVerification::delete_old_tokens_for_local_user(context.pool(), local_user_id).await?;
43
44     Ok(VerifyEmailResponse {})
45   }
46 }