1 use crate::PerformCrud;
2 use activitypub_federation::core::signatures::generate_actor_keypair;
3 use actix_web::web::Data;
4 use lemmy_api_common::{
5 person::{LoginResponse, Register},
9 local_site_to_slur_regex,
10 password_length_check,
11 send_new_applicant_email_to_admins,
12 send_verification_email,
17 generate_local_apub_endpoint,
18 generate_shared_inbox_url,
21 use lemmy_db_schema::{
22 aggregates::structs::PersonAggregates,
24 local_user::{LocalUser, LocalUserInsertForm},
25 person::{Person, PersonInsertForm},
26 registration_application::{RegistrationApplication, RegistrationApplicationInsertForm},
30 use lemmy_db_views::structs::{LocalUserView, SiteView};
31 use lemmy_db_views_actor::structs::PersonViewSafe;
35 utils::{check_slurs, check_slurs_opt, is_valid_actor_name},
38 use lemmy_websocket::{messages::CheckCaptcha, LemmyContext};
40 #[async_trait::async_trait(?Send)]
41 impl PerformCrud for Register {
42 type Response = LoginResponse;
44 #[tracing::instrument(skip(self, context, _websocket_id))]
47 context: &Data<LemmyContext>,
48 _websocket_id: Option<ConnectionId>,
49 ) -> Result<LoginResponse, LemmyError> {
50 let data: &Register = self;
52 let site_view = blocking(context.pool(), SiteView::read_local).await??;
53 let local_site = site_view.local_site;
55 if !local_site.open_registration {
56 return Err(LemmyError::from_message("registration_closed"));
59 password_length_check(&data.password)?;
60 honeypot_check(&data.honeypot)?;
62 if local_site.require_email_verification && data.email.is_none() {
63 return Err(LemmyError::from_message("email_required"));
66 if local_site.require_application && data.answer.is_none() {
67 return Err(LemmyError::from_message(
68 "registration_application_answer_required",
72 // Make sure passwords match
73 if data.password != data.password_verify {
74 return Err(LemmyError::from_message("passwords_dont_match"));
77 // Check if there are admins. False if admins exist
78 let no_admins = blocking(context.pool(), move |conn| {
79 PersonViewSafe::admins(conn).map(|a| a.is_empty())
83 // If its not the admin, check the captcha
84 if !no_admins && local_site.captcha_enabled {
91 .unwrap_or_else(|| "".to_string()),
95 .unwrap_or_else(|| "".to_string()),
99 return Err(LemmyError::from_message("captcha_incorrect"));
103 let slur_regex = local_site_to_slur_regex(&local_site);
104 check_slurs(&data.username, &slur_regex)?;
105 check_slurs_opt(&data.answer, &slur_regex)?;
107 let actor_keypair = generate_actor_keypair()?;
108 if !is_valid_actor_name(&data.username, local_site.actor_name_max_length as usize) {
109 return Err(LemmyError::from_message("invalid_username"));
111 let actor_id = generate_local_apub_endpoint(
112 EndpointType::Person,
114 &context.settings().get_protocol_and_hostname(),
117 // We have to create both a person, and local_user
119 // Register the new person
120 let person_form = PersonInsertForm::builder()
121 .name(data.username.to_owned())
122 .actor_id(Some(actor_id.clone()))
123 .private_key(Some(actor_keypair.private_key))
124 .public_key(actor_keypair.public_key)
125 .inbox_url(Some(generate_inbox_url(&actor_id)?))
126 .shared_inbox_url(Some(generate_shared_inbox_url(&actor_id)?))
127 .admin(Some(no_admins))
128 .instance_id(site_view.site.instance_id)
132 let inserted_person = blocking(context.pool(), move |conn| {
133 Person::create(conn, &person_form)
136 .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?;
138 // Create the local user
139 let local_user_form = LocalUserInsertForm::builder()
140 .person_id(inserted_person.id)
141 .email(data.email.as_deref().map(|s| s.to_lowercase()))
142 .password_encrypted(data.password.to_string())
143 .show_nsfw(Some(data.show_nsfw))
146 let inserted_local_user = match blocking(context.pool(), move |conn| {
147 LocalUser::create(conn, &local_user_form)
153 let err_type = if e.to_string()
154 == "duplicate key value violates unique constraint \"local_user_email_key\""
156 "email_already_exists"
158 "user_already_exists"
161 // If the local user creation errored, then delete that person
162 blocking(context.pool(), move |conn| {
163 Person::delete(conn, inserted_person.id)
167 return Err(LemmyError::from_error_message(e, err_type));
171 if local_site.require_application {
172 // Create the registration application
173 let form = RegistrationApplicationInsertForm {
174 local_user_id: inserted_local_user.id,
175 // We already made sure answer was not null above
176 answer: data.answer.to_owned().expect("must have an answer"),
179 blocking(context.pool(), move |conn| {
180 RegistrationApplication::create(conn, &form)
186 if local_site.application_email_admins {
187 send_new_applicant_email_to_admins(&data.username, context.pool(), context.settings())
191 let mut login_response = LoginResponse {
193 registration_created: false,
194 verify_email_sent: false,
197 // Log the user in directly if email verification and application aren't required
198 if !local_site.require_application && !local_site.require_email_verification {
199 login_response.jwt = Some(
201 inserted_local_user.id.0,
202 &context.secret().jwt_secret,
203 &context.settings().hostname,
208 if local_site.require_email_verification {
209 let local_user_view = LocalUserView {
210 local_user: inserted_local_user,
211 person: inserted_person,
212 counts: PersonAggregates::default(),
214 // we check at the beginning of this method that email is set
215 let email = local_user_view
219 .expect("email was provided");
221 send_verification_email(&local_user_view, &email, context.pool(), context.settings())
223 login_response.verify_email_sent = true;
226 if local_site.require_application {
227 login_response.registration_created = true;