1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, password_length_check, person::*};
5 generate_apub_endpoint,
6 generate_followers_url,
8 generate_shared_inbox_url,
11 use lemmy_db_queries::{
12 source::{local_user::LocalUser_, site::Site_},
19 use lemmy_db_schema::{
22 local_user::{LocalUser, LocalUserForm},
28 use lemmy_db_views_actor::person_view::PersonViewSafe;
30 apub::generate_actor_keypair,
32 settings::structs::Settings,
33 utils::{check_slurs, is_valid_username},
38 use lemmy_websocket::{messages::CheckCaptcha, LemmyContext};
40 #[async_trait::async_trait(?Send)]
41 impl PerformCrud for Register {
42 type Response = LoginResponse;
46 context: &Data<LemmyContext>,
47 _websocket_id: Option<ConnectionId>,
48 ) -> Result<LoginResponse, LemmyError> {
49 let data: &Register = &self;
51 // Make sure site has open registration
52 if let Ok(site) = blocking(context.pool(), move |conn| Site::read_simple(conn)).await? {
53 if !site.open_registration {
54 return Err(ApiError::err("registration_closed").into());
58 password_length_check(&data.password)?;
60 // Make sure passwords match
61 if data.password != data.password_verify {
62 return Err(ApiError::err("passwords_dont_match").into());
65 // Check if there are admins. False if admins exist
66 let no_admins = blocking(context.pool(), move |conn| {
67 PersonViewSafe::admins(conn).map(|a| a.is_empty())
71 // If its not the admin, check the captcha
72 if !no_admins && Settings::get().captcha().enabled {
79 .unwrap_or_else(|| "".to_string()),
83 .unwrap_or_else(|| "".to_string()),
87 return Err(ApiError::err("captcha_incorrect").into());
91 check_slurs(&data.username)?;
93 let actor_keypair = generate_actor_keypair()?;
94 if !is_valid_username(&data.username) {
95 return Err(ApiError::err("invalid_username").into());
97 let actor_id = generate_apub_endpoint(EndpointType::Person, &data.username)?;
99 // We have to create both a person, and local_user
101 // Register the new person
102 let person_form = PersonForm {
103 name: data.username.to_owned(),
106 preferred_username: None,
111 actor_id: Some(actor_id.clone()),
114 private_key: Some(Some(actor_keypair.private_key)),
115 public_key: Some(Some(actor_keypair.public_key)),
116 last_refreshed_at: None,
117 inbox_url: Some(generate_inbox_url(&actor_id)?),
118 shared_inbox_url: Some(Some(generate_shared_inbox_url(&actor_id)?)),
122 let inserted_person = match blocking(context.pool(), move |conn| {
123 Person::create(conn, &person_form)
129 return Err(ApiError::err("user_already_exists").into());
133 // Create the local user
134 let local_user_form = LocalUserForm {
135 person_id: inserted_person.id,
136 email: Some(data.email.to_owned()),
137 matrix_user_id: None,
138 password_encrypted: data.password.to_owned(),
139 admin: Some(no_admins),
140 show_nsfw: Some(data.show_nsfw),
141 theme: Some("browser".into()),
142 default_sort_type: Some(SortType::Active as i16),
143 default_listing_type: Some(ListingType::Subscribed as i16),
144 lang: Some("browser".into()),
145 show_avatars: Some(true),
146 send_notifications_to_email: Some(false),
149 let inserted_local_user = match blocking(context.pool(), move |conn| {
150 LocalUser::register(conn, &local_user_form)
156 let err_type = if e.to_string()
157 == "duplicate key value violates unique constraint \"local_user_email_key\""
159 "email_already_exists"
161 "user_already_exists"
164 // If the local user creation errored, then delete that person
165 blocking(context.pool(), move |conn| {
166 Person::delete(&conn, inserted_person.id)
170 return Err(ApiError::err(err_type).into());
174 let main_community_keypair = generate_actor_keypair()?;
176 // Create the main community if it doesn't exist
177 let main_community = match blocking(context.pool(), move |conn| {
178 Community::read(conn, CommunityId(2))
184 let default_community_name = "main";
185 let actor_id = generate_apub_endpoint(EndpointType::Community, default_community_name)?;
186 let community_form = CommunityForm {
187 name: default_community_name.to_string(),
188 title: "The Default Community".to_string(),
189 description: Some("The Default Community".to_string()),
191 creator_id: inserted_person.id,
195 actor_id: Some(actor_id.to_owned()),
197 private_key: Some(main_community_keypair.private_key),
198 public_key: Some(main_community_keypair.public_key),
199 last_refreshed_at: None,
203 followers_url: Some(generate_followers_url(&actor_id)?),
204 inbox_url: Some(generate_inbox_url(&actor_id)?),
205 shared_inbox_url: Some(Some(generate_shared_inbox_url(&actor_id)?)),
207 blocking(context.pool(), move |conn| {
208 Community::create(conn, &community_form)
214 // Sign them up for main community no matter what
215 let community_follower_form = CommunityFollowerForm {
216 community_id: main_community.id,
217 person_id: inserted_person.id,
221 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
222 if blocking(context.pool(), follow).await?.is_err() {
223 return Err(ApiError::err("community_follower_already_exists").into());
226 // If its an admin, add them as a mod and follower to main
228 let community_moderator_form = CommunityModeratorForm {
229 community_id: main_community.id,
230 person_id: inserted_person.id,
233 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
234 if blocking(context.pool(), join).await?.is_err() {
235 return Err(ApiError::err("community_moderator_already_exists").into());
241 jwt: Claims::jwt(inserted_local_user.id.0)?,