From d8a95c65178f0c63a48407539089950e8945d906 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 5 Apr 2018 07:31:22 -0400 Subject: [PATCH] Extract find_first_unread_anchor(). This is a pure refactoring. --- zerver/views/messages.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/zerver/views/messages.py b/zerver/views/messages.py index 0f3742a3b9..e35fc7afcd 100644 --- a/zerver/views/messages.py +++ b/zerver/views/messages.py @@ -629,9 +629,11 @@ def get_messages_backend(request: HttpRequest, user_profile: UserProfile, sa_conn = get_sqlalchemy_connection() - anchored_to_right = False # till we know better - - if use_first_unread_anchor: + def find_first_unread_anchor(sa_conn: Any, + inner_msg_id_col: ColumnElement, + user_profile: UserProfile, + narrow: List[Dict[str, Any]], + query: Query) -> int: condition = column("flags").op("&")(UserMessage.flags.read.mask) == 0 # We exclude messages on muted topics when finding the first unread @@ -660,18 +662,27 @@ def get_messages_backend(request: HttpRequest, user_profile: UserProfile, if len(first_unread_result) > 0: anchor = first_unread_result[0][0] else: - # Set values that will be used to short circuit the after_query - # altogether and avoid needless conditions in the before_query. - anchored_to_right = True - num_after = None - - # We only use LARGER_THAN_MAX_MESSAGE_ID for an edge case hack - # where num_before and num_after are 0, and it produces a query - # that returns zero results. anchor = LARGER_THAN_MAX_MESSAGE_ID + return anchor + + if use_first_unread_anchor: + anchor = find_first_unread_anchor( + sa_conn, + inner_msg_id_col, + user_profile, + narrow, + query + ) + anchored_to_left = (anchor == 0) + # Set values 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) + if anchored_to_right: + num_after = None + query = limit_query_to_range( query=query, num_before=num_before,