Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
populate_db: Remove Recipient.PERSONAL naming vestiges.
Alex Vandiver64633bdbb86b
diff --git a/zerver/lib/bulk_create.py b/zerver/lib/bulk_create.pyindex 7c91c3387c1..6a9991824f5 100644--- a/zerver/lib/bulk_create.py+++ b/zerver/lib/bulk_create.py@@ -1,7 +1,7 @@from collections.abc import Collection, Iterablefrom typing import Any-from django.db.models import Model, QuerySet+from django.db.models import QuerySetfrom django.utils.timezone import now as timezone_nowfrom zerver.lib.avatar import generate_and_upload_jdenticon_avatar@@ -144,37 +144,24 @@ def bulk_create_users()-def bulk_set_users_or_streams_recipient_fields(- model: type[Model],- objects: Collection[UserProfile]- | QuerySet[UserProfile]- | Collection[Stream]- | QuerySet[Stream],+def bulk_set_stream_recipient_fields(+ streams: Collection[Stream] | QuerySet[Stream],recipients: Iterable[Recipient] | None = None,) -> None:- assert model in [UserProfile, Stream]- for obj in objects:- assert isinstance(obj, model)-- if model == UserProfile:- recipient_type = Recipient.PERSONAL- elif model == Stream:- recipient_type = Recipient.STREAM-if recipients is None:- object_ids = [obj.id for obj in objects]- recipients = Recipient.objects.filter(type=recipient_type, type_id__in=object_ids)+ stream_ids = [stream.id for stream in streams]+ recipients = Recipient.objects.filter(type=Recipient.STREAM, type_id__in=stream_ids)- objects_dict = {obj.id: obj for obj in objects}+ streams_dict = {stream.id: stream for stream in streams}- objects_to_update = set()+ streams_to_update = set()for recipient in recipients:- assert recipient.type == recipient_type- result = objects_dict.get(recipient.type_id)- if result is not None:- result.recipient = recipient- objects_to_update.add(result)- model._default_manager.bulk_update(objects_to_update, ["recipient"])+ assert recipient.type == Recipient.STREAM+ stream = streams_dict.get(recipient.type_id)+ if stream is not None:+ stream.recipient = recipient+ streams_to_update.add(stream)+ Stream.objects.bulk_update(streams_to_update, ["recipient"])# This is only sed in populate_db, so doesn't really need tests@@ -221,7 +208,7 @@ def bulk_create_streams(realm: Realm, stream_dict: dict[str, dict[str, Any]]) ->]Recipient.objects.bulk_create(recipients_to_create)- bulk_set_users_or_streams_recipient_fields(Stream, streams_to_create, recipients_to_create)+ bulk_set_stream_recipient_fields(streams_to_create, recipients_to_create)def create_users(diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.pyindex 76e374f118f..3c6ba6288e0 100644--- a/zerver/lib/import_realm.py+++ b/zerver/lib/import_realm.py@@ -32,7 +32,7 @@ from zerver.actions.realm_settings import (from zerver.actions.user_settings import set_avatar_to_defaultfrom zerver.lib.avatar import generate_and_upload_jdenticon_avatarfrom zerver.lib.avatar_hash import user_avatar_base_path_from_ids-from zerver.lib.bulk_create import bulk_set_users_or_streams_recipient_fields+from zerver.lib.bulk_create import bulk_set_stream_recipient_fieldsfrom zerver.lib.export import Field, Path, Record, TableName, date_fields_for_tablefrom zerver.lib.markdown import markdown_convertfrom zerver.lib.markdown import version as markdown_version@@ -1686,7 +1686,7 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea)update_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_stream_recipient_fields(Stream.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/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.pyindex 6e10bea2a7b..acf25f50dae 100644--- a/zilencer/management/commands/populate_db.py+++ b/zilencer/management/commands/populate_db.py@@ -1315,7 +1315,14 @@ def generate_and_send_messages(message_batch_size = options["batch_size"]num_messages = 0random_max = 1000000- recipients: dict[int, tuple[int, int, dict[str, Any]]] = {}+ # Local message type discriminators for populate_db's random+ # message generation. These are NOT Recipient.type values — all+ # DMs use Recipient.DIRECT_MESSAGE_GROUP in the database.+ MSG_TYPE_GROUP_DM = "group_dm"+ MSG_TYPE_1_TO_1_DM = "1_to_1_dm"+ MSG_TYPE_STREAM = "stream"++ recipients: dict[int, tuple[str, int, dict[str, Any]]] = {}messages: list[Message] = []while num_messages < tot_messages:saved_data: dict[str, Any] = {}@@ -1330,17 +1337,17 @@ def generate_and_send_messages(and random.randint(1, random_max) * 100.0 / random_max < options["stickiness"]):# Use an old recipient- recipient_type, recipient_id, saved_data = recipients[num_messages - 1]- if recipient_type == Recipient.PERSONAL:+ msg_type, recipient_id, saved_data = recipients[num_messages - 1]+ if msg_type == MSG_TYPE_1_TO_1_DM:personals_pair = list(saved_data["personals_pair"])random.shuffle(personals_pair)- elif recipient_type == Recipient.STREAM:+ elif msg_type == MSG_TYPE_STREAM:message.subject = saved_data["subject"]message.recipient = get_recipient_by_id(recipient_id)- elif recipient_type == Recipient.DIRECT_MESSAGE_GROUP:+ elif msg_type == MSG_TYPE_GROUP_DM:message.recipient = get_recipient_by_id(recipient_id)elif randkey <= random_max * options["percent_direct_message_groups"] / 100.0:- recipient_type = Recipient.DIRECT_MESSAGE_GROUP+ msg_type = MSG_TYPE_GROUP_DMmessage.recipient = get_recipient_by_id(random.choice(recipient_direct_message_groups))elif (randkey@@ -1348,25 +1355,25 @@ def generate_and_send_messages(* (options["percent_direct_message_groups"] + options["percent_personals"])/ 100.0):- recipient_type = Recipient.PERSONAL+ msg_type = MSG_TYPE_1_TO_1_DMpersonals_pair = list(random.choice(personals_pairs))random.shuffle(personals_pair)elif randkey <= random_max * 1.0:- recipient_type = Recipient.STREAM+ msg_type = MSG_TYPE_STREAMmessage.recipient = get_recipient_by_id(random.choice(recipient_streams))- if recipient_type == Recipient.DIRECT_MESSAGE_GROUP:+ if msg_type == MSG_TYPE_GROUP_DM:sender_id = random.choice(direct_message_group_members[message.recipient.id])message.sender = get_user_profile_by_id(sender_id)message.subject = Message.DM_TOPIC- elif recipient_type == Recipient.PERSONAL:+ elif msg_type == MSG_TYPE_1_TO_1_DM:direct_message_group = get_or_create_direct_message_group(personals_pair)assert direct_message_group.recipient is not Nonemessage.recipient = direct_message_group.recipientmessage.sender = get_user_profile_by_id(personals_pair[1])message.subject = Message.DM_TOPICsaved_data["personals_pair"] = personals_pair- elif recipient_type == Recipient.STREAM:+ elif msg_type == MSG_TYPE_STREAM:# Pick a random subscriber to the streammessage.sender = random.choice(list(Subscription.objects.filter(recipient=message.recipient))@@ -1374,13 +1381,13 @@ def generate_and_send_messages(message.subject = random.choice(possible_topic_names[message.recipient.id])saved_data["subject"] = message.subject- message.is_channel_message = recipient_type == Recipient.STREAM+ message.is_channel_message = msg_type == MSG_TYPE_STREAMmessage.date_sent = choose_date_sent(num_messages, tot_messages, options["oldest_message_days"], options["threads"])messages.append(message)- recipients[num_messages] = (recipient_type, message.recipient.id, saved_data)+ recipients[num_messages] = (msg_type, message.recipient.id, saved_data)num_messages += 1if (num_messages % message_batch_size) == 0: