From 6b00c748fd0e2e3da3c75b192f4c2d8c59c4a1af Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Sun, 20 Mar 2022 20:57:46 -0700 Subject: [PATCH] onboarding: Deduplicate realm creation initial user logic. We now call this function inside do_create_user(..., realm_creation=True), which generally improves readability and robustness of the codebase. This fixes a bug where this onboarding content was not correctly done when creating a realm via LDAP, and also will be important as we add new code paths that might let you create a realm. --- zerver/lib/actions.py | 11 +++++++++++ zerver/views/registration.py | 8 -------- zilencer/management/commands/add_new_realm.py | 10 +++------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index c84d962881..6e7d18ae51 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -753,6 +753,17 @@ def do_create_user( default_stream_groups=default_stream_groups, realm_creation=realm_creation, ) + + if realm_creation: + assert realm.signup_notifications_stream is not None + bulk_add_subscriptions( + realm, [realm.signup_notifications_stream], [user_profile], acting_user=None + ) + + from zerver.lib.onboarding import send_initial_realm_messages + + send_initial_realm_messages(realm) + return user_profile diff --git a/zerver/views/registration.py b/zerver/views/registration.py index 10e5a305ff..f913fb1fc1 100644 --- a/zerver/views/registration.py +++ b/zerver/views/registration.py @@ -35,7 +35,6 @@ from zerver.forms import ( RegistrationForm, ) from zerver.lib.actions import ( - bulk_add_subscriptions, do_activate_mirror_dummy_user, do_change_full_name, do_change_password, @@ -46,7 +45,6 @@ from zerver.lib.actions import ( ) from zerver.lib.email_validation import email_allowed_for_realm, validate_email_not_already_in_realm from zerver.lib.exceptions import RateLimited -from zerver.lib.onboarding import send_initial_realm_messages from zerver.lib.pysa import mark_sanitized from zerver.lib.request import REQ, has_request_variables from zerver.lib.send_email import EmailNotDeliveredException, FromAddress, send_email @@ -456,12 +454,6 @@ def accounts_register( ) if realm_creation: - assert realm.signup_notifications_stream is not None - bulk_add_subscriptions( - realm, [realm.signup_notifications_stream], [user_profile], acting_user=None - ) - send_initial_realm_messages(realm) - # Because for realm creation, registration happens on the # root domain, we need to log them into the subdomain for # their new realm. diff --git a/zilencer/management/commands/add_new_realm.py b/zilencer/management/commands/add_new_realm.py index 7288c30967..170939a5df 100644 --- a/zilencer/management/commands/add_new_realm.py +++ b/zilencer/management/commands/add_new_realm.py @@ -1,8 +1,7 @@ from typing import Any -from zerver.lib.actions import bulk_add_subscriptions, do_create_realm, do_create_user +from zerver.lib.actions import do_create_realm, do_create_user from zerver.lib.management import ZulipBaseCommand -from zerver.lib.onboarding import send_initial_realm_messages from zerver.models import Realm, UserProfile @@ -14,15 +13,12 @@ class Command(ZulipBaseCommand): realm = do_create_realm(string_id, string_id) name = "{:02}-user".format(UserProfile.objects.filter(email__contains="user@").count()) - user = do_create_user( + do_create_user( f"{name}@{string_id}.zulip.com", "password", realm, name, role=UserProfile.ROLE_REALM_ADMINISTRATOR, + realm_creation=True, acting_user=None, ) - assert realm.signup_notifications_stream is not None - bulk_add_subscriptions(realm, [realm.signup_notifications_stream], [user], acting_user=None) - - send_initial_realm_messages(realm)