2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 person::{LoginResponse, SaveUserSettings},
6 utils::{local_user_view_from_jwt, sanitize_html_opt, send_verification_email},
10 actor_language::LocalUserLanguage,
11 local_user::{LocalUser, LocalUserUpdateForm},
12 person::{Person, PersonUpdateForm},
15 utils::{diesel_option_overwrite, diesel_option_overwrite_to_url},
17 use lemmy_db_views::structs::SiteView;
20 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
23 generate_totp_2fa_secret,
25 is_valid_display_name,
30 #[async_trait::async_trait(?Send)]
31 impl Perform for SaveUserSettings {
32 type Response = LoginResponse;
34 #[tracing::instrument(skip(context))]
35 async fn perform(&self, context: &Data<LemmyContext>) -> Result<LoginResponse, LemmyError> {
36 let data: &SaveUserSettings = self;
37 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
38 let site_view = SiteView::read_local(&mut context.pool()).await?;
40 let bio = sanitize_html_opt(&data.bio);
41 let display_name = sanitize_html_opt(&data.display_name);
43 let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
44 let banner = diesel_option_overwrite_to_url(&data.banner)?;
45 let bio = diesel_option_overwrite(bio);
46 let display_name = diesel_option_overwrite(display_name);
47 let matrix_user_id = diesel_option_overwrite(data.matrix_user_id.clone());
48 let email_deref = data.email.as_deref().map(str::to_lowercase);
49 let email = diesel_option_overwrite(email_deref.clone());
51 if let Some(Some(email)) = &email {
52 let previous_email = local_user_view.local_user.email.clone().unwrap_or_default();
53 // Only send the verification email if there was an email change
54 if previous_email.ne(email) {
55 send_verification_email(
65 // When the site requires email, make sure email is not Some(None). IE, an overwrite to a None value
66 if let Some(email) = &email {
67 if email.is_none() && site_view.local_site.require_email_verification {
68 return Err(LemmyErrorType::EmailRequired)?;
72 if let Some(Some(bio)) = &bio {
73 is_valid_bio_field(bio)?;
76 if let Some(Some(display_name)) = &display_name {
77 is_valid_display_name(
79 site_view.local_site.actor_name_max_length as usize,
83 if let Some(Some(matrix_user_id)) = &matrix_user_id {
84 is_valid_matrix_id(matrix_user_id)?;
87 let local_user_id = local_user_view.local_user.id;
88 let person_id = local_user_view.person.id;
89 let default_listing_type = data.default_listing_type;
90 let default_sort_type = data.default_sort_type;
91 let theme = sanitize_html_opt(&data.theme);
93 let person_form = PersonUpdateForm::builder()
94 .display_name(display_name)
96 .matrix_user_id(matrix_user_id)
97 .bot_account(data.bot_account)
102 Person::update(&mut context.pool(), person_id, &person_form)
104 .with_lemmy_type(LemmyErrorType::UserAlreadyExists)?;
106 if let Some(discussion_languages) = data.discussion_languages.clone() {
107 LocalUserLanguage::update(&mut context.pool(), discussion_languages, local_user_id).await?;
110 // If generate_totp is Some(false), this will clear it out from the database.
111 let (totp_2fa_secret, totp_2fa_url) = if let Some(generate) = data.generate_totp_2fa {
113 let secret = generate_totp_2fa_secret();
115 build_totp_2fa(&site_view.site.name, &local_user_view.person.name, &secret)?.get_url();
116 (Some(Some(secret)), Some(Some(url)))
118 (Some(None), Some(None))
124 let local_user_form = LocalUserUpdateForm::builder()
126 .show_avatars(data.show_avatars)
127 .show_read_posts(data.show_read_posts)
128 .show_new_post_notifs(data.show_new_post_notifs)
129 .send_notifications_to_email(data.send_notifications_to_email)
130 .show_nsfw(data.show_nsfw)
131 .blur_nsfw(data.blur_nsfw)
132 .auto_expand(data.auto_expand)
133 .show_bot_accounts(data.show_bot_accounts)
134 .show_scores(data.show_scores)
135 .default_sort_type(default_sort_type)
136 .default_listing_type(default_listing_type)
138 .interface_language(data.interface_language.clone())
139 .totp_2fa_secret(totp_2fa_secret)
140 .totp_2fa_url(totp_2fa_url)
141 .open_links_in_new_tab(data.open_links_in_new_tab)
142 .infinite_scroll_enabled(data.infinite_scroll_enabled)
146 LocalUser::update(&mut context.pool(), local_user_id, &local_user_form).await;
147 let updated_local_user = match local_user_res {
150 let err_type = if e.to_string()
151 == "duplicate key value violates unique constraint \"local_user_email_key\""
153 LemmyErrorType::EmailAlreadyExists
155 LemmyErrorType::UserAlreadyExists
158 return Err(e).with_lemmy_type(err_type);
166 updated_local_user.id.0,
167 &context.secret().jwt_secret,
168 &context.settings().hostname,
172 verify_email_sent: false,
173 registration_created: false,