diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 35d4f7661a..39b2b5ab94 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -837,7 +837,7 @@ def do_change_user_delivery_email(user_profile: UserProfile, new_email: str) -> delete_user_profile_caches([user_profile]) user_profile.delivery_email = new_email - if user_profile.realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: + if user_profile.email_address_is_realm_public(): user_profile.email = new_email user_profile.save(update_fields=["email", "delivery_email"]) else: @@ -850,7 +850,7 @@ def do_change_user_delivery_email(user_profile: UserProfile, new_email: str) -> event = dict(type='realm_user', op='update', person=payload) send_event(user_profile.realm, event, [user_profile.id]) - if user_profile.realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: + if user_profile.email_address_is_realm_public(): # Additionally, if we're also changing the publicly visible # email, we send a new_email event as well. send_user_email_update_event(user_profile) diff --git a/zerver/lib/create_user.py b/zerver/lib/create_user.py index a7c4721b7a..62424408b5 100644 --- a/zerver/lib/create_user.py +++ b/zerver/lib/create_user.py @@ -32,7 +32,7 @@ def copy_user_settings(source_profile: UserProfile, target_profile: UserProfile) copy_hotpots(source_profile, target_profile) def get_display_email_address(user_profile: UserProfile, realm: Realm) -> str: - if realm.email_address_visibility != Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: + if not user_profile.email_address_is_realm_public(): return "user%s@%s" % (user_profile.id, get_fake_email_domain()) return user_profile.delivery_email @@ -68,7 +68,7 @@ def create_user_profile(realm: Realm, email: str, password: Optional[str], if bot_type or not active: password = None - if realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: + if user_profile.email_address_is_realm_public(): # If emails are visible to everyone, we can set this here and save a DB query user_profile.email = get_display_email_address(user_profile, realm) user_profile.set_password(password) @@ -113,7 +113,7 @@ def create_user(email: str, password: Optional[str], realm: Realm, else: user_profile.save() - if realm.email_address_visibility != Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: + if not user_profile.email_address_is_realm_public(): # With restricted access to email addresses, we can't generate # the fake email addresses we use for display purposes without # a User ID, which isn't generated until the .save() above. diff --git a/zerver/models.py b/zerver/models.py index aea5cc1dbe..92aeef1d44 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -1039,6 +1039,11 @@ class UserProfile(AbstractBaseUser, PermissionsMixin): rows = UserProfile.objects.filter(id__in=user_ids).values('id', 'email') return {row['id']: row['email'] for row in rows} + def email_address_is_realm_public(self) -> bool: + if self.realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: + return True + return False + def can_create_streams(self) -> bool: if self.is_realm_admin: return True