Skip to content

Commit

Permalink
Add option to disable presence tracking for offline devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
sklamt committed Feb 3, 2025
1 parent ac1bf68 commit 0c31a65
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
self.presence_enabled = bool(presence_enabled)
# Whether to internally track presence, requires that presence is enabled,
self.track_presence = self.presence_enabled and presence_enabled != "untracked"
self.track_offline_presence = (
self.presence_enabled
and self.track_presence
and presence_enabled != "offline_untracked"
)

# Determines if presence results for offline users are included on initial/full sync
self.presence_include_offline_users_on_sync = presence_config.get(
Expand Down
29 changes: 24 additions & 5 deletions synapse/handlers/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def __init__(self, hs: "HomeServer"):

self._presence_enabled = hs.config.server.presence_enabled
self._track_presence = hs.config.server.track_presence
self._track_offline_presence = hs.config.server.track_offline_presence

self._federation = None
if hs.should_send_federation():
Expand Down Expand Up @@ -1094,17 +1095,26 @@ async def bump_presence_active_time(

user_id = user.to_string()

devices = self._user_to_device_to_current_state.setdefault(user_id, {})

device_state = devices.setdefault(
device_id,
UserDevicePresenceState.default(user_id, device_id),
)

# If Presence tracking is disabled for Offline devices and device is Offline, no-op
if (
not self._track_offline_presence
and device_state.state == PresenceState.OFFLINE
):
return

bump_active_time_counter.inc()

now = self.clock.time_msec()

# Update the device information & mark the device as online if it was
# unavailable.
devices = self._user_to_device_to_current_state.setdefault(user_id, {})
device_state = devices.setdefault(
device_id,
UserDevicePresenceState.default(user_id, device_id),
)
device_state.last_active_ts = now
if device_state.state == PresenceState.UNAVAILABLE:
device_state.state = PresenceState.ONLINE
Expand Down Expand Up @@ -1397,6 +1407,15 @@ async def set_state(
device_id,
UserDevicePresenceState.default(user_id, device_id),
)

# If Presence tracking is disabled for Offline devices and device was Offline and is still Offline, no-op
if (
not self._track_offline_presence
and device_state.state == PresenceState.OFFLINE
and presence == PresenceState.OFFLINE
):
return

device_state.state = presence
device_state.last_active_ts = now
if is_sync:
Expand Down

0 comments on commit 0c31a65

Please sign in to comment.