Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
huddles: Find huddle user ids more efficiently.
Steve Howell03557a55689b
diff --git a/zerver/lib/display_recipient.py b/zerver/lib/display_recipient.pyindex c1f9792d5df..d9b5f2da563 100644--- a/zerver/lib/display_recipient.py+++ b/zerver/lib/display_recipient.py@@ -147,7 +147,7 @@ def bulk_fetch_display_recipients(# Find all user ids whose UserProfiles we will need to fetch:user_ids_to_fetch: Set[int] = set()- huddle_user_ids: Dict[int, List[int]] = {}+ huddle_user_ids: Dict[int, Set[int]] = {}huddle_user_ids = bulk_get_huddle_user_ids([recipient for recipient in recipients if recipient.type == Recipient.HUDDLE])diff --git a/zerver/models.py b/zerver/models.pyindex e3202812c7f..e33bb5a14ac 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -3,6 +3,7 @@ import datetimeimport hashlibimport secretsimport time+from collections import defaultdictfrom contextlib import suppressfrom datetime import timedeltafrom email.headerregistry import Address@@ -2898,7 +2899,7 @@ def get_huddle_user_ids(recipient: Recipient) -> ValuesQuerySet["Subscription",)-def bulk_get_huddle_user_ids(recipients: List[Recipient]) -> Dict[int, List[int]]:+def bulk_get_huddle_user_ids(recipients: List[Recipient]) -> Dict[int, Set[int]]:"""Takes a list of huddle-type recipients, returns a dictmapping recipient id to list of user ids in the huddle.@@ -2909,15 +2910,11 @@ def bulk_get_huddle_user_ids(recipients: List[Recipient]) -> Dict[int, List[int]subscriptions = Subscription.objects.filter(recipient__in=recipients,- ).order_by("user_profile_id")-- result_dict: Dict[int, List[int]] = {}- for recipient in recipients:- result_dict[recipient.id] = [- subscription.user_profile_id- for subscription in subscriptions- if subscription.recipient_id == recipient.id- ]+ ).only("user_profile_id", "recipient_id")++ result_dict: Dict[int, Set[int]] = defaultdict(set)+ for subscription in subscriptions:+ result_dict[subscription.recipient_id].add(subscription.user_profile_id)return result_dictdiff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.pyindex 3fb3ece1f1c..bcbcdf0ea3b 100644--- a/zerver/tests/test_messages.py+++ b/zerver/tests/test_messages.py@@ -90,9 +90,9 @@ class TestBulkGetHuddleUserIds(ZulipTestCase):messages = Message.objects.filter(id__in=message_ids).order_by("id")first_huddle_recipient = messages[0].recipient- first_huddle_user_ids = list(get_huddle_user_ids(first_huddle_recipient))+ first_huddle_user_ids = set(get_huddle_user_ids(first_huddle_recipient))second_huddle_recipient = messages[1].recipient- second_huddle_user_ids = list(get_huddle_user_ids(second_huddle_recipient))+ second_huddle_user_ids = set(get_huddle_user_ids(second_huddle_recipient))huddle_user_ids = bulk_get_huddle_user_ids([first_huddle_recipient, second_huddle_recipient]