Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

models: Remove get_huddle_recipient and use get_or_create_huddle.

Sahil Batra91a58d026b7a
diff --git a/zerver/lib/recipient_users.py b/zerver/lib/recipient_users.py
index 0802f3ec4d0..1a9542f3bd7 100644
--- a/zerver/lib/recipient_users.py
+++ b/zerver/lib/recipient_users.py
@@ -3,7 +3,7 @@ from typing import Dict, Optional, Sequence
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _
-from zerver.models import Recipient, UserProfile, get_huddle_recipient, is_cross_realm_bot_email
+from zerver.models import Recipient, UserProfile, get_or_create_huddle, is_cross_realm_bot_email
def get_recipient_from_user_profiles(
@@ -45,8 +45,13 @@ def get_recipient_from_user_profiles(
# Otherwise, we need a huddle. Make sure the sender is included in huddle messages
recipient_profiles_map[sender.id] = sender
- user_ids = set(recipient_profiles_map)
- return get_huddle_recipient(user_ids)
+ user_ids = list(recipient_profiles_map)
+ huddle = get_or_create_huddle(user_ids)
+ return Recipient(
+ id=huddle.recipient_id,
+ type=Recipient.HUDDLE,
+ type_id=huddle.id,
+ )
def validate_recipient_user_profiles(
diff --git a/zerver/models.py b/zerver/models.py
index 7bcdce4b1ea..92f4fcfe571 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -2891,16 +2891,6 @@ def bulk_get_streams(realm: Realm, stream_names: Set[str]) -> Dict[str, Any]:
return {stream.name.lower(): stream for stream in streams}
-def get_huddle_recipient(user_profile_ids: Set[int]) -> Recipient:
- # The caller should ensure that user_profile_ids includes
- # the sender. Note that get_huddle hits the cache, and then
- # we hit another cache to get the recipient. We may want to
- # unify our caching strategy here.
- huddle = get_or_create_huddle(list(user_profile_ids))
- assert huddle.recipient is not None
- return huddle.recipient
-
-
def get_huddle_user_ids(recipient: Recipient) -> ValuesQuerySet["Subscription", int]:
assert recipient.type == Recipient.HUDDLE
@@ -4074,9 +4064,7 @@ def get_or_create_huddle(id_list: List[int]) -> Huddle:
)
def get_or_create_huddle_backend(huddle_hash: str, id_list: List[int]) -> Huddle:
with transaction.atomic():
- (huddle, created) = Huddle.objects.select_related("recipient").get_or_create(
- huddle_hash=huddle_hash
- )
+ (huddle, created) = Huddle.objects.get_or_create(huddle_hash=huddle_hash)
if created:
recipient = Recipient.objects.create(type_id=huddle.id, type=Recipient.HUDDLE)
huddle.recipient = recipient
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py
index f58970013eb..18e6ad2ccab 100644
--- a/zerver/tests/test_message_send.py
+++ b/zerver/tests/test_message_send.py
@@ -56,7 +56,7 @@ from zerver.models import (
UserMessage,
UserProfile,
flush_per_request_caches,
- get_huddle_recipient,
+ get_or_create_huddle,
get_realm,
get_stream,
get_system_bot,
@@ -660,16 +660,14 @@ class MessagePOSTTest(ZulipTestCase):
msg = self.get_last_message()
self.assertEqual("Test message", msg.content)
- self.assertEqual(
- msg.recipient_id,
- get_huddle_recipient(
- {
- self.example_user("hamlet").id,
- self.example_user("othello").id,
- self.example_user("cordelia").id,
- }
- ).id,
+ huddle = get_or_create_huddle(
+ [
+ self.example_user("hamlet").id,
+ self.example_user("othello").id,
+ self.example_user("cordelia").id,
+ ]
)
+ self.assertEqual(msg.recipient_id, huddle.recipient_id)
def test_personal_message_copying_self(self) -> None:
"""