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(context.pool(), &token)
24 .with_lemmy_type(LemmyErrorType::TokenNotFound)?;
26 let form = LocalUserUpdateForm::builder()
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)))
32 let local_user_id = verification.local_user_id;
34 LocalUser::update(context.pool(), local_user_id, &form).await?;
36 EmailVerification::delete_old_tokens_for_local_user(context.pool(), local_user_id).await?;
38 Ok(VerifyEmailResponse {})