From: Dessalines Date: Sun, 9 Aug 2020 02:36:29 +0000 (-0400) Subject: Add email overwrite on user settings save. Fixes #1069 X-Git-Url: http://these/git/%22https:/image.com/%7BcommunityRSSUrl%28?a=commitdiff_plain;h=221db1bd1b4d2058d142b8e25ba10a3d16b5d3e4;p=lemmy.git Add email overwrite on user settings save. Fixes #1069 - Also add get_user_secure to other locations. --- diff --git a/server/lemmy_db/src/user.rs b/server/lemmy_db/src/user.rs index d30609bc..73da1c24 100644 --- a/server/lemmy_db/src/user.rs +++ b/server/lemmy_db/src/user.rs @@ -46,7 +46,7 @@ pub struct UserForm { pub password_encrypted: String, pub admin: bool, pub banned: bool, - pub email: Option, + pub email: Option>, pub avatar: Option>, pub updated: Option, pub show_nsfw: bool, diff --git a/server/src/api/community.rs b/server/src/api/community.rs index ce2851b1..c7160848 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -710,7 +710,7 @@ impl Perform for Oper { 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, diff --git a/server/src/api/user.rs b/server/src/api/user.rs index f5ab84c5..2d589517 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -395,7 +395,7 @@ impl Perform for Oper { // 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 { 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 { 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 { } }; - 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 { }) .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, diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index b59e9e32..9bb058cd 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -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 } diff --git a/server/src/code_migrations.rs b/server/src/code_migrations.rs index 2e70040c..7f45237c 100644 --- a/server/src/code_migrations.rs +++ b/server/src/code_migrations.rs @@ -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()), diff --git a/ui/src/components/user.tsx b/ui/src/components/user.tsx index ad77e267..e14f56a1 100644 --- a/ui/src/components/user.tsx +++ b/ui/src/components/user.tsx @@ -90,8 +90,6 @@ export class User extends Component { 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 { creator_preferred_username: undefined, }, version: undefined, + my_user: undefined, }, }; @@ -724,7 +723,7 @@ export class User extends Component { 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 { 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 { 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); diff --git a/ui/src/interfaces.ts b/ui/src/interfaces.ts index df8e3f78..1e8899b9 100644 --- a/ui/src/interfaces.ts +++ b/ui/src/interfaces.ts @@ -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 {