2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 person::{VerifyEmail, VerifyEmailResponse},
6 utils::send_email_verification_success,
10 email_verification::EmailVerification,
11 local_user::{LocalUser, LocalUserUpdateForm},
15 use lemmy_db_views::structs::LocalUserView;
16 use lemmy_utils::error::LemmyError;
18 #[async_trait::async_trait(?Send)]
19 impl Perform for VerifyEmail {
20 type Response = VerifyEmailResponse;
24 context: &Data<LemmyContext>,
25 _websocket_id: Option<usize>,
26 ) -> Result<Self::Response, LemmyError> {
27 let token = self.token.clone();
28 let verification = EmailVerification::read_for_token(context.pool(), &token)
30 .map_err(|e| LemmyError::from_error_message(e, "token_not_found"))?;
32 let form = LocalUserUpdateForm::builder()
33 // necessary in case this is a new signup
34 .email_verified(Some(true))
35 // necessary in case email of an existing user was changed
36 .email(Some(Some(verification.email)))
38 let local_user_id = verification.local_user_id;
40 LocalUser::update(context.pool(), local_user_id, &form).await?;
42 let local_user_view = LocalUserView::read(context.pool(), local_user_id).await?;
44 send_email_verification_success(&local_user_view, context.settings())?;
46 EmailVerification::delete_old_tokens_for_local_user(context.pool(), local_user_id).await?;
48 Ok(VerifyEmailResponse {})