Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
narrow: Handle use of -is:dm with channels:public.
Abhay Upadhyayf7ac7b885aa4
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.pyindex 66f74536266..dad1cd78ff9 100644--- a/zerver/lib/narrow.py+++ b/zerver/lib/narrow.py@@ -302,8 +302,13 @@ class NarrowBuilder:self.is_dm_narrow = Falsedef check_not_both_channel_and_dm_narrow(- self, is_dm_narrow: bool = False, is_channel_narrow: bool = False+ self,+ maybe_negate: ConditionTransform,+ is_dm_narrow: bool = False,+ is_channel_narrow: bool = False,) -> None:+ if maybe_negate is not_:+ returnif is_dm_narrow:self.is_dm_narrow = Trueif is_channel_narrow:@@ -390,7 +395,12 @@ class NarrowBuilder:if operand in ["dm", "private"]:# "is:private" is a legacy alias for "is:dm"- self.check_not_both_channel_and_dm_narrow(is_dm_narrow=True)+ if maybe_negate is not_:+ self.check_not_both_channel_and_dm_narrow(+ maybe_negate=lambda cond: cond, is_channel_narrow=True+ )+ else:+ self.check_not_both_channel_and_dm_narrow(maybe_negate, is_dm_narrow=True)cond = column("flags", Integer).op("&")(UserMessage.flags.is_private.mask) != 0return query.where(maybe_negate(cond))elif operand == "starred":@@ -443,7 +453,7 @@ class NarrowBuilder:def by_channel(self, query: Select, operand: str | int, maybe_negate: ConditionTransform) -> Select:- self.check_not_both_channel_and_dm_narrow(is_channel_narrow=True)+ self.check_not_both_channel_and_dm_narrow(maybe_negate, is_channel_narrow=True)try:# Because you can see your own message history for@@ -488,7 +498,7 @@ class NarrowBuilder:return query.where(maybe_negate(cond))def by_channels(self, query: Select, operand: str, maybe_negate: ConditionTransform) -> Select:- self.check_not_both_channel_and_dm_narrow(is_channel_narrow=True)+ self.check_not_both_channel_and_dm_narrow(maybe_negate, is_channel_narrow=True)if operand == "public":# Get all both subscribed and non-subscribed public channels@@ -504,7 +514,7 @@ class NarrowBuilder:return query.where(maybe_negate(cond))def by_topic(self, query: Select, operand: str, maybe_negate: ConditionTransform) -> Select:- self.check_not_both_channel_and_dm_narrow(is_channel_narrow=True)+ self.check_not_both_channel_and_dm_narrow(maybe_negate, is_channel_narrow=True)if self.realm.is_zephyr_mirror_realm:# MIT users expect narrowing to topic "foo" to also show messages to /^foo(.d)*$/@@ -580,7 +590,7 @@ class NarrowBuilder:assert not self.is_web_public_queryassert self.user_profile is not None- self.check_not_both_channel_and_dm_narrow(is_dm_narrow=True)+ self.check_not_both_channel_and_dm_narrow(maybe_negate, is_dm_narrow=True)try:if isinstance(operand, str):@@ -688,7 +698,7 @@ class NarrowBuilder:assert not self.is_web_public_queryassert self.user_profile is not None- self.check_not_both_channel_and_dm_narrow(is_dm_narrow=True)+ self.check_not_both_channel_and_dm_narrow(maybe_negate, is_dm_narrow=True)try:if isinstance(operand, str):@@ -741,7 +751,7 @@ class NarrowBuilder:assert not self.is_web_public_queryassert self.user_profile is not None- self.check_not_both_channel_and_dm_narrow(is_dm_narrow=True)+ self.check_not_both_channel_and_dm_narrow(maybe_negate, is_dm_narrow=True)try:if isinstance(operand, str):diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex 1ceb33208e3..a9516b7d760 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -334,6 +334,18 @@ class NarrowBuilderTest(ZulipTestCase):"WHERE (flags & %(flags_1)s) != %(param_1)s AND realm_id = %(realm_id_1)s AND (sender_id = %(sender_id_1)s AND recipient_id = %(recipient_id_1)s OR sender_id = %(sender_id_2)s AND recipient_id = %(recipient_id_2)s)",)+ def test_negated_is_dm_with_dm_operator(self) -> None:+ expected_error_message = (+ "Invalid narrow operator: No message can be both a channel message and direct message"+ )+ is_term = NarrowParameter(operator="is", operand="dm", negated=True)+ self._build_query(is_term)++ topic_term = NarrowParameter(operator="dm", operand=self.othello_email)+ with self.assertRaises(BadNarrowOperatorError) as error:+ self._build_query(topic_term)+ self.assertEqual(expected_error_message, str(error.exception))+def test_combined_channel_dm(self) -> None:expected_error_message = ("Invalid narrow operator: No message can be both a channel message and direct message"@@ -351,6 +363,20 @@ class NarrowBuilderTest(ZulipTestCase):self._build_query(channels_term)self.assertEqual(expected_error_message, str(error.exception))+ def test_combined_channel_with_negated_is_dm(self) -> None:+ dm_term = NarrowParameter(operator="is", operand="dm", negated=True)+ self._build_query(dm_term)++ channel_term = NarrowParameter(operator="channels", operand="public")+ self._build_query(channel_term)++ def test_combined_negated_channel_with_is_dm(self) -> None:+ dm_term = NarrowParameter(operator="is", operand="dm")+ self._build_query(dm_term)++ channel_term = NarrowParameter(operator="channels", operand="public", negated=True)+ self._build_query(channel_term)+def test_add_term_using_dm_operator_not_the_same_user_as_operand_and_negated(self,) -> None: # NEGATED