]> Untitled Git - lemmy.git/commitdiff
Add email overwrite on user settings save. Fixes #1069
authorDessalines <tyhou13@gmx.com>
Sun, 9 Aug 2020 02:36:29 +0000 (22:36 -0400)
committerDessalines <tyhou13@gmx.com>
Sun, 9 Aug 2020 02:36:29 +0000 (22:36 -0400)
- Also add get_user_secure to other locations.

server/lemmy_db/src/user.rs
server/src/api/community.rs
server/src/api/user.rs
server/src/apub/fetcher.rs
server/src/code_migrations.rs
ui/src/components/user.tsx
ui/src/interfaces.ts

index d30609bc3cac7ea7b6998c65417952db4de61ae3..73da1c2446e52f49a8c451a1b7c7c7fbc6b50bd6 100644 (file)
@@ -46,7 +46,7 @@ pub struct UserForm {
   pub password_encrypted: String,
   pub admin: bool,
   pub banned: bool,
-  pub email: Option<String>,
+  pub email: Option<Option<String>>,
   pub avatar: Option<Option<String>>,
   pub updated: Option<chrono::NaiveDateTime>,
   pub show_nsfw: bool,
index ce2851b1d6544d6ca83b6bc9ef0c0132ba39feb1..c716084841e83611c1119e3d83660613368459b0 100644 (file)
@@ -710,7 +710,7 @@ impl Perform for Oper<BanFromCommunity> {
     blocking(pool, move |conn| ModBanFromCommunity::create(conn, &form)).await??;
 
     let user_id = data.user_id;
-    let user_view = blocking(pool, move |conn| UserView::read(conn, user_id)).await??;
+    let user_view = blocking(pool, move |conn| UserView::get_user_secure(conn, user_id)).await??;
 
     let res = BanFromCommunityResponse {
       user: user_view,
index f5ab84c51846816f9f523ac1c43c9c77f61a02fa..2d5895170d9d2fd46165bd3b05ae5294680fa3e2 100644 (file)
@@ -395,7 +395,7 @@ impl Perform for Oper<Register> {
     // Register the new user
     let user_form = UserForm {
       name: data.username.to_owned(),
-      email: data.email.to_owned(),
+      email: Some(data.email.to_owned()),
       matrix_user_id: None,
       avatar: None,
       banner: None,
@@ -559,11 +559,6 @@ impl Perform for Oper<SaveUserSettings> {
     let user_id = user.id;
     let read_user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
 
-    let email = match &data.email {
-      Some(email) => Some(email.to_owned()),
-      None => read_user.email,
-    };
-
     let bio = match &data.bio {
       Some(bio) => {
         if bio.chars().count() <= 300 {
@@ -577,6 +572,7 @@ impl Perform for Oper<SaveUserSettings> {
 
     let avatar = diesel_option_overwrite(&data.avatar);
     let banner = diesel_option_overwrite(&data.banner);
+    let email = diesel_option_overwrite(&data.email);
 
     // The DB constraint should stop too many characters
     let preferred_username = match &data.preferred_username {
@@ -700,7 +696,10 @@ impl Perform for Oper<GetUserDetails> {
       }
     };
 
-    let mut user_view = blocking(pool, move |conn| UserView::read(conn, user_details_id)).await??;
+    let user_view = blocking(pool, move |conn| {
+      UserView::get_user_secure(conn, user_details_id)
+    })
+    .await??;
 
     let page = data.page;
     let limit = data.limit;
@@ -747,13 +746,6 @@ impl Perform for Oper<GetUserDetails> {
     })
     .await??;
 
-    // If its not the same user, remove the email, and settings
-    // TODO an if let chain would be better here, but can't figure it out
-    // TODO separate out settings into its own thing
-    if user_id.is_none() || user_details_id != user_id.unwrap_or(0) {
-      user_view.email = None;
-    }
-
     // Return the jwt
     Ok(GetUserDetailsResponse {
       user: user_view,
index b59e9e321de8a24dcba8b1496142ff8c17969ce6..9bb058cd7ad2c80b75e8f162357416e48235ffdc 100644 (file)
@@ -151,7 +151,8 @@ pub async fn search_by_apub_id(
 
       let user = get_or_fetch_and_upsert_user(&user_uri, client, pool).await?;
 
-      response.users = vec![blocking(pool, move |conn| UserView::read(conn, user.id)).await??];
+      response.users =
+        vec![blocking(pool, move |conn| UserView::get_user_secure(conn, user.id)).await??];
 
       response
     }
index 2e70040c036db7b9548b5ac66513fbf0be62d231..7f45237c4b56b6fbdece7b82c76f93f15218705d 100644 (file)
@@ -51,7 +51,7 @@ fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
 
     let form = UserForm {
       name: cuser.name.to_owned(),
-      email: cuser.email.to_owned(),
+      email: Some(cuser.email.to_owned()),
       matrix_user_id: cuser.matrix_user_id.to_owned(),
       avatar: Some(cuser.avatar.to_owned()),
       banner: Some(cuser.banner.to_owned()),
index ad77e26705a164d8d97905f048365538ddccb35e..e14f56a19c000d434ca7d4c113e6085eac147089 100644 (file)
@@ -90,8 +90,6 @@ export class User extends Component<any, UserState> {
       comment_score: null,
       banned: null,
       avatar: null,
-      show_avatars: null,
-      send_notifications_to_email: null,
       actor_id: null,
       local: null,
     },
@@ -143,6 +141,7 @@ export class User extends Component<any, UserState> {
         creator_preferred_username: undefined,
       },
       version: undefined,
+      my_user: undefined,
     },
   };
 
@@ -724,7 +723,7 @@ export class User extends Component<any, UserState> {
                     class="form-check-input"
                     id="user-send-notifications-to-email"
                     type="checkbox"
-                    disabled={!this.state.user.email}
+                    disabled={!this.state.userSettingsForm.email}
                     checked={
                       this.state.userSettingsForm.send_notifications_to_email
                     }
@@ -922,9 +921,6 @@ export class User extends Component<any, UserState> {
 
   handleUserSettingsEmailChange(i: User, event: any) {
     i.state.userSettingsForm.email = event.target.value;
-    if (i.state.userSettingsForm.email == '' && !i.state.user.email) {
-      i.state.userSettingsForm.email = undefined;
-    }
     i.setState(i.state);
   }
 
@@ -1063,12 +1059,14 @@ export class User extends Component<any, UserState> {
           this.state.userSettingsForm.banner = UserService.Instance.user.banner;
           this.state.userSettingsForm.preferred_username =
             UserService.Instance.user.preferred_username;
-          this.state.userSettingsForm.email = this.state.user.email;
-          this.state.userSettingsForm.bio = this.state.user.bio;
-          this.state.userSettingsForm.send_notifications_to_email = this.state.user.send_notifications_to_email;
           this.state.userSettingsForm.show_avatars =
             UserService.Instance.user.show_avatars;
-          this.state.userSettingsForm.matrix_user_id = this.state.user.matrix_user_id;
+          this.state.userSettingsForm.email = UserService.Instance.user.email;
+          this.state.userSettingsForm.bio = UserService.Instance.user.bio;
+          this.state.userSettingsForm.send_notifications_to_email =
+            UserService.Instance.user.send_notifications_to_email;
+          this.state.userSettingsForm.matrix_user_id =
+            UserService.Instance.user.matrix_user_id;
         }
         this.state.loading = false;
         this.setState(this.state);
index df8e3f78997d357de6ff8f4336bc0436817fb4eb..1e8899b9192079d377f095a25d3916e40ea481f4 100644 (file)
@@ -139,7 +139,6 @@ export interface UserView {
   preferred_username?: string;
   avatar?: string;
   banner?: string;
-  email?: string;
   matrix_user_id?: string;
   bio?: string;
   local: boolean;
@@ -149,8 +148,6 @@ export interface UserView {
   number_of_comments: number;
   comment_score: number;
   banned: boolean;
-  show_avatars: boolean;
-  send_notifications_to_email: boolean;
 }
 
 export interface CommunityUser {