Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
presence: Deduplicate code formatting legacy presence info.
Mateusz Mandera0d79f6dd2734
diff --git a/zerver/actions/presence.py b/zerver/actions/presence.pyindex 8eaf6890a3b..f3593b2b713 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.pyindex 4fb52d99747..49e1a1c6a39 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_inforeturn 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)++ # 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=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.pyindex eae6e875e14..3bc4fb3d5fe 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,