Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
narrow: Return a dataclass from post_process_limited_query.
Anders Kaseorgbd5d2007224b
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.pyindex 4605f9f47f9..a1d2467af49 100644--- a/zerver/lib/narrow.py+++ b/zerver/lib/narrow.py@@ -1,16 +1,19 @@import osimport re+from dataclasses import dataclassfrom typing import (Any,Callable,Collection,Dict,+ Generic,Iterable,List,Mapping,Optional,Sequence,Tuple,+ TypeVar,Union,)@@ -20,7 +23,7 @@ from django.core.exceptions import ValidationErrorfrom django.db import connectionfrom django.utils.translation import gettext as _from sqlalchemy.dialects import postgresql-from sqlalchemy.engine import Connection, Row+from sqlalchemy.engine import Connectionfrom sqlalchemy.sql import (ClauseElement,ColumnElement,@@ -1065,15 +1068,27 @@ def limit_query_to_range(return query.where(id_col == anchor)+MessageRowT = TypeVar("MessageRowT", bound=Sequence[Any])+++@dataclass+class LimitedMessages(Generic[MessageRowT]):+ rows: List[MessageRowT]+ found_anchor: bool+ found_newest: bool+ found_oldest: bool+ history_limited: bool++def post_process_limited_query(- rows: Sequence[Union[Row, Sequence[Any]]],+ rows: Sequence[MessageRowT],num_before: int,num_after: int,anchor: int,anchored_to_left: bool,anchored_to_right: bool,first_visible_message_id: int,-) -> Dict[str, Any]:+) -> LimitedMessages[MessageRowT]:# Our queries may have fetched extra rows if they added# "headroom" to the limits, but we want to truncate those# rows.@@ -1083,9 +1098,7 @@ def post_process_limited_query(# that the clients will know that they got complete results.if first_visible_message_id > 0:- visible_rows: Sequence[Union[Row, Sequence[Any]]] = [- r for r in rows if r[0] >= first_visible_message_id- ]+ visible_rows: Sequence[MessageRowT] = [r for r in rows if r[0] >= first_visible_message_id]else:visible_rows = rows@@ -1107,7 +1120,7 @@ def post_process_limited_query(if num_after:after_rows = after_rows[:num_after]- visible_rows = [*before_rows, *anchor_rows, *after_rows]+ limited_rows = [*before_rows, *anchor_rows, *after_rows]found_anchor = len(anchor_rows) == 1found_oldest = anchored_to_left or (len(before_rows) < num_before)@@ -1125,8 +1138,8 @@ def post_process_limited_query(# messages were hidden.history_limited = rows_limited and found_oldest- return dict(- rows=visible_rows,+ return LimitedMessages(+ rows=limited_rows,found_anchor=found_anchor,found_newest=found_newest,found_oldest=found_oldest,diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex 60c8146a7b2..0e8ca7bbbcd 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -759,11 +759,11 @@ class PostProcessTest(ZulipTestCase):first_visible_message_id=first_visible_message_id,)- self.assertEqual(info["rows"], out_rows)- self.assertEqual(info["found_anchor"], found_anchor)- self.assertEqual(info["found_newest"], found_newest)- self.assertEqual(info["found_oldest"], found_oldest)- self.assertEqual(info["history_limited"], history_limited)+ self.assertEqual(info.rows, out_rows)+ self.assertEqual(info.found_anchor, found_anchor)+ self.assertEqual(info.found_newest, found_newest)+ self.assertEqual(info.found_oldest, found_oldest)+ self.assertEqual(info.history_limited, history_limited)# typical 2-sided query, with a bunch of tests for different# values of first_visible_message_id.diff --git a/zerver/views/message_fetch.py b/zerver/views/message_fetch.pyindex 887c488598c..751aee8ad8b 100644--- a/zerver/views/message_fetch.py+++ b/zerver/views/message_fetch.py@@ -249,7 +249,7 @@ def get_messages_backend(first_visible_message_id=first_visible_message_id,)- rows = query_info["rows"]+ rows = query_info.rows# The following is a little messy, but ensures that the code paths# are similar regardless of the value of include_history. The@@ -308,10 +308,10 @@ def get_messages_backend(messages=message_list,result="success",msg="",- found_anchor=query_info["found_anchor"],- found_oldest=query_info["found_oldest"],- found_newest=query_info["found_newest"],- history_limited=query_info["history_limited"],+ found_anchor=query_info.found_anchor,+ found_oldest=query_info.found_oldest,+ found_newest=query_info.found_newest,+ history_limited=query_info.history_limited,anchor=anchor,)return json_success(request, data=ret)