From 29832de5f653d4035d1ceb3ad6d9f22efc30e3ed Mon Sep 17 00:00:00 2001 From: Lauryn Menard Date: Wed, 5 Apr 2023 14:46:41 +0200 Subject: [PATCH] narrow: Extract function for calculating huddle recipients. Extracts the code from `by_group_pm_with` that calculates the huddle recipients. Prep commit for implementing new `by_*` method for "dm-including". --- zerver/lib/narrow.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.py index 897f7c75fe..1c382ed026 100644 --- a/zerver/lib/narrow.py +++ b/zerver/lib/narrow.py @@ -12,6 +12,7 @@ from typing import ( Mapping, Optional, Sequence, + Set, Tuple, TypeVar, Union, @@ -585,6 +586,24 @@ class NarrowBuilder: ) return query.where(maybe_negate(cond)) + def _get_huddle_recipients(self, other_user: UserProfile) -> Set[int]: + self_recipient_ids = [ + recipient_tuple["recipient_id"] + for recipient_tuple in Subscription.objects.filter( + user_profile=self.user_profile, + recipient__type=Recipient.HUDDLE, + ).values("recipient_id") + ] + narrow_recipient_ids = [ + recipient_tuple["recipient_id"] + for recipient_tuple in Subscription.objects.filter( + user_profile=other_user, + recipient__type=Recipient.HUDDLE, + ).values("recipient_id") + ] + + return set(self_recipient_ids) & set(narrow_recipient_ids) + def by_group_pm_with( self, query: Select, operand: Union[str, int], maybe_negate: ConditionTransform ) -> Select: @@ -600,22 +619,7 @@ class NarrowBuilder: except UserProfile.DoesNotExist: raise BadNarrowOperatorError("unknown user " + str(operand)) - self_recipient_ids = [ - recipient_tuple["recipient_id"] - for recipient_tuple in Subscription.objects.filter( - user_profile=self.user_profile, - recipient__type=Recipient.HUDDLE, - ).values("recipient_id") - ] - narrow_recipient_ids = [ - recipient_tuple["recipient_id"] - for recipient_tuple in Subscription.objects.filter( - user_profile=narrow_profile, - recipient__type=Recipient.HUDDLE, - ).values("recipient_id") - ] - - recipient_ids = set(self_recipient_ids) & set(narrow_recipient_ids) + recipient_ids = self._get_huddle_recipients(narrow_profile) cond = column("recipient_id", Integer).in_(recipient_ids) return query.where(maybe_negate(cond))