2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 person::{LoginResponse, SaveUserSettings},
5 utils::{get_local_user_view_from_jwt, send_verification_email},
10 actor_language::LocalUserLanguage,
11 local_site::LocalSite,
12 local_user::{LocalUser, LocalUserUpdateForm},
13 person::{Person, PersonUpdateForm},
16 utils::{diesel_option_overwrite, diesel_option_overwrite_to_url},
21 utils::{is_valid_display_name, is_valid_matrix_id},
25 #[async_trait::async_trait(?Send)]
26 impl Perform for SaveUserSettings {
27 type Response = LoginResponse;
29 #[tracing::instrument(skip(context, _websocket_id))]
32 context: &Data<LemmyContext>,
33 _websocket_id: Option<ConnectionId>,
34 ) -> Result<LoginResponse, LemmyError> {
35 let data: &SaveUserSettings = self;
37 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38 let local_site = LocalSite::read(context.pool()).await?;
40 let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
41 let banner = diesel_option_overwrite_to_url(&data.banner)?;
42 let bio = diesel_option_overwrite(&data.bio);
43 let display_name = diesel_option_overwrite(&data.display_name);
44 let matrix_user_id = diesel_option_overwrite(&data.matrix_user_id);
45 let bot_account = data.bot_account;
46 let email_deref = data.email.as_deref().map(str::to_lowercase);
47 let email = diesel_option_overwrite(&email_deref);
49 if let Some(Some(email)) = &email {
50 let previous_email = local_user_view.local_user.email.clone().unwrap_or_default();
51 // Only send the verification email if there was an email change
52 if previous_email.ne(email) {
53 send_verification_email(&local_user_view, email, context.pool(), context.settings())
58 // When the site requires email, make sure email is not Some(None). IE, an overwrite to a None value
59 if let Some(email) = &email {
60 if email.is_none() && local_site.require_email_verification {
61 return Err(LemmyError::from_message("email_required"));
65 if let Some(Some(bio)) = &bio {
66 if bio.chars().count() > 300 {
67 return Err(LemmyError::from_message("bio_length_overflow"));
71 if let Some(Some(display_name)) = &display_name {
72 if !is_valid_display_name(
74 local_site.actor_name_max_length as usize,
76 return Err(LemmyError::from_message("invalid_username"));
80 if let Some(Some(matrix_user_id)) = &matrix_user_id {
81 if !is_valid_matrix_id(matrix_user_id) {
82 return Err(LemmyError::from_message("invalid_matrix_id"));
86 let local_user_id = local_user_view.local_user.id;
87 let person_id = local_user_view.person.id;
88 let default_listing_type = data.default_listing_type;
89 let default_sort_type = data.default_sort_type;
91 let person_form = PersonUpdateForm::builder()
92 .display_name(display_name)
94 .matrix_user_id(matrix_user_id)
95 .bot_account(bot_account)
100 Person::update(context.pool(), person_id, &person_form)
102 .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?;
104 if let Some(discussion_languages) = data.discussion_languages.clone() {
105 LocalUserLanguage::update(context.pool(), discussion_languages, local_user_id).await?;
108 let local_user_form = LocalUserUpdateForm::builder()
110 .show_avatars(data.show_avatars)
111 .show_read_posts(data.show_read_posts)
112 .show_new_post_notifs(data.show_new_post_notifs)
113 .send_notifications_to_email(data.send_notifications_to_email)
114 .show_nsfw(data.show_nsfw)
115 .show_bot_accounts(data.show_bot_accounts)
116 .show_scores(data.show_scores)
117 .default_sort_type(default_sort_type)
118 .default_listing_type(default_listing_type)
119 .theme(data.theme.clone())
120 .interface_language(data.interface_language.clone())
123 let local_user_res = LocalUser::update(context.pool(), local_user_id, &local_user_form).await;
124 let updated_local_user = match local_user_res {
127 let err_type = if e.to_string()
128 == "duplicate key value violates unique constraint \"local_user_email_key\""
130 "email_already_exists"
132 "user_already_exists"
135 return Err(LemmyError::from_error_message(e, err_type));
143 updated_local_user.id.0,
144 &context.secret().jwt_secret,
145 &context.settings().hostname,
149 verify_email_sent: false,
150 registration_created: false,