This commit is contained in:
Sebastian Klamt 2025-03-11 10:35:22 +00:00 committed by GitHub
commit e5d378e507
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 5 deletions

View file

@ -0,0 +1 @@
Add option to disable presence tracking for offline devices.

View file

@ -386,6 +386,11 @@ class ServerConfig(Config):
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(

View file

@ -202,6 +202,7 @@ class BasePresenceHandler(abc.ABC):
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():
@ -1094,17 +1095,26 @@ class PresenceHandler(BasePresenceHandler):
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
@ -1397,6 +1407,15 @@ class PresenceHandler(BasePresenceHandler):
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: