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},
6 utils::{blocking, honeypot_check, password_length_check, send_verification_email},
10 generate_local_apub_endpoint,
11 generate_shared_inbox_url,
14 use lemmy_db_schema::{
15 aggregates::structs::PersonAggregates,
17 local_user::{LocalUser, LocalUserForm},
18 person::{Person, PersonForm},
19 registration_application::{RegistrationApplication, RegistrationApplicationForm},
24 use lemmy_db_views::structs::LocalUserView;
25 use lemmy_db_views_actor::structs::PersonViewSafe;
29 utils::{check_slurs, check_slurs_opt, is_valid_actor_name},
32 use lemmy_websocket::{messages::CheckCaptcha, LemmyContext};
34 #[async_trait::async_trait(?Send)]
35 impl PerformCrud for Register {
36 type Response = LoginResponse;
38 #[tracing::instrument(skip(self, context, _websocket_id))]
41 context: &Data<LemmyContext>,
42 _websocket_id: Option<ConnectionId>,
43 ) -> Result<LoginResponse, LemmyError> {
44 let data: &Register = self;
46 // no email verification, or applications if the site is not setup yet
47 let (mut email_verification, mut require_application) = (false, false);
49 // Make sure site has open registration
50 if let Ok(site) = blocking(context.pool(), Site::read_local_site).await? {
51 if !site.open_registration {
52 return Err(LemmyError::from_message("registration_closed"));
54 email_verification = site.require_email_verification;
55 require_application = site.require_application;
58 password_length_check(&data.password)?;
59 honeypot_check(&data.honeypot)?;
61 if email_verification && data.email.is_none() {
62 return Err(LemmyError::from_message("email_required"));
65 if require_application && data.answer.is_none() {
66 return Err(LemmyError::from_message(
67 "registration_application_answer_required",
71 // Make sure passwords match
72 if data.password != data.password_verify {
73 return Err(LemmyError::from_message("passwords_dont_match"));
76 // Check if there are admins. False if admins exist
77 let no_admins = blocking(context.pool(), move |conn| {
78 PersonViewSafe::admins(conn).map(|a| a.is_empty())
82 // If its not the admin, check the captcha
83 if !no_admins && context.settings().captcha.enabled {
90 .unwrap_or_else(|| "".to_string()),
94 .unwrap_or_else(|| "".to_string()),
98 return Err(LemmyError::from_message("captcha_incorrect"));
102 let slur_regex = &context.settings().slur_regex();
103 check_slurs(&data.username, slur_regex)?;
104 check_slurs_opt(&data.answer, slur_regex)?;
106 let actor_keypair = generate_actor_keypair()?;
107 if !is_valid_actor_name(&data.username, context.settings().actor_name_max_length) {
108 return Err(LemmyError::from_message("invalid_username"));
110 let actor_id = generate_local_apub_endpoint(
111 EndpointType::Person,
113 &context.settings().get_protocol_and_hostname(),
116 // We have to create both a person, and local_user
118 // Register the new person
119 let person_form = PersonForm {
120 name: data.username.to_owned(),
121 actor_id: Some(actor_id.clone()),
122 private_key: Some(Some(actor_keypair.private_key)),
123 public_key: Some(actor_keypair.public_key),
124 inbox_url: Some(generate_inbox_url(&actor_id)?),
125 shared_inbox_url: Some(Some(generate_shared_inbox_url(&actor_id)?)),
126 admin: Some(no_admins),
127 ..PersonForm::default()
131 let inserted_person = blocking(context.pool(), move |conn| {
132 Person::create(conn, &person_form)
135 .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?;
137 // Create the local user
138 let local_user_form = LocalUserForm {
139 person_id: Some(inserted_person.id),
140 email: Some(data.email.as_deref().map(|s| s.to_lowercase())),
141 password_encrypted: Some(data.password.to_string()),
142 show_nsfw: Some(data.show_nsfw),
143 email_verified: Some(false),
144 ..LocalUserForm::default()
147 let inserted_local_user = match blocking(context.pool(), move |conn| {
148 LocalUser::register(conn, &local_user_form)
154 let err_type = if e.to_string()
155 == "duplicate key value violates unique constraint \"local_user_email_key\""
157 "email_already_exists"
159 "user_already_exists"
162 // If the local user creation errored, then delete that person
163 blocking(context.pool(), move |conn| {
164 Person::delete(conn, inserted_person.id)
168 return Err(LemmyError::from_error_message(e, err_type));
172 if require_application {
173 // Create the registration application
174 let form = RegistrationApplicationForm {
175 local_user_id: Some(inserted_local_user.id),
176 // We already made sure answer was not null above
177 answer: data.answer.to_owned(),
178 ..RegistrationApplicationForm::default()
181 blocking(context.pool(), move |conn| {
182 RegistrationApplication::create(conn, &form)
187 let mut login_response = LoginResponse {
189 registration_created: false,
190 verify_email_sent: false,
193 // Log the user in directly if email verification and application aren't required
194 if !require_application && !email_verification {
195 login_response.jwt = Some(
197 inserted_local_user.id.0,
198 &context.secret().jwt_secret,
199 &context.settings().hostname,
204 if email_verification {
205 let local_user_view = LocalUserView {
206 local_user: inserted_local_user,
207 person: inserted_person,
208 counts: PersonAggregates::default(),
210 // we check at the beginning of this method that email is set
211 let email = local_user_view
215 .expect("email was provided");
216 send_verification_email(&local_user_view, &email, context.pool(), context.settings())
218 login_response.verify_email_sent = true;
221 if require_application {
222 login_response.registration_created = true;