]> Untitled Git - lemmy.git/blobdiff - crates/api_crud/src/user/create.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / api_crud / src / user / create.rs
index 302e2f98e62543265bd8415af69f0ae2ad94d507..c2f42bba563ac780aa1990fe451f0ef8bc02360f 100644 (file)
@@ -30,7 +30,7 @@ use lemmy_db_schema::{
 use lemmy_db_views::structs::{LocalUserView, SiteView};
 use lemmy_utils::{
   claims::Claims,
-  error::LemmyError,
+  error::{LemmyError, LemmyErrorExt, LemmyErrorType},
   utils::{
     slurs::{check_slurs, check_slurs_opt},
     validation::is_valid_actor_name,
@@ -45,38 +45,36 @@ impl PerformCrud for Register {
   async fn perform(&self, context: &Data<LemmyContext>) -> Result<LoginResponse, LemmyError> {
     let data: &Register = self;
 
-    let site_view = SiteView::read_local(context.pool()).await?;
+    let site_view = SiteView::read_local(&mut context.pool()).await?;
     let local_site = site_view.local_site;
     let require_registration_application =
       local_site.registration_mode == RegistrationMode::RequireApplication;
 
     if local_site.registration_mode == RegistrationMode::Closed {
-      return Err(LemmyError::from_message("registration_closed"));
+      return Err(LemmyErrorType::RegistrationClosed)?;
     }
 
     password_length_check(&data.password)?;
     honeypot_check(&data.honeypot)?;
 
     if local_site.require_email_verification && data.email.is_none() {
-      return Err(LemmyError::from_message("email_required"));
+      return Err(LemmyErrorType::EmailRequired)?;
     }
 
     if local_site.site_setup && require_registration_application && data.answer.is_none() {
-      return Err(LemmyError::from_message(
-        "registration_application_answer_required",
-      ));
+      return Err(LemmyErrorType::RegistrationApplicationAnswerRequired)?;
     }
 
     // Make sure passwords match
     if data.password != data.password_verify {
-      return Err(LemmyError::from_message("passwords_dont_match"));
+      return Err(LemmyErrorType::PasswordsDoNotMatch)?;
     }
 
     if local_site.site_setup && local_site.captcha_enabled {
       if let Some(captcha_uuid) = &data.captcha_uuid {
         let uuid = uuid::Uuid::parse_str(captcha_uuid)?;
         let check = CaptchaAnswer::check_captcha(
-          context.pool(),
+          &mut context.pool(),
           CheckCaptchaAnswer {
             uuid,
             answer: data.captcha_answer.clone().unwrap_or_default(),
@@ -84,10 +82,10 @@ impl PerformCrud for Register {
         )
         .await?;
         if !check {
-          return Err(LemmyError::from_message("captcha_incorrect"));
+          return Err(LemmyErrorType::CaptchaIncorrect)?;
         }
       } else {
-        return Err(LemmyError::from_message("captcha_incorrect"));
+        return Err(LemmyErrorType::CaptchaIncorrect)?;
       }
     }
 
@@ -104,8 +102,8 @@ impl PerformCrud for Register {
     )?;
 
     if let Some(email) = &data.email {
-      if LocalUser::is_email_taken(context.pool(), email).await? {
-        return Err(LemmyError::from_message("email_already_exists"));
+      if LocalUser::is_email_taken(&mut context.pool(), email).await? {
+        return Err(LemmyErrorType::EmailAlreadyExists)?;
       }
     }
 
@@ -125,9 +123,9 @@ impl PerformCrud for Register {
       .build();
 
     // insert the person
-    let inserted_person = Person::create(context.pool(), &person_form)
+    let inserted_person = Person::create(&mut context.pool(), &person_form)
       .await
-      .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?;
+      .with_lemmy_type(LemmyErrorType::UserAlreadyExists)?;
 
     // Automatically set their application as accepted, if they created this with open registration.
     // Also fixes a bug which allows users to log in when registrations are changed to closed.
@@ -142,7 +140,7 @@ impl PerformCrud for Register {
       .accepted_application(accepted_application)
       .build();
 
-    let inserted_local_user = LocalUser::create(context.pool(), &local_user_form).await?;
+    let inserted_local_user = LocalUser::create(&mut context.pool(), &local_user_form).await?;
 
     if local_site.site_setup && require_registration_application {
       // Create the registration application
@@ -152,12 +150,12 @@ impl PerformCrud for Register {
         answer: data.answer.clone().expect("must have an answer"),
       };
 
-      RegistrationApplication::create(context.pool(), &form).await?;
+      RegistrationApplication::create(&mut context.pool(), &form).await?;
     }
 
     // Email the admins
     if local_site.application_email_admins {
-      send_new_applicant_email_to_admins(&data.username, context.pool(), context.settings())
+      send_new_applicant_email_to_admins(&data.username, &mut context.pool(), context.settings())
         .await?;
     }
 
@@ -193,8 +191,13 @@ impl PerformCrud for Register {
           .clone()
           .expect("email was provided");
 
-        send_verification_email(&local_user_view, &email, context.pool(), context.settings())
-          .await?;
+        send_verification_email(
+          &local_user_view,
+          &email,
+          &mut context.pool(),
+          context.settings(),
+        )
+        .await?;
         login_response.verify_email_sent = true;
       }