1 use crate::PerformCrud;
2 use activitypub_federation::http_signatures::generate_actor_keypair;
3 use actix_web::web::Data;
4 use lemmy_api_common::{
6 person::{LoginResponse, Register},
9 generate_local_apub_endpoint,
10 generate_shared_inbox_url,
12 local_site_to_slur_regex,
13 password_length_check,
14 send_new_applicant_email_to_admins,
15 send_verification_email,
18 websocket::handlers::captcha::CheckCaptcha,
20 use lemmy_db_schema::{
21 aggregates::structs::PersonAggregates,
23 local_user::{LocalUser, LocalUserInsertForm},
24 person::{Person, PersonInsertForm},
25 registration_application::{RegistrationApplication, RegistrationApplicationInsertForm},
30 use lemmy_db_views::structs::{LocalUserView, SiteView};
35 slurs::{check_slurs, check_slurs_opt},
36 validation::is_valid_actor_name,
41 #[async_trait::async_trait(?Send)]
42 impl PerformCrud for Register {
43 type Response = LoginResponse;
45 #[tracing::instrument(skip(self, context, _websocket_id))]
48 context: &Data<LemmyContext>,
49 _websocket_id: Option<ConnectionId>,
50 ) -> Result<LoginResponse, LemmyError> {
51 let data: &Register = self;
53 let site_view = SiteView::read_local(context.pool()).await?;
54 let local_site = site_view.local_site;
55 let require_registration_application =
56 local_site.registration_mode == RegistrationMode::RequireApplication;
58 if local_site.registration_mode == RegistrationMode::Closed {
59 return Err(LemmyError::from_message("registration_closed"));
62 password_length_check(&data.password)?;
63 honeypot_check(&data.honeypot)?;
65 if local_site.require_email_verification && data.email.is_none() {
66 return Err(LemmyError::from_message("email_required"));
69 if local_site.site_setup && require_registration_application && data.answer.is_none() {
70 return Err(LemmyError::from_message(
71 "registration_application_answer_required",
75 // Make sure passwords match
76 if data.password != data.password_verify {
77 return Err(LemmyError::from_message("passwords_dont_match"));
80 // If the site is set up, check the captcha
81 if local_site.site_setup && local_site.captcha_enabled {
85 uuid: data.captcha_uuid.clone().unwrap_or_default(),
86 answer: data.captcha_answer.clone().unwrap_or_default(),
90 return Err(LemmyError::from_message("captcha_incorrect"));
94 let slur_regex = local_site_to_slur_regex(&local_site);
95 check_slurs(&data.username, &slur_regex)?;
96 check_slurs_opt(&data.answer, &slur_regex)?;
98 let actor_keypair = generate_actor_keypair()?;
99 is_valid_actor_name(&data.username, local_site.actor_name_max_length as usize)?;
100 let actor_id = generate_local_apub_endpoint(
101 EndpointType::Person,
103 &context.settings().get_protocol_and_hostname(),
106 // We have to create both a person, and local_user
108 // Register the new person
109 let person_form = PersonInsertForm::builder()
110 .name(data.username.clone())
111 .actor_id(Some(actor_id.clone()))
112 .private_key(Some(actor_keypair.private_key))
113 .public_key(actor_keypair.public_key)
114 .inbox_url(Some(generate_inbox_url(&actor_id)?))
115 .shared_inbox_url(Some(generate_shared_inbox_url(&actor_id)?))
116 // If its the initial site setup, they are an admin
117 .admin(Some(!local_site.site_setup))
118 .instance_id(site_view.site.instance_id)
122 let inserted_person = Person::create(context.pool(), &person_form)
124 .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?;
126 // Create the local user
127 let local_user_form = LocalUserInsertForm::builder()
128 .person_id(inserted_person.id)
129 .email(data.email.as_deref().map(str::to_lowercase))
130 .password_encrypted(data.password.to_string())
131 .show_nsfw(Some(data.show_nsfw))
134 let inserted_local_user = match LocalUser::create(context.pool(), &local_user_form).await {
137 let err_type = if e.to_string()
138 == "duplicate key value violates unique constraint \"local_user_email_key\""
140 "email_already_exists"
142 "user_already_exists"
145 // If the local user creation errored, then delete that person
146 Person::delete(context.pool(), inserted_person.id).await?;
148 return Err(LemmyError::from_error_message(e, err_type));
152 if local_site.site_setup && require_registration_application {
153 // Create the registration application
154 let form = RegistrationApplicationInsertForm {
155 local_user_id: inserted_local_user.id,
156 // We already made sure answer was not null above
157 answer: data.answer.clone().expect("must have an answer"),
160 RegistrationApplication::create(context.pool(), &form).await?;
164 if local_site.application_email_admins {
165 send_new_applicant_email_to_admins(&data.username, context.pool(), context.settings())
169 let mut login_response = LoginResponse {
171 registration_created: false,
172 verify_email_sent: false,
175 // Log the user in directly if the site is not setup, or email verification and application aren't required
176 if !local_site.site_setup
177 || (!require_registration_application && !local_site.require_email_verification)
179 login_response.jwt = Some(
181 inserted_local_user.id.0,
182 &context.secret().jwt_secret,
183 &context.settings().hostname,
188 if local_site.require_email_verification {
189 let local_user_view = LocalUserView {
190 local_user: inserted_local_user,
191 person: inserted_person,
192 counts: PersonAggregates::default(),
194 // we check at the beginning of this method that email is set
195 let email = local_user_view
199 .expect("email was provided");
201 send_verification_email(&local_user_view, &email, context.pool(), context.settings())
203 login_response.verify_email_sent = true;
206 if require_registration_application {
207 login_response.registration_created = true;