2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 person::{VerifyEmail, VerifyEmailResponse},
9 email_verification::EmailVerification,
10 local_user::{LocalUser, LocalUserUpdateForm},
14 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
16 #[async_trait::async_trait(?Send)]
17 impl Perform for VerifyEmail {
18 type Response = VerifyEmailResponse;
20 async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError> {
21 let token = self.token.clone();
22 let verification = EmailVerification::read_for_token(&mut context.pool(), &token)
24 .with_lemmy_type(LemmyErrorType::TokenNotFound)?;
26 let form = LocalUserUpdateForm {
27 // necessary in case this is a new signup
28 email_verified: Some(true),
29 // necessary in case email of an existing user was changed
30 email: Some(Some(verification.email)),
33 let local_user_id = verification.local_user_id;
35 LocalUser::update(&mut context.pool(), local_user_id, &form).await?;
37 EmailVerification::delete_old_tokens_for_local_user(&mut context.pool(), local_user_id).await?;
39 Ok(VerifyEmailResponse {})