Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
narrow_term: Refactor a `NeverNegatedNarrowTerm`.
PieterCKc861bdd494db
diff --git a/zerver/lib/events.py b/zerver/lib/events.pyindex f4981ea9b12..c83b2b5c078 100644--- a/zerver/lib/events.py+++ b/zerver/lib/events.py@@ -40,7 +40,7 @@ from zerver.lib.message import (remove_message_id_from_unread_mgs,)from zerver.lib.muted_users import get_user_mutes-from zerver.lib.narrow_helpers import NarrowTerm, read_stop_words+from zerver.lib.narrow_helpers import NeverNegatedNarrowTerm, read_stop_wordsfrom zerver.lib.narrow_predicate import check_narrow_for_eventsfrom zerver.lib.onboarding_steps import get_next_onboarding_stepsfrom zerver.lib.presence import get_presence_for_user, get_presences_for_realm@@ -1787,7 +1787,7 @@ def do_events_register(include_subscribers: bool = True,include_streams: bool = True,client_capabilities: ClientCapabilities = DEFAULT_CLIENT_CAPABILITIES,- narrow: Collection[NarrowTerm] = [],+ narrow: Collection[NeverNegatedNarrowTerm] = [],fetch_event_types: Collection[str] | None = None,spectator_requested_language: str | None = None,pronouns_field_type_supported: bool = True,diff --git a/zerver/lib/home.py b/zerver/lib/home.pyindex 262476fe46b..2fce1214b53 100644--- a/zerver/lib/home.py+++ b/zerver/lib/home.py@@ -14,7 +14,7 @@ from zerver.lib.i18n import (get_language_list,get_language_translation_data,)-from zerver.lib.narrow_helpers import NarrowTerm+from zerver.lib.narrow_helpers import NeverNegatedNarrowTermfrom zerver.lib.realm_description import get_realm_rendered_descriptionfrom zerver.lib.request import RequestNotesfrom zerver.models import Message, Realm, Stream, UserProfile@@ -121,7 +121,7 @@ def build_page_params_for_home_page_load(user_profile: UserProfile | None,realm: Realm,insecure_desktop_app: bool,- narrow: list[NarrowTerm],+ narrow: list[NeverNegatedNarrowTerm],narrow_stream: Stream | None,narrow_topic_name: str | None,) -> tuple[int, dict[str, object]]:diff --git a/zerver/lib/narrow_helpers.py b/zerver/lib/narrow_helpers.pyindex 296ac17d6c2..1dc31a69146 100644--- a/zerver/lib/narrow_helpers.py+++ b/zerver/lib/narrow_helpers.py@@ -20,24 +20,31 @@ from users:import osfrom collections.abc import Collection, Sequence-from dataclasses import dataclass+from dataclasses import dataclass, fieldfrom django.conf import settings@dataclassclass NarrowTerm:- # In our current use case we don't yet handle negated narrow terms.operator: stroperand: str+ negated: bool-def narrow_dataclasses_from_tuples(tups: Collection[Sequence[str]]) -> Collection[NarrowTerm]:+@dataclass+class NeverNegatedNarrowTerm(NarrowTerm):+ negated: bool = field(default=False, init=False)+++def narrow_dataclasses_from_tuples(+ tups: Collection[Sequence[str]],+) -> Collection[NeverNegatedNarrowTerm]:"""This method assumes that the callers are in our event-handling codepath, andtherefore as of summer 2023, they do not yet support the "negated" flag."""- return [NarrowTerm(operator=tup[0], operand=tup[1]) for tup in tups]+ return [NeverNegatedNarrowTerm(operator=tup[0], operand=tup[1]) for tup in tups]stop_words_list: list[str] | None = Nonediff --git a/zerver/lib/narrow_predicate.py b/zerver/lib/narrow_predicate.pyindex 954f578916f..a6d1390e536 100644--- a/zerver/lib/narrow_predicate.py+++ b/zerver/lib/narrow_predicate.py@@ -4,7 +4,7 @@ from typing import Any, Protocolfrom django.utils.translation import gettext as _from zerver.lib.exceptions import JsonableError-from zerver.lib.narrow_helpers import NarrowTerm+from zerver.lib.narrow_helpers import NeverNegatedNarrowTermfrom zerver.lib.topic import RESOLVED_TOPIC_PREFIX, get_topic_from_message_info# "stream" is a legacy alias for "channel"@@ -13,7 +13,7 @@ channel_operators: list[str] = ["channel", "stream"]channels_operators: list[str] = ["channels", "streams"]-def check_narrow_for_events(narrow: Collection[NarrowTerm]) -> None:+def check_narrow_for_events(narrow: Collection[NeverNegatedNarrowTerm]) -> None:supported_operators = [*channel_operators, "topic", "sender", "is"]unsupported_is_operands = ["followed"]for narrow_term in narrow:@@ -31,7 +31,7 @@ class NarrowPredicate(Protocol):def build_narrow_predicate(- narrow: Collection[NarrowTerm],+ narrow: Collection[NeverNegatedNarrowTerm],) -> NarrowPredicate:"""Changes to this function should come with corresponding changes toNarrowLibraryTest."""diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex a9516b7d760..1a31479b89c 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -42,7 +42,7 @@ from zerver.lib.narrow import (ok_to_include_history,post_process_limited_query,)-from zerver.lib.narrow_helpers import NarrowTerm+from zerver.lib.narrow_helpers import NeverNegatedNarrowTermfrom zerver.lib.narrow_predicate import build_narrow_predicatefrom zerver.lib.sqlalchemy_utils import get_sqlalchemy_connectionfrom zerver.lib.streams import StreamDict, create_streams_if_needed, get_public_streams_queryset@@ -755,7 +755,9 @@ class NarrowBuilderTest(ZulipTestCase):class NarrowLibraryTest(ZulipTestCase):def test_build_narrow_predicate(self) -> None:- narrow_predicate = build_narrow_predicate([NarrowTerm(operator="channel", operand="devel")])+ narrow_predicate = build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="channel", operand="devel")]+ )self.assertTrue(narrow_predicate(@@ -779,7 +781,9 @@ class NarrowLibraryTest(ZulipTestCase):###- narrow_predicate = build_narrow_predicate([NarrowTerm(operator="topic", operand="bark")])+ narrow_predicate = build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="topic", operand="bark")]+ )self.assertTrue(narrow_predicate(@@ -817,8 +821,8 @@ class NarrowLibraryTest(ZulipTestCase):narrow_predicate = build_narrow_predicate([- NarrowTerm(operator="channel", operand="devel"),- NarrowTerm(operator="topic", operand="python"),+ NeverNegatedNarrowTerm(operator="channel", operand="devel"),+ NeverNegatedNarrowTerm(operator="topic", operand="python"),])@@ -851,7 +855,7 @@ class NarrowLibraryTest(ZulipTestCase):###narrow_predicate = build_narrow_predicate(- [NarrowTerm(operator="sender", operand="[email protected]")]+ [NeverNegatedNarrowTerm(operator="sender", operand="[email protected]")])self.assertTrue(@@ -870,7 +874,9 @@ class NarrowLibraryTest(ZulipTestCase):###- narrow_predicate = build_narrow_predicate([NarrowTerm(operator="is", operand="dm")])+ narrow_predicate = build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="is", operand="dm")]+ )self.assertTrue(narrow_predicate(@@ -888,7 +894,9 @@ class NarrowLibraryTest(ZulipTestCase):###- narrow_predicate = build_narrow_predicate([NarrowTerm(operator="is", operand="private")])+ narrow_predicate = build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="is", operand="private")]+ )self.assertTrue(narrow_predicate(@@ -906,7 +914,9 @@ class NarrowLibraryTest(ZulipTestCase):###- narrow_predicate = build_narrow_predicate([NarrowTerm(operator="is", operand="starred")])+ narrow_predicate = build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="is", operand="starred")]+ )self.assertTrue(narrow_predicate(@@ -924,7 +934,9 @@ class NarrowLibraryTest(ZulipTestCase):###- narrow_predicate = build_narrow_predicate([NarrowTerm(operator="is", operand="alerted")])+ narrow_predicate = build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="is", operand="alerted")]+ )self.assertTrue(narrow_predicate(@@ -942,7 +954,9 @@ class NarrowLibraryTest(ZulipTestCase):###- narrow_predicate = build_narrow_predicate([NarrowTerm(operator="is", operand="mentioned")])+ narrow_predicate = build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="is", operand="mentioned")]+ )self.assertTrue(narrow_predicate(@@ -960,7 +974,9 @@ class NarrowLibraryTest(ZulipTestCase):###- narrow_predicate = build_narrow_predicate([NarrowTerm(operator="is", operand="unread")])+ narrow_predicate = build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="is", operand="unread")]+ )self.assertTrue(narrow_predicate(@@ -978,7 +994,9 @@ class NarrowLibraryTest(ZulipTestCase):###- narrow_predicate = build_narrow_predicate([NarrowTerm(operator="is", operand="resolved")])+ narrow_predicate = build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="is", operand="resolved")]+ )self.assertTrue(narrow_predicate(@@ -1002,9 +1020,11 @@ class NarrowLibraryTest(ZulipTestCase):def test_build_narrow_predicate_invalid(self) -> None:with self.assertRaises(JsonableError):- build_narrow_predicate([NarrowTerm(operator="invalid_operator", operand="operand")])+ build_narrow_predicate(+ [NeverNegatedNarrowTerm(operator="invalid_operator", operand="operand")]+ )with self.assertRaises(JsonableError):- build_narrow_predicate([NarrowTerm(operator="is", operand="followed")])+ build_narrow_predicate([NeverNegatedNarrowTerm(operator="is", operand="followed")])def test_is_spectator_compatible(self) -> None:self.assertTrue(is_spectator_compatible([]))diff --git a/zerver/views/home.py b/zerver/views/home.pyindex a1044198e12..de2c72b988a 100644--- a/zerver/views/home.py+++ b/zerver/views/home.py@@ -13,7 +13,7 @@ from zerver.decorator import web_public_view, zulip_login_requiredfrom zerver.forms import ToSFormfrom zerver.lib.compatibility import is_outdated_desktop_app, is_unsupported_browserfrom zerver.lib.home import build_page_params_for_home_page_load, get_user_permission_info-from zerver.lib.narrow_helpers import NarrowTerm+from zerver.lib.narrow_helpers import NeverNegatedNarrowTermfrom zerver.lib.request import RequestNotesfrom zerver.lib.streams import access_stream_by_namefrom zerver.lib.subdomains import get_subdomain@@ -113,13 +113,13 @@ def accounts_accept_terms(request: HttpRequest) -> HttpResponse:def detect_narrowed_window(request: HttpRequest, user_profile: UserProfile | None-) -> tuple[list[NarrowTerm], Stream | None, str | None]:+) -> tuple[list[NeverNegatedNarrowTerm], Stream | None, str | None]:"""This function implements Zulip's support for a mini Zulip windowthat just handles messages from a single narrow"""if user_profile is None:return [], None, None- narrow: list[NarrowTerm] = []+ narrow: list[NeverNegatedNarrowTerm] = []narrow_stream = Nonenarrow_topic_name = request.GET.get("topic")@@ -129,11 +129,11 @@ def detect_narrowed_window(narrow_stream_name = request.GET.get("stream")assert narrow_stream_name is not None(narrow_stream, ignored_sub) = access_stream_by_name(user_profile, narrow_stream_name)- narrow = [NarrowTerm(operator="stream", operand=narrow_stream.name)]+ narrow = [NeverNegatedNarrowTerm(operator="stream", operand=narrow_stream.name)]except Exception:logging.warning("Invalid narrow requested, ignoring", extra=dict(request=request))if narrow_stream is not None and narrow_topic_name is not None:- narrow.append(NarrowTerm(operator="topic", operand=narrow_topic_name))+ narrow.append(NeverNegatedNarrowTerm(operator="topic", operand=narrow_topic_name))return narrow, narrow_stream, narrow_topic_name