diff --git a/zerver/actions/presence.py b/zerver/actions/presence.py index 8eaf6890a3..f3593b2b71 100644 --- a/zerver/actions/presence.py +++ b/zerver/actions/presence.py @@ -45,7 +45,9 @@ def send_presence_changed( # The mobile app handles these events so we need to use the old format. # The format of the event should also account for the slim_presence # API parameter when this becomes possible in the future. - presence_dict = format_legacy_presence_dict(presence) + presence_dict = format_legacy_presence_dict( + presence.last_active_time, presence.last_connected_time + ) event = dict( type="presence", email=user_profile.email, diff --git a/zerver/lib/presence.py b/zerver/lib/presence.py index 4fb52d9974..49e1a1c6a3 100644 --- a/zerver/lib/presence.py +++ b/zerver/lib/presence.py @@ -48,36 +48,18 @@ def get_modern_user_presence_info( def get_legacy_user_presence_info( last_active_time: datetime.datetime, last_connected_time: datetime.datetime ) -> Dict[str, Any]: - # Reformats the modern UserPresence data structure so that legacy - # API clients can still access presence data. - # - # We expect this code to remain mostly unchanged until we can delete it. - - if timezone_now() - last_active_time > datetime.timedelta(minutes=2): - dt = last_connected_time - status = UserPresence.LEGACY_STATUS_IDLE - else: - dt = last_active_time - status = UserPresence.LEGACY_STATUS_ACTIVE - - client_name = "website" - timestamp = datetime_to_timestamp(dt) - - # This field was never used by clients of the legacy API, so we - # just set it to a fixed value for API format compatibility. - pushable = False + """ + Reformats the modern UserPresence data structure so that legacy + API clients can still access presence data. + We expect this code to remain mostly unchanged until we can delete it. + """ # Now we put things together in the legacy presence format with # one client + an `aggregated` field. # # TODO: Look at whether we can drop to just the "aggregated" field # if no clients look at the rest. - most_recent_info = dict( - client=client_name, - status=status, - timestamp=timestamp, - pushable=pushable, - ) + most_recent_info = format_legacy_presence_dict(last_active_time, last_connected_time) result = {} @@ -89,28 +71,34 @@ def get_legacy_user_presence_info( timestamp=most_recent_info["timestamp"], ) - result[client_name] = most_recent_info + result["website"] = most_recent_info return result -def format_legacy_presence_dict(presence: UserPresence) -> Dict[str, Any]: +def format_legacy_presence_dict( + last_active_time: datetime.datetime, last_connected_time: datetime.datetime +) -> Dict[str, Any]: """ This function assumes it's being called right after the presence object was updated, and is not meant to be used on old presence data. """ if ( - presence.last_active_time + last_active_time + datetime.timedelta(seconds=settings.PRESENCE_LEGACY_EVENT_OFFSET_FOR_ACTIVITY_SECONDS) - >= presence.last_connected_time + >= last_connected_time ): status = UserPresence.LEGACY_STATUS_ACTIVE - timestamp = datetime_to_timestamp(presence.last_active_time) + timestamp = datetime_to_timestamp(last_active_time) else: status = UserPresence.LEGACY_STATUS_IDLE - timestamp = datetime_to_timestamp(presence.last_connected_time) + timestamp = datetime_to_timestamp(last_connected_time) - return dict(client="website", status=status, timestamp=timestamp, pushable=False) + # This field was never used by clients of the legacy API, so we + # just set it to a fixed value for API format compatibility. + pushable = False + + return dict(client="website", status=status, timestamp=timestamp, pushable=pushable) def get_presence_for_user( diff --git a/zerver/tests/test_presence.py b/zerver/tests/test_presence.py index eae6e875e1..3bc4fb3d5f 100644 --- a/zerver/tests/test_presence.py +++ b/zerver/tests/test_presence.py @@ -679,7 +679,7 @@ class FormatLegacyPresenceDictTest(ZulipTestCase): user_profile=hamlet, realm=hamlet.realm, last_active_time=now, last_connected_time=now ) self.assertEqual( - format_legacy_presence_dict(presence), + format_legacy_presence_dict(presence.last_active_time, presence.last_connected_time), dict( client="website", status=UserPresence.LEGACY_STATUS_ACTIVE, @@ -695,7 +695,7 @@ class FormatLegacyPresenceDictTest(ZulipTestCase): last_connected_time=now, ) self.assertEqual( - format_legacy_presence_dict(presence), + format_legacy_presence_dict(presence.last_active_time, presence.last_connected_time), dict( client="website", status=UserPresence.LEGACY_STATUS_ACTIVE, @@ -711,7 +711,7 @@ class FormatLegacyPresenceDictTest(ZulipTestCase): last_connected_time=now, ) self.assertEqual( - format_legacy_presence_dict(presence), + format_legacy_presence_dict(presence.last_active_time, presence.last_connected_time), dict( client="website", status=UserPresence.LEGACY_STATUS_IDLE,