Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
create_user: Stop creating personal recipients.
Alex Vandivereb24253193b1
diff --git a/zerver/lib/bulk_create.py b/zerver/lib/bulk_create.pyindex 2484ebd9906..7c91c3387c1 100644--- a/zerver/lib/bulk_create.py+++ b/zerver/lib/bulk_create.py@@ -18,7 +18,6 @@ from zerver.models import (RealmUserDefault,Recipient,Stream,- Subscription,UserGroupMembership,UserProfile,)@@ -101,8 +100,6 @@ def bulk_create_users(for user_profile in profiles_to_create:generate_and_upload_jdenticon_avatar(user_profile, str(realm.uuid), future=False)- user_ids = {user.id for user in profiles_to_create}-RealmAuditLog.objects.bulk_create(RealmAuditLog(realm=realm,@@ -113,31 +110,6 @@ def bulk_create_users(for profile_ in profiles_to_create)- recipients_to_create = [- Recipient(type_id=user_id, type=Recipient.PERSONAL) for user_id in user_ids- ]-- Recipient.objects.bulk_create(recipients_to_create)-- bulk_set_users_or_streams_recipient_fields(- UserProfile, profiles_to_create, recipients_to_create- )-- recipients_by_user_id: dict[int, Recipient] = {}- for recipient in recipients_to_create:- recipients_by_user_id[recipient.type_id] = recipient-- subscriptions_to_create = [- Subscription(- user_profile_id=user_profile.id,- recipient=recipients_by_user_id[user_profile.id],- is_user_active=user_profile.is_active,- )- for user_profile in profiles_to_create- ]-- Subscription.objects.bulk_create(subscriptions_to_create)-full_members_system_group = NamedUserGroup.objects.get(name=SystemGroups.FULL_MEMBERS, realm_for_sharding=realm, is_system_group=True)diff --git a/zerver/lib/create_user.py b/zerver/lib/create_user.pyindex b7e59639801..3e045203824 100644--- a/zerver/lib/create_user.py+++ b/zerver/lib/create_user.py@@ -9,15 +9,7 @@ from zerver.lib.i18n import get_default_language_for_new_userfrom zerver.lib.onboarding_steps import copy_onboarding_stepsfrom zerver.lib.timezone import canonicalize_timezonefrom zerver.lib.upload import copy_avatar-from zerver.models import (- Realm,- RealmUserDefault,- Recipient,- Stream,- Subscription,- UserBaseSettings,- UserProfile,-)+from zerver.models import Realm, RealmUserDefault, Stream, UserBaseSettings, UserProfilefrom zerver.models.realms import get_fake_email_domain@@ -249,11 +241,4 @@ def create_user(user_profile.email = get_display_email_address(user_profile)user_profile.save(update_fields=["email"])- recipient = Recipient.objects.create(type_id=user_profile.id, type=Recipient.PERSONAL)- user_profile.recipient = recipient- user_profile.save(update_fields=["recipient"])-- Subscription.objects.create(- user_profile=user_profile, recipient=recipient, is_user_active=user_profile.is_active- )return user_profilediff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.pyindex bc6b8ec5281..dcbb774d951 100644--- a/zerver/lib/import_realm.py+++ b/zerver/lib/import_realm.py@@ -1690,7 +1690,6 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Reaupdate_model_ids(Recipient, data, "recipient")bulk_import_model(data, Recipient)bulk_set_users_or_streams_recipient_fields(Stream, Stream.objects.filter(realm=realm))- bulk_set_users_or_streams_recipient_fields(UserProfile, UserProfile.objects.filter(realm=realm))re_map_foreign_keys(data, "zerver_subscription", "user_profile", related_table="user_profile")get_direct_message_groups_from_subscription(data, "zerver_subscription")diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex 078cc961bb6..6d2cac64ecb 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -70,7 +70,7 @@ from zerver.lib.test_helpers import (from zerver.lib.thumbnail import BadImageErrorfrom zerver.lib.topic import DB_TOPIC_NAMEfrom zerver.lib.upload import claim_attachment, upload_avatar_image, upload_message_attachment-from zerver.lib.utils import assert_is_not_none, get_fk_field_name+from zerver.lib.utils import get_fk_field_namefrom zerver.models import (AlertWord,Attachment,@@ -653,12 +653,6 @@ class RealmImportExportTest(ExportFile):self.assertIn(pm_c_msg_id, exported_message_ids)self.assertIn(pm_d_msg_id, exported_message_ids)- personal_recipient_type_ids = (- r["type_id"] for r in realm_data["zerver_recipient"] if r["type"] == Recipient.PERSONAL- )- for user_profile_id in [cordelia.id, hamlet.id, iago.id, othello.id, polonius.id]:- self.assertIn(user_profile_id, personal_recipient_type_ids)-def test_get_consented_user_ids(self) -> None:realm = get_realm("zulip")consented_user = self.example_user("iago")@@ -1021,7 +1015,7 @@ class RealmImportExportTest(ExportFile):self.assertEqual(get_consented_user_ids(realm), consented_user_ids)- # Remove the recipient of the welcome bot to test exporting bots without personal recipients.+ # Remove the recipient of the welcome bot to test exporting bots without it.internal_realm = get_realm(settings.SYSTEM_BOT_REALM)welcome_bot = get_system_bot(settings.WELCOME_BOT, internal_realm.id)welcome_bot.recipient = None@@ -1799,12 +1793,7 @@ class RealmImportExportTest(ExportFile):imported_denmark_stream = Stream.objects.get(name="Denmark", realm=imported_realm)self.assertEqual(imported_denmark_stream.creator, imported_hamlet_user)- # Check recipient_id was generated correctly for the imported users and streams.- for user_profile in UserProfile.objects.filter(realm=imported_realm):- self.assertEqual(- user_profile.recipient_id,- Recipient.objects.get(type=Recipient.PERSONAL, type_id=user_profile.id).id,- )+ # Check recipient_id was generated correctly for the imported streams.for stream in Stream.objects.filter(realm=imported_realm):self.assertEqual(stream.recipient_id,@@ -1949,7 +1938,7 @@ class RealmImportExportTest(ExportFile):)imported_prospero_user = get_user_by_delivery_email(prospero_email, imported_realm)- self.assertIsNotNone(imported_prospero_user.recipient)+ self.assertIsNone(imported_prospero_user.recipient)# Ensure RealmExport.export_path is excluded from the export and thus None after importing.exported_realm_exports = read_json("realm.json")["zerver_realmexport"]@@ -2044,17 +2033,10 @@ class RealmImportExportTest(ExportFile):assert recipient is not Nonereturn recipient- def get_recipient_user(r: Realm) -> Recipient:- return assert_is_not_none(UserProfile.objects.get(full_name="Iago", realm=r).recipient)-@getterdef get_stream_recipient_type(r: Realm) -> int:return get_recipient_stream(r).type- @getter- def get_user_recipient_type(r: Realm) -> int:- return get_recipient_user(r).type-# test subscriptiondef get_subscribers(recipient: Recipient) -> set[str]:subscriptions = Subscription.objects.filter(recipient=recipient)@@ -2065,10 +2047,6 @@ class RealmImportExportTest(ExportFile):def get_stream_subscribers(r: Realm) -> set[str]:return get_subscribers(get_recipient_stream(r))- @getter- def get_user_subscribers(r: Realm) -> set[str]:- return get_subscribers(get_recipient_user(r))-# test custom profile fields@getterdef get_custom_profile_field_names(r: Realm) -> set[str]:diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex d8750c4cd99..847aefea5d3 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -1068,7 +1068,7 @@ class LoginTest(ZulipTestCase):# to sending messages, such as getting the welcome bot, looking up# the alert words for a realm, etc.with (- self.assert_database_query_count(104),+ self.assert_database_query_count(101),self.assert_memcached_count(19),self.captureOnCommitCallbacks(execute=True),):diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 42412b3acf1..018b15a2643 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -645,11 +645,9 @@ class PermissionTest(ZulipTestCase):for_admin=False,)- def test_access_user_by_id_when_personal_recipient_is_none(self) -> None:+ def test_access_user_by_id_with_null_recipient(self) -> None:self.set_up_db_for_testing_user_access()polonius = self.example_user("polonius")-- # Removing the personal recipient to ensure a new direct message group is used for 1:1 dms.polonius.recipient = Nonepolonius.save()@@ -1151,7 +1149,7 @@ class QueryCountTest(ZulipTestCase):prereg_user = PreregistrationUser.objects.get(email="[email protected]")with (- self.assert_database_query_count(102),+ self.assert_database_query_count(99),self.assert_memcached_count(24),self.capture_send_event_calls(expected_num_events=11) as events,):@@ -1474,11 +1472,7 @@ class AdminCreateUserTest(ZulipTestCase):# Make sure the new user got the realm's default languageself.assertEqual(new_user.default_language, "ja")- # Make sure the recipient field is set correctly.- self.assertEqual(- new_user.recipient,- Recipient.objects.get(type=Recipient.PERSONAL, type_id=new_user.id),- )+ self.assertIsNone(new_user.recipient)# we can't create the same user twice.result = self.client_post("/json/users", valid_params)@@ -3763,10 +3757,8 @@ class GetProfileTest(ZulipTestCase):)self.assertEqual(inaccessible_user_ids, {othello.id})- def test_get_inaccessible_user_ids_when_personal_recipient_is_none(self) -> None:+ def test_get_inaccessible_user_ids_with_null_recipient(self) -> None:polonius = self.example_user("polonius")-- # Removing the personal recipient to ensure we use direct message group.polonius.recipient = Nonepolonius.save()