2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 person::{VerifyEmail, VerifyEmailResponse},
5 utils::{blocking, send_email_verification_success},
9 email_verification::EmailVerification,
10 local_user::{LocalUser, LocalUserForm},
14 use lemmy_db_views::structs::LocalUserView;
15 use lemmy_utils::error::LemmyError;
16 use lemmy_websocket::LemmyContext;
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 = blocking(context.pool(), move |conn| {
29 EmailVerification::read_for_token(conn, &token)
32 .map_err(|e| LemmyError::from_error_message(e, "token_not_found"))?;
34 let form = LocalUserForm {
35 // necessary in case this is a new signup
36 email_verified: Some(true),
37 // necessary in case email of an existing user was changed
38 email: Some(Some(verification.email)),
39 ..LocalUserForm::default()
41 let local_user_id = verification.local_user_id;
42 blocking(context.pool(), move |conn| {
43 LocalUser::update(conn, local_user_id, &form)
47 let local_user_view = blocking(context.pool(), move |conn| {
48 LocalUserView::read(conn, local_user_id)
52 send_email_verification_success(&local_user_view, &context.settings())?;
54 blocking(context.pool(), move |conn| {
55 EmailVerification::delete_old_tokens_for_local_user(conn, local_user_id)
59 Ok(VerifyEmailResponse {})