95
95
get_updates_counter = Counter ("synapse_handler_presence_get_updates" , "" , ["type" ])
96
96
97
97
notify_reason_counter = Counter (
98
- "synapse_handler_presence_notify_reason" , "" , ["reason" ]
98
+ "synapse_handler_presence_notify_reason" , "" , ["locality" , " reason" ]
99
99
)
100
100
state_transition_counter = Counter (
101
- "synapse_handler_presence_state_transition" , "" , ["from" , "to" ]
101
+ "synapse_handler_presence_state_transition" , "" , ["locality" , " from" , "to" ]
102
102
)
103
103
104
-
105
104
# If a user was last active in the last LAST_ACTIVE_GRANULARITY, consider them
106
105
# "currently_active"
107
106
LAST_ACTIVE_GRANULARITY = 60 * 1000
@@ -567,8 +566,8 @@ async def process_replication_rows(
567
566
for new_state in states :
568
567
old_state = self .user_to_current_state .get (new_state .user_id )
569
568
self .user_to_current_state [new_state .user_id ] = new_state
570
-
571
- if not old_state or should_notify (old_state , new_state ):
569
+ is_mine = self . is_mine_id ( new_state . user_id )
570
+ if not old_state or should_notify (old_state , new_state , is_mine ):
572
571
state_to_notify .append (new_state )
573
572
574
573
stream_id = token
@@ -1499,23 +1498,31 @@ async def _handle_state_delta(self, room_id: str, deltas: List[JsonDict]) -> Non
1499
1498
)
1500
1499
1501
1500
1502
- def should_notify (old_state : UserPresenceState , new_state : UserPresenceState ) -> bool :
1501
+ def should_notify (
1502
+ old_state : UserPresenceState , new_state : UserPresenceState , is_mine : bool
1503
+ ) -> bool :
1503
1504
"""Decides if a presence state change should be sent to interested parties."""
1505
+ user_location = "remote"
1506
+ if is_mine :
1507
+ user_location = "local"
1508
+
1504
1509
if old_state == new_state :
1505
1510
return False
1506
1511
1507
1512
if old_state .status_msg != new_state .status_msg :
1508
- notify_reason_counter .labels ("status_msg_change" ).inc ()
1513
+ notify_reason_counter .labels (user_location , "status_msg_change" ).inc ()
1509
1514
return True
1510
1515
1511
1516
if old_state .state != new_state .state :
1512
- notify_reason_counter .labels ("state_change" ).inc ()
1513
- state_transition_counter .labels (old_state .state , new_state .state ).inc ()
1517
+ notify_reason_counter .labels (user_location , "state_change" ).inc ()
1518
+ state_transition_counter .labels (
1519
+ user_location , old_state .state , new_state .state
1520
+ ).inc ()
1514
1521
return True
1515
1522
1516
1523
if old_state .state == PresenceState .ONLINE :
1517
1524
if new_state .currently_active != old_state .currently_active :
1518
- notify_reason_counter .labels ("current_active_change" ).inc ()
1525
+ notify_reason_counter .labels (user_location , "current_active_change" ).inc ()
1519
1526
return True
1520
1527
1521
1528
if (
@@ -1524,12 +1531,16 @@ def should_notify(old_state: UserPresenceState, new_state: UserPresenceState) ->
1524
1531
):
1525
1532
# Only notify about last active bumps if we're not currently active
1526
1533
if not new_state .currently_active :
1527
- notify_reason_counter .labels ("last_active_change_online" ).inc ()
1534
+ notify_reason_counter .labels (
1535
+ user_location , "last_active_change_online"
1536
+ ).inc ()
1528
1537
return True
1529
1538
1530
1539
elif new_state .last_active_ts - old_state .last_active_ts > LAST_ACTIVE_GRANULARITY :
1531
1540
# Always notify for a transition where last active gets bumped.
1532
- notify_reason_counter .labels ("last_active_change_not_online" ).inc ()
1541
+ notify_reason_counter .labels (
1542
+ user_location , "last_active_change_not_online"
1543
+ ).inc ()
1533
1544
return True
1534
1545
1535
1546
return False
@@ -1989,7 +2000,7 @@ def handle_update(
1989
2000
)
1990
2001
1991
2002
# Check whether the change was something worth notifying about
1992
- if should_notify (prev_state , new_state ):
2003
+ if should_notify (prev_state , new_state , is_mine ):
1993
2004
new_state = new_state .copy_and_replace (last_federation_update_ts = now )
1994
2005
persist_and_notify = True
1995
2006
0 commit comments