2023-09-05 09:33:46 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2022-04-13 18:12:25 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-10-16 16:36:53 +00:00
|
|
|
person::VerifyEmail,
|
2023-09-28 14:06:45 +00:00
|
|
|
utils::send_new_applicant_email_to_admins,
|
2023-10-16 16:36:53 +00:00
|
|
|
SuccessResponse,
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
2024-07-30 12:34:17 +00:00
|
|
|
use lemmy_db_schema::source::{
|
|
|
|
email_verification::EmailVerification,
|
|
|
|
local_user::{LocalUser, LocalUserUpdateForm},
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
2024-05-15 02:37:30 +00:00
|
|
|
use lemmy_db_views::structs::{LocalUserView, SiteView};
|
2024-04-16 12:48:15 +00:00
|
|
|
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
pub async fn verify_email(
|
|
|
|
data: Json<VerifyEmail>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-10-16 16:36:53 +00:00
|
|
|
) -> LemmyResult<Json<SuccessResponse>> {
|
2024-04-16 12:48:15 +00:00
|
|
|
let site_view = SiteView::read_local(&mut context.pool())
|
|
|
|
.await?
|
|
|
|
.ok_or(LemmyErrorType::LocalSiteNotSetup)?;
|
2023-09-05 09:33:46 +00:00
|
|
|
let token = data.token.clone();
|
|
|
|
let verification = EmailVerification::read_for_token(&mut context.pool(), &token)
|
2024-04-16 12:48:15 +00:00
|
|
|
.await?
|
|
|
|
.ok_or(LemmyErrorType::TokenNotFound)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
let form = LocalUserUpdateForm {
|
|
|
|
// necessary in case this is a new signup
|
|
|
|
email_verified: Some(true),
|
|
|
|
// necessary in case email of an existing user was changed
|
|
|
|
email: Some(Some(verification.email)),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let local_user_id = verification.local_user_id;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2024-05-15 02:37:30 +00:00
|
|
|
LocalUser::update(&mut context.pool(), local_user_id, &form).await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
EmailVerification::delete_old_tokens_for_local_user(&mut context.pool(), local_user_id).await?;
|
2022-11-09 10:05:00 +00:00
|
|
|
|
2023-09-28 14:06:45 +00:00
|
|
|
// send out notification about registration application to admins if enabled
|
2024-07-30 12:34:17 +00:00
|
|
|
if site_view.local_site.application_email_admins {
|
2024-05-15 02:37:30 +00:00
|
|
|
let local_user = LocalUserView::read(&mut context.pool(), local_user_id)
|
2024-04-16 12:48:15 +00:00
|
|
|
.await?
|
|
|
|
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
|
|
|
|
2024-05-15 02:37:30 +00:00
|
|
|
send_new_applicant_email_to_admins(
|
|
|
|
&local_user.person.name,
|
|
|
|
&mut context.pool(),
|
|
|
|
context.settings(),
|
|
|
|
)
|
|
|
|
.await?;
|
2023-09-28 14:06:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 16:36:53 +00:00
|
|
|
Ok(Json(SuccessResponse::default()))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|