presence: Deduplicate code formatting legacy presence info.

This also removes the error in one of these functions that was using a
different constant instead of
PRESENCE_LEGACY_EVENT_OFFSET_FOR_ACTIVITY_SECONDS.
This commit is contained in:
Mateusz Mandera 2023-04-04 19:35:06 +02:00 committed by Tim Abbott
parent ed91e72c5c
commit 0d79f6dd27
3 changed files with 25 additions and 35 deletions

View File

@ -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,

View File

@ -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(

View File

@ -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,