2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 person::{VerifyEmail, VerifyEmailResponse},
6 send_email_verification_success,
10 email_verification::EmailVerification,
11 local_user::{LocalUser, LocalUserForm},
15 use lemmy_db_views::local_user_view::LocalUserView;
16 use lemmy_utils::LemmyError;
17 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 impl Perform for VerifyEmail {
21 type Response = VerifyEmailResponse;
25 context: &Data<LemmyContext>,
26 _websocket_id: Option<usize>,
27 ) -> Result<Self::Response, LemmyError> {
28 let token = self.token.clone();
29 let verification = blocking(context.pool(), move |conn| {
30 EmailVerification::read_for_token(conn, &token)
33 .map_err(|e| LemmyError::from_error_message(e, "token_not_found"))?;
35 let form = LocalUserForm {
36 // necessary in case this is a new signup
37 email_verified: Some(true),
38 // necessary in case email of an existing user was changed
39 email: Some(Some(verification.email)),
40 ..LocalUserForm::default()
42 let local_user_id = verification.local_user_id;
43 blocking(context.pool(), move |conn| {
44 LocalUser::update(conn, local_user_id, &form)
48 let local_user_view = blocking(context.pool(), move |conn| {
49 LocalUserView::read(conn, local_user_id)
53 send_email_verification_success(&local_user_view, &context.settings())?;
55 blocking(context.pool(), move |conn| {
56 EmailVerification::delete_old_tokens_for_local_user(conn, local_user_id)
60 Ok(VerifyEmailResponse {})