Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
narrow: Stop using anchor=None as a proxy for first_unread.
Shubham Padiaa8b688383bdf
diff --git a/zerver/actions/message_summary.py b/zerver/actions/message_summary.pyindex b1669dd7447..cc4c18a0ce1 100644--- a/zerver/actions/message_summary.py+++ b/zerver/actions/message_summary.py@@ -10,6 +10,7 @@ from zerver.lib.markdown import markdown_convertfrom zerver.lib.message import messages_for_idsfrom zerver.lib.narrow import (LARGER_THAN_MAX_MESSAGE_ID,+ AnchorInfo,NarrowParameter,clean_narrow_for_message_fetch,fetch_messages,@@ -94,7 +95,7 @@ def do_summarize_narrow(user_profile=user_profile,realm=user_profile.realm,is_web_public_query=False,- anchor=LARGER_THAN_MAX_MESSAGE_ID,+ anchor_info=AnchorInfo(type="message_id", value=LARGER_THAN_MAX_MESSAGE_ID),include_anchor=True,num_before=MAX_MESSAGES_SUMMARIZED,num_after=0,diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.pyindex 829dcd237ac..01778da9fbf 100644--- a/zerver/lib/narrow.py+++ b/zerver/lib/narrow.py@@ -1,7 +1,7 @@import refrom collections.abc import Callable, Iterable, Sequencefrom dataclasses import dataclass-from typing import Any, Generic, TypeAlias, TypeVar+from typing import Any, Generic, Literal, TypeAlias, TypedDict, TypeVarfrom django.conf import settingsfrom django.contrib.auth.models import AnonymousUser@@ -188,6 +188,14 @@ def is_web_public_narrow(narrow: Iterable[NarrowParameter] | None) -> bool:LARGER_THAN_MAX_MESSAGE_ID = 10000000000000000+class AnchorInfo(TypedDict):+ type: Literal["message_id", "first_unread"]+ value: int | None+++DEFAULT_ANCHOR_INFO: AnchorInfo = AnchorInfo(type="first_unread", value=None)++class BadNarrowOperatorError(JsonableError):code = ErrorCode.BAD_NARROWdata_fields = ["desc"]@@ -1198,28 +1206,31 @@ def find_first_unread_anchor(return anchor-def parse_anchor_value(anchor_val: str | None, use_first_unread_anchor: bool) -> int | None:+def parse_anchor_value(anchor_val: str | None, use_first_unread_anchor: bool) -> AnchorInfo:"""Given the anchor and use_first_unread_anchor parameters passed by- the client, computes what anchor value the client requested,+ the client, computes what anchor type and value the client requested,handling backwards-compatibility and the various string-valued- fields. We encode use_first_unread_anchor as anchor=None.+ fields."""if use_first_unread_anchor:# Backwards-compatibility: Before we added support for the# special string-typed anchor values, clients would pass# anchor=None and use_first_unread_anchor=True to indicate# what is now expressed as anchor="first_unread".- return None+ return AnchorInfo(type="first_unread", value=None)if anchor_val is None:- # Throw an exception if neither an anchor argument not+ # Throw an exception if neither an anchor argument nor# use_first_unread_anchor was specified.raise JsonableError(_("Missing 'anchor' argument."))if anchor_val == "oldest":- return 0+ return AnchorInfo(type="message_id", value=0)if anchor_val == "newest":- return LARGER_THAN_MAX_MESSAGE_ID+ return AnchorInfo(+ type="message_id",+ value=LARGER_THAN_MAX_MESSAGE_ID,+ )if anchor_val == "first_unread":- return None+ return AnchorInfo(type="first_unread", value=None)try:# We don't use `.isnumeric()` to support negative numbers for# anchor. We don't recommend it in the API (if you want the@@ -1228,10 +1239,12 @@ def parse_anchor_value(anchor_val: str | None, use_first_unread_anchor: bool) -># supporting it for backwards-compatibilityanchor = int(anchor_val)if anchor < 0:- return 0+ anchor_value = 0elif anchor > LARGER_THAN_MAX_MESSAGE_ID:- return LARGER_THAN_MAX_MESSAGE_ID- return anchor+ anchor_value = LARGER_THAN_MAX_MESSAGE_ID+ else:+ anchor_value = anchor+ return AnchorInfo(type="message_id", value=anchor_value)except ValueError:raise JsonableError(_("Invalid anchor"))@@ -1420,7 +1433,7 @@ def fetch_messages(user_profile: UserProfile | None,realm: Realm,is_web_public_query: bool,- anchor: int | None,+ anchor_info: AnchorInfo | None,include_anchor: bool,num_before: int,num_after: int,@@ -1461,16 +1474,19 @@ def fetch_messages(is_web_public_query=is_web_public_query,)+ if anchor_info is None:+ anchor_info = DEFAULT_ANCHOR_INFOanchored_to_left = Falseanchored_to_right = False+ anchor_type = anchor_info["type"]+ anchor_value = anchor_info["value"]first_visible_message_id = get_first_visible_message_id(realm)with get_sqlalchemy_connection() as sa_conn:if client_requested_message_ids is not None:query = query.filter(inner_msg_id_col.in_(client_requested_message_ids))else:- if anchor is None:- # `anchor=None` corresponds to the anchor="first_unread" parameter.- anchor = find_first_unread_anchor(+ if anchor_type == "first_unread":+ anchor_value = find_first_unread_anchor(sa_conn,user_profile,narrow,@@ -1480,11 +1496,13 @@ def fetch_messages(need_user_message,)- anchored_to_left = anchor == 0+ assert anchor_value is not None++ anchored_to_left = anchor_value == 0# Set value that will be used to short circuit the after_query# altogether and avoid needless conditions in the before_query.- anchored_to_right = anchor >= LARGER_THAN_MAX_MESSAGE_ID+ anchored_to_right = anchor_value >= LARGER_THAN_MAX_MESSAGE_IDif anchored_to_right:num_after = 0@@ -1492,7 +1510,7 @@ def fetch_messages(query=query,num_before=num_before,num_after=num_after,- anchor=anchor,+ anchor=anchor_value,include_anchor=include_anchor,anchored_to_left=anchored_to_left,anchored_to_right=anchored_to_right,@@ -1528,12 +1546,12 @@ def fetch_messages(is_search=is_search,)- assert anchor is not None+ assert anchor_value is not Nonequery_info = post_process_limited_query(rows=rows,num_before=num_before,num_after=num_after,- anchor=anchor,+ anchor=anchor_value,anchored_to_left=anchored_to_left,anchored_to_right=anchored_to_right,first_visible_message_id=first_visible_message_id,@@ -1545,7 +1563,7 @@ def fetch_messages(found_newest=query_info.found_newest,found_oldest=query_info.found_oldest,history_limited=query_info.history_limited,- anchor=anchor,+ anchor=anchor_value,include_history=include_history,is_search=is_search,)diff --git a/zerver/views/message_fetch.py b/zerver/views/message_fetch.pyindex 693618e2611..9eec21d4468 100644--- a/zerver/views/message_fetch.py+++ b/zerver/views/message_fetch.py@@ -141,9 +141,9 @@ def get_messages_backend(elif client_requested_message_ids is not None:include_anchor = False- anchor = None+ anchor_info = Noneif client_requested_message_ids is None:- anchor = parse_anchor_value(anchor_val, use_first_unread_anchor_val)+ anchor_info = parse_anchor_value(anchor_val, use_first_unread_anchor_val)realm = get_valid_realm_from_request(request)narrow = clean_narrow_for_message_fetch(narrow, realm, maybe_user_profile)@@ -242,7 +242,7 @@ def get_messages_backend(user_profile=user_profile,realm=realm,is_web_public_query=is_web_public_query,- anchor=anchor,+ anchor_info=anchor_info,include_anchor=include_anchor,num_before=num_before,num_after=num_after,diff --git a/zerver/views/message_flags.py b/zerver/views/message_flags.pyindex 4622a79f744..8dc334c2d16 100644--- a/zerver/views/message_flags.py+++ b/zerver/views/message_flags.py@@ -88,7 +88,7 @@ def update_message_flags_for_narrow(num_before: Json[NonNegativeInt],operation: Annotated[str, ApiParamConfig("op")],) -> HttpResponse:- anchor = parse_anchor_value(anchor_val, use_first_unread_anchor=False)+ anchor_info = parse_anchor_value(anchor_val, use_first_unread_anchor=False)if num_before > 0 and num_after > 0 and not include_anchor:raise JsonableError(_("The anchor can only be excluded at an end of the range"))@@ -106,7 +106,7 @@ def update_message_flags_for_narrow(user_profile=user_profile,realm=user_profile.realm,is_web_public_query=False,- anchor=anchor,+ anchor_info=anchor_info,include_anchor=include_anchor,num_before=num_before,num_after=num_after,