Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
narrow: Update check to prevent conflicting dm and channel narrow.
Lauryn Menard692d5e970687
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.pyindex 0467d8e8c18..8b73b3027cd 100644--- a/zerver/lib/narrow.py+++ b/zerver/lib/narrow.py@@ -289,18 +289,20 @@ class NarrowBuilder:"pm_with": self.by_dm,"group_pm_with": self.by_group_pm_with,}- self.is_stream_narrow = False+ self.is_channel_narrow = Falseself.is_dm_narrow = False- def check_not_both_stream_dm(- self, is_dm_narrow: bool = False, is_stream_narrow: bool = False+ def check_not_both_channel_and_dm_narrow(+ self, is_dm_narrow: bool = False, is_channel_narrow: bool = False) -> None:if is_dm_narrow:self.is_dm_narrow = True- if is_stream_narrow:- self.is_stream_narrow = True- if self.is_stream_narrow and self.is_dm_narrow:- raise BadNarrowOperatorError("No message can be both a stream and a DM message")+ if is_channel_narrow:+ self.is_channel_narrow = True+ if self.is_channel_narrow and self.is_dm_narrow:+ raise BadNarrowOperatorError(+ "No message can be both a channel message and direct message"+ )def add_term(self, query: Select, term: Dict[str, Any]) -> Select:"""@@ -375,7 +377,7 @@ class NarrowBuilder:if operand in ["dm", "private"]:# "is:private" is a legacy alias for "is:dm"- self.check_not_both_stream_dm(is_dm_narrow=True)+ self.check_not_both_channel_and_dm_narrow(is_dm_narrow=True)cond = column("flags", Integer).op("&")(UserMessage.flags.is_private.mask) != 0return query.where(maybe_negate(cond))elif operand == "starred":@@ -425,7 +427,7 @@ class NarrowBuilder:def by_stream(self, query: Select, operand: Union[str, int], maybe_negate: ConditionTransform) -> Select:- self.check_not_both_stream_dm(is_stream_narrow=True)+ self.check_not_both_channel_and_dm_narrow(is_channel_narrow=True)try:# Because you can see your own message history for@@ -468,7 +470,7 @@ class NarrowBuilder:return query.where(maybe_negate(cond))def by_streams(self, query: Select, operand: str, maybe_negate: ConditionTransform) -> Select:- self.check_not_both_stream_dm(is_stream_narrow=True)+ self.check_not_both_channel_and_dm_narrow(is_channel_narrow=True)if operand == "public":# Get all both subscribed and non-subscribed public streams@@ -484,7 +486,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_stream_dm(is_stream_narrow=True)+ self.check_not_both_channel_and_dm_narrow(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)*$/@@ -562,7 +564,7 @@ class NarrowBuilder:assert not self.is_web_public_queryassert self.user_profile is not None- self.check_not_both_stream_dm(is_dm_narrow=True)+ self.check_not_both_channel_and_dm_narrow(is_dm_narrow=True)try:if isinstance(operand, str):@@ -663,7 +665,7 @@ class NarrowBuilder:assert not self.is_web_public_queryassert self.user_profile is not None- self.check_not_both_stream_dm(is_dm_narrow=True)+ self.check_not_both_channel_and_dm_narrow(is_dm_narrow=True)try:if isinstance(operand, str):@@ -714,7 +716,7 @@ class NarrowBuilder:assert not self.is_web_public_queryassert self.user_profile is not None- self.check_not_both_stream_dm(is_dm_narrow=True)+ self.check_not_both_channel_and_dm_narrow(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 2d8666bbb25..e298934b070 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -311,14 +311,21 @@ class NarrowBuilderTest(ZulipTestCase):)def test_combined_channel_dm(self) -> None:+ expected_error_message = (+ "Invalid narrow operator: No message can be both a channel message and direct message"+ )term1 = dict(operator="dm", operand=self.othello_email)self._build_query(term1)topic_term = dict(operator="topic", operand="bogus")- self.assertRaises(BadNarrowOperatorError, self._build_query, topic_term)-- channel_term = dict(operator="streams", operand="public")- self.assertRaises(BadNarrowOperatorError, self._build_query, channel_term)+ with self.assertRaises(BadNarrowOperatorError) as error:+ self._build_query(topic_term)+ self.assertEqual(expected_error_message, str(error.exception))++ channels_term = dict(operator="streams", operand="public")+ with self.assertRaises(BadNarrowOperatorError) as error:+ self._build_query(channels_term)+ self.assertEqual(expected_error_message, str(error.exception))def test_add_term_using_dm_operator_not_the_same_user_as_operand_and_negated(self,