Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit ea75346

Browse files
authored
Track presence state per-device and combine to a user state. (#16066)
Tracks presence on an individual per-device basis and combine the per-device state into a per-user state. This should help in situations where a user has multiple devices with conflicting status (e.g. one is syncing with unavailable and one is syncing with online). The tie-breaking is done by priority: BUSY > ONLINE > UNAVAILABLE > OFFLINE
1 parent 36ae861 commit ea75346

File tree

10 files changed

+765
-64
lines changed

10 files changed

+765
-64
lines changed

changelog.d/16066.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where multi-device accounts could cause high load due to presence.

changelog.d/16170.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where multi-device accounts could cause high load due to presence.

changelog.d/16170.misc

Lines changed: 0 additions & 1 deletion
This file was deleted.

changelog.d/16171.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where multi-device accounts could cause high load due to presence.

changelog.d/16171.misc

Lines changed: 0 additions & 1 deletion
This file was deleted.

changelog.d/16172.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where multi-device accounts could cause high load due to presence.

changelog.d/16172.misc

Lines changed: 0 additions & 1 deletion
This file was deleted.

synapse/api/presence.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,53 @@
2020
from synapse.types import JsonDict
2121

2222

23+
@attr.s(slots=True, auto_attribs=True)
24+
class UserDevicePresenceState:
25+
"""
26+
Represents the current presence state of a user's device.
27+
28+
user_id: The user ID.
29+
device_id: The user's device ID.
30+
state: The presence state, see PresenceState.
31+
last_active_ts: Time in msec that the device last interacted with server.
32+
last_sync_ts: Time in msec that the device last *completed* a sync
33+
(or event stream).
34+
"""
35+
36+
user_id: str
37+
device_id: Optional[str]
38+
state: str
39+
last_active_ts: int
40+
last_sync_ts: int
41+
42+
@classmethod
43+
def default(
44+
cls, user_id: str, device_id: Optional[str]
45+
) -> "UserDevicePresenceState":
46+
"""Returns a default presence state."""
47+
return cls(
48+
user_id=user_id,
49+
device_id=device_id,
50+
state=PresenceState.OFFLINE,
51+
last_active_ts=0,
52+
last_sync_ts=0,
53+
)
54+
55+
2356
@attr.s(slots=True, frozen=True, auto_attribs=True)
2457
class UserPresenceState:
2558
"""Represents the current presence state of the user.
2659
27-
user_id
28-
last_active: Time in msec that the user last interacted with server.
29-
last_federation_update: Time in msec since either a) we sent a presence
60+
user_id: The user ID.
61+
state: The presence state, see PresenceState.
62+
last_active_ts: Time in msec that the user last interacted with server.
63+
last_federation_update_ts: Time in msec since either a) we sent a presence
3064
update to other servers or b) we received a presence update, depending
3165
on if is a local user or not.
32-
last_user_sync: Time in msec that the user last *completed* a sync
66+
last_user_sync_ts: Time in msec that the user last *completed* a sync
3367
(or event stream).
3468
status_msg: User set status message.
69+
currently_active: True if the user is currently syncing.
3570
"""
3671

3772
user_id: str

0 commit comments

Comments
 (0)