2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2020-04-22 17:39:04 -04:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
2014-08-12 10:10:52 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-08-12 22:14:34 -04:00
|
|
|
|
2023-09-05 09:58:51 -04:00
|
|
|
"""
|
|
|
|
This module is responsible for keeping track of presence status of local
|
2016-02-15 12:10:40 -05:00
|
|
|
and remote users.
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
The methods that define policy are:
|
|
|
|
- PresenceHandler._update_states
|
|
|
|
- PresenceHandler._handle_timeouts
|
|
|
|
- should_notify
|
2023-09-05 09:58:51 -04:00
|
|
|
|
|
|
|
# Tracking local presence
|
|
|
|
|
|
|
|
For local users, presence is tracked on a per-device basis. When a user has multiple
|
|
|
|
devices the user presence state is derived by coalescing the presence from each
|
|
|
|
device:
|
|
|
|
|
|
|
|
BUSY > ONLINE > UNAVAILABLE > OFFLINE
|
|
|
|
|
|
|
|
The time that each device was last active and last synced is tracked in order to
|
|
|
|
automatically downgrade a device's presence state:
|
|
|
|
|
|
|
|
A device may move from ONLINE -> UNAVAILABLE, if it has not been active for
|
|
|
|
a period of time.
|
|
|
|
|
|
|
|
A device may go from any state -> OFFLINE, if it is not active and has not
|
|
|
|
synced for a period of time.
|
|
|
|
|
|
|
|
The timeouts are handled using a wheel timer, which has coarse buckets. Timings
|
|
|
|
do not need to be exact.
|
|
|
|
|
|
|
|
Generally a device's presence state is updated whenever a user syncs (via the
|
|
|
|
set_presence parameter), when the presence API is called, or if "pro-active"
|
|
|
|
events occur, including:
|
|
|
|
|
|
|
|
* Sending an event, receipt, read marker.
|
|
|
|
* Updating typing status.
|
|
|
|
|
|
|
|
The busy state has special status that it cannot is not downgraded by a call to
|
|
|
|
sync with a lower priority state *and* it takes a long period of time to transition
|
|
|
|
to offline.
|
|
|
|
|
|
|
|
# Persisting (and restoring) presence
|
|
|
|
|
|
|
|
For all users, presence is persisted on a per-user basis. Data is kept in-memory
|
|
|
|
and persisted periodically. When Synapse starts each worker loads the current
|
|
|
|
presence state and then tracks the presence stream to keep itself up-to-date.
|
|
|
|
|
|
|
|
When restoring presence for local users a pseudo-device is created to match the
|
|
|
|
user state; this device follows the normal timeout logic (see above) and will
|
|
|
|
automatically be replaced with any information from currently available devices.
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
"""
|
2020-04-22 17:39:04 -04:00
|
|
|
import abc
|
2021-04-14 12:06:06 -04:00
|
|
|
import contextlib
|
2023-08-29 11:44:07 -04:00
|
|
|
import itertools
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2021-04-20 09:11:24 -04:00
|
|
|
from bisect import bisect
|
2016-02-15 12:10:40 -05:00
|
|
|
from contextlib import contextmanager
|
2021-09-20 08:56:23 -04:00
|
|
|
from types import TracebackType
|
2021-04-06 09:38:30 -04:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
2023-09-05 09:58:51 -04:00
|
|
|
AbstractSet,
|
2021-09-10 12:03:18 -04:00
|
|
|
Any,
|
2021-04-28 06:07:47 -04:00
|
|
|
Callable,
|
2021-04-22 11:43:50 -04:00
|
|
|
Collection,
|
2023-08-15 08:11:20 -04:00
|
|
|
ContextManager,
|
2021-04-06 09:38:30 -04:00
|
|
|
Dict,
|
2021-09-20 08:56:23 -04:00
|
|
|
Generator,
|
2021-04-06 09:38:30 -04:00
|
|
|
Iterable,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Set,
|
|
|
|
Tuple,
|
2021-09-20 08:56:23 -04:00
|
|
|
Type,
|
2021-04-06 09:38:30 -04:00
|
|
|
)
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from prometheus_client import Counter
|
|
|
|
|
2019-03-26 08:45:22 -04:00
|
|
|
import synapse.metrics
|
2022-05-27 07:14:36 -04:00
|
|
|
from synapse.api.constants import EduTypes, EventTypes, Membership, PresenceState
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.api.errors import SynapseError
|
2023-09-05 09:58:51 -04:00
|
|
|
from synapse.api.presence import UserDevicePresenceState, UserPresenceState
|
2021-10-27 12:27:23 -04:00
|
|
|
from synapse.appservice import ApplicationService
|
2021-04-06 09:38:30 -04:00
|
|
|
from synapse.events.presence_router import PresenceRouter
|
2019-07-03 10:07:04 -04:00
|
|
|
from synapse.logging.context import run_in_background
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.metrics import LaterGauge
|
2023-08-10 07:39:46 -04:00
|
|
|
from synapse.metrics.background_process_metrics import (
|
|
|
|
run_as_background_process,
|
|
|
|
wrap_as_background_process,
|
|
|
|
)
|
2021-04-14 12:06:06 -04:00
|
|
|
from synapse.replication.http.presence import (
|
|
|
|
ReplicationBumpPresenceActiveTime,
|
|
|
|
ReplicationPresenceSetState,
|
|
|
|
)
|
2021-04-20 09:11:24 -04:00
|
|
|
from synapse.replication.http.streams import ReplicationGetStreamUpdates
|
2021-04-14 12:06:06 -04:00
|
|
|
from synapse.replication.tcp.commands import ClearUserSyncsCommand
|
2021-04-20 09:11:24 -04:00
|
|
|
from synapse.replication.tcp.streams import PresenceFederationStream, PresenceStream
|
2020-08-05 16:38:57 -04:00
|
|
|
from synapse.storage.databases.main import DataStore
|
2021-09-21 13:34:26 -04:00
|
|
|
from synapse.streams import EventSource
|
2023-01-25 16:34:37 -05:00
|
|
|
from synapse.types import (
|
|
|
|
JsonDict,
|
|
|
|
StrCollection,
|
|
|
|
StreamKeyType,
|
|
|
|
UserID,
|
|
|
|
get_domain_from_id,
|
|
|
|
)
|
2018-08-10 09:50:21 -04:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2016-02-19 06:50:48 -05:00
|
|
|
from synapse.util.metrics import Measure
|
2016-02-15 12:10:40 -05:00
|
|
|
from synapse.util.wheel_timer import WheelTimer
|
2018-05-21 20:47:37 -04:00
|
|
|
|
2020-11-17 09:09:40 -05:00
|
|
|
if TYPE_CHECKING:
|
2020-10-29 11:18:17 -04:00
|
|
|
from synapse.server import HomeServer
|
2020-02-26 10:33:26 -05:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-03-04 11:50:23 -05:00
|
|
|
|
2018-05-21 20:47:37 -04:00
|
|
|
notified_presence_counter = Counter("synapse_handler_presence_notified_presence", "")
|
2018-05-22 18:32:57 -04:00
|
|
|
federation_presence_out_counter = Counter(
|
|
|
|
"synapse_handler_presence_federation_presence_out", ""
|
|
|
|
)
|
2018-05-21 20:47:37 -04:00
|
|
|
presence_updates_counter = Counter("synapse_handler_presence_presence_updates", "")
|
|
|
|
timers_fired_counter = Counter("synapse_handler_presence_timers_fired", "")
|
|
|
|
federation_presence_counter = Counter(
|
|
|
|
"synapse_handler_presence_federation_presence", ""
|
|
|
|
)
|
|
|
|
bump_active_time_counter = Counter("synapse_handler_presence_bump_active_time", "")
|
2016-02-19 04:50:54 -05:00
|
|
|
|
2018-05-21 20:47:37 -04:00
|
|
|
get_updates_counter = Counter("synapse_handler_presence_get_updates", "", ["type"])
|
2016-06-03 08:40:55 -04:00
|
|
|
|
2018-05-22 18:32:57 -04:00
|
|
|
notify_reason_counter = Counter(
|
2023-07-18 05:36:40 -04:00
|
|
|
"synapse_handler_presence_notify_reason", "", ["locality", "reason"]
|
2018-05-22 18:32:57 -04:00
|
|
|
)
|
|
|
|
state_transition_counter = Counter(
|
2023-07-18 05:36:40 -04:00
|
|
|
"synapse_handler_presence_state_transition", "", ["locality", "from", "to"]
|
2016-09-06 06:31:01 -04:00
|
|
|
)
|
2016-09-05 09:12:11 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
# If a user was last active in the last LAST_ACTIVE_GRANULARITY, consider them
|
|
|
|
# "currently_active"
|
2016-02-02 12:18:50 -05:00
|
|
|
LAST_ACTIVE_GRANULARITY = 60 * 1000
|
2015-03-23 13:25:44 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
# How long to wait until a new /events or /sync request before assuming
|
|
|
|
# the client has gone.
|
|
|
|
SYNC_ONLINE_TIMEOUT = 30 * 1000
|
2023-09-05 10:39:38 -04:00
|
|
|
# Busy status waits longer, but does eventually go offline.
|
|
|
|
BUSY_ONLINE_TIMEOUT = 60 * 60 * 1000
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
# How long to wait before marking the user as idle. Compared against last active
|
|
|
|
IDLE_TIMER = 5 * 60 * 1000
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
# How often we expect remote servers to resend us presence.
|
|
|
|
FEDERATION_TIMEOUT = 30 * 60 * 1000
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
# How often to resend presence to remote servers
|
|
|
|
FEDERATION_PING_INTERVAL = 25 * 60 * 1000
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-06-02 10:20:15 -04:00
|
|
|
# How long we will wait before assuming that the syncs from an external process
|
|
|
|
# are dead.
|
|
|
|
EXTERNAL_PROCESS_EXPIRY = 5 * 60 * 1000
|
|
|
|
|
2021-04-14 12:06:06 -04:00
|
|
|
# Delay before a worker tells the presence handler that a user has stopped
|
|
|
|
# syncing.
|
|
|
|
UPDATE_SYNCING_USERS_MS = 10 * 1000
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
assert LAST_ACTIVE_GRANULARITY < IDLE_TIMER
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
class BasePresenceHandler(abc.ABC):
|
2021-04-23 07:21:55 -04:00
|
|
|
"""Parts of the PresenceHandler that are shared between workers and presence
|
|
|
|
writer"""
|
2020-04-22 17:39:04 -04:00
|
|
|
|
2020-10-29 11:18:17 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2023-09-06 03:30:53 -04:00
|
|
|
self.hs = hs
|
2020-04-22 17:39:04 -04:00
|
|
|
self.clock = hs.get_clock()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2022-06-01 11:02:53 -04:00
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
2021-04-19 05:50:49 -04:00
|
|
|
self.presence_router = hs.get_presence_router()
|
|
|
|
self.state = hs.get_state_handler()
|
2021-04-20 08:37:54 -04:00
|
|
|
self.is_mine_id = hs.is_mine_id
|
2021-04-19 05:50:49 -04:00
|
|
|
|
2023-08-10 07:39:46 -04:00
|
|
|
self._presence_enabled = hs.config.server.use_presence
|
|
|
|
|
2021-04-19 05:50:49 -04:00
|
|
|
self._federation = None
|
2021-04-20 09:11:24 -04:00
|
|
|
if hs.should_send_federation():
|
2021-04-19 05:50:49 -04:00
|
|
|
self._federation = hs.get_federation_sender()
|
|
|
|
|
2021-04-20 09:11:24 -04:00
|
|
|
self._federation_queue = PresenceFederationQueue(hs, self)
|
2020-04-22 17:39:04 -04:00
|
|
|
|
2023-08-10 07:39:46 -04:00
|
|
|
self.VALID_PRESENCE: Tuple[str, ...] = (
|
|
|
|
PresenceState.ONLINE,
|
|
|
|
PresenceState.UNAVAILABLE,
|
|
|
|
PresenceState.OFFLINE,
|
|
|
|
)
|
|
|
|
|
2023-08-28 11:03:23 -04:00
|
|
|
if hs.config.experimental.msc3026_enabled:
|
2023-08-10 07:39:46 -04:00
|
|
|
self.VALID_PRESENCE += (PresenceState.BUSY,)
|
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
active_presence = self.store.take_presence_startup_info()
|
2023-09-05 09:58:51 -04:00
|
|
|
# The combined status across all user devices.
|
2020-04-22 17:39:04 -04:00
|
|
|
self.user_to_current_state = {state.user_id: state for state in active_presence}
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
async def user_syncing(
|
2023-08-28 13:08:49 -04:00
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
device_id: Optional[str],
|
|
|
|
affect_presence: bool,
|
|
|
|
presence_state: str,
|
2020-04-22 17:39:04 -04:00
|
|
|
) -> ContextManager[None]:
|
|
|
|
"""Returns a context manager that should surround any stream requests
|
|
|
|
from the user.
|
|
|
|
|
|
|
|
This allows us to keep track of who is currently streaming and who isn't
|
|
|
|
without having to have timers outside of this module to avoid flickering
|
|
|
|
when users disconnect/reconnect.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: the user that is starting a sync
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id: the user's device that is starting a sync
|
2020-04-22 17:39:04 -04:00
|
|
|
affect_presence: If false this function will be a no-op.
|
|
|
|
Useful for streams that are not associated with an actual
|
|
|
|
client that is being used by a user.
|
2022-04-13 11:21:07 -04:00
|
|
|
presence_state: The presence state indicated in the sync request
|
2020-04-22 17:39:04 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2023-08-29 11:44:07 -04:00
|
|
|
def get_currently_syncing_users_for_replication(
|
|
|
|
self,
|
|
|
|
) -> Iterable[Tuple[str, Optional[str]]]:
|
|
|
|
"""Get an iterable of syncing users and devices on this worker, to send to the presence handler
|
2020-04-22 17:39:04 -04:00
|
|
|
|
|
|
|
This is called when a replication connection is established. It should return
|
2023-08-29 11:44:07 -04:00
|
|
|
a list of tuples of user ID & device ID, which are then sent as USER_SYNC commands
|
|
|
|
to inform the process handling presence about those users/devices.
|
2020-04-22 17:39:04 -04:00
|
|
|
|
|
|
|
Returns:
|
2023-08-29 11:44:07 -04:00
|
|
|
An iterable of tuples of user ID and device ID.
|
2020-04-22 17:39:04 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
async def get_state(self, target_user: UserID) -> UserPresenceState:
|
|
|
|
results = await self.get_states([target_user.to_string()])
|
|
|
|
return results[0]
|
|
|
|
|
|
|
|
async def get_states(
|
|
|
|
self, target_user_ids: Iterable[str]
|
|
|
|
) -> List[UserPresenceState]:
|
|
|
|
"""Get the presence state for users."""
|
|
|
|
|
|
|
|
updates_d = await self.current_state_for_users(target_user_ids)
|
|
|
|
updates = list(updates_d.values())
|
|
|
|
|
|
|
|
for user_id in set(target_user_ids) - {u.user_id for u in updates}:
|
|
|
|
updates.append(UserPresenceState.default(user_id))
|
|
|
|
|
|
|
|
return updates
|
|
|
|
|
|
|
|
async def current_state_for_users(
|
|
|
|
self, user_ids: Iterable[str]
|
|
|
|
) -> Dict[str, UserPresenceState]:
|
|
|
|
"""Get the current presence state for multiple users.
|
|
|
|
|
|
|
|
Returns:
|
2022-11-16 10:25:24 -05:00
|
|
|
A mapping of `user_id` -> `UserPresenceState`
|
2020-04-22 17:39:04 -04:00
|
|
|
"""
|
2022-02-21 11:03:06 -05:00
|
|
|
states = {}
|
|
|
|
missing = []
|
|
|
|
for user_id in user_ids:
|
|
|
|
state = self.user_to_current_state.get(user_id, None)
|
|
|
|
if state:
|
|
|
|
states[user_id] = state
|
|
|
|
else:
|
|
|
|
missing.append(user_id)
|
2020-04-22 17:39:04 -04:00
|
|
|
|
|
|
|
if missing:
|
|
|
|
# There are things not in our in memory cache. Lets pull them out of
|
|
|
|
# the database.
|
|
|
|
res = await self.store.get_presence_for_users(missing)
|
|
|
|
states.update(res)
|
|
|
|
|
2022-02-21 11:03:06 -05:00
|
|
|
for user_id in missing:
|
|
|
|
# if user has no state in database, create the state
|
|
|
|
if not res.get(user_id, None):
|
|
|
|
new_state = UserPresenceState.default(user_id)
|
|
|
|
states[user_id] = new_state
|
|
|
|
self.user_to_current_state[user_id] = new_state
|
2020-04-22 17:39:04 -04:00
|
|
|
|
|
|
|
return states
|
|
|
|
|
2022-04-13 11:21:07 -04:00
|
|
|
async def current_state_for_user(self, user_id: str) -> UserPresenceState:
|
|
|
|
"""Get the current presence state for a user."""
|
|
|
|
res = await self.current_state_for_users([user_id])
|
|
|
|
return res[user_id]
|
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
@abc.abstractmethod
|
|
|
|
async def set_state(
|
2021-05-18 09:13:45 -04:00
|
|
|
self,
|
|
|
|
target_user: UserID,
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id: Optional[str],
|
2021-05-18 09:13:45 -04:00
|
|
|
state: JsonDict,
|
|
|
|
force_notify: bool = False,
|
2023-08-28 11:03:23 -04:00
|
|
|
is_sync: bool = False,
|
2020-04-22 17:39:04 -04:00
|
|
|
) -> None:
|
2021-05-18 09:13:45 -04:00
|
|
|
"""Set the presence state of the user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_user: The ID of the user to set the presence state of.
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id: the device that the user is setting the presence state of.
|
2021-05-18 09:13:45 -04:00
|
|
|
state: The presence state as a JSON dictionary.
|
|
|
|
force_notify: Whether to force notification of the update to clients.
|
2023-08-28 11:03:23 -04:00
|
|
|
is_sync: True if this update was from a sync, which results in
|
|
|
|
*not* overriding a previously set BUSY status, updating the
|
|
|
|
user's last_user_sync_ts, and ignoring the "status_msg" field of
|
|
|
|
the `state` dict.
|
2021-05-18 09:13:45 -04:00
|
|
|
"""
|
2020-04-22 17:39:04 -04:00
|
|
|
|
2020-05-22 11:11:35 -04:00
|
|
|
@abc.abstractmethod
|
2023-08-28 13:08:49 -04:00
|
|
|
async def bump_presence_active_time(
|
|
|
|
self, user: UserID, device_id: Optional[str]
|
|
|
|
) -> None:
|
2020-05-22 11:11:35 -04:00
|
|
|
"""We've seen the user do something that indicates they're interacting
|
|
|
|
with the app.
|
|
|
|
"""
|
|
|
|
|
2022-11-04 14:43:14 -04:00
|
|
|
async def update_external_syncs_row( # noqa: B027 (no-op by design)
|
2023-08-29 11:44:07 -04:00
|
|
|
self,
|
|
|
|
process_id: str,
|
|
|
|
user_id: str,
|
|
|
|
device_id: Optional[str],
|
|
|
|
is_syncing: bool,
|
|
|
|
sync_time_msec: int,
|
2021-04-28 06:07:47 -04:00
|
|
|
) -> None:
|
2021-04-14 12:06:06 -04:00
|
|
|
"""Update the syncing users for an external process as a delta.
|
|
|
|
|
|
|
|
This is a no-op when presence is handled by a different worker.
|
|
|
|
|
|
|
|
Args:
|
2021-04-28 06:07:47 -04:00
|
|
|
process_id: An identifier for the process the users are
|
2021-04-14 12:06:06 -04:00
|
|
|
syncing against. This allows synapse to process updates
|
|
|
|
as user start and stop syncing against a given process.
|
2021-04-28 06:07:47 -04:00
|
|
|
user_id: The user who has started or stopped syncing
|
2023-08-29 11:44:07 -04:00
|
|
|
device_id: The user's device that has started or stopped syncing
|
2021-04-28 06:07:47 -04:00
|
|
|
is_syncing: Whether or not the user is now syncing
|
|
|
|
sync_time_msec: Time in ms when the user was last syncing
|
2021-04-14 12:06:06 -04:00
|
|
|
"""
|
|
|
|
|
2022-11-04 14:43:14 -04:00
|
|
|
async def update_external_syncs_clear( # noqa: B027 (no-op by design)
|
|
|
|
self, process_id: str
|
|
|
|
) -> None:
|
2021-04-14 12:06:06 -04:00
|
|
|
"""Marks all users that had been marked as syncing by a given process
|
|
|
|
as offline.
|
|
|
|
|
|
|
|
Used when the process has stopped/disappeared.
|
|
|
|
|
|
|
|
This is a no-op when presence is handled by a different worker.
|
|
|
|
"""
|
|
|
|
|
2021-04-20 09:11:24 -04:00
|
|
|
async def process_replication_rows(
|
|
|
|
self, stream_name: str, instance_name: str, token: int, rows: list
|
2021-09-20 08:56:23 -04:00
|
|
|
) -> None:
|
2021-04-20 09:11:24 -04:00
|
|
|
"""Process streams received over replication."""
|
|
|
|
await self._federation_queue.process_replication_rows(
|
|
|
|
stream_name, instance_name, token, rows
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_federation_queue(self) -> "PresenceFederationQueue":
|
|
|
|
"""Get the presence federation queue."""
|
|
|
|
return self._federation_queue
|
2021-04-14 12:06:06 -04:00
|
|
|
|
2021-04-19 05:50:49 -04:00
|
|
|
async def maybe_send_presence_to_interested_destinations(
|
|
|
|
self, states: List[UserPresenceState]
|
2021-09-20 08:56:23 -04:00
|
|
|
) -> None:
|
2021-04-19 05:50:49 -04:00
|
|
|
"""If this instance is a federation sender, send the states to all
|
2021-04-20 08:37:54 -04:00
|
|
|
destinations that are interested. Filters out any states for remote
|
|
|
|
users.
|
2021-04-19 05:50:49 -04:00
|
|
|
"""
|
|
|
|
|
2021-04-20 09:11:24 -04:00
|
|
|
if not self._federation:
|
2021-04-19 05:50:49 -04:00
|
|
|
return
|
|
|
|
|
2021-04-20 08:37:54 -04:00
|
|
|
states = [s for s in states if self.is_mine_id(s.user_id)]
|
|
|
|
|
|
|
|
if not states:
|
|
|
|
return
|
|
|
|
|
2021-06-11 10:19:42 -04:00
|
|
|
hosts_to_states = await get_interested_remotes(
|
2021-04-19 05:50:49 -04:00
|
|
|
self.store,
|
|
|
|
self.presence_router,
|
|
|
|
states,
|
|
|
|
)
|
|
|
|
|
2021-06-11 10:19:42 -04:00
|
|
|
for destination, host_states in hosts_to_states.items():
|
2023-09-04 12:14:09 -04:00
|
|
|
await self._federation.send_presence_to_destinations(
|
|
|
|
host_states, [destination]
|
|
|
|
)
|
2021-04-19 05:50:49 -04:00
|
|
|
|
2023-01-25 16:34:37 -05:00
|
|
|
async def send_full_presence_to_users(self, user_ids: StrCollection) -> None:
|
2021-05-18 09:13:45 -04:00
|
|
|
"""
|
|
|
|
Adds to the list of users who should receive a full snapshot of presence
|
|
|
|
upon their next sync. Note that this only works for local users.
|
|
|
|
|
|
|
|
Then, grabs the current presence state for a given set of users and adds it
|
|
|
|
to the top of the presence stream.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_ids: The IDs of the local users to send full presence to.
|
|
|
|
"""
|
|
|
|
# Retrieve one of the users from the given set
|
|
|
|
if not user_ids:
|
|
|
|
raise Exception(
|
|
|
|
"send_full_presence_to_users must be called with at least one user"
|
|
|
|
)
|
|
|
|
user_id = next(iter(user_ids))
|
|
|
|
|
|
|
|
# Mark all users as receiving full presence on their next sync
|
|
|
|
await self.store.add_users_to_send_full_presence_to(user_ids)
|
|
|
|
|
|
|
|
# Add a new entry to the presence stream. Since we use stream tokens to determine whether a
|
|
|
|
# local user should receive a full snapshot of presence when they sync, we need to bump the
|
|
|
|
# presence stream so that subsequent syncs with no presence activity in between won't result
|
|
|
|
# in the client receiving multiple full snapshots of presence.
|
|
|
|
#
|
|
|
|
# If we bump the stream ID, then the user will get a higher stream token next sync, and thus
|
|
|
|
# correctly won't receive a second snapshot.
|
|
|
|
|
|
|
|
# Get the current presence state for one of the users (defaults to offline if not found)
|
|
|
|
current_presence_state = await self.get_state(UserID.from_string(user_id))
|
|
|
|
|
|
|
|
# Convert the UserPresenceState object into a serializable dict
|
|
|
|
state = {
|
|
|
|
"presence": current_presence_state.state,
|
|
|
|
"status_message": current_presence_state.status_msg,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Copy the presence state to the tip of the presence stream.
|
|
|
|
|
|
|
|
# We set force_notify=True here so that this presence update is guaranteed to
|
|
|
|
# increment the presence stream ID (which resending the current user's presence
|
|
|
|
# otherwise would not do).
|
2023-08-28 13:08:49 -04:00
|
|
|
await self.set_state(
|
|
|
|
UserID.from_string(user_id), None, state, force_notify=True
|
|
|
|
)
|
2021-05-18 09:13:45 -04:00
|
|
|
|
2021-08-26 07:53:52 -04:00
|
|
|
async def is_visible(self, observed_user: UserID, observer_user: UserID) -> bool:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"Attempting to check presence on a non-presence worker."
|
|
|
|
)
|
|
|
|
|
2021-04-14 12:06:06 -04:00
|
|
|
|
|
|
|
class _NullContextManager(ContextManager[None]):
|
|
|
|
"""A context manager which does nothing."""
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
def __exit__(
|
|
|
|
self,
|
|
|
|
exc_type: Optional[Type[BaseException]],
|
|
|
|
exc_val: Optional[BaseException],
|
|
|
|
exc_tb: Optional[TracebackType],
|
|
|
|
) -> None:
|
2021-04-14 12:06:06 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class WorkerPresenceHandler(BasePresenceHandler):
|
2021-04-28 06:07:47 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-04-14 12:06:06 -04:00
|
|
|
super().__init__(hs)
|
2021-04-23 07:21:55 -04:00
|
|
|
self._presence_writer_instance = hs.config.worker.writers.presence[0]
|
|
|
|
|
|
|
|
# Route presence EDUs to the right worker
|
|
|
|
hs.get_federation_registry().register_instances_for_edu(
|
2022-05-27 07:14:36 -04:00
|
|
|
EduTypes.PRESENCE,
|
2021-04-23 07:21:55 -04:00
|
|
|
hs.config.worker.writers.presence,
|
|
|
|
)
|
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
# The number of ongoing syncs on this process, by (user ID, device ID).
|
2021-04-14 12:06:06 -04:00
|
|
|
# Empty if _presence_enabled is false.
|
2023-08-29 11:44:07 -04:00
|
|
|
self._user_device_to_num_current_syncs: Dict[
|
|
|
|
Tuple[str, Optional[str]], int
|
|
|
|
] = {}
|
2021-04-14 12:06:06 -04:00
|
|
|
|
|
|
|
self.notifier = hs.get_notifier()
|
|
|
|
self.instance_id = hs.get_instance_id()
|
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
# (user_id, device_id) -> last_sync_ms. Lists the devices that have stopped
|
|
|
|
# syncing but we haven't notified the presence writer of that yet
|
|
|
|
self._user_devices_going_offline: Dict[Tuple[str, Optional[str]], int] = {}
|
2021-04-14 12:06:06 -04:00
|
|
|
|
|
|
|
self._bump_active_client = ReplicationBumpPresenceActiveTime.make_client(hs)
|
|
|
|
self._set_state_client = ReplicationPresenceSetState.make_client(hs)
|
|
|
|
|
|
|
|
self._send_stop_syncing_loop = self.clock.looping_call(
|
|
|
|
self.send_stop_syncing, UPDATE_SYNCING_USERS_MS
|
|
|
|
)
|
|
|
|
|
|
|
|
hs.get_reactor().addSystemEventTrigger(
|
|
|
|
"before",
|
|
|
|
"shutdown",
|
|
|
|
run_as_background_process,
|
|
|
|
"generic_presence.on_shutdown",
|
|
|
|
self._on_shutdown,
|
|
|
|
)
|
|
|
|
|
2021-12-07 07:34:38 -05:00
|
|
|
async def _on_shutdown(self) -> None:
|
2021-04-14 12:06:06 -04:00
|
|
|
if self._presence_enabled:
|
2022-03-10 08:01:56 -05:00
|
|
|
self.hs.get_replication_command_handler().send_command(
|
2021-04-14 12:06:06 -04:00
|
|
|
ClearUserSyncsCommand(self.instance_id)
|
|
|
|
)
|
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
def send_user_sync(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
device_id: Optional[str],
|
|
|
|
is_syncing: bool,
|
|
|
|
last_sync_ms: int,
|
|
|
|
) -> None:
|
2021-04-14 12:06:06 -04:00
|
|
|
if self._presence_enabled:
|
2022-03-10 08:01:56 -05:00
|
|
|
self.hs.get_replication_command_handler().send_user_sync(
|
2023-08-29 11:44:07 -04:00
|
|
|
self.instance_id, user_id, device_id, is_syncing, last_sync_ms
|
2021-04-14 12:06:06 -04:00
|
|
|
)
|
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
def mark_as_coming_online(self, user_id: str, device_id: Optional[str]) -> None:
|
2021-04-23 07:21:55 -04:00
|
|
|
"""A user has started syncing. Send a UserSync to the presence writer,
|
|
|
|
unless they had recently stopped syncing.
|
2021-04-14 12:06:06 -04:00
|
|
|
"""
|
2023-08-29 11:44:07 -04:00
|
|
|
going_offline = self._user_devices_going_offline.pop((user_id, device_id), None)
|
2021-04-14 12:06:06 -04:00
|
|
|
if not going_offline:
|
2021-04-23 07:21:55 -04:00
|
|
|
# Safe to skip because we haven't yet told the presence writer they
|
|
|
|
# were offline
|
2023-08-29 11:44:07 -04:00
|
|
|
self.send_user_sync(user_id, device_id, True, self.clock.time_msec())
|
2021-04-14 12:06:06 -04:00
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
def mark_as_going_offline(self, user_id: str, device_id: Optional[str]) -> None:
|
2021-04-23 07:21:55 -04:00
|
|
|
"""A user has stopped syncing. We wait before notifying the presence
|
|
|
|
writer as its likely they'll come back soon. This allows us to avoid
|
|
|
|
sending a stopped syncing immediately followed by a started syncing
|
|
|
|
notification to the presence writer
|
2021-04-14 12:06:06 -04:00
|
|
|
"""
|
2023-08-29 11:44:07 -04:00
|
|
|
self._user_devices_going_offline[(user_id, device_id)] = self.clock.time_msec()
|
2021-04-14 12:06:06 -04:00
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
def send_stop_syncing(self) -> None:
|
2021-04-23 07:21:55 -04:00
|
|
|
"""Check if there are any users who have stopped syncing a while ago and
|
|
|
|
haven't come back yet. If there are poke the presence writer about them.
|
2021-04-14 12:06:06 -04:00
|
|
|
"""
|
|
|
|
now = self.clock.time_msec()
|
2023-08-29 11:44:07 -04:00
|
|
|
for (user_id, device_id), last_sync_ms in list(
|
|
|
|
self._user_devices_going_offline.items()
|
|
|
|
):
|
2021-04-14 12:06:06 -04:00
|
|
|
if now - last_sync_ms > UPDATE_SYNCING_USERS_MS:
|
2023-08-29 11:44:07 -04:00
|
|
|
self._user_devices_going_offline.pop((user_id, device_id), None)
|
|
|
|
self.send_user_sync(user_id, device_id, False, last_sync_ms)
|
2021-04-14 12:06:06 -04:00
|
|
|
|
|
|
|
async def user_syncing(
|
2023-08-28 13:08:49 -04:00
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
device_id: Optional[str],
|
|
|
|
affect_presence: bool,
|
|
|
|
presence_state: str,
|
2021-04-14 12:06:06 -04:00
|
|
|
) -> ContextManager[None]:
|
|
|
|
"""Record that a user is syncing.
|
|
|
|
|
|
|
|
Called by the sync and events servlets to record that a user has connected to
|
|
|
|
this worker and is waiting for some events.
|
|
|
|
"""
|
|
|
|
if not affect_presence or not self._presence_enabled:
|
|
|
|
return _NullContextManager()
|
|
|
|
|
2023-08-28 11:03:23 -04:00
|
|
|
# Note that this causes last_active_ts to be incremented which is not
|
|
|
|
# what the spec wants.
|
|
|
|
await self.set_state(
|
|
|
|
UserID.from_string(user_id),
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id,
|
2023-08-28 11:03:23 -04:00
|
|
|
state={"presence": presence_state},
|
|
|
|
is_sync=True,
|
|
|
|
)
|
2022-04-13 11:21:07 -04:00
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
curr_sync = self._user_device_to_num_current_syncs.get((user_id, device_id), 0)
|
|
|
|
self._user_device_to_num_current_syncs[(user_id, device_id)] = curr_sync + 1
|
2021-04-14 12:06:06 -04:00
|
|
|
|
2023-08-28 11:03:23 -04:00
|
|
|
# If this is the first in-flight sync, notify replication
|
2023-08-29 11:44:07 -04:00
|
|
|
if self._user_device_to_num_current_syncs[(user_id, device_id)] == 1:
|
|
|
|
self.mark_as_coming_online(user_id, device_id)
|
2021-04-14 12:06:06 -04:00
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
def _end() -> None:
|
2021-04-14 12:06:06 -04:00
|
|
|
# We check that the user_id is in user_to_num_current_syncs because
|
|
|
|
# user_to_num_current_syncs may have been cleared if we are
|
|
|
|
# shutting down.
|
2023-08-29 11:44:07 -04:00
|
|
|
if (user_id, device_id) in self._user_device_to_num_current_syncs:
|
|
|
|
self._user_device_to_num_current_syncs[(user_id, device_id)] -= 1
|
2021-04-14 12:06:06 -04:00
|
|
|
|
2023-08-28 11:03:23 -04:00
|
|
|
# If there are no more in-flight syncs, notify replication
|
2023-08-29 11:44:07 -04:00
|
|
|
if self._user_device_to_num_current_syncs[(user_id, device_id)] == 0:
|
|
|
|
self.mark_as_going_offline(user_id, device_id)
|
2021-04-14 12:06:06 -04:00
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2021-09-20 08:56:23 -04:00
|
|
|
def _user_syncing() -> Generator[None, None, None]:
|
2021-04-14 12:06:06 -04:00
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
_end()
|
|
|
|
|
|
|
|
return _user_syncing()
|
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
async def notify_from_replication(
|
|
|
|
self, states: List[UserPresenceState], stream_id: int
|
|
|
|
) -> None:
|
2021-04-14 12:06:06 -04:00
|
|
|
parties = await get_interested_parties(self.store, self.presence_router, states)
|
|
|
|
room_ids_to_states, users_to_states = parties
|
|
|
|
|
|
|
|
self.notifier.on_new_event(
|
2022-05-16 11:35:31 -04:00
|
|
|
StreamKeyType.PRESENCE,
|
2021-04-14 12:06:06 -04:00
|
|
|
stream_id,
|
|
|
|
rooms=room_ids_to_states.keys(),
|
|
|
|
users=users_to_states.keys(),
|
|
|
|
)
|
|
|
|
|
2021-04-20 09:11:24 -04:00
|
|
|
async def process_replication_rows(
|
|
|
|
self, stream_name: str, instance_name: str, token: int, rows: list
|
2021-09-20 08:56:23 -04:00
|
|
|
) -> None:
|
2021-04-20 09:11:24 -04:00
|
|
|
await super().process_replication_rows(stream_name, instance_name, token, rows)
|
|
|
|
|
|
|
|
if stream_name != PresenceStream.NAME:
|
|
|
|
return
|
|
|
|
|
2021-04-14 12:06:06 -04:00
|
|
|
states = [
|
|
|
|
UserPresenceState(
|
|
|
|
row.user_id,
|
|
|
|
row.state,
|
|
|
|
row.last_active_ts,
|
|
|
|
row.last_federation_update_ts,
|
|
|
|
row.last_user_sync_ts,
|
|
|
|
row.status_msg,
|
|
|
|
row.currently_active,
|
|
|
|
)
|
|
|
|
for row in rows
|
|
|
|
]
|
|
|
|
|
2021-06-11 08:08:30 -04:00
|
|
|
# The list of states to notify sync streams and remote servers about.
|
|
|
|
# This is calculated by comparing the old and new states for each user
|
|
|
|
# using `should_notify(..)`.
|
|
|
|
#
|
|
|
|
# Note that this is necessary as the presence writer will periodically
|
|
|
|
# flush presence state changes that should not be notified about to the
|
|
|
|
# DB, and so will be sent over the replication stream.
|
|
|
|
state_to_notify = []
|
|
|
|
|
|
|
|
for new_state in states:
|
|
|
|
old_state = self.user_to_current_state.get(new_state.user_id)
|
|
|
|
self.user_to_current_state[new_state.user_id] = new_state
|
2023-07-18 05:36:40 -04:00
|
|
|
is_mine = self.is_mine_id(new_state.user_id)
|
|
|
|
if not old_state or should_notify(old_state, new_state, is_mine):
|
2021-06-11 08:08:30 -04:00
|
|
|
state_to_notify.append(new_state)
|
2021-04-14 12:06:06 -04:00
|
|
|
|
|
|
|
stream_id = token
|
2021-06-11 08:08:30 -04:00
|
|
|
await self.notify_from_replication(state_to_notify, stream_id)
|
|
|
|
|
|
|
|
# If this is a federation sender, notify about presence updates.
|
|
|
|
await self.maybe_send_presence_to_interested_destinations(state_to_notify)
|
2021-04-14 12:06:06 -04:00
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
def get_currently_syncing_users_for_replication(
|
|
|
|
self,
|
|
|
|
) -> Iterable[Tuple[str, Optional[str]]]:
|
2021-04-14 12:06:06 -04:00
|
|
|
return [
|
2023-08-29 11:44:07 -04:00
|
|
|
user_id_device_id
|
|
|
|
for user_id_device_id, count in self._user_device_to_num_current_syncs.items()
|
2021-04-14 12:06:06 -04:00
|
|
|
if count > 0
|
|
|
|
]
|
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
async def set_state(
|
|
|
|
self,
|
|
|
|
target_user: UserID,
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id: Optional[str],
|
2021-04-28 06:07:47 -04:00
|
|
|
state: JsonDict,
|
2021-05-18 09:13:45 -04:00
|
|
|
force_notify: bool = False,
|
2023-08-28 11:03:23 -04:00
|
|
|
is_sync: bool = False,
|
2021-04-28 06:07:47 -04:00
|
|
|
) -> None:
|
2021-05-18 09:13:45 -04:00
|
|
|
"""Set the presence state of the user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_user: The ID of the user to set the presence state of.
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id: the device that the user is setting the presence state of.
|
2021-05-18 09:13:45 -04:00
|
|
|
state: The presence state as a JSON dictionary.
|
|
|
|
force_notify: Whether to force notification of the update to clients.
|
2023-08-28 11:03:23 -04:00
|
|
|
is_sync: True if this update was from a sync, which results in
|
|
|
|
*not* overriding a previously set BUSY status, updating the
|
|
|
|
user's last_user_sync_ts, and ignoring the "status_msg" field of
|
|
|
|
the `state` dict.
|
2021-05-18 09:13:45 -04:00
|
|
|
"""
|
2021-04-14 12:06:06 -04:00
|
|
|
presence = state["presence"]
|
|
|
|
|
2023-08-10 07:39:46 -04:00
|
|
|
if presence not in self.VALID_PRESENCE:
|
2021-04-14 12:06:06 -04:00
|
|
|
raise SynapseError(400, "Invalid presence state")
|
|
|
|
|
|
|
|
user_id = target_user.to_string()
|
|
|
|
|
|
|
|
# If presence is disabled, no-op
|
2023-08-10 07:39:46 -04:00
|
|
|
if not self._presence_enabled:
|
2021-04-14 12:06:06 -04:00
|
|
|
return
|
|
|
|
|
2021-04-23 07:21:55 -04:00
|
|
|
# Proxy request to instance that writes presence
|
2021-04-14 12:06:06 -04:00
|
|
|
await self._set_state_client(
|
2021-04-23 07:21:55 -04:00
|
|
|
instance_name=self._presence_writer_instance,
|
|
|
|
user_id=user_id,
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id=device_id,
|
2021-04-23 07:21:55 -04:00
|
|
|
state=state,
|
2021-05-18 09:13:45 -04:00
|
|
|
force_notify=force_notify,
|
2023-08-28 11:03:23 -04:00
|
|
|
is_sync=is_sync,
|
2021-04-14 12:06:06 -04:00
|
|
|
)
|
|
|
|
|
2023-08-28 13:08:49 -04:00
|
|
|
async def bump_presence_active_time(
|
|
|
|
self, user: UserID, device_id: Optional[str]
|
|
|
|
) -> None:
|
2021-04-14 12:06:06 -04:00
|
|
|
"""We've seen the user do something that indicates they're interacting
|
|
|
|
with the app.
|
|
|
|
"""
|
|
|
|
# If presence is disabled, no-op
|
2023-08-10 07:39:46 -04:00
|
|
|
if not self._presence_enabled:
|
2021-04-14 12:06:06 -04:00
|
|
|
return
|
|
|
|
|
2021-04-23 07:21:55 -04:00
|
|
|
# Proxy request to instance that writes presence
|
2021-04-14 12:06:06 -04:00
|
|
|
user_id = user.to_string()
|
2021-04-23 07:21:55 -04:00
|
|
|
await self._bump_active_client(
|
2023-08-28 13:08:49 -04:00
|
|
|
instance_name=self._presence_writer_instance,
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
2021-04-23 07:21:55 -04:00
|
|
|
)
|
2021-04-14 12:06:06 -04:00
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
|
|
|
|
class PresenceHandler(BasePresenceHandler):
|
2020-10-29 11:18:17 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-04-22 17:39:04 -04:00
|
|
|
super().__init__(hs)
|
2021-09-10 12:03:18 -04:00
|
|
|
self.wheel_timer: WheelTimer[str] = WheelTimer()
|
2016-02-15 12:10:40 -05:00
|
|
|
self.notifier = hs.get_notifier()
|
2016-08-26 09:54:30 -04:00
|
|
|
|
2018-03-12 12:17:08 -04:00
|
|
|
federation_registry = hs.get_federation_registry()
|
|
|
|
|
2022-05-27 07:14:36 -04:00
|
|
|
federation_registry.register_edu_handler(
|
|
|
|
EduTypes.PRESENCE, self.incoming_presence
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2018-05-21 20:47:37 -04:00
|
|
|
LaterGauge(
|
2018-05-22 11:56:03 -04:00
|
|
|
"synapse_handlers_presence_user_to_current_state_size",
|
|
|
|
"",
|
|
|
|
[],
|
|
|
|
lambda: len(self.user_to_current_state),
|
|
|
|
)
|
2016-03-01 07:56:39 -05:00
|
|
|
|
2023-09-05 09:58:51 -04:00
|
|
|
# The per-device presence state, maps user to devices to per-device presence state.
|
|
|
|
self._user_to_device_to_current_state: Dict[
|
|
|
|
str, Dict[Optional[str], UserDevicePresenceState]
|
|
|
|
] = {}
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
now = self.clock.time_msec()
|
2022-05-06 12:41:57 -04:00
|
|
|
if self._presence_enabled:
|
|
|
|
for state in self.user_to_current_state.values():
|
2023-09-05 09:58:51 -04:00
|
|
|
# Create a psuedo-device to properly handle time outs. This will
|
|
|
|
# be overridden by any "real" devices within SYNC_ONLINE_TIMEOUT.
|
|
|
|
pseudo_device_id = None
|
|
|
|
self._user_to_device_to_current_state[state.user_id] = {
|
|
|
|
pseudo_device_id: UserDevicePresenceState(
|
|
|
|
user_id=state.user_id,
|
|
|
|
device_id=pseudo_device_id,
|
|
|
|
state=state.state,
|
|
|
|
last_active_ts=state.last_active_ts,
|
|
|
|
last_sync_ts=state.last_user_sync_ts,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
self.wheel_timer.insert(
|
2022-05-06 12:41:57 -04:00
|
|
|
now=now, obj=state.user_id, then=state.last_active_ts + IDLE_TIMER
|
2016-02-15 12:10:40 -05:00
|
|
|
)
|
|
|
|
self.wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=state.user_id,
|
2022-05-06 12:41:57 -04:00
|
|
|
then=state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT,
|
2016-02-15 12:10:40 -05:00
|
|
|
)
|
2022-05-06 12:41:57 -04:00
|
|
|
if self.is_mine_id(state.user_id):
|
|
|
|
self.wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=state.user_id,
|
|
|
|
then=state.last_federation_update_ts + FEDERATION_PING_INTERVAL,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=state.user_id,
|
|
|
|
then=state.last_federation_update_ts + FEDERATION_TIMEOUT,
|
|
|
|
)
|
2016-02-15 12:10:40 -05:00
|
|
|
|
|
|
|
# Set of users who have presence in the `user_to_current_state` that
|
|
|
|
# have not yet been persisted
|
2021-07-16 13:22:36 -04:00
|
|
|
self.unpersisted_users_changes: Set[str] = set()
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2019-06-06 09:45:17 -04:00
|
|
|
hs.get_reactor().addSystemEventTrigger(
|
|
|
|
"before",
|
|
|
|
"shutdown",
|
|
|
|
run_as_background_process,
|
|
|
|
"presence.on_shutdown",
|
|
|
|
self._on_shutdown,
|
|
|
|
)
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2016-06-02 10:20:15 -04:00
|
|
|
# Keeps track of the number of *ongoing* syncs on this process. While
|
|
|
|
# this is non zero a user will never go offline.
|
2023-08-29 11:44:07 -04:00
|
|
|
self._user_device_to_num_current_syncs: Dict[
|
|
|
|
Tuple[str, Optional[str]], int
|
|
|
|
] = {}
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2016-06-02 10:20:15 -04:00
|
|
|
# Keeps track of the number of *ongoing* syncs on other processes.
|
2023-08-29 11:44:07 -04:00
|
|
|
#
|
2023-09-05 09:58:51 -04:00
|
|
|
# While any sync is ongoing on another process the user's device will never
|
2016-06-02 10:20:15 -04:00
|
|
|
# go offline.
|
2023-08-29 11:44:07 -04:00
|
|
|
#
|
2016-06-02 10:20:15 -04:00
|
|
|
# Each process has a unique identifier and an update frequency. If
|
|
|
|
# no update is received from that process within the update period then
|
|
|
|
# we assume that all the sync requests on that process have stopped.
|
2023-08-29 11:44:07 -04:00
|
|
|
# Stored as a dict from process_id to set of (user_id, device_id), and
|
|
|
|
# a dict of process_id to millisecond timestamp last updated.
|
|
|
|
self.external_process_to_current_syncs: Dict[
|
|
|
|
str, Set[Tuple[str, Optional[str]]]
|
|
|
|
] = {}
|
2021-07-16 13:22:36 -04:00
|
|
|
self.external_process_last_updated_ms: Dict[str, int] = {}
|
2019-10-18 01:42:26 -04:00
|
|
|
|
2017-03-27 10:56:25 -04:00
|
|
|
self.external_sync_linearizer = Linearizer(name="external_sync_linearizer")
|
2016-06-02 10:20:15 -04:00
|
|
|
|
2021-03-03 05:21:46 -05:00
|
|
|
if self._presence_enabled:
|
|
|
|
# Start a LoopingCall in 30s that fires every 5s.
|
|
|
|
# The initial delay is to allow disconnected clients a chance to
|
|
|
|
# reconnect before we treat them as offline.
|
|
|
|
self.clock.call_later(
|
2023-08-10 07:39:46 -04:00
|
|
|
30, self.clock.looping_call, self._handle_timeouts, 5000
|
2019-05-28 16:20:11 -04:00
|
|
|
)
|
|
|
|
|
2023-08-10 07:39:46 -04:00
|
|
|
self.clock.call_later(
|
|
|
|
60,
|
|
|
|
self.clock.looping_call,
|
|
|
|
self._persist_unpersisted_changes,
|
|
|
|
60 * 1000,
|
|
|
|
)
|
2016-08-30 10:39:50 -04:00
|
|
|
|
2018-05-22 18:32:57 -04:00
|
|
|
LaterGauge(
|
|
|
|
"synapse_handlers_presence_wheel_timer_size",
|
|
|
|
"",
|
|
|
|
[],
|
|
|
|
lambda: len(self.wheel_timer),
|
|
|
|
)
|
2016-02-19 04:50:54 -05:00
|
|
|
|
2019-03-26 08:45:22 -04:00
|
|
|
# Used to handle sending of presence to newly joined users/servers
|
2021-03-03 05:21:46 -05:00
|
|
|
if self._presence_enabled:
|
2019-03-26 08:45:22 -04:00
|
|
|
self.notifier.add_replication_callback(self.notify_new_event)
|
|
|
|
|
|
|
|
# Presence is best effort and quickly heals itself, so lets just always
|
|
|
|
# stream from the current state when we restart.
|
2022-01-04 11:10:27 -05:00
|
|
|
self._event_pos = self.store.get_room_max_stream_ordering()
|
2019-03-26 08:45:22 -04:00
|
|
|
self._event_processing = False
|
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
async def _on_shutdown(self) -> None:
|
2016-02-15 12:10:40 -05:00
|
|
|
"""Gets called when shutting down. This lets us persist any updates that
|
|
|
|
we haven't yet persisted, e.g. updates that only changes some internal
|
|
|
|
timers. This allows changes to persist across startup without having to
|
|
|
|
persist every single change.
|
|
|
|
|
|
|
|
If this does not run it simply means that some of the timers will fire
|
|
|
|
earlier than they should when synapse is restarted. This affect of this
|
|
|
|
is some spurious presence changes that will self-correct.
|
|
|
|
"""
|
2018-08-13 02:47:46 -04:00
|
|
|
# If the DB pool has already terminated, don't try updating
|
2020-08-05 16:38:57 -04:00
|
|
|
if not self.store.db_pool.is_running():
|
2018-08-13 02:47:46 -04:00
|
|
|
return
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
logger.info(
|
2016-09-13 08:26:33 -04:00
|
|
|
"Performing _on_shutdown. Persisting %d unpersisted changes",
|
2016-02-15 12:10:40 -05:00
|
|
|
len(self.user_to_current_state),
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
if self.unpersisted_users_changes:
|
2020-02-26 10:33:26 -05:00
|
|
|
await self.store.update_presence(
|
2016-02-15 12:10:40 -05:00
|
|
|
[
|
|
|
|
self.user_to_current_state[user_id]
|
|
|
|
for user_id in self.unpersisted_users_changes
|
|
|
|
]
|
|
|
|
)
|
|
|
|
logger.info("Finished _on_shutdown")
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2023-08-10 07:39:46 -04:00
|
|
|
@wrap_as_background_process("persist_presence_changes")
|
2021-04-28 06:07:47 -04:00
|
|
|
async def _persist_unpersisted_changes(self) -> None:
|
2016-08-30 10:39:50 -04:00
|
|
|
"""We periodically persist the unpersisted changes, as otherwise they
|
|
|
|
may stack up and slow down shutdown times.
|
|
|
|
"""
|
|
|
|
unpersisted = self.unpersisted_users_changes
|
|
|
|
self.unpersisted_users_changes = set()
|
|
|
|
|
2016-08-30 10:50:20 -04:00
|
|
|
if unpersisted:
|
2019-09-03 07:44:14 -04:00
|
|
|
logger.info("Persisting %d unpersisted presence updates", len(unpersisted))
|
2020-02-26 10:33:26 -05:00
|
|
|
await self.store.update_presence(
|
2018-08-17 11:08:45 -04:00
|
|
|
[self.user_to_current_state[user_id] for user_id in unpersisted]
|
2019-05-28 16:20:11 -04:00
|
|
|
)
|
2016-08-30 10:39:50 -04:00
|
|
|
|
2021-05-18 09:13:45 -04:00
|
|
|
async def _update_states(
|
|
|
|
self, new_states: Iterable[UserPresenceState], force_notify: bool = False
|
|
|
|
) -> None:
|
2016-02-15 12:10:40 -05:00
|
|
|
"""Updates presence of users. Sets the appropriate timeouts. Pokes
|
|
|
|
the notifier and federation if and only if the changed presence state
|
|
|
|
should be sent to clients/servers.
|
2021-02-17 12:31:37 -05:00
|
|
|
|
|
|
|
Args:
|
|
|
|
new_states: The new user presence state updates to process.
|
2021-05-18 09:13:45 -04:00
|
|
|
force_notify: Whether to force notifying clients of this presence state update,
|
|
|
|
even if it doesn't change the state of a user's presence (e.g online -> online).
|
|
|
|
This is currently used to bump the max presence stream ID without changing any
|
|
|
|
user's presence (see PresenceHandler.add_users_to_send_full_presence_to).
|
2016-02-15 12:10:40 -05:00
|
|
|
"""
|
2022-05-06 12:41:57 -04:00
|
|
|
if not self._presence_enabled:
|
|
|
|
# We shouldn't get here if presence is disabled, but we check anyway
|
|
|
|
# to ensure that we don't a) send out presence federation and b)
|
|
|
|
# don't add things to the wheel timer that will never be handled.
|
|
|
|
logger.warning("Tried to update presence states when presence is disabled")
|
|
|
|
return
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
now = self.clock.time_msec()
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-19 06:50:48 -05:00
|
|
|
with Measure(self.clock, "presence_update_states"):
|
2020-02-26 10:33:26 -05:00
|
|
|
# NOTE: We purposefully don't await between now and when we've
|
2016-02-19 06:50:48 -05:00
|
|
|
# calculated what we want to do with the new states, to avoid races.
|
2016-02-18 04:54:08 -05:00
|
|
|
|
2016-02-19 06:50:48 -05:00
|
|
|
to_notify = {} # Changes we want to notify everyone about
|
|
|
|
to_federation_ping = {} # These need sending keep-alives
|
2014-09-02 08:30:36 -04:00
|
|
|
|
2016-09-09 09:26:05 -04:00
|
|
|
# Only bother handling the last presence change for each user
|
|
|
|
new_states_dict = {}
|
|
|
|
for new_state in new_states:
|
|
|
|
new_states_dict[new_state.user_id] = new_state
|
2021-02-17 12:31:37 -05:00
|
|
|
new_states = new_states_dict.values()
|
2016-09-09 09:26:05 -04:00
|
|
|
|
2016-02-19 06:50:48 -05:00
|
|
|
for new_state in new_states:
|
|
|
|
user_id = new_state.user_id
|
2015-08-18 05:30:07 -04:00
|
|
|
|
2016-02-19 06:50:48 -05:00
|
|
|
# Its fine to not hit the database here, as the only thing not in
|
|
|
|
# the current state cache are OFFLINE states, where the only field
|
|
|
|
# of interest is last_active which is safe enough to assume is 0
|
|
|
|
# here.
|
|
|
|
prev_state = self.user_to_current_state.get(
|
|
|
|
user_id, UserPresenceState.default(user_id)
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-19 06:50:48 -05:00
|
|
|
new_state, should_notify, should_ping = handle_update(
|
|
|
|
prev_state,
|
|
|
|
new_state,
|
2016-05-16 14:08:40 -04:00
|
|
|
is_mine=self.is_mine_id(user_id),
|
2016-02-19 06:50:48 -05:00
|
|
|
wheel_timer=self.wheel_timer,
|
|
|
|
now=now,
|
|
|
|
)
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2021-05-18 09:13:45 -04:00
|
|
|
if force_notify:
|
|
|
|
should_notify = True
|
|
|
|
|
2016-02-19 06:50:48 -05:00
|
|
|
self.user_to_current_state[user_id] = new_state
|
2014-08-13 14:18:55 -04:00
|
|
|
|
2016-02-19 06:50:48 -05:00
|
|
|
if should_notify:
|
|
|
|
to_notify[user_id] = new_state
|
|
|
|
elif should_ping:
|
|
|
|
to_federation_ping[user_id] = new_state
|
2016-02-19 04:50:54 -05:00
|
|
|
|
2016-02-19 06:50:48 -05:00
|
|
|
# TODO: We should probably ensure there are no races hereafter
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2018-05-21 20:47:37 -04:00
|
|
|
presence_updates_counter.inc(len(new_states))
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2016-02-19 06:50:48 -05:00
|
|
|
if to_notify:
|
2018-05-21 20:47:37 -04:00
|
|
|
notified_presence_counter.inc(len(to_notify))
|
2020-02-26 10:33:26 -05:00
|
|
|
await self._persist_and_notify(list(to_notify.values()))
|
2016-02-19 06:50:48 -05:00
|
|
|
|
2020-02-21 07:15:07 -05:00
|
|
|
self.unpersisted_users_changes |= {s.user_id for s in new_states}
|
2016-02-19 06:50:48 -05:00
|
|
|
self.unpersisted_users_changes -= set(to_notify.keys())
|
2014-11-18 10:25:55 -05:00
|
|
|
|
2021-04-19 05:50:49 -04:00
|
|
|
# Check if we need to resend any presence states to remote hosts. We
|
|
|
|
# only do this for states that haven't been updated in a while to
|
|
|
|
# ensure that the remote host doesn't time the presence state out.
|
|
|
|
#
|
|
|
|
# Note that since these are states that have *not* been updated,
|
|
|
|
# they won't get sent down the normal presence replication stream,
|
|
|
|
# and so we have to explicitly send them via the federation stream.
|
2016-02-19 06:50:48 -05:00
|
|
|
to_federation_ping = {
|
|
|
|
user_id: state
|
|
|
|
for user_id, state in to_federation_ping.items()
|
|
|
|
if user_id not in to_notify
|
|
|
|
}
|
|
|
|
if to_federation_ping:
|
2018-05-21 20:47:37 -04:00
|
|
|
federation_presence_out_counter.inc(len(to_federation_ping))
|
2016-02-23 09:03:46 -05:00
|
|
|
|
2021-06-11 10:19:42 -04:00
|
|
|
hosts_to_states = await get_interested_remotes(
|
2021-04-19 05:50:49 -04:00
|
|
|
self.store,
|
|
|
|
self.presence_router,
|
|
|
|
list(to_federation_ping.values()),
|
|
|
|
)
|
|
|
|
|
2021-06-11 10:19:42 -04:00
|
|
|
for destination, states in hosts_to_states.items():
|
2023-09-04 12:14:09 -04:00
|
|
|
await self._federation_queue.send_presence_to_destinations(
|
2021-06-11 10:19:42 -04:00
|
|
|
states, [destination]
|
2021-04-20 09:11:24 -04:00
|
|
|
)
|
2014-11-18 10:25:55 -05:00
|
|
|
|
2023-08-10 07:39:46 -04:00
|
|
|
@wrap_as_background_process("handle_presence_timeouts")
|
2021-04-28 06:07:47 -04:00
|
|
|
async def _handle_timeouts(self) -> None:
|
2016-02-15 12:10:40 -05:00
|
|
|
"""Checks the presence of users that have timed out and updates as
|
|
|
|
appropriate.
|
|
|
|
"""
|
2019-07-29 12:02:18 -04:00
|
|
|
logger.debug("Handling presence timeouts")
|
2016-02-15 12:10:40 -05:00
|
|
|
now = self.clock.time_msec()
|
2014-11-18 10:25:55 -05:00
|
|
|
|
2019-05-28 16:20:11 -04:00
|
|
|
# Fetch the list of users that *may* have timed out. Things may have
|
|
|
|
# changed since the timeout was set, so we won't necessarily have to
|
|
|
|
# take any action.
|
|
|
|
users_to_check = set(self.wheel_timer.fetch(now))
|
|
|
|
|
|
|
|
# Check whether the lists of syncing processes from an external
|
|
|
|
# process have expired.
|
|
|
|
expired_process_ids = [
|
|
|
|
process_id
|
|
|
|
for process_id, last_update in self.external_process_last_updated_ms.items()
|
|
|
|
if now - last_update > EXTERNAL_PROCESS_EXPIRY
|
|
|
|
]
|
|
|
|
for process_id in expired_process_ids:
|
2019-10-18 01:42:26 -04:00
|
|
|
# For each expired process drop tracking info and check the users
|
|
|
|
# that were syncing on that process to see if they need to be timed
|
|
|
|
# out.
|
2019-05-28 16:20:11 -04:00
|
|
|
users_to_check.update(
|
2023-08-29 11:44:07 -04:00
|
|
|
user_id
|
|
|
|
for user_id, device_id in self.external_process_to_current_syncs.pop(
|
|
|
|
process_id, ()
|
|
|
|
)
|
2019-05-28 16:20:11 -04:00
|
|
|
)
|
2019-10-18 01:42:26 -04:00
|
|
|
self.external_process_last_updated_ms.pop(process_id)
|
2019-05-28 16:20:11 -04:00
|
|
|
|
|
|
|
states = [
|
|
|
|
self.user_to_current_state.get(user_id, UserPresenceState.default(user_id))
|
|
|
|
for user_id in users_to_check
|
|
|
|
]
|
|
|
|
|
|
|
|
timers_fired_counter.inc(len(states))
|
|
|
|
|
2023-09-05 09:58:51 -04:00
|
|
|
# Set of user ID & device IDs which are currently syncing.
|
|
|
|
syncing_user_devices = {
|
|
|
|
user_id_device_id
|
|
|
|
for user_id_device_id, count in self._user_device_to_num_current_syncs.items()
|
2020-04-22 17:39:04 -04:00
|
|
|
if count
|
|
|
|
}
|
2023-09-05 09:58:51 -04:00
|
|
|
syncing_user_devices.update(
|
|
|
|
itertools.chain(*self.external_process_to_current_syncs.values())
|
2023-08-29 11:44:07 -04:00
|
|
|
)
|
2020-04-22 17:39:04 -04:00
|
|
|
|
2019-05-28 16:20:11 -04:00
|
|
|
changes = handle_timeouts(
|
|
|
|
states,
|
|
|
|
is_mine_fn=self.is_mine_id,
|
2023-09-05 09:58:51 -04:00
|
|
|
syncing_user_devices=syncing_user_devices,
|
|
|
|
user_to_devices=self._user_to_device_to_current_state,
|
2019-05-28 16:20:11 -04:00
|
|
|
now=now,
|
|
|
|
)
|
2015-08-18 05:30:07 -04:00
|
|
|
|
2020-02-26 10:33:26 -05:00
|
|
|
return await self._update_states(changes)
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2023-08-28 13:08:49 -04:00
|
|
|
async def bump_presence_active_time(
|
|
|
|
self, user: UserID, device_id: Optional[str]
|
|
|
|
) -> None:
|
2016-02-15 12:10:40 -05:00
|
|
|
"""We've seen the user do something that indicates they're interacting
|
|
|
|
with the app.
|
2015-08-18 05:30:07 -04:00
|
|
|
"""
|
2018-08-17 11:08:45 -04:00
|
|
|
# If presence is disabled, no-op
|
2023-08-10 07:39:46 -04:00
|
|
|
if not self._presence_enabled:
|
2018-08-17 11:08:45 -04:00
|
|
|
return
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
user_id = user.to_string()
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2016-02-19 06:32:04 -05:00
|
|
|
bump_active_time_counter.inc()
|
|
|
|
|
2023-09-05 09:58:51 -04:00
|
|
|
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
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2023-09-05 09:58:51 -04:00
|
|
|
# Update the user state, this will always update last_active_ts and
|
|
|
|
# might update the presence state.
|
|
|
|
prev_state = await self.current_state_for_user(user_id)
|
|
|
|
new_fields: Dict[str, Any] = {
|
|
|
|
"last_active_ts": now,
|
|
|
|
"state": _combine_device_states(devices.values()),
|
|
|
|
}
|
2016-02-18 04:54:08 -05:00
|
|
|
|
2020-02-26 10:33:26 -05:00
|
|
|
await self._update_states([prev_state.copy_and_replace(**new_fields)])
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2020-02-26 10:33:26 -05:00
|
|
|
async def user_syncing(
|
2022-04-13 11:21:07 -04:00
|
|
|
self,
|
|
|
|
user_id: str,
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id: Optional[str],
|
2022-04-13 11:21:07 -04:00
|
|
|
affect_presence: bool = True,
|
|
|
|
presence_state: str = PresenceState.ONLINE,
|
2020-02-26 10:33:26 -05:00
|
|
|
) -> ContextManager[None]:
|
2016-02-15 12:10:40 -05:00
|
|
|
"""Returns a context manager that should surround any stream requests
|
|
|
|
from the user.
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
This allows us to keep track of who is currently streaming and who isn't
|
|
|
|
without having to have timers outside of this module to avoid flickering
|
|
|
|
when users disconnect/reconnect.
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
Args:
|
2023-08-28 13:08:49 -04:00
|
|
|
user_id: the user that is starting a sync
|
|
|
|
device_id: the user's device that is starting a sync
|
2021-09-20 08:56:23 -04:00
|
|
|
affect_presence: If false this function will be a no-op.
|
2016-02-15 12:10:40 -05:00
|
|
|
Useful for streams that are not associated with an actual
|
|
|
|
client that is being used by a user.
|
2022-04-13 11:21:07 -04:00
|
|
|
presence_state: The presence state indicated in the sync request
|
2016-02-15 12:10:40 -05:00
|
|
|
"""
|
2023-08-10 07:39:46 -04:00
|
|
|
if not affect_presence or not self._presence_enabled:
|
|
|
|
return _NullContextManager()
|
2018-08-17 11:08:45 -04:00
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
curr_sync = self._user_device_to_num_current_syncs.get((user_id, device_id), 0)
|
|
|
|
self._user_device_to_num_current_syncs[(user_id, device_id)] = curr_sync + 1
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2023-08-28 11:03:23 -04:00
|
|
|
# Note that this causes last_active_ts to be incremented which is not
|
|
|
|
# what the spec wants.
|
|
|
|
await self.set_state(
|
|
|
|
UserID.from_string(user_id),
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id,
|
2023-08-28 11:03:23 -04:00
|
|
|
state={"presence": presence_state},
|
|
|
|
is_sync=True,
|
|
|
|
)
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
async def _end() -> None:
|
2018-04-27 06:07:40 -04:00
|
|
|
try:
|
2023-08-29 11:44:07 -04:00
|
|
|
self._user_device_to_num_current_syncs[(user_id, device_id)] -= 1
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2020-02-26 10:33:26 -05:00
|
|
|
prev_state = await self.current_state_for_user(user_id)
|
|
|
|
await self._update_states(
|
2019-06-20 05:32:02 -04:00
|
|
|
[
|
2016-02-15 12:10:40 -05:00
|
|
|
prev_state.copy_and_replace(
|
2016-02-18 05:11:43 -05:00
|
|
|
last_user_sync_ts=self.clock.time_msec()
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
|
|
|
]
|
2016-02-15 12:10:40 -05:00
|
|
|
)
|
2018-04-27 06:07:40 -04:00
|
|
|
except Exception:
|
|
|
|
logger.exception("Error updating presence after sync")
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
@contextmanager
|
2021-09-20 08:56:23 -04:00
|
|
|
def _user_syncing() -> Generator[None, None, None]:
|
2016-02-15 12:10:40 -05:00
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
2023-08-10 07:39:46 -04:00
|
|
|
run_in_background(_end)
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return _user_syncing()
|
2015-08-17 05:40:23 -04:00
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
def get_currently_syncing_users_for_replication(
|
|
|
|
self,
|
|
|
|
) -> Iterable[Tuple[str, Optional[str]]]:
|
2020-04-22 17:39:04 -04:00
|
|
|
# since we are the process handling presence, there is nothing to do here.
|
|
|
|
return []
|
2016-06-02 10:20:15 -04:00
|
|
|
|
2020-02-26 10:33:26 -05:00
|
|
|
async def update_external_syncs_row(
|
2023-08-29 11:44:07 -04:00
|
|
|
self,
|
|
|
|
process_id: str,
|
|
|
|
user_id: str,
|
|
|
|
device_id: Optional[str],
|
|
|
|
is_syncing: bool,
|
|
|
|
sync_time_msec: int,
|
2021-04-28 06:07:47 -04:00
|
|
|
) -> None:
|
2017-03-27 10:56:25 -04:00
|
|
|
"""Update the syncing users for an external process as a delta.
|
|
|
|
|
|
|
|
Args:
|
2021-04-28 06:07:47 -04:00
|
|
|
process_id: An identifier for the process the users are
|
2017-03-27 10:56:25 -04:00
|
|
|
syncing against. This allows synapse to process updates
|
|
|
|
as user start and stop syncing against a given process.
|
2021-04-28 06:07:47 -04:00
|
|
|
user_id: The user who has started or stopped syncing
|
2023-08-29 11:44:07 -04:00
|
|
|
device_id: The user's device that has started or stopped syncing
|
2021-04-28 06:07:47 -04:00
|
|
|
is_syncing: Whether or not the user is now syncing
|
|
|
|
sync_time_msec: Time in ms when the user was last syncing
|
2017-03-27 10:56:25 -04:00
|
|
|
"""
|
2022-04-05 10:43:52 -04:00
|
|
|
async with self.external_sync_linearizer.queue(process_id):
|
2020-02-26 10:33:26 -05:00
|
|
|
prev_state = await self.current_state_for_user(user_id)
|
2017-03-27 10:56:25 -04:00
|
|
|
|
|
|
|
process_presence = self.external_process_to_current_syncs.setdefault(
|
|
|
|
process_id, set()
|
|
|
|
)
|
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
# USER_SYNC is sent when a user's device starts or stops syncing on
|
|
|
|
# a remote # process. (But only for the initial and last sync for that
|
|
|
|
# device.)
|
2023-08-28 11:03:23 -04:00
|
|
|
#
|
2023-08-29 11:44:07 -04:00
|
|
|
# When a device *starts* syncing it also calls set_state(...) which
|
2023-08-28 11:03:23 -04:00
|
|
|
# will update the state, last_active_ts, and last_user_sync_ts.
|
2023-08-29 11:44:07 -04:00
|
|
|
# Simply ensure the user & device is tracked as syncing in this case.
|
2023-08-28 11:03:23 -04:00
|
|
|
#
|
2023-08-29 11:44:07 -04:00
|
|
|
# When a device *stops* syncing, update the last_user_sync_ts and mark
|
2023-08-28 11:03:23 -04:00
|
|
|
# them as no longer syncing. Note this doesn't quite match the
|
|
|
|
# monolith behaviour, which updates last_user_sync_ts at the end of
|
|
|
|
# every sync, not just the last in-flight sync.
|
2023-08-29 11:44:07 -04:00
|
|
|
if is_syncing and (user_id, device_id) not in process_presence:
|
|
|
|
process_presence.add((user_id, device_id))
|
|
|
|
elif not is_syncing and (user_id, device_id) in process_presence:
|
2023-09-05 09:58:51 -04:00
|
|
|
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_sync_ts = sync_time_msec
|
|
|
|
|
2023-08-28 11:03:23 -04:00
|
|
|
new_state = prev_state.copy_and_replace(
|
|
|
|
last_user_sync_ts=sync_time_msec
|
2017-03-27 10:56:25 -04:00
|
|
|
)
|
2023-08-28 11:03:23 -04:00
|
|
|
await self._update_states([new_state])
|
2017-03-31 06:36:32 -04:00
|
|
|
|
2023-08-29 11:44:07 -04:00
|
|
|
process_presence.discard((user_id, device_id))
|
2017-03-27 10:56:25 -04:00
|
|
|
|
2017-03-31 06:46:20 -04:00
|
|
|
self.external_process_last_updated_ms[process_id] = self.clock.time_msec()
|
2017-03-27 10:56:25 -04:00
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
async def update_external_syncs_clear(self, process_id: str) -> None:
|
2017-03-27 10:56:25 -04:00
|
|
|
"""Marks all users that had been marked as syncing by a given process
|
|
|
|
as offline.
|
|
|
|
|
|
|
|
Used when the process has stopped/disappeared.
|
|
|
|
"""
|
2022-04-05 10:43:52 -04:00
|
|
|
async with self.external_sync_linearizer.queue(process_id):
|
2017-03-27 10:56:25 -04:00
|
|
|
process_presence = self.external_process_to_current_syncs.pop(
|
|
|
|
process_id, set()
|
|
|
|
)
|
2023-09-05 09:58:51 -04:00
|
|
|
|
2017-03-27 10:56:25 -04:00
|
|
|
time_now_ms = self.clock.time_msec()
|
|
|
|
|
2023-09-05 09:58:51 -04:00
|
|
|
# Mark each device as having a last sync time.
|
|
|
|
updated_users = set()
|
|
|
|
for user_id, device_id in process_presence:
|
|
|
|
device_state = self._user_to_device_to_current_state.setdefault(
|
|
|
|
user_id, {}
|
|
|
|
).setdefault(
|
|
|
|
device_id, UserDevicePresenceState.default(user_id, device_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
device_state.last_sync_ts = time_now_ms
|
|
|
|
updated_users.add(user_id)
|
|
|
|
|
|
|
|
# Update each user (and insert into the appropriate timers to check if
|
|
|
|
# they've gone offline).
|
|
|
|
prev_states = await self.current_state_for_users(updated_users)
|
2020-02-26 10:33:26 -05:00
|
|
|
await self._update_states(
|
2017-03-27 10:56:25 -04:00
|
|
|
[
|
|
|
|
prev_state.copy_and_replace(last_user_sync_ts=time_now_ms)
|
2020-06-15 07:03:36 -04:00
|
|
|
for prev_state in prev_states.values()
|
2017-03-27 10:56:25 -04:00
|
|
|
]
|
|
|
|
)
|
|
|
|
self.external_process_last_updated_ms.pop(process_id, None)
|
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
async def _persist_and_notify(self, states: List[UserPresenceState]) -> None:
|
2016-02-15 12:10:40 -05:00
|
|
|
"""Persist states in the database, poke the notifier and send to
|
|
|
|
interested remote servers
|
|
|
|
"""
|
2020-02-26 10:33:26 -05:00
|
|
|
stream_id, max_token = await self.store.update_presence(states)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-04-06 09:38:30 -04:00
|
|
|
parties = await get_interested_parties(self.store, self.presence_router, states)
|
2017-04-10 11:48:30 -04:00
|
|
|
room_ids_to_states, users_to_states = parties
|
2014-09-01 10:38:37 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
self.notifier.on_new_event(
|
2022-05-16 11:35:31 -04:00
|
|
|
StreamKeyType.PRESENCE,
|
2016-02-15 12:10:40 -05:00
|
|
|
stream_id,
|
|
|
|
rooms=room_ids_to_states.keys(),
|
2017-04-10 11:48:30 -04:00
|
|
|
users=[UserID.from_string(u) for u in users_to_states],
|
2014-11-20 11:24:00 -05:00
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-04-19 05:50:49 -04:00
|
|
|
# We only want to poke the local federation sender, if any, as other
|
|
|
|
# workers will receive the presence updates via the presence replication
|
|
|
|
# stream (which is updated by `store.update_presence`).
|
|
|
|
await self.maybe_send_presence_to_interested_destinations(states)
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
async def incoming_presence(self, origin: str, content: JsonDict) -> None:
|
2016-02-15 12:10:40 -05:00
|
|
|
"""Called when we receive a `m.presence` EDU from a remote server."""
|
2020-05-15 06:44:00 -04:00
|
|
|
if not self._presence_enabled:
|
|
|
|
return
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
now = self.clock.time_msec()
|
|
|
|
updates = []
|
|
|
|
for push in content.get("push", []):
|
|
|
|
# A "push" contains a list of presence that we are probably interested
|
|
|
|
# in.
|
|
|
|
user_id = push.get("user_id", None)
|
|
|
|
if not user_id:
|
|
|
|
logger.info(
|
|
|
|
"Got presence update from %r with no 'user_id': %r", origin, push
|
|
|
|
)
|
|
|
|
continue
|
2016-09-08 10:04:46 -04:00
|
|
|
|
|
|
|
if get_domain_from_id(user_id) != origin:
|
|
|
|
logger.info(
|
|
|
|
"Got presence update from %r with bad 'user_id': %r",
|
|
|
|
origin,
|
|
|
|
user_id,
|
|
|
|
)
|
|
|
|
continue
|
2014-08-13 14:18:55 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
presence_state = push.get("presence", None)
|
|
|
|
if not presence_state:
|
|
|
|
logger.info(
|
|
|
|
"Got presence update from %r with no 'presence_state': %r",
|
|
|
|
origin,
|
|
|
|
push,
|
|
|
|
)
|
|
|
|
continue
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-18 05:11:43 -05:00
|
|
|
new_fields = {"state": presence_state, "last_federation_update_ts": now}
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
last_active_ago = push.get("last_active_ago", None)
|
|
|
|
if last_active_ago is not None:
|
2016-02-18 05:11:43 -05:00
|
|
|
new_fields["last_active_ts"] = now - last_active_ago
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
new_fields["status_msg"] = push.get("status_msg", None)
|
2016-02-23 05:40:11 -05:00
|
|
|
new_fields["currently_active"] = push.get("currently_active", False)
|
2014-09-01 11:16:35 -04:00
|
|
|
|
2020-02-26 10:33:26 -05:00
|
|
|
prev_state = await self.current_state_for_user(user_id)
|
2016-02-15 12:10:40 -05:00
|
|
|
updates.append(prev_state.copy_and_replace(**new_fields))
|
2015-03-23 13:25:44 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
if updates:
|
2018-05-21 20:47:37 -04:00
|
|
|
federation_presence_counter.inc(len(updates))
|
2020-02-26 10:33:26 -05:00
|
|
|
await self._update_states(updates)
|
2014-09-01 11:16:35 -04:00
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
async def set_state(
|
2021-05-18 09:13:45 -04:00
|
|
|
self,
|
|
|
|
target_user: UserID,
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id: Optional[str],
|
2021-05-18 09:13:45 -04:00
|
|
|
state: JsonDict,
|
|
|
|
force_notify: bool = False,
|
2023-08-28 11:03:23 -04:00
|
|
|
is_sync: bool = False,
|
2021-04-28 06:07:47 -04:00
|
|
|
) -> None:
|
2021-05-18 09:13:45 -04:00
|
|
|
"""Set the presence state of the user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_user: The ID of the user to set the presence state of.
|
2023-08-28 13:08:49 -04:00
|
|
|
device_id: the device that the user is setting the presence state of.
|
2021-05-18 09:13:45 -04:00
|
|
|
state: The presence state as a JSON dictionary.
|
|
|
|
force_notify: Whether to force notification of the update to clients.
|
2023-08-28 11:03:23 -04:00
|
|
|
is_sync: True if this update was from a sync, which results in
|
|
|
|
*not* overriding a previously set BUSY status, updating the
|
|
|
|
user's last_user_sync_ts, and ignoring the "status_msg" field of
|
|
|
|
the `state` dict.
|
2021-05-18 09:13:45 -04:00
|
|
|
"""
|
2016-02-15 12:10:40 -05:00
|
|
|
status_msg = state.get("status_msg", None)
|
|
|
|
presence = state["presence"]
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2023-08-10 07:39:46 -04:00
|
|
|
if presence not in self.VALID_PRESENCE:
|
2016-02-18 04:16:32 -05:00
|
|
|
raise SynapseError(400, "Invalid presence state")
|
|
|
|
|
2022-05-06 12:41:57 -04:00
|
|
|
# If presence is disabled, no-op
|
2023-08-10 07:39:46 -04:00
|
|
|
if not self._presence_enabled:
|
2022-05-06 12:41:57 -04:00
|
|
|
return
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
user_id = target_user.to_string()
|
2023-08-28 11:03:23 -04:00
|
|
|
now = self.clock.time_msec()
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2020-02-26 10:33:26 -05:00
|
|
|
prev_state = await self.current_state_for_user(user_id)
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2023-08-28 11:03:23 -04:00
|
|
|
# Syncs do not override a previous presence of busy.
|
|
|
|
#
|
|
|
|
# TODO: This is a hack for lack of multi-device support. Unfortunately
|
|
|
|
# removing this requires coordination with clients.
|
|
|
|
if prev_state.state == PresenceState.BUSY and is_sync:
|
|
|
|
presence = PresenceState.BUSY
|
|
|
|
|
2023-09-05 09:58:51 -04:00
|
|
|
# Update the device specific information.
|
|
|
|
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.state = presence
|
|
|
|
device_state.last_active_ts = now
|
|
|
|
if is_sync:
|
|
|
|
device_state.last_sync_ts = now
|
|
|
|
|
|
|
|
# Based on the state of each user's device calculate the new presence state.
|
|
|
|
presence = _combine_device_states(devices.values())
|
|
|
|
|
2016-08-10 07:57:30 -04:00
|
|
|
new_fields = {"state": presence}
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2023-08-28 11:03:23 -04:00
|
|
|
if presence == PresenceState.ONLINE or presence == PresenceState.BUSY:
|
|
|
|
new_fields["last_active_ts"] = now
|
2016-08-10 07:57:30 -04:00
|
|
|
|
2023-08-28 11:03:23 -04:00
|
|
|
if is_sync:
|
|
|
|
new_fields["last_user_sync_ts"] = now
|
|
|
|
else:
|
|
|
|
# Syncs do not override the status message.
|
|
|
|
new_fields["status_msg"] = status_msg
|
2016-02-15 12:10:40 -05:00
|
|
|
|
2021-05-18 09:13:45 -04:00
|
|
|
await self._update_states(
|
|
|
|
[prev_state.copy_and_replace(**new_fields)], force_notify=force_notify
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
async def is_visible(self, observed_user: UserID, observer_user: UserID) -> bool:
|
2016-02-18 04:09:50 -05:00
|
|
|
"""Returns whether a user can see another user's presence."""
|
2020-02-26 10:33:26 -05:00
|
|
|
observer_room_ids = await self.store.get_rooms_for_user(
|
2017-03-16 07:51:46 -04:00
|
|
|
observer_user.to_string()
|
|
|
|
)
|
2020-02-26 10:33:26 -05:00
|
|
|
observed_room_ids = await self.store.get_rooms_for_user(
|
2017-03-16 07:51:46 -04:00
|
|
|
observed_user.to_string()
|
|
|
|
)
|
2014-08-28 13:43:03 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
if observer_room_ids & observed_room_ids:
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
2014-08-28 13:43:03 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return False
|
2014-08-28 13:43:03 -04:00
|
|
|
|
2020-06-16 12:10:28 -04:00
|
|
|
async def get_all_presence_updates(
|
|
|
|
self, instance_name: str, last_id: int, current_id: int, limit: int
|
|
|
|
) -> Tuple[List[Tuple[int, list]], int, bool]:
|
2016-03-01 09:49:41 -05:00
|
|
|
"""
|
|
|
|
Gets a list of presence update rows from between the given stream ids.
|
|
|
|
Each row has:
|
|
|
|
- stream_id(str)
|
|
|
|
- user_id(str)
|
|
|
|
- state(str)
|
|
|
|
- last_active_ts(int)
|
|
|
|
- last_federation_update_ts(int)
|
|
|
|
- last_user_sync_ts(int)
|
|
|
|
- status_msg(int)
|
|
|
|
- currently_active(int)
|
2020-06-16 12:10:28 -04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
instance_name: The writer we want to fetch updates from. Unused
|
|
|
|
here since there is only ever one writer.
|
|
|
|
last_id: The token to fetch updates from. Exclusive.
|
|
|
|
current_id: The token to fetch updates up to. Inclusive.
|
|
|
|
limit: The requested limit for the number of rows to return. The
|
|
|
|
function may return more or fewer rows.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A tuple consisting of: the updates, a token to use to fetch
|
|
|
|
subsequent updates, and whether we returned fewer rows than exists
|
|
|
|
between the requested tokens due to the limit.
|
|
|
|
|
|
|
|
The token returned can be used in a subsequent call to this
|
2020-10-23 12:38:40 -04:00
|
|
|
function to get further updates.
|
2020-06-16 12:10:28 -04:00
|
|
|
|
|
|
|
The updates are a list of 2-tuples of stream ID and the row data
|
2016-03-01 09:49:41 -05:00
|
|
|
"""
|
2020-06-16 12:10:28 -04:00
|
|
|
|
2016-03-01 09:49:41 -05:00
|
|
|
# TODO(markjh): replicate the unpersisted changes.
|
|
|
|
# This could use the in-memory stores for recent changes.
|
2020-06-16 12:10:28 -04:00
|
|
|
rows = await self.store.get_all_presence_updates(
|
|
|
|
instance_name, last_id, current_id, limit
|
|
|
|
)
|
2019-07-23 09:00:55 -04:00
|
|
|
return rows
|
2016-03-01 09:49:41 -05:00
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
def notify_new_event(self) -> None:
|
2019-03-26 08:45:22 -04:00
|
|
|
"""Called when new events have happened. Handles users and servers
|
|
|
|
joining rooms and require being sent presence.
|
|
|
|
"""
|
|
|
|
|
2019-03-28 09:48:41 -04:00
|
|
|
if self._event_processing:
|
|
|
|
return
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
async def _process_presence() -> None:
|
2019-03-28 09:55:21 -04:00
|
|
|
assert not self._event_processing
|
2019-03-26 08:45:22 -04:00
|
|
|
|
|
|
|
self._event_processing = True
|
|
|
|
try:
|
2020-02-26 10:33:26 -05:00
|
|
|
await self._unsafe_process()
|
2019-03-26 08:45:22 -04:00
|
|
|
finally:
|
|
|
|
self._event_processing = False
|
|
|
|
|
|
|
|
run_as_background_process("presence.notify_new_event", _process_presence)
|
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
async def _unsafe_process(self) -> None:
|
2019-03-26 08:45:22 -04:00
|
|
|
# Loop round handling deltas until we're up to date
|
|
|
|
while True:
|
|
|
|
with Measure(self.clock, "presence_delta"):
|
2019-10-10 06:29:01 -04:00
|
|
|
room_max_stream_ordering = self.store.get_room_max_stream_ordering()
|
|
|
|
if self._event_pos == room_max_stream_ordering:
|
2019-03-26 08:45:22 -04:00
|
|
|
return
|
|
|
|
|
2019-10-10 06:29:01 -04:00
|
|
|
logger.debug(
|
|
|
|
"Processing presence stats %s->%s",
|
|
|
|
self._event_pos,
|
|
|
|
room_max_stream_ordering,
|
|
|
|
)
|
2022-06-01 11:02:53 -04:00
|
|
|
(
|
|
|
|
max_pos,
|
|
|
|
deltas,
|
|
|
|
) = await self._storage_controllers.state.get_current_state_deltas(
|
2019-10-10 06:29:01 -04:00
|
|
|
self._event_pos, room_max_stream_ordering
|
|
|
|
)
|
2021-05-05 12:27:05 -04:00
|
|
|
|
|
|
|
# We may get multiple deltas for different rooms, but we want to
|
|
|
|
# handle them on a room by room basis, so we batch them up by
|
|
|
|
# room.
|
|
|
|
deltas_by_room: Dict[str, List[JsonDict]] = {}
|
|
|
|
for delta in deltas:
|
|
|
|
deltas_by_room.setdefault(delta["room_id"], []).append(delta)
|
|
|
|
|
|
|
|
for room_id, deltas_for_room in deltas_by_room.items():
|
|
|
|
await self._handle_state_delta(room_id, deltas_for_room)
|
2019-03-26 08:45:22 -04:00
|
|
|
|
2019-10-10 06:29:01 -04:00
|
|
|
self._event_pos = max_pos
|
2019-03-26 08:45:22 -04:00
|
|
|
|
|
|
|
# Expose current event processing position to prometheus
|
|
|
|
synapse.metrics.event_processing_positions.labels("presence").set(
|
2019-10-10 06:29:01 -04:00
|
|
|
max_pos
|
2019-03-26 08:45:22 -04:00
|
|
|
)
|
|
|
|
|
2021-05-05 12:27:05 -04:00
|
|
|
async def _handle_state_delta(self, room_id: str, deltas: List[JsonDict]) -> None:
|
|
|
|
"""Process current state deltas for the room to find new joins that need
|
|
|
|
to be handled.
|
2019-03-26 08:45:22 -04:00
|
|
|
"""
|
2021-05-05 12:27:05 -04:00
|
|
|
|
|
|
|
# Sets of newly joined users. Note that if the local server is
|
|
|
|
# joining a remote room for the first time we'll see both the joining
|
|
|
|
# user and all remote users as newly joined.
|
|
|
|
newly_joined_users = set()
|
Be smarter about which hosts to send presence to when processing room joins (#9402)
This PR attempts to eliminate unnecessary presence sending work when your local server joins a room, or when a remote server joins a room your server is participating in by processing state deltas in chunks rather than individually.
---
When your server joins a room for the first time, it requests the historical state as well. This chunk of new state is passed to the presence handler which, after filtering that state down to only membership joins, will send presence updates to homeservers for each join processed.
It turns out that we were being a bit naive and processing each event individually, and sending out presence updates for every one of those joins. Even if many different joins were users on the same server (hello IRC bridges), we'd send presence to that same homeserver for every remote user join we saw.
This PR attempts to deduplicate all of that by processing the entire batch of state deltas at once, instead of only doing each join individually. We process the joins and note down which servers need which presence:
* If it was a local user join, send that user's latest presence to all servers in the room
* If it was a remote user join, send the presence for all local users in the room to that homeserver
We deduplicate by inserting all of those pending updates into a dictionary of the form:
```
{
server_name1: {presence_update1, ...},
server_name2: {presence_update1, presence_update2, ...}
}
```
Only after building this dict do we then start sending out presence updates.
2021-02-19 06:37:29 -05:00
|
|
|
|
2019-03-26 08:45:22 -04:00
|
|
|
for delta in deltas:
|
2021-05-05 12:27:05 -04:00
|
|
|
assert room_id == delta["room_id"]
|
|
|
|
|
2019-03-26 08:45:22 -04:00
|
|
|
typ = delta["type"]
|
|
|
|
state_key = delta["state_key"]
|
|
|
|
event_id = delta["event_id"]
|
|
|
|
prev_event_id = delta["prev_event_id"]
|
|
|
|
|
|
|
|
logger.debug("Handling: %r %r, %s", typ, state_key, event_id)
|
|
|
|
|
Be smarter about which hosts to send presence to when processing room joins (#9402)
This PR attempts to eliminate unnecessary presence sending work when your local server joins a room, or when a remote server joins a room your server is participating in by processing state deltas in chunks rather than individually.
---
When your server joins a room for the first time, it requests the historical state as well. This chunk of new state is passed to the presence handler which, after filtering that state down to only membership joins, will send presence updates to homeservers for each join processed.
It turns out that we were being a bit naive and processing each event individually, and sending out presence updates for every one of those joins. Even if many different joins were users on the same server (hello IRC bridges), we'd send presence to that same homeserver for every remote user join we saw.
This PR attempts to deduplicate all of that by processing the entire batch of state deltas at once, instead of only doing each join individually. We process the joins and note down which servers need which presence:
* If it was a local user join, send that user's latest presence to all servers in the room
* If it was a remote user join, send the presence for all local users in the room to that homeserver
We deduplicate by inserting all of those pending updates into a dictionary of the form:
```
{
server_name1: {presence_update1, ...},
server_name2: {presence_update1, presence_update2, ...}
}
```
Only after building this dict do we then start sending out presence updates.
2021-02-19 06:37:29 -05:00
|
|
|
# Drop any event that isn't a membership join
|
2019-03-26 08:45:22 -04:00
|
|
|
if typ != EventTypes.Member:
|
|
|
|
continue
|
|
|
|
|
2019-04-26 06:13:16 -04:00
|
|
|
if event_id is None:
|
|
|
|
# state has been deleted, so this is not a join. We only care about
|
|
|
|
# joins.
|
|
|
|
continue
|
|
|
|
|
2020-02-26 10:33:26 -05:00
|
|
|
event = await self.store.get_event(event_id, allow_none=True)
|
2019-06-05 10:45:46 -04:00
|
|
|
if not event or event.content.get("membership") != Membership.JOIN:
|
2019-03-26 08:45:22 -04:00
|
|
|
# We only care about joins
|
|
|
|
continue
|
|
|
|
|
|
|
|
if prev_event_id:
|
2020-02-26 10:33:26 -05:00
|
|
|
prev_event = await self.store.get_event(prev_event_id, allow_none=True)
|
2019-06-05 10:45:46 -04:00
|
|
|
if (
|
|
|
|
prev_event
|
|
|
|
and prev_event.content.get("membership") == Membership.JOIN
|
|
|
|
):
|
2019-03-26 08:45:22 -04:00
|
|
|
# Ignore changes to join events.
|
|
|
|
continue
|
|
|
|
|
2021-05-05 12:27:05 -04:00
|
|
|
newly_joined_users.add(state_key)
|
2019-03-28 09:48:41 -04:00
|
|
|
|
2021-05-05 12:27:05 -04:00
|
|
|
if not newly_joined_users:
|
|
|
|
# If nobody has joined then there's nothing to do.
|
|
|
|
return
|
Be smarter about which hosts to send presence to when processing room joins (#9402)
This PR attempts to eliminate unnecessary presence sending work when your local server joins a room, or when a remote server joins a room your server is participating in by processing state deltas in chunks rather than individually.
---
When your server joins a room for the first time, it requests the historical state as well. This chunk of new state is passed to the presence handler which, after filtering that state down to only membership joins, will send presence updates to homeservers for each join processed.
It turns out that we were being a bit naive and processing each event individually, and sending out presence updates for every one of those joins. Even if many different joins were users on the same server (hello IRC bridges), we'd send presence to that same homeserver for every remote user join we saw.
This PR attempts to deduplicate all of that by processing the entire batch of state deltas at once, instead of only doing each join individually. We process the joins and note down which servers need which presence:
* If it was a local user join, send that user's latest presence to all servers in the room
* If it was a remote user join, send the presence for all local users in the room to that homeserver
We deduplicate by inserting all of those pending updates into a dictionary of the form:
```
{
server_name1: {presence_update1, ...},
server_name2: {presence_update1, presence_update2, ...}
}
```
Only after building this dict do we then start sending out presence updates.
2021-02-19 06:37:29 -05:00
|
|
|
|
2021-05-05 12:27:05 -04:00
|
|
|
# We want to send:
|
|
|
|
# 1. presence states of all local users in the room to newly joined
|
|
|
|
# remote servers
|
|
|
|
# 2. presence states of newly joined users to all remote servers in
|
|
|
|
# the room.
|
|
|
|
#
|
|
|
|
# TODO: Only send presence states to remote hosts that don't already
|
|
|
|
# have them (because they already share rooms).
|
|
|
|
|
|
|
|
# Get all the users who were already in the room, by fetching the
|
|
|
|
# current users in the room and removing the newly joined users.
|
|
|
|
users = await self.store.get_users_in_room(room_id)
|
|
|
|
prev_users = set(users) - newly_joined_users
|
|
|
|
|
|
|
|
# Construct sets for all the local users and remote hosts that were
|
|
|
|
# already in the room
|
|
|
|
prev_local_users = []
|
|
|
|
prev_remote_hosts = set()
|
|
|
|
for user_id in prev_users:
|
|
|
|
if self.is_mine_id(user_id):
|
|
|
|
prev_local_users.append(user_id)
|
|
|
|
else:
|
|
|
|
prev_remote_hosts.add(get_domain_from_id(user_id))
|
|
|
|
|
|
|
|
# Similarly, construct sets for all the local users and remote hosts
|
|
|
|
# that were *not* already in the room. Care needs to be taken with the
|
|
|
|
# calculating the remote hosts, as a host may have already been in the
|
|
|
|
# room even if there is a newly joined user from that host.
|
|
|
|
newly_joined_local_users = []
|
|
|
|
newly_joined_remote_hosts = set()
|
|
|
|
for user_id in newly_joined_users:
|
|
|
|
if self.is_mine_id(user_id):
|
|
|
|
newly_joined_local_users.append(user_id)
|
|
|
|
else:
|
|
|
|
host = get_domain_from_id(user_id)
|
|
|
|
if host not in prev_remote_hosts:
|
|
|
|
newly_joined_remote_hosts.add(host)
|
2019-03-28 09:48:41 -04:00
|
|
|
|
2021-05-05 12:27:05 -04:00
|
|
|
# Send presence states of all local users in the room to newly joined
|
|
|
|
# remote servers. (We actually only send states for local users already
|
|
|
|
# in the room, as we'll send states for newly joined local users below.)
|
|
|
|
if prev_local_users and newly_joined_remote_hosts:
|
|
|
|
local_states = await self.current_state_for_users(prev_local_users)
|
2019-03-28 09:48:41 -04:00
|
|
|
|
|
|
|
# Filter out old presence, i.e. offline presence states where
|
|
|
|
# the user hasn't been active for a week. We can change this
|
|
|
|
# depending on what we want the UX to be, but at the least we
|
|
|
|
# should filter out offline presence where the state is just the
|
|
|
|
# default state.
|
|
|
|
now = self.clock.time_msec()
|
|
|
|
states = [
|
|
|
|
state
|
2021-05-05 12:27:05 -04:00
|
|
|
for state in local_states.values()
|
2019-03-28 09:48:41 -04:00
|
|
|
if state.state != PresenceState.OFFLINE
|
|
|
|
or now - state.last_active_ts < 7 * 24 * 60 * 60 * 1000
|
|
|
|
or state.status_msg is not None
|
|
|
|
]
|
|
|
|
|
2023-09-04 12:14:09 -04:00
|
|
|
await self._federation_queue.send_presence_to_destinations(
|
2021-05-05 12:27:05 -04:00
|
|
|
destinations=newly_joined_remote_hosts,
|
|
|
|
states=states,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Send presence states of newly joined users to all remote servers in
|
|
|
|
# the room
|
|
|
|
if newly_joined_local_users and (
|
|
|
|
prev_remote_hosts or newly_joined_remote_hosts
|
|
|
|
):
|
|
|
|
local_states = await self.current_state_for_users(newly_joined_local_users)
|
2023-09-04 12:14:09 -04:00
|
|
|
await self._federation_queue.send_presence_to_destinations(
|
2021-05-05 12:27:05 -04:00
|
|
|
destinations=prev_remote_hosts | newly_joined_remote_hosts,
|
|
|
|
states=list(local_states.values()),
|
|
|
|
)
|
2019-03-26 08:45:22 -04:00
|
|
|
|
2014-08-28 13:43:03 -04:00
|
|
|
|
2023-07-18 05:36:40 -04:00
|
|
|
def should_notify(
|
|
|
|
old_state: UserPresenceState, new_state: UserPresenceState, is_mine: bool
|
|
|
|
) -> bool:
|
2016-02-15 12:10:40 -05:00
|
|
|
"""Decides if a presence state change should be sent to interested parties."""
|
2023-07-18 05:36:40 -04:00
|
|
|
user_location = "remote"
|
|
|
|
if is_mine:
|
|
|
|
user_location = "local"
|
|
|
|
|
2016-09-06 05:28:35 -04:00
|
|
|
if old_state == new_state:
|
|
|
|
return False
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
if old_state.status_msg != new_state.status_msg:
|
2023-07-18 05:36:40 -04:00
|
|
|
notify_reason_counter.labels(user_location, "status_msg_change").inc()
|
2016-02-15 12:10:40 -05:00
|
|
|
return True
|
2014-08-28 13:43:03 -04:00
|
|
|
|
2016-09-06 05:23:38 -04:00
|
|
|
if old_state.state != new_state.state:
|
2023-07-18 05:36:40 -04:00
|
|
|
notify_reason_counter.labels(user_location, "state_change").inc()
|
|
|
|
state_transition_counter.labels(
|
|
|
|
user_location, old_state.state, new_state.state
|
|
|
|
).inc()
|
2016-09-06 05:23:38 -04:00
|
|
|
return True
|
2014-08-28 13:43:03 -04:00
|
|
|
|
2016-09-06 05:23:38 -04:00
|
|
|
if old_state.state == PresenceState.ONLINE:
|
2016-02-15 12:10:40 -05:00
|
|
|
if new_state.currently_active != old_state.currently_active:
|
2023-07-18 05:36:40 -04:00
|
|
|
notify_reason_counter.labels(user_location, "current_active_change").inc()
|
2016-02-15 12:10:40 -05:00
|
|
|
return True
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2016-08-30 10:00:14 -04:00
|
|
|
if (
|
|
|
|
new_state.last_active_ts - old_state.last_active_ts
|
|
|
|
> LAST_ACTIVE_GRANULARITY
|
|
|
|
):
|
2020-10-23 12:38:40 -04:00
|
|
|
# Only notify about last active bumps if we're not currently active
|
2016-09-06 05:23:38 -04:00
|
|
|
if not new_state.currently_active:
|
2023-07-18 05:36:40 -04:00
|
|
|
notify_reason_counter.labels(
|
|
|
|
user_location, "last_active_change_online"
|
|
|
|
).inc()
|
2016-08-30 10:00:14 -04:00
|
|
|
return True
|
|
|
|
|
|
|
|
elif new_state.last_active_ts - old_state.last_active_ts > LAST_ACTIVE_GRANULARITY:
|
2016-02-15 12:10:40 -05:00
|
|
|
# Always notify for a transition where last active gets bumped.
|
2023-07-18 05:36:40 -04:00
|
|
|
notify_reason_counter.labels(
|
|
|
|
user_location, "last_active_change_not_online"
|
|
|
|
).inc()
|
2016-02-15 12:10:40 -05:00
|
|
|
return True
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
return False
|
2015-05-14 10:29:58 -04:00
|
|
|
|
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
def format_user_presence_state(
|
|
|
|
state: UserPresenceState, now: int, include_user_id: bool = True
|
|
|
|
) -> JsonDict:
|
2021-10-21 12:42:25 -04:00
|
|
|
"""Convert UserPresenceState to a JSON format that can be sent down to clients
|
2016-02-15 12:10:40 -05:00
|
|
|
and to other servers.
|
2017-03-15 10:50:33 -04:00
|
|
|
|
2021-10-21 12:42:25 -04:00
|
|
|
Args:
|
|
|
|
state: The user presence state to format.
|
|
|
|
now: The current timestamp since the epoch in ms.
|
|
|
|
include_user_id: Whether to include `user_id` in the returned dictionary.
|
|
|
|
As this function can be used both to format presence updates for client /sync
|
|
|
|
responses and for federation /send requests, only the latter needs the include
|
|
|
|
the `user_id` field.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A JSON dictionary with the following keys:
|
|
|
|
* presence: The presence state as a str.
|
|
|
|
* user_id: Optional. Included if `include_user_id` is truthy. The canonical
|
|
|
|
Matrix ID of the user.
|
|
|
|
* last_active_ago: Optional. Included if `last_active_ts` is set on `state`.
|
|
|
|
The timestamp that the user was last active.
|
|
|
|
* status_msg: Optional. Included if `status_msg` is set on `state`. The user's
|
|
|
|
status.
|
|
|
|
* currently_active: Optional. Included only if `state.state` is "online".
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
{
|
|
|
|
"presence": "online",
|
|
|
|
"user_id": "@alice:example.com",
|
|
|
|
"last_active_ago": 16783813918,
|
|
|
|
"status_msg": "Hello world!",
|
|
|
|
"currently_active": True
|
|
|
|
}
|
2016-02-15 12:10:40 -05:00
|
|
|
"""
|
2021-10-18 15:01:10 -04:00
|
|
|
content: JsonDict = {"presence": state.state}
|
2017-03-15 10:27:34 -04:00
|
|
|
if include_user_id:
|
|
|
|
content["user_id"] = state.user_id
|
2016-02-18 05:11:43 -05:00
|
|
|
if state.last_active_ts:
|
|
|
|
content["last_active_ago"] = now - state.last_active_ts
|
2021-08-09 12:21:04 -04:00
|
|
|
if state.status_msg:
|
2016-02-15 12:10:40 -05:00
|
|
|
content["status_msg"] = state.status_msg
|
|
|
|
if state.state == PresenceState.ONLINE:
|
|
|
|
content["currently_active"] = state.currently_active
|
2015-05-14 10:29:58 -04:00
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
return content
|
2015-05-14 10:29:58 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-09-21 13:34:26 -04:00
|
|
|
class PresenceEventSource(EventSource[int, UserPresenceState]):
|
2020-10-29 11:18:17 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2016-05-16 14:08:40 -04:00
|
|
|
# We can't call get_presence_handler here because there's a cycle:
|
|
|
|
#
|
|
|
|
# Presence -> Notifier -> PresenceEventSource -> Presence
|
|
|
|
#
|
2021-05-18 09:13:45 -04:00
|
|
|
# Same with get_presence_router:
|
2021-04-06 09:38:30 -04:00
|
|
|
#
|
|
|
|
# AuthHandler -> Notifier -> PresenceEventSource -> ModuleApi -> AuthHandler
|
2016-05-16 14:08:40 -04:00
|
|
|
self.get_presence_handler = hs.get_presence_handler
|
2021-04-06 09:38:30 -04:00
|
|
|
self.get_presence_router = hs.get_presence_router
|
2014-08-29 12:09:15 -04:00
|
|
|
self.clock = hs.get_clock()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2014-08-29 12:09:15 -04:00
|
|
|
|
2020-02-26 10:33:26 -05:00
|
|
|
async def get_new_events(
|
2016-02-15 12:10:40 -05:00
|
|
|
self,
|
2021-04-28 06:07:47 -04:00
|
|
|
user: UserID,
|
|
|
|
from_key: Optional[int],
|
2022-10-14 08:30:05 -04:00
|
|
|
# Having a default limit doesn't match the EventSource API, but some
|
|
|
|
# callers do not provide it. It is unused in this class.
|
|
|
|
limit: int = 0,
|
2023-01-25 16:34:37 -05:00
|
|
|
room_ids: Optional[StrCollection] = None,
|
2021-09-21 13:34:26 -04:00
|
|
|
is_guest: bool = False,
|
2021-04-28 06:07:47 -04:00
|
|
|
explicit_room_id: Optional[str] = None,
|
2021-09-21 13:34:26 -04:00
|
|
|
include_offline: bool = True,
|
2021-10-27 12:27:23 -04:00
|
|
|
service: Optional[ApplicationService] = None,
|
2021-04-06 09:38:30 -04:00
|
|
|
) -> Tuple[List[UserPresenceState], int]:
|
2016-02-15 12:10:40 -05:00
|
|
|
# The process for getting presence events are:
|
|
|
|
# 1. Get the rooms the user is in.
|
|
|
|
# 2. Get the list of user in the rooms.
|
|
|
|
# 3. Get the list of users that are in the user's presence list.
|
|
|
|
# 4. If there is a from_key set, cross reference the list of users
|
|
|
|
# with the `presence_stream_cache` to see which ones we actually
|
|
|
|
# need to check.
|
|
|
|
# 5. Load current state for the users.
|
|
|
|
#
|
|
|
|
# We don't try and limit the presence updates by the current token, as
|
|
|
|
# sending down the rare duplicate is not a concern.
|
|
|
|
|
2021-04-06 09:38:30 -04:00
|
|
|
user_id = user.to_string()
|
|
|
|
stream_change_cache = self.store.presence_stream_cache
|
|
|
|
|
2016-02-23 09:03:46 -05:00
|
|
|
with Measure(self.clock, "presence.get_new_events"):
|
2016-02-23 08:49:16 -05:00
|
|
|
if from_key is not None:
|
|
|
|
from_key = int(from_key)
|
2014-08-29 12:09:15 -04:00
|
|
|
|
2021-05-18 09:13:45 -04:00
|
|
|
# Check if this user should receive all current, online user presence. We only
|
|
|
|
# bother to do this if from_key is set, as otherwise the user will receive all
|
|
|
|
# user presence anyways.
|
|
|
|
if await self.store.should_user_receive_full_presence_with_token(
|
|
|
|
user_id, from_key
|
|
|
|
):
|
|
|
|
# This user has been specified by a module to receive all current, online
|
|
|
|
# user presence. Removing from_key and setting include_offline to false
|
|
|
|
# will do effectively this.
|
|
|
|
from_key = None
|
|
|
|
include_offline = False
|
|
|
|
|
2019-06-21 06:10:27 -04:00
|
|
|
max_token = self.store.get_current_presence_token()
|
|
|
|
if from_key == max_token:
|
|
|
|
# This is necessary as due to the way stream ID generators work
|
|
|
|
# we may get updates that have a stream ID greater than the max
|
2019-07-01 05:22:42 -04:00
|
|
|
# token (e.g. max_token is N but stream generator may return
|
|
|
|
# results for N+2, due to N+1 not having finished being
|
|
|
|
# persisted yet).
|
|
|
|
#
|
|
|
|
# This is usually fine, as it just means that we may send down
|
|
|
|
# some presence updates multiple times. However, we need to be
|
|
|
|
# careful that the sync stream either actually does make some
|
|
|
|
# progress or doesn't return, otherwise clients will end up
|
|
|
|
# tight looping calling /sync due to it immediately returning
|
|
|
|
# the same token repeatedly.
|
|
|
|
#
|
|
|
|
# Hence this guard where we just return nothing so that the sync
|
|
|
|
# doesn't return. C.f. #5503.
|
2019-08-30 11:28:26 -04:00
|
|
|
return [], max_token
|
2019-06-21 06:10:27 -04:00
|
|
|
|
2022-07-25 05:21:06 -04:00
|
|
|
# Figure out which other users this user should explicitly receive
|
|
|
|
# updates for
|
|
|
|
additional_users_interested_in = (
|
|
|
|
await self.get_presence_router().get_interested_users(user.to_string())
|
|
|
|
)
|
2016-02-23 09:48:23 -05:00
|
|
|
|
2021-04-06 09:38:30 -04:00
|
|
|
# We have a set of users that we're interested in the presence of. We want to
|
|
|
|
# cross-reference that with the users that have actually changed their presence.
|
2016-02-23 09:48:23 -05:00
|
|
|
|
2021-04-06 09:38:30 -04:00
|
|
|
# Check whether this user should see all user updates
|
2020-10-29 11:18:17 -04:00
|
|
|
|
2022-07-25 05:21:06 -04:00
|
|
|
if additional_users_interested_in == PresenceRouter.ALL_USERS:
|
2021-04-06 09:38:30 -04:00
|
|
|
# Provide presence state for all users
|
|
|
|
presence_updates = await self._filter_all_presence_updates_for_user(
|
|
|
|
user_id, include_offline, from_key
|
|
|
|
)
|
2016-06-03 08:40:55 -04:00
|
|
|
|
2021-04-06 09:38:30 -04:00
|
|
|
return presence_updates, max_token
|
|
|
|
|
|
|
|
# Make mypy happy. users_interested_in should now be a set
|
2022-07-25 05:21:06 -04:00
|
|
|
assert not isinstance(additional_users_interested_in, str)
|
|
|
|
|
|
|
|
# We always care about our own presence.
|
|
|
|
additional_users_interested_in.add(user_id)
|
|
|
|
|
|
|
|
if explicit_room_id:
|
|
|
|
user_ids = await self.store.get_users_in_room(explicit_room_id)
|
|
|
|
additional_users_interested_in.update(user_ids)
|
2021-04-06 09:38:30 -04:00
|
|
|
|
|
|
|
# The set of users that we're interested in and that have had a presence update.
|
|
|
|
# We'll actually pull the presence updates for these users at the end.
|
2023-01-25 16:34:37 -05:00
|
|
|
interested_and_updated_users: StrCollection
|
2021-04-06 09:38:30 -04:00
|
|
|
|
2022-03-30 07:10:12 -04:00
|
|
|
if from_key is not None:
|
2021-04-06 09:38:30 -04:00
|
|
|
# First get all users that have had a presence update
|
2022-12-05 15:19:14 -05:00
|
|
|
result = stream_change_cache.get_all_entities_changed(from_key)
|
2021-04-06 09:38:30 -04:00
|
|
|
|
|
|
|
# Cross-reference users we're interested in with those that have had updates.
|
2022-12-05 15:19:14 -05:00
|
|
|
if result.hit:
|
|
|
|
updated_users = result.entities
|
|
|
|
|
2022-07-25 05:21:06 -04:00
|
|
|
# If we have the full list of changes for presence we can
|
|
|
|
# simply check which ones share a room with the user.
|
2021-04-06 09:38:30 -04:00
|
|
|
get_updates_counter.labels("stream").inc()
|
2022-07-25 05:21:06 -04:00
|
|
|
|
|
|
|
sharing_users = await self.store.do_users_share_a_room(
|
|
|
|
user_id, updated_users
|
|
|
|
)
|
|
|
|
|
|
|
|
interested_and_updated_users = (
|
|
|
|
sharing_users.union(additional_users_interested_in)
|
|
|
|
).intersection(updated_users)
|
|
|
|
|
2016-02-23 09:48:23 -05:00
|
|
|
else:
|
2021-04-06 09:38:30 -04:00
|
|
|
# Too many possible updates. Find all users we can see and check
|
|
|
|
# if any of them have changed.
|
|
|
|
get_updates_counter.labels("full").inc()
|
2014-08-29 12:09:15 -04:00
|
|
|
|
2022-07-25 05:21:06 -04:00
|
|
|
users_interested_in = (
|
|
|
|
await self.store.get_users_who_share_room_with_user(user_id)
|
|
|
|
)
|
|
|
|
users_interested_in.update(additional_users_interested_in)
|
|
|
|
|
2021-04-06 09:38:30 -04:00
|
|
|
interested_and_updated_users = (
|
|
|
|
stream_change_cache.get_entities_changed(
|
|
|
|
users_interested_in, from_key
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# No from_key has been specified. Return the presence for all users
|
|
|
|
# this user is interested in
|
2022-07-25 05:21:06 -04:00
|
|
|
interested_and_updated_users = (
|
|
|
|
await self.store.get_users_who_share_room_with_user(user_id)
|
|
|
|
)
|
|
|
|
interested_and_updated_users.update(additional_users_interested_in)
|
2021-04-06 09:38:30 -04:00
|
|
|
|
|
|
|
# Retrieve the current presence state for each user
|
|
|
|
users_to_state = await self.get_presence_handler().current_state_for_users(
|
|
|
|
interested_and_updated_users
|
|
|
|
)
|
|
|
|
presence_updates = list(users_to_state.values())
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-04-06 09:38:30 -04:00
|
|
|
if not include_offline:
|
|
|
|
# Filter out offline presence states
|
|
|
|
presence_updates = self._filter_offline_presence_state(presence_updates)
|
|
|
|
|
|
|
|
return presence_updates, max_token
|
|
|
|
|
|
|
|
async def _filter_all_presence_updates_for_user(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
include_offline: bool,
|
|
|
|
from_key: Optional[int] = None,
|
|
|
|
) -> List[UserPresenceState]:
|
|
|
|
"""
|
|
|
|
Computes the presence updates a user should receive.
|
|
|
|
|
|
|
|
First pulls presence updates from the database. Then consults PresenceRouter
|
|
|
|
for whether any updates should be excluded by user ID.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: The User ID of the user to compute presence updates for.
|
|
|
|
include_offline: Whether to include offline presence states from the results.
|
|
|
|
from_key: The minimum stream ID of updates to pull from the database
|
|
|
|
before filtering.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of presence states for the given user to receive.
|
|
|
|
"""
|
2022-12-02 10:28:41 -05:00
|
|
|
updated_users = None
|
2021-04-06 09:38:30 -04:00
|
|
|
if from_key:
|
|
|
|
# Only return updates since the last sync
|
2022-12-05 15:19:14 -05:00
|
|
|
result = self.store.presence_stream_cache.get_all_entities_changed(from_key)
|
|
|
|
if result.hit:
|
|
|
|
updated_users = result.entities
|
2021-04-06 09:38:30 -04:00
|
|
|
|
2022-12-02 10:28:41 -05:00
|
|
|
if updated_users is not None:
|
2021-04-06 09:38:30 -04:00
|
|
|
# Get the actual presence update for each change
|
|
|
|
users_to_state = await self.get_presence_handler().current_state_for_users(
|
|
|
|
updated_users
|
|
|
|
)
|
|
|
|
presence_updates = list(users_to_state.values())
|
|
|
|
|
|
|
|
if not include_offline:
|
|
|
|
# Filter out offline states
|
|
|
|
presence_updates = self._filter_offline_presence_state(presence_updates)
|
2017-03-15 10:27:34 -04:00
|
|
|
else:
|
2021-04-06 09:38:30 -04:00
|
|
|
users_to_state = await self.store.get_presence_for_all_users(
|
|
|
|
include_offline=include_offline
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-04-06 09:38:30 -04:00
|
|
|
presence_updates = list(users_to_state.values())
|
|
|
|
|
|
|
|
# TODO: This feels wildly inefficient, and it's unfortunate we need to ask the
|
|
|
|
# module for information on a number of users when we then only take the info
|
|
|
|
# for a single user
|
|
|
|
|
|
|
|
# Filter through the presence router
|
|
|
|
users_to_state_set = await self.get_presence_router().get_users_for_states(
|
|
|
|
presence_updates
|
|
|
|
)
|
|
|
|
|
|
|
|
# We only want the mapping for the syncing user
|
|
|
|
presence_updates = list(users_to_state_set[user_id])
|
|
|
|
|
|
|
|
# Return presence information for all users
|
|
|
|
return presence_updates
|
|
|
|
|
|
|
|
def _filter_offline_presence_state(
|
|
|
|
self, presence_updates: Iterable[UserPresenceState]
|
|
|
|
) -> List[UserPresenceState]:
|
|
|
|
"""Given an iterable containing user presence updates, return a list with any offline
|
|
|
|
presence states removed.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
presence_updates: Presence states to filter
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A new list with any offline presence states removed.
|
|
|
|
"""
|
|
|
|
return [
|
|
|
|
update
|
|
|
|
for update in presence_updates
|
|
|
|
if update.state != PresenceState.OFFLINE
|
|
|
|
]
|
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
def get_current_key(self) -> int:
|
2016-02-15 12:10:40 -05:00
|
|
|
return self.store.get_current_presence_token()
|
2014-08-13 14:18:55 -04:00
|
|
|
|
2016-02-18 06:52:33 -05:00
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
def handle_timeouts(
|
|
|
|
user_states: List[UserPresenceState],
|
|
|
|
is_mine_fn: Callable[[str], bool],
|
2023-09-05 09:58:51 -04:00
|
|
|
syncing_user_devices: AbstractSet[Tuple[str, Optional[str]]],
|
|
|
|
user_to_devices: Dict[str, Dict[Optional[str], UserDevicePresenceState]],
|
2021-04-28 06:07:47 -04:00
|
|
|
now: int,
|
|
|
|
) -> List[UserPresenceState]:
|
2016-02-18 06:52:33 -05:00
|
|
|
"""Checks the presence of users that have timed out and updates as
|
|
|
|
appropriate.
|
|
|
|
|
|
|
|
Args:
|
2021-04-28 06:07:47 -04:00
|
|
|
user_states: List of UserPresenceState's to check.
|
|
|
|
is_mine_fn: Function that returns if a user_id is ours
|
2023-09-05 09:58:51 -04:00
|
|
|
syncing_user_devices: A set of (user ID, device ID) tuples with active syncs..
|
|
|
|
user_to_devices: A map of user ID to device ID to UserDevicePresenceState.
|
2021-04-28 06:07:47 -04:00
|
|
|
now: Current time in ms.
|
2016-02-18 06:52:33 -05:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of UserPresenceState updates
|
|
|
|
"""
|
|
|
|
changes = {} # Actual changes we need to notify people about
|
|
|
|
|
|
|
|
for state in user_states:
|
2023-09-05 09:58:51 -04:00
|
|
|
user_id = state.user_id
|
|
|
|
is_mine = is_mine_fn(user_id)
|
|
|
|
|
|
|
|
new_state = handle_timeout(
|
|
|
|
state,
|
|
|
|
is_mine,
|
|
|
|
syncing_user_devices,
|
|
|
|
user_to_devices.get(user_id, {}),
|
|
|
|
now,
|
|
|
|
)
|
2016-02-18 06:52:33 -05:00
|
|
|
if new_state:
|
|
|
|
changes[state.user_id] = new_state
|
|
|
|
|
2018-05-31 05:03:47 -04:00
|
|
|
return list(changes.values())
|
2016-02-18 06:52:33 -05:00
|
|
|
|
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
def handle_timeout(
|
2023-09-05 09:58:51 -04:00
|
|
|
state: UserPresenceState,
|
|
|
|
is_mine: bool,
|
|
|
|
syncing_device_ids: AbstractSet[Tuple[str, Optional[str]]],
|
|
|
|
user_devices: Dict[Optional[str], UserDevicePresenceState],
|
|
|
|
now: int,
|
2021-04-28 06:07:47 -04:00
|
|
|
) -> Optional[UserPresenceState]:
|
2016-02-18 06:52:33 -05:00
|
|
|
"""Checks the presence of the user to see if any of the timers have elapsed
|
|
|
|
|
|
|
|
Args:
|
2023-09-05 09:58:51 -04:00
|
|
|
state: UserPresenceState to check.
|
2021-04-28 06:07:47 -04:00
|
|
|
is_mine: Whether the user is ours
|
2023-09-05 09:58:51 -04:00
|
|
|
syncing_user_devices: A set of (user ID, device ID) tuples with active syncs..
|
|
|
|
user_devices: A map of device ID to UserDevicePresenceState.
|
2021-04-28 06:07:47 -04:00
|
|
|
now: Current time in ms.
|
2016-02-18 06:52:33 -05:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A UserPresenceState update or None if no update.
|
|
|
|
"""
|
|
|
|
if state.state == PresenceState.OFFLINE:
|
|
|
|
# No timeouts are associated with offline states.
|
|
|
|
return None
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
if is_mine:
|
2023-09-05 09:58:51 -04:00
|
|
|
# Check per-device whether the device should be considered idle or offline
|
|
|
|
# due to timeouts.
|
|
|
|
device_changed = False
|
|
|
|
offline_devices = []
|
|
|
|
for device_id, device_state in user_devices.items():
|
|
|
|
if device_state.state == PresenceState.ONLINE:
|
|
|
|
if now - device_state.last_active_ts > IDLE_TIMER:
|
|
|
|
# Currently online, but last activity ages ago so auto
|
|
|
|
# idle
|
|
|
|
device_state.state = PresenceState.UNAVAILABLE
|
|
|
|
device_changed = True
|
|
|
|
|
|
|
|
# If there are have been no sync for a while (and none ongoing),
|
|
|
|
# set presence to offline.
|
|
|
|
if (state.user_id, device_id) not in syncing_device_ids:
|
|
|
|
# If the user has done something recently but hasn't synced,
|
|
|
|
# don't set them as offline.
|
|
|
|
sync_or_active = max(
|
|
|
|
device_state.last_sync_ts, device_state.last_active_ts
|
|
|
|
)
|
|
|
|
|
2023-09-05 10:39:38 -04:00
|
|
|
# Implementations aren't meant to timeout a device with a busy
|
|
|
|
# state, but it needs to timeout *eventually* or else the user
|
|
|
|
# will be stuck in that state.
|
|
|
|
online_timeout = (
|
|
|
|
BUSY_ONLINE_TIMEOUT
|
|
|
|
if device_state.state == PresenceState.BUSY
|
|
|
|
else SYNC_ONLINE_TIMEOUT
|
|
|
|
)
|
|
|
|
if now - sync_or_active > online_timeout:
|
2023-09-05 09:58:51 -04:00
|
|
|
# Mark the device as going offline.
|
|
|
|
offline_devices.append(device_id)
|
|
|
|
device_changed = True
|
|
|
|
|
|
|
|
# Offline devices are not needed and do not add information.
|
|
|
|
for device_id in offline_devices:
|
|
|
|
user_devices.pop(device_id)
|
|
|
|
|
|
|
|
# If the presence state of the devices changed, then (maybe) update
|
|
|
|
# the user's overall presence state.
|
|
|
|
if device_changed:
|
|
|
|
new_presence = _combine_device_states(user_devices.values())
|
|
|
|
if new_presence != state.state:
|
|
|
|
state = state.copy_and_replace(state=new_presence)
|
2016-02-18 06:52:33 -05:00
|
|
|
changed = True
|
|
|
|
|
2023-09-05 09:58:51 -04:00
|
|
|
if now - state.last_active_ts > LAST_ACTIVE_GRANULARITY:
|
|
|
|
# So that we send down a notification that we've
|
|
|
|
# stopped updating.
|
|
|
|
changed = True
|
|
|
|
|
2016-02-18 06:52:33 -05:00
|
|
|
if now - state.last_federation_update_ts > FEDERATION_PING_INTERVAL:
|
|
|
|
# Need to send ping to other servers to ensure they don't
|
|
|
|
# timeout and set us to offline
|
|
|
|
changed = True
|
|
|
|
else:
|
2017-11-16 20:53:50 -05:00
|
|
|
# We expect to be poked occasionally by the other side.
|
2016-02-18 06:52:33 -05:00
|
|
|
# This is to protect against forgetful/buggy servers, so that
|
|
|
|
# no one gets stuck online forever.
|
|
|
|
if now - state.last_federation_update_ts > FEDERATION_TIMEOUT:
|
|
|
|
# The other side seems to have disappeared.
|
2021-08-09 12:21:04 -04:00
|
|
|
state = state.copy_and_replace(state=PresenceState.OFFLINE)
|
2016-02-18 06:52:33 -05:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
return state if changed else None
|
|
|
|
|
|
|
|
|
2021-04-28 06:07:47 -04:00
|
|
|
def handle_update(
|
|
|
|
prev_state: UserPresenceState,
|
|
|
|
new_state: UserPresenceState,
|
|
|
|
is_mine: bool,
|
|
|
|
wheel_timer: WheelTimer,
|
|
|
|
now: int,
|
|
|
|
) -> Tuple[UserPresenceState, bool, bool]:
|
2016-02-18 06:52:33 -05:00
|
|
|
"""Given a presence update:
|
|
|
|
1. Add any appropriate timers.
|
|
|
|
2. Check if we should notify anyone.
|
|
|
|
|
|
|
|
Args:
|
2021-04-28 06:07:47 -04:00
|
|
|
prev_state
|
|
|
|
new_state
|
|
|
|
is_mine: Whether the user is ours
|
|
|
|
wheel_timer
|
|
|
|
now: Time now in ms
|
2016-02-18 06:52:33 -05:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
3-tuple: `(new_state, persist_and_notify, federation_ping)` where:
|
|
|
|
- new_state: is the state to actually persist
|
2021-04-28 06:07:47 -04:00
|
|
|
- persist_and_notify: whether to persist and notify people
|
|
|
|
- federation_ping: whether we should send a ping over federation
|
2016-02-18 06:52:33 -05:00
|
|
|
"""
|
|
|
|
user_id = new_state.user_id
|
|
|
|
|
|
|
|
persist_and_notify = False
|
|
|
|
federation_ping = False
|
|
|
|
|
|
|
|
# If the users are ours then we want to set up a bunch of timers
|
|
|
|
# to time things out.
|
|
|
|
if is_mine:
|
|
|
|
if new_state.state == PresenceState.ONLINE:
|
|
|
|
# Idle timer
|
|
|
|
wheel_timer.insert(
|
|
|
|
now=now, obj=user_id, then=new_state.last_active_ts + IDLE_TIMER
|
|
|
|
)
|
|
|
|
|
2016-02-19 05:58:27 -05:00
|
|
|
active = now - new_state.last_active_ts < LAST_ACTIVE_GRANULARITY
|
|
|
|
new_state = new_state.copy_and_replace(currently_active=active)
|
|
|
|
|
|
|
|
if active:
|
|
|
|
wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=user_id,
|
|
|
|
then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY,
|
|
|
|
)
|
|
|
|
|
2016-02-18 06:52:33 -05:00
|
|
|
if new_state.state != PresenceState.OFFLINE:
|
|
|
|
# User has stopped syncing
|
|
|
|
wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=user_id,
|
|
|
|
then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT,
|
|
|
|
)
|
|
|
|
|
|
|
|
last_federate = new_state.last_federation_update_ts
|
|
|
|
if now - last_federate > FEDERATION_PING_INTERVAL:
|
|
|
|
# Been a while since we've poked remote servers
|
|
|
|
new_state = new_state.copy_and_replace(last_federation_update_ts=now)
|
|
|
|
federation_ping = True
|
|
|
|
|
2023-09-05 10:39:38 -04:00
|
|
|
if new_state.state == PresenceState.BUSY:
|
|
|
|
wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=user_id,
|
|
|
|
then=new_state.last_user_sync_ts + BUSY_ONLINE_TIMEOUT,
|
|
|
|
)
|
|
|
|
|
2016-02-18 06:52:33 -05:00
|
|
|
else:
|
|
|
|
wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=user_id,
|
|
|
|
then=new_state.last_federation_update_ts + FEDERATION_TIMEOUT,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check whether the change was something worth notifying about
|
2023-07-18 05:36:40 -04:00
|
|
|
if should_notify(prev_state, new_state, is_mine):
|
2016-02-18 06:52:33 -05:00
|
|
|
new_state = new_state.copy_and_replace(last_federation_update_ts=now)
|
|
|
|
persist_and_notify = True
|
|
|
|
|
|
|
|
return new_state, persist_and_notify, federation_ping
|
2017-04-11 10:19:26 -04:00
|
|
|
|
2017-04-11 10:30:02 -04:00
|
|
|
|
2023-09-05 09:58:51 -04:00
|
|
|
PRESENCE_BY_PRIORITY = {
|
|
|
|
PresenceState.BUSY: 4,
|
|
|
|
PresenceState.ONLINE: 3,
|
|
|
|
PresenceState.UNAVAILABLE: 2,
|
|
|
|
PresenceState.OFFLINE: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _combine_device_states(
|
|
|
|
device_states: Iterable[UserDevicePresenceState],
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Find the device to use presence information from.
|
|
|
|
|
|
|
|
Orders devices by priority, then last_active_ts.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
device_states: An iterable of device presence states
|
|
|
|
|
|
|
|
Return:
|
|
|
|
The combined presence state.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Based on (all) the user's devices calculate the new presence state.
|
|
|
|
presence = PresenceState.OFFLINE
|
|
|
|
last_active_ts = -1
|
|
|
|
|
|
|
|
# Find the device to use the presence state of based on the presence priority,
|
|
|
|
# but tie-break with how recently the device has been seen.
|
|
|
|
for device_state in device_states:
|
|
|
|
if (PRESENCE_BY_PRIORITY[device_state.state], device_state.last_active_ts) > (
|
|
|
|
PRESENCE_BY_PRIORITY[presence],
|
|
|
|
last_active_ts,
|
|
|
|
):
|
|
|
|
presence = device_state.state
|
|
|
|
last_active_ts = device_state.last_active_ts
|
|
|
|
|
|
|
|
return presence
|
|
|
|
|
|
|
|
|
2020-07-23 16:47:36 -04:00
|
|
|
async def get_interested_parties(
|
2021-04-06 09:38:30 -04:00
|
|
|
store: DataStore, presence_router: PresenceRouter, states: List[UserPresenceState]
|
2020-07-23 16:47:36 -04:00
|
|
|
) -> Tuple[Dict[str, List[UserPresenceState]], Dict[str, List[UserPresenceState]]]:
|
2017-04-11 10:30:02 -04:00
|
|
|
"""Given a list of states return which entities (rooms, users)
|
|
|
|
are interested in the given states.
|
|
|
|
|
|
|
|
Args:
|
2021-04-06 09:38:30 -04:00
|
|
|
store: The homeserver's data store.
|
|
|
|
presence_router: A module for augmenting the destinations for presence updates.
|
|
|
|
states: A list of incoming user presence updates.
|
2017-04-11 10:30:02 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-07-23 16:47:36 -04:00
|
|
|
A 2-tuple of `(room_ids_to_states, users_to_states)`,
|
2017-04-11 10:30:02 -04:00
|
|
|
with each item being a dict of `entity_name` -> `[UserPresenceState]`
|
|
|
|
"""
|
2021-07-16 13:22:36 -04:00
|
|
|
room_ids_to_states: Dict[str, List[UserPresenceState]] = {}
|
|
|
|
users_to_states: Dict[str, List[UserPresenceState]] = {}
|
2017-04-11 10:30:02 -04:00
|
|
|
for state in states:
|
2020-07-23 16:47:36 -04:00
|
|
|
room_ids = await store.get_rooms_for_user(state.user_id)
|
2017-04-11 10:30:02 -04:00
|
|
|
for room_id in room_ids:
|
|
|
|
room_ids_to_states.setdefault(room_id, []).append(state)
|
|
|
|
|
|
|
|
# Always notify self
|
|
|
|
users_to_states.setdefault(state.user_id, []).append(state)
|
|
|
|
|
2021-04-06 09:38:30 -04:00
|
|
|
# Ask a presence routing module for any additional parties if one
|
|
|
|
# is loaded.
|
|
|
|
router_users_to_states = await presence_router.get_users_for_states(states)
|
|
|
|
|
|
|
|
# Update the dictionaries with additional destinations and state to send
|
|
|
|
for user_id, user_states in router_users_to_states.items():
|
|
|
|
users_to_states.setdefault(user_id, []).extend(user_states)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return room_ids_to_states, users_to_states
|
2017-04-11 10:30:02 -04:00
|
|
|
|
|
|
|
|
2020-07-23 16:47:36 -04:00
|
|
|
async def get_interested_remotes(
|
2021-04-06 09:38:30 -04:00
|
|
|
store: DataStore,
|
|
|
|
presence_router: PresenceRouter,
|
|
|
|
states: List[UserPresenceState],
|
2021-06-11 10:19:42 -04:00
|
|
|
) -> Dict[str, Set[UserPresenceState]]:
|
2017-04-11 10:19:26 -04:00
|
|
|
"""Given a list of presence states figure out which remote servers
|
|
|
|
should be sent which.
|
|
|
|
|
|
|
|
All the presence states should be for local users only.
|
|
|
|
|
|
|
|
Args:
|
2021-04-06 09:38:30 -04:00
|
|
|
store: The homeserver's data store.
|
|
|
|
presence_router: A module for augmenting the destinations for presence updates.
|
|
|
|
states: A list of incoming user presence updates.
|
2017-04-11 10:19:26 -04:00
|
|
|
|
|
|
|
Returns:
|
2021-06-11 10:19:42 -04:00
|
|
|
A map from destinations to presence states to send to that destination.
|
2017-04-11 10:19:26 -04:00
|
|
|
"""
|
2021-06-11 10:19:42 -04:00
|
|
|
hosts_and_states: Dict[str, Set[UserPresenceState]] = {}
|
2017-04-11 10:28:24 -04:00
|
|
|
|
2017-04-11 10:19:26 -04:00
|
|
|
# First we look up the rooms each user is in (as well as any explicit
|
|
|
|
# subscriptions), then for each distinct room we look up the remote
|
|
|
|
# hosts in those rooms.
|
2021-04-06 09:38:30 -04:00
|
|
|
room_ids_to_states, users_to_states = await get_interested_parties(
|
|
|
|
store, presence_router, states
|
|
|
|
)
|
2017-04-11 10:19:26 -04:00
|
|
|
|
2020-06-15 07:03:36 -04:00
|
|
|
for room_id, states in room_ids_to_states.items():
|
2022-08-24 15:15:37 -04:00
|
|
|
hosts = await store.get_current_hosts_in_room(room_id)
|
2021-06-11 10:19:42 -04:00
|
|
|
for host in hosts:
|
|
|
|
hosts_and_states.setdefault(host, set()).update(states)
|
2017-04-11 10:19:26 -04:00
|
|
|
|
2020-06-15 07:03:36 -04:00
|
|
|
for user_id, states in users_to_states.items():
|
2017-04-11 10:19:26 -04:00
|
|
|
host = get_domain_from_id(user_id)
|
2021-06-11 10:19:42 -04:00
|
|
|
hosts_and_states.setdefault(host, set()).update(states)
|
2017-04-11 10:19:26 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return hosts_and_states
|
2021-04-20 09:11:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PresenceFederationQueue:
|
|
|
|
"""Handles sending ad hoc presence updates over federation, which are *not*
|
|
|
|
due to state updates (that get handled via the presence stream), e.g.
|
|
|
|
federation pings and sending existing present states to newly joined hosts.
|
|
|
|
|
|
|
|
Only the last N minutes will be queued, so if a federation sender instance
|
|
|
|
is down for longer then some updates will be dropped. This is OK as presence
|
|
|
|
is ephemeral, and so it will self correct eventually.
|
|
|
|
|
|
|
|
On workers the class tracks the last received position of the stream from
|
|
|
|
replication, and handles querying for missed updates over HTTP replication,
|
|
|
|
c.f. `get_current_token` and `get_replication_rows`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# How long to keep entries in the queue for. Workers that are down for
|
|
|
|
# longer than this duration will miss out on older updates.
|
|
|
|
_KEEP_ITEMS_IN_QUEUE_FOR_MS = 5 * 60 * 1000
|
|
|
|
|
|
|
|
# How often to check if we can expire entries from the queue.
|
|
|
|
_CLEAR_ITEMS_EVERY_MS = 60 * 1000
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer", presence_handler: BasePresenceHandler):
|
|
|
|
self._clock = hs.get_clock()
|
|
|
|
self._notifier = hs.get_notifier()
|
|
|
|
self._instance_name = hs.get_instance_name()
|
|
|
|
self._presence_handler = presence_handler
|
|
|
|
self._repl_client = ReplicationGetStreamUpdates.make_client(hs)
|
|
|
|
|
|
|
|
# Should we keep a queue of recent presence updates? We only bother if
|
|
|
|
# another process may be handling federation sending.
|
|
|
|
self._queue_presence_updates = True
|
|
|
|
|
|
|
|
# Whether this instance is a presence writer.
|
2021-04-23 07:21:55 -04:00
|
|
|
self._presence_writer = self._instance_name in hs.config.worker.writers.presence
|
2021-04-20 09:11:24 -04:00
|
|
|
|
|
|
|
# The FederationSender instance, if this process sends federation traffic directly.
|
|
|
|
self._federation = None
|
|
|
|
|
|
|
|
if hs.should_send_federation():
|
|
|
|
self._federation = hs.get_federation_sender()
|
|
|
|
|
|
|
|
# We don't bother queuing up presence states if only this instance
|
|
|
|
# is sending federation.
|
|
|
|
if hs.config.worker.federation_shard_config.instances == [
|
|
|
|
self._instance_name
|
|
|
|
]:
|
|
|
|
self._queue_presence_updates = False
|
|
|
|
|
|
|
|
# The queue of recently queued updates as tuples of: `(timestamp,
|
|
|
|
# stream_id, destinations, user_ids)`. We don't store the full states
|
|
|
|
# for efficiency, and remote workers will already have the full states
|
|
|
|
# cached.
|
2023-01-25 16:34:37 -05:00
|
|
|
self._queue: List[Tuple[int, int, StrCollection, Set[str]]] = []
|
2021-04-20 09:11:24 -04:00
|
|
|
|
|
|
|
self._next_id = 1
|
|
|
|
|
|
|
|
# Map from instance name to current token
|
2021-07-16 13:22:36 -04:00
|
|
|
self._current_tokens: Dict[str, int] = {}
|
2021-04-20 09:11:24 -04:00
|
|
|
|
|
|
|
if self._queue_presence_updates:
|
|
|
|
self._clock.looping_call(self._clear_queue, self._CLEAR_ITEMS_EVERY_MS)
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
def _clear_queue(self) -> None:
|
2021-04-20 09:11:24 -04:00
|
|
|
"""Clear out older entries from the queue."""
|
|
|
|
clear_before = self._clock.time_msec() - self._KEEP_ITEMS_IN_QUEUE_FOR_MS
|
|
|
|
|
|
|
|
# The queue is sorted by timestamp, so we can bisect to find the right
|
|
|
|
# place to purge before. Note that we are searching using a 1-tuple with
|
|
|
|
# the time, which does The Right Thing since the queue is a tuple where
|
|
|
|
# the first item is a timestamp.
|
|
|
|
index = bisect(self._queue, (clear_before,))
|
|
|
|
self._queue = self._queue[index:]
|
|
|
|
|
2023-09-04 12:14:09 -04:00
|
|
|
async def send_presence_to_destinations(
|
2023-01-25 16:34:37 -05:00
|
|
|
self, states: Collection[UserPresenceState], destinations: StrCollection
|
2021-04-20 09:11:24 -04:00
|
|
|
) -> None:
|
|
|
|
"""Send the presence states to the given destinations.
|
|
|
|
|
|
|
|
Will forward to the local federation sender (if there is one) and queue
|
|
|
|
to send over replication (if there are other federation sender instances.).
|
|
|
|
|
2021-04-23 07:21:55 -04:00
|
|
|
Must only be called on the presence writer process.
|
2021-04-20 09:11:24 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
# This should only be called on a presence writer.
|
|
|
|
assert self._presence_writer
|
|
|
|
|
2023-01-16 09:20:12 -05:00
|
|
|
if not states or not destinations:
|
|
|
|
# Ignore calls which either don't have any new states or don't need
|
|
|
|
# to be sent anywhere.
|
|
|
|
return
|
|
|
|
|
2021-04-20 09:11:24 -04:00
|
|
|
if self._federation:
|
2023-09-04 12:14:09 -04:00
|
|
|
await self._federation.send_presence_to_destinations(
|
2021-04-20 09:11:24 -04:00
|
|
|
states=states,
|
|
|
|
destinations=destinations,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not self._queue_presence_updates:
|
|
|
|
return
|
|
|
|
|
|
|
|
now = self._clock.time_msec()
|
|
|
|
|
|
|
|
stream_id = self._next_id
|
|
|
|
self._next_id += 1
|
|
|
|
|
|
|
|
self._queue.append((now, stream_id, destinations, {s.user_id for s in states}))
|
|
|
|
|
|
|
|
self._notifier.notify_replication()
|
|
|
|
|
|
|
|
def get_current_token(self, instance_name: str) -> int:
|
|
|
|
"""Get the current position of the stream.
|
|
|
|
|
|
|
|
On workers this returns the last stream ID received from replication.
|
|
|
|
"""
|
|
|
|
if instance_name == self._instance_name:
|
|
|
|
return self._next_id - 1
|
|
|
|
else:
|
|
|
|
return self._current_tokens.get(instance_name, 0)
|
|
|
|
|
|
|
|
async def get_replication_rows(
|
|
|
|
self,
|
|
|
|
instance_name: str,
|
|
|
|
from_token: int,
|
|
|
|
upto_token: int,
|
|
|
|
target_row_count: int,
|
|
|
|
) -> Tuple[List[Tuple[int, Tuple[str, str]]], int, bool]:
|
|
|
|
"""Get all the updates between the two tokens.
|
|
|
|
|
|
|
|
We return rows in the form of `(destination, user_id)` to keep the size
|
|
|
|
of each row bounded (rather than returning the sets in a row).
|
|
|
|
|
2021-04-23 07:21:55 -04:00
|
|
|
On workers this will query the presence writer process via HTTP replication.
|
2021-04-20 09:11:24 -04:00
|
|
|
"""
|
|
|
|
if instance_name != self._instance_name:
|
2021-04-23 07:21:55 -04:00
|
|
|
# If not local we query over http replication from the presence
|
|
|
|
# writer
|
2021-04-20 09:11:24 -04:00
|
|
|
result = await self._repl_client(
|
|
|
|
instance_name=instance_name,
|
|
|
|
stream_name=PresenceFederationStream.NAME,
|
|
|
|
from_token=from_token,
|
|
|
|
upto_token=upto_token,
|
|
|
|
)
|
|
|
|
return result["updates"], result["upto_token"], result["limited"]
|
|
|
|
|
2021-04-28 09:42:50 -04:00
|
|
|
# If the from_token is the current token then there's nothing to return
|
|
|
|
# and we can trivially no-op.
|
|
|
|
if from_token == self._next_id - 1:
|
|
|
|
return [], upto_token, False
|
|
|
|
|
2021-04-20 09:11:24 -04:00
|
|
|
# We can find the correct position in the queue by noting that there is
|
|
|
|
# exactly one entry per stream ID, and that the last entry has an ID of
|
|
|
|
# `self._next_id - 1`, so we can count backwards from the end.
|
|
|
|
#
|
2021-04-28 09:42:50 -04:00
|
|
|
# Since we are returning all states in the range `from_token < stream_id
|
|
|
|
# <= upto_token` we look for the index with a `stream_id` of `from_token
|
|
|
|
# + 1`.
|
|
|
|
#
|
2021-04-20 09:11:24 -04:00
|
|
|
# Since the start of the queue is periodically truncated we need to
|
|
|
|
# handle the case where `from_token` stream ID has already been dropped.
|
2021-04-28 09:42:50 -04:00
|
|
|
start_idx = max(from_token + 1 - self._next_id, -len(self._queue))
|
2021-04-20 09:11:24 -04:00
|
|
|
|
2021-07-16 13:22:36 -04:00
|
|
|
to_send: List[Tuple[int, Tuple[str, str]]] = []
|
2021-04-20 09:11:24 -04:00
|
|
|
limited = False
|
|
|
|
new_id = upto_token
|
|
|
|
for _, stream_id, destinations, user_ids in self._queue[start_idx:]:
|
2021-04-28 09:42:50 -04:00
|
|
|
if stream_id <= from_token:
|
|
|
|
# Paranoia check that we are actually only sending states that
|
|
|
|
# are have stream_id strictly greater than from_token. We should
|
|
|
|
# never hit this.
|
|
|
|
logger.warning(
|
|
|
|
"Tried returning presence federation stream ID: %d less than from_token: %d (next_id: %d, len: %d)",
|
|
|
|
stream_id,
|
|
|
|
from_token,
|
|
|
|
self._next_id,
|
|
|
|
len(self._queue),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2021-04-20 09:11:24 -04:00
|
|
|
if stream_id > upto_token:
|
|
|
|
break
|
|
|
|
|
|
|
|
new_id = stream_id
|
|
|
|
|
|
|
|
to_send.extend(
|
|
|
|
(stream_id, (destination, user_id))
|
|
|
|
for destination in destinations
|
|
|
|
for user_id in user_ids
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(to_send) > target_row_count:
|
|
|
|
limited = True
|
|
|
|
break
|
|
|
|
|
|
|
|
return to_send, new_id, limited
|
|
|
|
|
|
|
|
async def process_replication_rows(
|
|
|
|
self, stream_name: str, instance_name: str, token: int, rows: list
|
2021-09-20 08:56:23 -04:00
|
|
|
) -> None:
|
2021-04-20 09:11:24 -04:00
|
|
|
if stream_name != PresenceFederationStream.NAME:
|
|
|
|
return
|
|
|
|
|
|
|
|
# We keep track of the current tokens (so that we can catch up with anything we missed after a disconnect)
|
|
|
|
self._current_tokens[instance_name] = token
|
|
|
|
|
|
|
|
# If we're a federation sender we pull out the presence states to send
|
|
|
|
# and forward them on.
|
|
|
|
if not self._federation:
|
|
|
|
return
|
|
|
|
|
2021-07-16 13:22:36 -04:00
|
|
|
hosts_to_users: Dict[str, Set[str]] = {}
|
2021-04-20 09:11:24 -04:00
|
|
|
for row in rows:
|
|
|
|
hosts_to_users.setdefault(row.destination, set()).add(row.user_id)
|
|
|
|
|
|
|
|
for host, user_ids in hosts_to_users.items():
|
|
|
|
states = await self._presence_handler.current_state_for_users(user_ids)
|
2023-09-04 12:14:09 -04:00
|
|
|
await self._federation.send_presence_to_destinations(
|
2021-04-20 09:11:24 -04:00
|
|
|
states=states.values(),
|
|
|
|
destinations=[host],
|
|
|
|
)
|