2016-07-15 06:19:07 -06:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2019-07-25 09:08:24 -06:00
|
|
|
# Copyright 2019 New Vector Ltd
|
2020-10-07 06:00:17 -06:00
|
|
|
# Copyright 2019,2020 The Matrix.org Foundation C.I.C.
|
2016-07-15 06:19:07 -06: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.
|
2018-07-09 00:09:20 -06:00
|
|
|
import logging
|
2023-10-17 06:47:42 -06:00
|
|
|
from typing import TYPE_CHECKING, Dict, Iterable, List, Mapping, Optional, Set, Tuple
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2016-07-20 10:58:44 -06:00
|
|
|
from synapse.api import errors
|
2022-05-27 05:14:36 -06:00
|
|
|
from synapse.api.constants import EduTypes, EventTypes
|
2019-02-18 08:24:13 -07:00
|
|
|
from synapse.api.errors import (
|
2020-09-22 04:42:55 -06:00
|
|
|
Codes,
|
2019-02-18 08:24:13 -07:00
|
|
|
FederationDeniedError,
|
|
|
|
HttpResponseException,
|
2023-01-10 04:17:59 -07:00
|
|
|
InvalidAPICallError,
|
2019-02-18 08:24:13 -07:00
|
|
|
RequestSendFailed,
|
2020-02-10 14:35:26 -07:00
|
|
|
SynapseError,
|
2019-02-18 08:24:13 -07:00
|
|
|
)
|
2019-09-03 03:21:30 -06:00
|
|
|
from synapse.logging.opentracing import log_kv, set_tag, trace
|
2022-04-04 08:25:20 -06:00
|
|
|
from synapse.metrics.background_process_metrics import (
|
|
|
|
run_as_background_process,
|
|
|
|
wrap_as_background_process,
|
|
|
|
)
|
2023-10-17 06:47:42 -06:00
|
|
|
from synapse.storage.databases.main.client_ips import DeviceLastConnectionInfo
|
2020-06-01 09:47:30 -06:00
|
|
|
from synapse.types import (
|
2020-10-07 06:00:17 -06:00
|
|
|
JsonDict,
|
2023-09-06 01:30:53 -06:00
|
|
|
JsonMapping,
|
|
|
|
ScheduledTask,
|
2023-01-26 10:31:58 -07:00
|
|
|
StrCollection,
|
2022-05-16 09:35:31 -06:00
|
|
|
StreamKeyType,
|
2020-09-11 05:22:55 -06:00
|
|
|
StreamToken,
|
2023-09-06 01:30:53 -06:00
|
|
|
TaskStatus,
|
2023-01-10 04:17:59 -07:00
|
|
|
UserID,
|
2020-06-01 09:47:30 -06:00
|
|
|
get_domain_from_id,
|
|
|
|
get_verify_key_from_cross_signing_key,
|
|
|
|
)
|
2016-07-15 06:19:07 -06:00
|
|
|
from synapse.util import stringutils
|
2018-08-10 07:50:21 -06:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2017-03-03 09:02:53 -07:00
|
|
|
from synapse.util.caches.expiringcache import ExpiringCache
|
2022-09-07 05:03:32 -06:00
|
|
|
from synapse.util.cancellation import cancellable
|
2017-02-02 11:36:17 -07:00
|
|
|
from synapse.util.metrics import measure_func
|
2023-09-11 06:11:02 -06:00
|
|
|
from synapse.util.retryutils import (
|
|
|
|
NotRetryingDestination,
|
|
|
|
filter_destinations_by_retry_limiter,
|
|
|
|
)
|
2016-07-15 06:19:07 -06:00
|
|
|
|
2020-10-07 06:58:21 -06:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 05:12:48 -06:00
|
|
|
from synapse.server import HomeServer
|
2020-10-07 06:58:21 -06:00
|
|
|
|
2016-07-15 06:19:07 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-09-06 01:30:53 -06:00
|
|
|
DELETE_DEVICE_MSGS_TASK_NAME = "delete_device_messages"
|
2020-02-10 14:35:26 -07:00
|
|
|
MAX_DEVICE_DISPLAY_NAME_LEN = 100
|
2022-05-27 09:47:32 -06:00
|
|
|
DELETE_STALE_DEVICES_INTERVAL_MS = 24 * 60 * 60 * 1000
|
2020-02-10 14:35:26 -07:00
|
|
|
|
2016-07-15 06:19:07 -06:00
|
|
|
|
2021-10-08 05:44:43 -06:00
|
|
|
class DeviceWorkerHandler:
|
2022-11-22 12:08:04 -07:00
|
|
|
device_list_updater: "DeviceListWorkerUpdater"
|
|
|
|
|
2020-10-07 06:58:21 -06:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-10-08 05:44:43 -06:00
|
|
|
self.clock = hs.get_clock()
|
2017-01-26 09:06:54 -07:00
|
|
|
self.hs = hs
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2021-10-08 05:44:43 -06:00
|
|
|
self.notifier = hs.get_notifier()
|
2017-01-25 07:27:27 -07:00
|
|
|
self.state = hs.get_state_handler()
|
2023-05-05 13:18:47 -06:00
|
|
|
self._appservice_handler = hs.get_application_service_handler()
|
2022-05-31 06:17:50 -06:00
|
|
|
self._state_storage = hs.get_storage_controllers().state
|
2017-11-01 04:29:34 -06:00
|
|
|
self._auth_handler = hs.get_auth_handler()
|
2023-09-06 01:30:53 -06:00
|
|
|
self._event_sources = hs.get_event_sources()
|
2021-10-08 05:44:43 -06:00
|
|
|
self.server_name = hs.hostname
|
2022-08-19 10:17:10 -06:00
|
|
|
self._msc3852_enabled = hs.config.experimental.msc3852_enabled
|
2023-05-05 13:18:47 -06:00
|
|
|
self._query_appservices_for_keys = (
|
|
|
|
hs.config.experimental.msc3984_appservice_key_query
|
|
|
|
)
|
2023-09-13 04:54:16 -06:00
|
|
|
self._task_scheduler = hs.get_task_scheduler()
|
2019-03-04 11:24:32 -07:00
|
|
|
|
2022-11-22 12:08:04 -07:00
|
|
|
self.device_list_updater = DeviceListWorkerUpdater(hs)
|
|
|
|
|
2023-09-13 04:54:16 -06:00
|
|
|
self._task_scheduler.register_action(
|
|
|
|
self._delete_device_messages, DELETE_DEVICE_MSGS_TASK_NAME
|
|
|
|
)
|
|
|
|
|
2019-09-03 03:21:30 -06:00
|
|
|
@trace
|
2021-01-28 06:34:19 -07:00
|
|
|
async def get_devices_by_user(self, user_id: str) -> List[JsonDict]:
|
2019-03-04 11:24:32 -07:00
|
|
|
"""
|
|
|
|
Retrieve the given user's devices
|
|
|
|
|
|
|
|
Args:
|
2020-07-17 05:09:25 -06:00
|
|
|
user_id: The user ID to query for devices.
|
2019-03-04 11:24:32 -07:00
|
|
|
Returns:
|
2020-07-17 05:09:25 -06:00
|
|
|
info on each device
|
2019-03-04 11:24:32 -07:00
|
|
|
"""
|
|
|
|
|
2019-09-03 03:21:30 -06:00
|
|
|
set_tag("user_id", user_id)
|
2020-07-17 05:09:25 -06:00
|
|
|
device_map = await self.store.get_devices_by_user(user_id)
|
2019-03-04 11:24:32 -07:00
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
ips = await self.store.get_last_client_ip_by_device(user_id, device_id=None)
|
2019-03-04 11:24:32 -07:00
|
|
|
|
|
|
|
devices = list(device_map.values())
|
|
|
|
for device in devices:
|
|
|
|
_update_device_from_client_ips(device, ips)
|
|
|
|
|
2019-09-03 03:21:30 -06:00
|
|
|
log_kv(device_map)
|
2019-07-23 07:00:55 -06:00
|
|
|
return devices
|
2019-03-04 11:24:32 -07:00
|
|
|
|
2022-11-22 12:08:04 -07:00
|
|
|
async def get_dehydrated_device(
|
|
|
|
self, user_id: str
|
|
|
|
) -> Optional[Tuple[str, JsonDict]]:
|
|
|
|
"""Retrieve the information for a dehydrated device.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: the user whose dehydrated device we are looking for
|
|
|
|
Returns:
|
|
|
|
a tuple whose first item is the device ID, and the second item is
|
|
|
|
the dehydrated device information
|
|
|
|
"""
|
|
|
|
return await self.store.get_dehydrated_device(user_id)
|
|
|
|
|
2019-09-03 03:21:30 -06:00
|
|
|
@trace
|
2021-01-28 06:34:19 -07:00
|
|
|
async def get_device(self, user_id: str, device_id: str) -> JsonDict:
|
2019-03-04 11:24:32 -07:00
|
|
|
"""Retrieve the given device
|
|
|
|
|
|
|
|
Args:
|
2020-07-17 05:09:25 -06:00
|
|
|
user_id: The user to get the device from
|
|
|
|
device_id: The device to fetch.
|
2019-03-04 11:24:32 -07:00
|
|
|
|
|
|
|
Returns:
|
2020-07-17 05:09:25 -06:00
|
|
|
info on the device
|
2019-03-04 11:24:32 -07:00
|
|
|
Raises:
|
|
|
|
errors.NotFoundError: if the device was not found
|
|
|
|
"""
|
2021-12-13 08:39:43 -07:00
|
|
|
device = await self.store.get_device(user_id, device_id)
|
|
|
|
if device is None:
|
|
|
|
raise errors.NotFoundError()
|
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
ips = await self.store.get_last_client_ip_by_device(user_id, device_id)
|
2019-03-04 11:24:32 -07:00
|
|
|
_update_device_from_client_ips(device, ips)
|
2019-09-03 03:21:30 -06:00
|
|
|
|
2022-07-21 06:01:52 -06:00
|
|
|
set_tag("device", str(device))
|
|
|
|
set_tag("ips", str(ips))
|
2019-09-03 03:21:30 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return device
|
2019-03-04 11:24:32 -07:00
|
|
|
|
2022-09-07 05:03:32 -06:00
|
|
|
@cancellable
|
2022-06-17 04:42:03 -06:00
|
|
|
async def get_device_changes_in_shared_rooms(
|
2023-01-26 10:31:58 -07:00
|
|
|
self, user_id: str, room_ids: StrCollection, from_token: StreamToken
|
2022-11-22 12:08:04 -07:00
|
|
|
) -> Set[str]:
|
2022-06-17 04:42:03 -06:00
|
|
|
"""Get the set of users whose devices have changed who share a room with
|
|
|
|
the given user.
|
2019-03-04 11:24:32 -07:00
|
|
|
"""
|
2022-06-17 04:42:03 -06:00
|
|
|
changed_users = await self.store.get_device_list_changes_in_rooms(
|
|
|
|
room_ids, from_token.device_list_key
|
|
|
|
)
|
2019-09-03 03:21:30 -06:00
|
|
|
|
2022-06-17 04:42:03 -06:00
|
|
|
if changed_users is not None:
|
|
|
|
# We also check if the given user has changed their device. If
|
|
|
|
# they're in no rooms then the above query won't include them.
|
|
|
|
changed = await self.store.get_users_whose_devices_changed(
|
|
|
|
from_token.device_list_key, [user_id]
|
|
|
|
)
|
|
|
|
changed_users.update(changed)
|
|
|
|
return changed_users
|
2019-03-04 11:24:32 -07:00
|
|
|
|
2022-06-17 04:42:03 -06:00
|
|
|
# If the DB returned None then the `from_token` is too old, so we fall
|
|
|
|
# back on looking for device updates for all users.
|
2019-03-04 11:24:32 -07:00
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
users_who_share_room = await self.store.get_users_who_share_room_with_user(
|
2020-03-30 03:18:33 -06:00
|
|
|
user_id
|
|
|
|
)
|
2020-03-30 03:11:26 -06:00
|
|
|
|
|
|
|
tracked_users = set(users_who_share_room)
|
2020-03-30 07:00:11 -06:00
|
|
|
|
|
|
|
# Always tell the user about their own devices
|
2020-03-27 06:26:47 -06:00
|
|
|
tracked_users.add(user_id)
|
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
changed = await self.store.get_users_whose_devices_changed(
|
2020-03-27 06:26:47 -06:00
|
|
|
from_token.device_list_key, tracked_users
|
|
|
|
)
|
|
|
|
|
2022-06-17 04:42:03 -06:00
|
|
|
return changed
|
|
|
|
|
|
|
|
@trace
|
|
|
|
@measure_func("device.get_user_ids_changed")
|
2022-09-07 05:03:32 -06:00
|
|
|
@cancellable
|
2022-06-17 04:42:03 -06:00
|
|
|
async def get_user_ids_changed(
|
|
|
|
self, user_id: str, from_token: StreamToken
|
|
|
|
) -> JsonDict:
|
|
|
|
"""Get list of users that have had the devices updated, or have newly
|
|
|
|
joined a room, that `user_id` may be interested in.
|
|
|
|
"""
|
|
|
|
|
|
|
|
set_tag("user_id", user_id)
|
2022-07-21 06:01:52 -06:00
|
|
|
set_tag("from_token", str(from_token))
|
2022-06-17 04:42:03 -06:00
|
|
|
now_room_key = self.store.get_room_max_token()
|
|
|
|
|
|
|
|
room_ids = await self.store.get_rooms_for_user(user_id)
|
|
|
|
|
|
|
|
changed = await self.get_device_changes_in_shared_rooms(
|
|
|
|
user_id, room_ids, from_token
|
|
|
|
)
|
|
|
|
|
2019-03-04 11:24:32 -07:00
|
|
|
# Then work out if any users have since joined
|
|
|
|
rooms_changed = self.store.get_rooms_that_changed(room_ids, from_token.room_key)
|
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
member_events = await self.store.get_membership_changes_for_user(
|
2019-03-04 11:09:06 -07:00
|
|
|
user_id, from_token.room_key, now_room_key
|
2019-03-04 11:24:32 -07:00
|
|
|
)
|
|
|
|
rooms_changed.update(event.room_id for event in member_events)
|
|
|
|
|
2020-09-11 05:22:55 -06:00
|
|
|
stream_ordering = from_token.room_key.stream
|
2019-03-04 11:24:32 -07:00
|
|
|
|
|
|
|
possibly_changed = set(changed)
|
|
|
|
possibly_left = set()
|
|
|
|
for room_id in rooms_changed:
|
2023-04-06 10:42:39 -06:00
|
|
|
# Check if the forward extremities have changed. If not then we know
|
|
|
|
# the current state won't have changed, and so we can skip this room.
|
|
|
|
try:
|
|
|
|
if not await self.store.have_room_forward_extremities_changed_since(
|
|
|
|
room_id, stream_ordering
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
except errors.StoreError:
|
|
|
|
pass
|
|
|
|
|
2022-09-23 10:55:15 -06:00
|
|
|
current_state_ids = await self._state_storage.get_current_state_ids(
|
|
|
|
room_id, await_full_state=False
|
|
|
|
)
|
2019-03-04 11:24:32 -07:00
|
|
|
|
|
|
|
# The user may have left the room
|
|
|
|
# TODO: Check if they actually did or if we were just invited.
|
|
|
|
if room_id not in room_ids:
|
2021-04-20 04:50:49 -06:00
|
|
|
for etype, state_key in current_state_ids.keys():
|
2019-03-04 11:24:32 -07:00
|
|
|
if etype != EventTypes.Member:
|
|
|
|
continue
|
|
|
|
possibly_left.add(state_key)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Fetch the current state at the time.
|
|
|
|
try:
|
2021-03-17 07:20:08 -06:00
|
|
|
event_ids = await self.store.get_forward_extremities_for_room_at_stream_ordering(
|
2019-03-04 11:24:32 -07:00
|
|
|
room_id, stream_ordering=stream_ordering
|
|
|
|
)
|
|
|
|
except errors.StoreError:
|
|
|
|
# we have purged the stream_ordering index since the stream
|
|
|
|
# ordering: treat it the same as a new room
|
|
|
|
event_ids = []
|
|
|
|
|
|
|
|
# special-case for an empty prev state: include all members
|
|
|
|
# in the changed list
|
|
|
|
if not event_ids:
|
2019-09-03 03:21:30 -06:00
|
|
|
log_kv(
|
|
|
|
{"event": "encountered empty previous state", "room_id": room_id}
|
|
|
|
)
|
2021-04-20 04:50:49 -06:00
|
|
|
for etype, state_key in current_state_ids.keys():
|
2019-03-04 11:24:32 -07:00
|
|
|
if etype != EventTypes.Member:
|
|
|
|
continue
|
|
|
|
possibly_changed.add(state_key)
|
|
|
|
continue
|
|
|
|
|
|
|
|
current_member_id = current_state_ids.get((EventTypes.Member, user_id))
|
|
|
|
if not current_member_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# mapping from event_id -> state_dict
|
2022-05-31 06:17:50 -06:00
|
|
|
prev_state_ids = await self._state_storage.get_state_ids_for_events(
|
2022-09-23 10:55:15 -06:00
|
|
|
event_ids,
|
|
|
|
await_full_state=False,
|
2022-05-25 05:59:04 -06:00
|
|
|
)
|
2019-03-04 11:24:32 -07:00
|
|
|
|
|
|
|
# Check if we've joined the room? If so we just blindly add all the users to
|
|
|
|
# the "possibly changed" users.
|
2020-06-15 05:03:36 -06:00
|
|
|
for state_dict in prev_state_ids.values():
|
2019-03-04 11:24:32 -07:00
|
|
|
member_event = state_dict.get((EventTypes.Member, user_id), None)
|
|
|
|
if not member_event or member_event != current_member_id:
|
2021-04-20 04:50:49 -06:00
|
|
|
for etype, state_key in current_state_ids.keys():
|
2019-03-04 11:24:32 -07:00
|
|
|
if etype != EventTypes.Member:
|
|
|
|
continue
|
|
|
|
possibly_changed.add(state_key)
|
|
|
|
break
|
|
|
|
|
|
|
|
# If there has been any change in membership, include them in the
|
|
|
|
# possibly changed list. We'll check if they are joined below,
|
|
|
|
# and we're not toooo worried about spuriously adding users.
|
2020-06-15 05:03:36 -06:00
|
|
|
for key, event_id in current_state_ids.items():
|
2019-03-04 11:24:32 -07:00
|
|
|
etype, state_key = key
|
|
|
|
if etype != EventTypes.Member:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# check if this member has changed since any of the extremities
|
|
|
|
# at the stream_ordering, and add them to the list if so.
|
2020-06-15 05:03:36 -06:00
|
|
|
for state_dict in prev_state_ids.values():
|
2019-03-04 11:24:32 -07:00
|
|
|
prev_event_id = state_dict.get(key, None)
|
|
|
|
if not prev_event_id or prev_event_id != event_id:
|
|
|
|
if state_key != user_id:
|
|
|
|
possibly_changed.add(state_key)
|
|
|
|
break
|
|
|
|
|
|
|
|
if possibly_changed or possibly_left:
|
2022-06-17 04:42:03 -06:00
|
|
|
possibly_joined = possibly_changed
|
|
|
|
possibly_left = possibly_changed | possibly_left
|
|
|
|
|
|
|
|
# Double check if we still share rooms with the given user.
|
2022-09-29 07:55:12 -06:00
|
|
|
users_rooms = await self.store.get_rooms_for_users(possibly_left)
|
2022-06-17 04:42:03 -06:00
|
|
|
for changed_user_id, entries in users_rooms.items():
|
2022-09-29 07:55:12 -06:00
|
|
|
if any(rid in room_ids for rid in entries):
|
2022-06-17 04:42:03 -06:00
|
|
|
possibly_left.discard(changed_user_id)
|
|
|
|
else:
|
|
|
|
possibly_joined.discard(changed_user_id)
|
|
|
|
|
2019-03-04 11:24:32 -07:00
|
|
|
else:
|
2020-10-07 06:58:21 -06:00
|
|
|
possibly_joined = set()
|
|
|
|
possibly_left = set()
|
2019-03-04 11:24:32 -07:00
|
|
|
|
2019-09-03 03:21:30 -06:00
|
|
|
result = {"changed": list(possibly_joined), "left": list(possibly_left)}
|
|
|
|
|
|
|
|
log_kv(result)
|
|
|
|
|
|
|
|
return result
|
2019-03-04 11:24:32 -07:00
|
|
|
|
2020-10-07 06:58:21 -06:00
|
|
|
async def on_federation_query_user_devices(self, user_id: str) -> JsonDict:
|
2023-10-31 07:58:30 -06:00
|
|
|
if not self.hs.is_mine(UserID.from_string(user_id)):
|
|
|
|
raise SynapseError(400, "User is not hosted on this homeserver")
|
|
|
|
|
2020-09-01 05:41:21 -06:00
|
|
|
stream_id, devices = await self.store.get_e2e_device_keys_for_federation_query(
|
|
|
|
user_id
|
|
|
|
)
|
2020-07-17 05:09:25 -06:00
|
|
|
master_key = await self.store.get_e2e_cross_signing_key(user_id, "master")
|
|
|
|
self_signing_key = await self.store.get_e2e_cross_signing_key(
|
2020-02-07 08:45:39 -07:00
|
|
|
user_id, "self_signing"
|
|
|
|
)
|
|
|
|
|
2023-05-05 13:18:47 -06:00
|
|
|
# Check if the application services have any results.
|
|
|
|
if self._query_appservices_for_keys:
|
|
|
|
# Query the appservice for all devices for this user.
|
|
|
|
query: Dict[str, Optional[List[str]]] = {user_id: None}
|
|
|
|
|
|
|
|
# Query the appservices for any keys.
|
|
|
|
appservice_results = await self._appservice_handler.query_keys(query)
|
|
|
|
|
|
|
|
# Merge results, overriding anything from the database.
|
|
|
|
appservice_devices = appservice_results.get("device_keys", {}).get(
|
|
|
|
user_id, {}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Filter the database results to only those devices that the appservice has
|
|
|
|
# *not* responded with.
|
|
|
|
devices = [d for d in devices if d["device_id"] not in appservice_devices]
|
|
|
|
# Append the appservice response by wrapping each result in another dictionary.
|
|
|
|
devices.extend(
|
|
|
|
{"device_id": device_id, "keys": device}
|
|
|
|
for device_id, device in appservice_devices.items()
|
|
|
|
)
|
|
|
|
|
|
|
|
# TODO Handle cross-signing keys.
|
|
|
|
|
2020-02-07 08:45:39 -07:00
|
|
|
return {
|
|
|
|
"user_id": user_id,
|
|
|
|
"stream_id": stream_id,
|
|
|
|
"devices": devices,
|
|
|
|
"master_key": master_key,
|
|
|
|
"self_signing_key": self_signing_key,
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:42:43 -06:00
|
|
|
async def handle_room_un_partial_stated(self, room_id: str) -> None:
|
|
|
|
"""Handles sending appropriate device list updates in a room that has
|
|
|
|
gone from partial to full state.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO(faster_joins): worker mode support
|
|
|
|
# https://github.com/matrix-org/synapse/issues/12994
|
|
|
|
logger.error(
|
|
|
|
"Trying handling device list state for partial join: not supported on workers."
|
|
|
|
)
|
|
|
|
|
2023-09-14 07:56:07 -06:00
|
|
|
DEVICE_MSGS_DELETE_BATCH_LIMIT = 1000
|
|
|
|
DEVICE_MSGS_DELETE_SLEEP_MS = 1000
|
2023-09-13 04:54:16 -06:00
|
|
|
|
|
|
|
async def _delete_device_messages(
|
|
|
|
self,
|
|
|
|
task: ScheduledTask,
|
|
|
|
) -> Tuple[TaskStatus, Optional[JsonMapping], Optional[str]]:
|
|
|
|
"""Scheduler task to delete device messages in batch of `DEVICE_MSGS_DELETE_BATCH_LIMIT`."""
|
|
|
|
assert task.params is not None
|
|
|
|
user_id = task.params["user_id"]
|
|
|
|
device_id = task.params["device_id"]
|
|
|
|
up_to_stream_id = task.params["up_to_stream_id"]
|
|
|
|
|
2023-09-14 07:56:07 -06:00
|
|
|
# Delete the messages in batches to avoid too much DB load.
|
|
|
|
while True:
|
|
|
|
res = await self.store.delete_messages_for_device(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
up_to_stream_id=up_to_stream_id,
|
|
|
|
limit=DeviceHandler.DEVICE_MSGS_DELETE_BATCH_LIMIT,
|
|
|
|
)
|
2023-09-13 04:54:16 -06:00
|
|
|
|
2023-09-14 07:56:07 -06:00
|
|
|
if res < DeviceHandler.DEVICE_MSGS_DELETE_BATCH_LIMIT:
|
|
|
|
return TaskStatus.COMPLETE, None, None
|
|
|
|
|
|
|
|
await self.clock.sleep(DeviceHandler.DEVICE_MSGS_DELETE_SLEEP_MS / 1000.0)
|
2023-09-13 04:54:16 -06:00
|
|
|
|
2019-03-04 11:24:32 -07:00
|
|
|
|
|
|
|
class DeviceHandler(DeviceWorkerHandler):
|
2022-11-22 12:08:04 -07:00
|
|
|
device_list_updater: "DeviceListUpdater"
|
|
|
|
|
2020-10-07 06:58:21 -06:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__(hs)
|
2019-03-04 11:24:32 -07:00
|
|
|
|
2017-01-26 09:06:54 -07:00
|
|
|
self.federation_sender = hs.get_federation_sender()
|
2023-01-13 12:32:10 -07:00
|
|
|
self._account_data_handler = hs.get_account_data_handler()
|
2022-08-24 13:15:37 -06:00
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
2023-08-08 13:04:46 -06:00
|
|
|
self.db_pool = hs.get_datastores().main.db_pool
|
2017-03-01 07:12:11 -07:00
|
|
|
|
2019-07-29 09:34:44 -06:00
|
|
|
self.device_list_updater = DeviceListUpdater(hs, self)
|
2017-01-26 09:06:54 -07:00
|
|
|
|
2018-03-12 10:17:08 -06:00
|
|
|
federation_registry = hs.get_federation_registry()
|
|
|
|
|
|
|
|
federation_registry.register_edu_handler(
|
2022-05-27 05:14:36 -06:00
|
|
|
EduTypes.DEVICE_LIST_UPDATE,
|
|
|
|
self.device_list_updater.incoming_device_list_update,
|
2017-01-26 09:06:54 -07:00
|
|
|
)
|
2017-01-25 07:27:27 -07:00
|
|
|
|
2022-04-04 08:25:20 -06:00
|
|
|
# Whether `_handle_new_device_update_async` is currently processing.
|
|
|
|
self._handle_new_device_update_is_processing = False
|
|
|
|
|
|
|
|
# If a new device update may have happened while the loop was
|
|
|
|
# processing.
|
|
|
|
self._handle_new_device_update_new_data = False
|
|
|
|
|
|
|
|
# On start up check if there are any updates pending.
|
|
|
|
hs.get_reactor().callWhenRunning(self._handle_new_device_update_async)
|
|
|
|
|
2022-05-27 09:47:32 -06:00
|
|
|
self._delete_stale_devices_after = hs.config.server.delete_stale_devices_after
|
|
|
|
|
|
|
|
# Ideally we would run this on a worker and condition this on the
|
|
|
|
# "run_background_tasks_on" setting, but this would mean making the notification
|
|
|
|
# of device list changes over federation work on workers, which is nontrivial.
|
|
|
|
if self._delete_stale_devices_after is not None:
|
|
|
|
self.clock.looping_call(
|
|
|
|
run_as_background_process,
|
|
|
|
DELETE_STALE_DEVICES_INTERVAL_MS,
|
|
|
|
"delete_stale_devices",
|
|
|
|
self._delete_stale_devices,
|
|
|
|
)
|
|
|
|
|
2021-09-20 06:56:23 -06:00
|
|
|
def _check_device_name_length(self, name: Optional[str]) -> None:
|
2020-09-22 04:42:55 -06:00
|
|
|
"""
|
|
|
|
Checks whether a device name is longer than the maximum allowed length.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: The name of the device.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
SynapseError: if the device name is too long.
|
|
|
|
"""
|
|
|
|
if name and len(name) > MAX_DEVICE_DISPLAY_NAME_LEN:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"Device display name is too long (max %i)"
|
|
|
|
% (MAX_DEVICE_DISPLAY_NAME_LEN,),
|
|
|
|
errcode=Codes.TOO_LARGE,
|
|
|
|
)
|
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
async def check_device_registered(
|
2020-10-07 06:58:21 -06:00
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
device_id: Optional[str],
|
|
|
|
initial_device_display_name: Optional[str] = None,
|
2021-12-06 10:43:06 -07:00
|
|
|
auth_provider_id: Optional[str] = None,
|
|
|
|
auth_provider_session_id: Optional[str] = None,
|
2020-10-07 06:58:21 -06:00
|
|
|
) -> str:
|
2016-07-15 06:19:07 -06:00
|
|
|
"""
|
|
|
|
If the given device has not been registered, register it with the
|
|
|
|
supplied display name.
|
|
|
|
|
|
|
|
If no device_id is supplied, we make one up.
|
|
|
|
|
|
|
|
Args:
|
2020-10-07 06:58:21 -06:00
|
|
|
user_id: @user:id
|
|
|
|
device_id: device id supplied by client
|
|
|
|
initial_device_display_name: device display name from client
|
2021-12-06 10:43:06 -07:00
|
|
|
auth_provider_id: The SSO IdP the user used, if any.
|
|
|
|
auth_provider_session_id: The session ID (sid) got from the SSO IdP.
|
2016-07-15 06:19:07 -06:00
|
|
|
Returns:
|
2020-10-07 06:58:21 -06:00
|
|
|
device id (generated if none was supplied)
|
2016-07-15 06:19:07 -06:00
|
|
|
"""
|
2020-09-22 04:42:55 -06:00
|
|
|
|
|
|
|
self._check_device_name_length(initial_device_display_name)
|
|
|
|
|
2016-07-15 06:19:07 -06:00
|
|
|
if device_id is not None:
|
2020-07-17 05:09:25 -06:00
|
|
|
new_device = await self.store.store_device(
|
2016-07-15 06:19:07 -06:00
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
initial_device_display_name=initial_device_display_name,
|
2021-12-06 10:43:06 -07:00
|
|
|
auth_provider_id=auth_provider_id,
|
|
|
|
auth_provider_session_id=auth_provider_session_id,
|
2016-07-15 06:19:07 -06:00
|
|
|
)
|
2017-01-25 07:27:27 -07:00
|
|
|
if new_device:
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.notify_device_update(user_id, [device_id])
|
2019-07-23 07:00:55 -06:00
|
|
|
return device_id
|
2016-07-15 06:19:07 -06:00
|
|
|
|
|
|
|
# if the device id is not specified, we'll autogen one, but loop a few
|
|
|
|
# times in case of a clash.
|
|
|
|
attempts = 0
|
|
|
|
while attempts < 5:
|
2020-10-07 06:58:21 -06:00
|
|
|
new_device_id = stringutils.random_string(10).upper()
|
2020-07-17 05:09:25 -06:00
|
|
|
new_device = await self.store.store_device(
|
2017-01-25 07:27:27 -07:00
|
|
|
user_id=user_id,
|
2020-10-07 06:58:21 -06:00
|
|
|
device_id=new_device_id,
|
2017-01-25 07:27:27 -07:00
|
|
|
initial_device_display_name=initial_device_display_name,
|
2021-12-06 10:43:06 -07:00
|
|
|
auth_provider_id=auth_provider_id,
|
|
|
|
auth_provider_session_id=auth_provider_session_id,
|
2017-01-25 07:27:27 -07:00
|
|
|
)
|
|
|
|
if new_device:
|
2020-10-07 06:58:21 -06:00
|
|
|
await self.notify_device_update(user_id, [new_device_id])
|
|
|
|
return new_device_id
|
2017-01-25 07:27:27 -07:00
|
|
|
attempts += 1
|
2016-07-15 06:19:07 -06:00
|
|
|
|
2016-07-20 10:58:44 -06:00
|
|
|
raise errors.StoreError(500, "Couldn't generate a device ID.")
|
2016-07-20 09:34:00 -06:00
|
|
|
|
2022-05-27 09:47:32 -06:00
|
|
|
async def _delete_stale_devices(self) -> None:
|
|
|
|
"""Background task that deletes devices which haven't been accessed for more than
|
|
|
|
a configured time period.
|
|
|
|
"""
|
|
|
|
# We should only be running this job if the config option is defined.
|
|
|
|
assert self._delete_stale_devices_after is not None
|
|
|
|
now_ms = self.clock.time_msec()
|
|
|
|
since_ms = now_ms - self._delete_stale_devices_after
|
|
|
|
devices = await self.store.get_local_devices_not_accessed_since(since_ms)
|
|
|
|
|
|
|
|
for user_id, user_devices in devices.items():
|
|
|
|
await self.delete_devices(user_id, user_devices)
|
|
|
|
|
2019-09-03 03:21:30 -06:00
|
|
|
@trace
|
2020-07-17 05:09:25 -06:00
|
|
|
async def delete_all_devices_for_user(
|
|
|
|
self, user_id: str, except_device_id: Optional[str] = None
|
|
|
|
) -> None:
|
2017-11-29 08:44:59 -07:00
|
|
|
"""Delete all of the user's devices
|
|
|
|
|
|
|
|
Args:
|
2020-07-17 05:09:25 -06:00
|
|
|
user_id: The user to remove all devices from
|
|
|
|
except_device_id: optional device id which should not be deleted
|
2017-11-29 08:44:59 -07:00
|
|
|
"""
|
2020-07-17 05:09:25 -06:00
|
|
|
device_map = await self.store.get_devices_by_user(user_id)
|
2018-05-31 03:03:47 -06:00
|
|
|
device_ids = list(device_map)
|
2017-11-29 08:44:59 -07:00
|
|
|
if except_device_id is not None:
|
|
|
|
device_ids = [d for d in device_ids if d != except_device_id]
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.delete_devices(user_id, device_ids)
|
2017-11-29 08:44:59 -07:00
|
|
|
|
2023-03-31 06:51:51 -06:00
|
|
|
async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
|
2017-03-13 11:53:23 -06:00
|
|
|
"""Delete several devices
|
|
|
|
|
|
|
|
Args:
|
2020-07-17 05:09:25 -06:00
|
|
|
user_id: The user to delete devices from.
|
|
|
|
device_ids: The list of device IDs to delete
|
2017-03-13 11:53:23 -06:00
|
|
|
"""
|
2023-09-06 01:30:53 -06:00
|
|
|
to_device_stream_id = self._event_sources.get_current_token().to_device_key
|
2017-03-13 11:53:23 -06:00
|
|
|
|
|
|
|
try:
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.store.delete_devices(user_id, device_ids)
|
2018-03-15 09:11:17 -06:00
|
|
|
except errors.StoreError as e:
|
2017-03-13 11:53:23 -06:00
|
|
|
if e.code == 404:
|
|
|
|
# no match
|
2019-09-03 03:21:30 -06:00
|
|
|
set_tag("error", True)
|
|
|
|
set_tag("reason", "User doesn't have that device id.")
|
2017-03-13 11:53:23 -06:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2023-01-13 12:32:10 -07:00
|
|
|
# Delete data specific to each device. Not optimised as it is not
|
2017-03-13 11:53:23 -06:00
|
|
|
# considered as part of a critical path.
|
|
|
|
for device_id in device_ids:
|
2020-07-17 05:09:25 -06:00
|
|
|
await self._auth_handler.delete_access_tokens_for_user(
|
|
|
|
user_id, device_id=device_id
|
2017-03-13 11:53:23 -06:00
|
|
|
)
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.store.delete_e2e_keys_by_device(
|
2017-03-13 11:53:23 -06:00
|
|
|
user_id=user_id, device_id=device_id
|
|
|
|
)
|
|
|
|
|
2023-01-13 12:32:10 -07:00
|
|
|
if self.hs.config.experimental.msc3890_enabled:
|
|
|
|
# Remove any local notification settings for this device in accordance
|
|
|
|
# with MSC3890.
|
|
|
|
await self._account_data_handler.remove_account_data_for_user(
|
|
|
|
user_id,
|
|
|
|
f"org.matrix.msc3890.local_notification_settings.{device_id}",
|
|
|
|
)
|
|
|
|
|
2023-09-06 01:30:53 -06:00
|
|
|
# Delete device messages asynchronously and in batches using the task scheduler
|
2023-10-18 09:42:01 -06:00
|
|
|
# We specify an upper stream id to avoid deleting non delivered messages
|
|
|
|
# if an user re-uses a device ID.
|
2023-09-06 01:30:53 -06:00
|
|
|
await self._task_scheduler.schedule_task(
|
|
|
|
DELETE_DEVICE_MSGS_TASK_NAME,
|
|
|
|
resource_id=device_id,
|
|
|
|
params={
|
|
|
|
"user_id": user_id,
|
|
|
|
"device_id": device_id,
|
|
|
|
"up_to_stream_id": to_device_stream_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-04-14 06:12:37 -06:00
|
|
|
# Pushers are deleted after `delete_access_tokens_for_user` is called so that
|
|
|
|
# modules using `on_logged_out` hook can use them if needed.
|
|
|
|
await self.hs.get_pusherpool().remove_pushers_by_devices(user_id, device_ids)
|
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.notify_device_update(user_id, device_ids)
|
2017-03-13 11:53:23 -06:00
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
async def update_device(self, user_id: str, device_id: str, content: dict) -> None:
|
2016-07-25 10:51:24 -06:00
|
|
|
"""Update the given device
|
|
|
|
|
|
|
|
Args:
|
2020-07-17 05:09:25 -06:00
|
|
|
user_id: The user to update devices of.
|
|
|
|
device_id: The device to update.
|
|
|
|
content: body of update request
|
2016-07-25 10:51:24 -06:00
|
|
|
"""
|
|
|
|
|
2020-02-10 14:35:26 -07:00
|
|
|
# Reject a new displayname which is too long.
|
|
|
|
new_display_name = content.get("display_name")
|
2020-09-22 04:42:55 -06:00
|
|
|
|
|
|
|
self._check_device_name_length(new_display_name)
|
2020-02-10 14:35:26 -07:00
|
|
|
|
2016-07-25 10:51:24 -06:00
|
|
|
try:
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.store.update_device(
|
2020-02-10 14:35:26 -07:00
|
|
|
user_id, device_id, new_display_name=new_display_name
|
2016-07-25 10:51:24 -06:00
|
|
|
)
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.notify_device_update(user_id, [device_id])
|
2018-03-15 09:11:17 -06:00
|
|
|
except errors.StoreError as e:
|
2016-07-25 10:51:24 -06:00
|
|
|
if e.code == 404:
|
|
|
|
raise errors.NotFoundError()
|
|
|
|
else:
|
|
|
|
raise
|
2016-07-22 07:52:53 -06:00
|
|
|
|
2019-09-03 03:21:30 -06:00
|
|
|
@trace
|
2017-02-02 11:36:17 -07:00
|
|
|
@measure_func("notify_device_update")
|
2020-10-07 06:58:21 -06:00
|
|
|
async def notify_device_update(
|
2023-01-26 10:31:58 -07:00
|
|
|
self, user_id: str, device_ids: StrCollection
|
2020-10-07 06:58:21 -06:00
|
|
|
) -> None:
|
2017-01-26 09:30:37 -07:00
|
|
|
"""Notify that a user's device(s) has changed. Pokes the notifier, and
|
|
|
|
remote servers if the user is local.
|
2021-10-21 10:42:25 -06:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: The Matrix ID of the user who's device list has been updated.
|
|
|
|
device_ids: The device IDs that have changed.
|
2017-01-26 09:30:37 -07:00
|
|
|
"""
|
2020-07-17 07:11:05 -06:00
|
|
|
if not device_ids:
|
|
|
|
# No changes to notify about, so this is a no-op.
|
|
|
|
return
|
|
|
|
|
2022-04-04 08:25:20 -06:00
|
|
|
room_ids = await self.store.get_rooms_for_user(user_id)
|
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
position = await self.store.add_device_change_to_streams(
|
2022-04-04 08:25:20 -06:00
|
|
|
user_id,
|
|
|
|
device_ids,
|
|
|
|
room_ids=room_ids,
|
2017-01-25 07:27:27 -07:00
|
|
|
)
|
|
|
|
|
2020-07-17 07:11:05 -06:00
|
|
|
if not position:
|
|
|
|
# This should only happen if there are no updates, so we bail.
|
|
|
|
return
|
|
|
|
|
2019-03-07 08:53:14 -07:00
|
|
|
for device_id in device_ids:
|
|
|
|
logger.debug(
|
|
|
|
"Notifying about update %r/%r, ID: %r", user_id, device_id, position
|
|
|
|
)
|
|
|
|
|
2020-03-27 06:26:47 -06:00
|
|
|
# specify the user ID too since the user should always get their own device list
|
|
|
|
# updates, even if they aren't in any rooms.
|
2022-04-04 08:25:20 -06:00
|
|
|
self.notifier.on_new_event(
|
2022-05-16 09:35:31 -06:00
|
|
|
StreamKeyType.DEVICE_LIST, position, users={user_id}, rooms=room_ids
|
2022-04-04 08:25:20 -06:00
|
|
|
)
|
2022-02-12 07:33:49 -07:00
|
|
|
|
2022-04-26 10:07:21 -06:00
|
|
|
# We may need to do some processing asynchronously for local user IDs.
|
|
|
|
if self.hs.is_mine_id(user_id):
|
|
|
|
self._handle_new_device_update_async()
|
2017-01-25 07:27:27 -07:00
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
async def notify_user_signature_update(
|
|
|
|
self, from_user_id: str, user_ids: List[str]
|
|
|
|
) -> None:
|
2019-07-25 09:08:24 -06:00
|
|
|
"""Notify a user that they have made new signatures of other users.
|
|
|
|
|
|
|
|
Args:
|
2020-07-17 05:09:25 -06:00
|
|
|
from_user_id: the user who made the signature
|
|
|
|
user_ids: the users IDs that have new signatures
|
2019-07-25 09:08:24 -06:00
|
|
|
"""
|
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
position = await self.store.add_user_signature_change_to_streams(
|
2019-07-25 09:08:24 -06:00
|
|
|
from_user_id, user_ids
|
|
|
|
)
|
|
|
|
|
2022-05-16 09:35:31 -06:00
|
|
|
self.notifier.on_new_event(
|
|
|
|
StreamKeyType.DEVICE_LIST, position, users=[from_user_id]
|
|
|
|
)
|
2019-07-25 09:08:24 -06:00
|
|
|
|
2020-10-07 06:00:17 -06:00
|
|
|
async def store_dehydrated_device(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
2023-07-24 09:23:19 -06:00
|
|
|
device_id: Optional[str],
|
2020-10-07 06:00:17 -06:00
|
|
|
device_data: JsonDict,
|
|
|
|
initial_device_display_name: Optional[str] = None,
|
2023-08-08 13:04:46 -06:00
|
|
|
keys_for_device: Optional[JsonDict] = None,
|
2020-10-07 06:00:17 -06:00
|
|
|
) -> str:
|
2023-08-08 13:04:46 -06:00
|
|
|
"""Store a dehydrated device for a user, optionally storing the keys associated with
|
|
|
|
it as well. If the user had a previous dehydrated device, it is removed.
|
2020-10-07 06:00:17 -06:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: the user that we are storing the device for
|
2023-07-24 09:23:19 -06:00
|
|
|
device_id: device id supplied by client
|
2020-10-07 06:00:17 -06:00
|
|
|
device_data: the dehydrated device information
|
|
|
|
initial_device_display_name: The display name to use for the device
|
2023-08-08 13:04:46 -06:00
|
|
|
keys_for_device: keys for the dehydrated device
|
2020-10-07 06:00:17 -06:00
|
|
|
Returns:
|
|
|
|
device id of the dehydrated device
|
|
|
|
"""
|
|
|
|
device_id = await self.check_device_registered(
|
|
|
|
user_id,
|
2023-07-24 09:23:19 -06:00
|
|
|
device_id,
|
2020-10-07 06:00:17 -06:00
|
|
|
initial_device_display_name,
|
|
|
|
)
|
2023-08-08 13:04:46 -06:00
|
|
|
|
|
|
|
time_now = self.clock.time_msec()
|
|
|
|
|
2020-10-07 06:00:17 -06:00
|
|
|
old_device_id = await self.store.store_dehydrated_device(
|
2023-08-08 13:04:46 -06:00
|
|
|
user_id, device_id, device_data, time_now, keys_for_device
|
2020-10-07 06:00:17 -06:00
|
|
|
)
|
2023-08-08 13:04:46 -06:00
|
|
|
|
2020-10-07 06:00:17 -06:00
|
|
|
if old_device_id is not None:
|
2022-06-07 05:43:35 -06:00
|
|
|
await self.delete_devices(user_id, [old_device_id])
|
2023-08-08 13:04:46 -06:00
|
|
|
|
2020-10-07 06:00:17 -06:00
|
|
|
return device_id
|
|
|
|
|
|
|
|
async def rehydrate_device(
|
|
|
|
self, user_id: str, access_token: str, device_id: str
|
|
|
|
) -> dict:
|
|
|
|
"""Process a rehydration request from the user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: the user who is rehydrating the device
|
|
|
|
access_token: the access token used for the request
|
|
|
|
device_id: the ID of the device that will be rehydrated
|
|
|
|
Returns:
|
|
|
|
a dict containing {"success": True}
|
|
|
|
"""
|
|
|
|
success = await self.store.remove_dehydrated_device(user_id, device_id)
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
raise errors.NotFoundError()
|
|
|
|
|
|
|
|
# If the dehydrated device was successfully deleted (the device ID
|
|
|
|
# matched the stored dehydrated device), then modify the access
|
2023-09-13 06:33:39 -06:00
|
|
|
# token and refresh token to use the dehydrated device's ID and
|
|
|
|
# copy the old device display name to the dehydrated device,
|
|
|
|
# and destroy the old device ID
|
2020-10-07 06:00:17 -06:00
|
|
|
old_device_id = await self.store.set_device_for_access_token(
|
|
|
|
access_token, device_id
|
|
|
|
)
|
2023-09-13 06:33:39 -06:00
|
|
|
await self.store.set_device_for_refresh_token(user_id, old_device_id, device_id)
|
2020-10-07 06:00:17 -06:00
|
|
|
old_device = await self.store.get_device(user_id, old_device_id)
|
2021-12-13 08:39:43 -07:00
|
|
|
if old_device is None:
|
|
|
|
raise errors.NotFoundError()
|
2020-10-07 06:00:17 -06:00
|
|
|
await self.store.update_device(user_id, device_id, old_device["display_name"])
|
|
|
|
# can't call self.delete_device because that will clobber the
|
|
|
|
# access token so call the storage layer directly
|
2022-06-07 05:43:35 -06:00
|
|
|
await self.store.delete_devices(user_id, [old_device_id])
|
2020-10-07 06:00:17 -06:00
|
|
|
await self.store.delete_e2e_keys_by_device(
|
|
|
|
user_id=user_id, device_id=old_device_id
|
|
|
|
)
|
|
|
|
|
|
|
|
# tell everyone that the old device is gone and that the dehydrated
|
|
|
|
# device has a new display name
|
|
|
|
await self.notify_device_update(user_id, [old_device_id, device_id])
|
|
|
|
|
|
|
|
return {"success": True}
|
|
|
|
|
2023-08-04 11:49:54 -06:00
|
|
|
async def delete_dehydrated_device(self, user_id: str, device_id: str) -> None:
|
|
|
|
"""
|
|
|
|
Delete a stored dehydrated device.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: the user_id to delete the device from
|
|
|
|
device_id: id of the dehydrated device to delete
|
|
|
|
"""
|
|
|
|
success = await self.store.remove_dehydrated_device(user_id, device_id)
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
raise errors.NotFoundError()
|
|
|
|
|
|
|
|
await self.delete_devices(user_id, [device_id])
|
|
|
|
await self.store.delete_e2e_keys_by_device(user_id=user_id, device_id=device_id)
|
|
|
|
|
2022-04-04 08:25:20 -06:00
|
|
|
@wrap_as_background_process("_handle_new_device_update_async")
|
|
|
|
async def _handle_new_device_update_async(self) -> None:
|
|
|
|
"""Called when we have a new local device list update that we need to
|
|
|
|
send out over federation.
|
|
|
|
|
|
|
|
This happens in the background so as not to block the original request
|
|
|
|
that generated the device update.
|
|
|
|
"""
|
|
|
|
if self._handle_new_device_update_is_processing:
|
|
|
|
self._handle_new_device_update_new_data = True
|
|
|
|
return
|
|
|
|
|
|
|
|
self._handle_new_device_update_is_processing = True
|
|
|
|
|
|
|
|
# The stream ID we processed previous iteration (if any), and the set of
|
|
|
|
# hosts we've already poked about for this update. This is so that we
|
|
|
|
# don't poke the same remote server about the same update repeatedly.
|
|
|
|
current_stream_id = None
|
|
|
|
hosts_already_sent_to: Set[str] = set()
|
|
|
|
|
|
|
|
try:
|
2022-11-22 09:46:52 -07:00
|
|
|
stream_id, room_id = await self.store.get_device_change_last_converted_pos()
|
|
|
|
|
2022-04-04 08:25:20 -06:00
|
|
|
while True:
|
|
|
|
self._handle_new_device_update_new_data = False
|
2022-11-22 09:46:52 -07:00
|
|
|
max_stream_id = self.store.get_device_stream_token()
|
|
|
|
rows = await self.store.get_uncoverted_outbound_room_pokes(
|
|
|
|
stream_id, room_id
|
|
|
|
)
|
2022-04-04 08:25:20 -06:00
|
|
|
if not rows:
|
|
|
|
# If the DB returned nothing then there is nothing left to
|
|
|
|
# do, *unless* a new device list update happened during the
|
|
|
|
# DB query.
|
2022-11-22 09:46:52 -07:00
|
|
|
|
|
|
|
# Advance `(stream_id, room_id)`.
|
|
|
|
# `max_stream_id` comes from *before* the query for unconverted
|
|
|
|
# rows, which means that any unconverted rows must have a larger
|
|
|
|
# stream ID.
|
|
|
|
if max_stream_id > stream_id:
|
|
|
|
stream_id, room_id = max_stream_id, ""
|
|
|
|
await self.store.set_device_change_last_converted_pos(
|
|
|
|
stream_id, room_id
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert max_stream_id == stream_id
|
|
|
|
# Avoid moving `room_id` backwards.
|
|
|
|
|
2022-04-04 08:25:20 -06:00
|
|
|
if self._handle_new_device_update_new_data:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
for user_id, device_id, room_id, stream_id, opentracing_context in rows:
|
2022-04-14 06:05:31 -06:00
|
|
|
hosts = set()
|
|
|
|
|
|
|
|
# Ignore any users that aren't ours
|
|
|
|
if self.hs.is_mine_id(user_id):
|
2022-08-24 13:15:37 -06:00
|
|
|
hosts = set(
|
2022-09-23 06:44:03 -06:00
|
|
|
await self._storage_controllers.state.get_current_hosts_in_room_or_partial_state_approximation(
|
2022-08-24 13:15:37 -06:00
|
|
|
room_id
|
|
|
|
)
|
|
|
|
)
|
2022-04-14 06:05:31 -06:00
|
|
|
hosts.discard(self.server_name)
|
2022-09-23 06:44:03 -06:00
|
|
|
# For rooms with partial state, `hosts` is merely an
|
|
|
|
# approximation. When we transition to a full state room, we
|
|
|
|
# will have to send out device list updates to any servers we
|
|
|
|
# missed.
|
2022-04-04 08:25:20 -06:00
|
|
|
|
|
|
|
# Check if we've already sent this update to some hosts
|
|
|
|
if current_stream_id == stream_id:
|
|
|
|
hosts -= hosts_already_sent_to
|
|
|
|
|
|
|
|
await self.store.add_device_list_outbound_pokes(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
room_id=room_id,
|
|
|
|
hosts=hosts,
|
|
|
|
context=opentracing_context,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Notify replication that we've updated the device list stream.
|
|
|
|
self.notifier.notify_replication()
|
|
|
|
|
|
|
|
if hosts:
|
|
|
|
logger.info(
|
|
|
|
"Sending device list update notif for %r to: %r",
|
|
|
|
user_id,
|
|
|
|
hosts,
|
|
|
|
)
|
2023-09-04 10:14:09 -06:00
|
|
|
await self.federation_sender.send_device_messages(
|
|
|
|
hosts, immediate=False
|
|
|
|
)
|
|
|
|
# TODO: when called, this isn't in a logging context.
|
|
|
|
# This leads to log spam, sentry event spam, and massive
|
|
|
|
# memory usage.
|
|
|
|
# See https://github.com/matrix-org/synapse/issues/12552.
|
|
|
|
# log_kv(
|
|
|
|
# {"message": "sent device update to host", "host": host}
|
|
|
|
# )
|
2022-04-04 08:25:20 -06:00
|
|
|
|
|
|
|
if current_stream_id != stream_id:
|
|
|
|
# Clear the set of hosts we've already sent to as we're
|
|
|
|
# processing a new update.
|
|
|
|
hosts_already_sent_to.clear()
|
|
|
|
|
|
|
|
hosts_already_sent_to.update(hosts)
|
|
|
|
current_stream_id = stream_id
|
|
|
|
|
2022-11-22 09:46:52 -07:00
|
|
|
# Advance `(stream_id, room_id)`.
|
|
|
|
_, _, room_id, stream_id, _ = rows[-1]
|
|
|
|
await self.store.set_device_change_last_converted_pos(
|
|
|
|
stream_id, room_id
|
|
|
|
)
|
|
|
|
|
2022-04-04 08:25:20 -06:00
|
|
|
finally:
|
|
|
|
self._handle_new_device_update_is_processing = False
|
|
|
|
|
2022-09-28 07:42:43 -06:00
|
|
|
async def handle_room_un_partial_stated(self, room_id: str) -> None:
|
|
|
|
"""Handles sending appropriate device list updates in a room that has
|
|
|
|
gone from partial to full state.
|
|
|
|
"""
|
|
|
|
|
2022-09-28 16:22:35 -06:00
|
|
|
# We defer to the device list updater to handle pending remote device
|
|
|
|
# list updates.
|
2022-09-28 07:42:43 -06:00
|
|
|
await self.device_list_updater.handle_room_un_partial_stated(room_id)
|
|
|
|
|
2022-09-28 16:22:35 -06:00
|
|
|
# Replay local updates.
|
|
|
|
(
|
|
|
|
join_event_id,
|
|
|
|
device_lists_stream_id,
|
|
|
|
) = await self.store.get_join_event_id_and_device_lists_stream_id_for_partial_state(
|
|
|
|
room_id
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get the local device list changes that have happened in the room since
|
|
|
|
# we started joining. If there are no updates there's nothing left to do.
|
|
|
|
changes = await self.store.get_device_list_changes_in_room(
|
|
|
|
room_id, device_lists_stream_id
|
|
|
|
)
|
|
|
|
local_changes = {(u, d) for u, d in changes if self.hs.is_mine_id(u)}
|
|
|
|
if not local_changes:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Note: We have persisted the full state at this point, we just haven't
|
|
|
|
# cleared the `partial_room` flag.
|
|
|
|
join_state_ids = await self._state_storage.get_state_ids_for_event(
|
|
|
|
join_event_id, await_full_state=False
|
|
|
|
)
|
|
|
|
current_state_ids = await self.store.get_partial_current_state_ids(room_id)
|
|
|
|
|
|
|
|
# Now we need to work out all servers that might have been in the room
|
|
|
|
# at any point during our join.
|
|
|
|
|
|
|
|
# First we look for any membership states that have changed between the
|
|
|
|
# initial join and now...
|
|
|
|
all_keys = set(join_state_ids)
|
|
|
|
all_keys.update(current_state_ids)
|
|
|
|
|
|
|
|
potentially_changed_hosts = set()
|
|
|
|
for etype, state_key in all_keys:
|
|
|
|
if etype != EventTypes.Member:
|
|
|
|
continue
|
|
|
|
|
|
|
|
prev = join_state_ids.get((etype, state_key))
|
|
|
|
current = current_state_ids.get((etype, state_key))
|
|
|
|
|
|
|
|
if prev != current:
|
|
|
|
potentially_changed_hosts.add(get_domain_from_id(state_key))
|
|
|
|
|
|
|
|
# ... then we add all the hosts that are currently joined to the room...
|
|
|
|
current_hosts_in_room = await self.store.get_current_hosts_in_room(room_id)
|
|
|
|
potentially_changed_hosts.update(current_hosts_in_room)
|
|
|
|
|
|
|
|
# ... and finally we remove any hosts that we were told about, as we
|
|
|
|
# will have sent device list updates to those hosts when they happened.
|
|
|
|
known_hosts_at_join = await self.store.get_partial_state_servers_at_join(
|
|
|
|
room_id
|
|
|
|
)
|
2023-02-03 08:39:59 -07:00
|
|
|
assert known_hosts_at_join is not None
|
2022-09-28 16:22:35 -06:00
|
|
|
potentially_changed_hosts.difference_update(known_hosts_at_join)
|
|
|
|
|
|
|
|
potentially_changed_hosts.discard(self.server_name)
|
|
|
|
|
|
|
|
if not potentially_changed_hosts:
|
|
|
|
# Nothing to do.
|
|
|
|
return
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Found %d changed hosts to send device list updates to",
|
|
|
|
len(potentially_changed_hosts),
|
|
|
|
)
|
|
|
|
|
|
|
|
for user_id, device_id in local_changes:
|
|
|
|
await self.store.add_device_list_outbound_pokes(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
room_id=room_id,
|
|
|
|
hosts=potentially_changed_hosts,
|
|
|
|
context=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Notify things that device lists need to be sent out.
|
|
|
|
self.notifier.notify_replication()
|
2023-09-04 10:14:09 -06:00
|
|
|
await self.federation_sender.send_device_messages(
|
|
|
|
potentially_changed_hosts, immediate=False
|
|
|
|
)
|
2022-09-28 16:22:35 -06:00
|
|
|
|
2017-03-01 07:12:11 -07:00
|
|
|
|
2020-10-07 06:58:21 -06:00
|
|
|
def _update_device_from_client_ips(
|
2023-10-17 06:47:42 -06:00
|
|
|
device: JsonDict, client_ips: Mapping[Tuple[str, str], DeviceLastConnectionInfo]
|
2020-10-07 06:58:21 -06:00
|
|
|
) -> None:
|
2023-10-17 06:47:42 -06:00
|
|
|
ip = client_ips.get((device["user_id"], device["device_id"]))
|
2022-08-19 10:17:10 -06:00
|
|
|
device.update(
|
|
|
|
{
|
2023-10-17 06:47:42 -06:00
|
|
|
"last_seen_user_agent": ip.user_agent if ip else None,
|
|
|
|
"last_seen_ts": ip.last_seen if ip else None,
|
|
|
|
"last_seen_ip": ip.ip if ip else None,
|
2022-08-19 10:17:10 -06:00
|
|
|
}
|
|
|
|
)
|
2017-03-01 07:12:11 -07:00
|
|
|
|
|
|
|
|
2022-11-22 12:08:04 -07:00
|
|
|
class DeviceListWorkerUpdater:
|
|
|
|
"Handles incoming device list updates from federation and contacts the main process over replication"
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
from synapse.replication.http.devices import (
|
2023-01-10 04:17:59 -07:00
|
|
|
ReplicationMultiUserDevicesResyncRestServlet,
|
2022-11-22 12:08:04 -07:00
|
|
|
)
|
|
|
|
|
2023-01-10 04:17:59 -07:00
|
|
|
self._multi_user_device_resync_client = (
|
|
|
|
ReplicationMultiUserDevicesResyncRestServlet.make_client(hs)
|
|
|
|
)
|
|
|
|
|
|
|
|
async def multi_user_device_resync(
|
|
|
|
self, user_ids: List[str], mark_failed_as_stale: bool = True
|
2023-09-07 06:45:43 -06:00
|
|
|
) -> Dict[str, Optional[JsonMapping]]:
|
2023-01-10 04:17:59 -07:00
|
|
|
"""
|
|
|
|
Like `user_device_resync` but operates on multiple users **from the same origin**
|
|
|
|
at once.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict from User ID to the same Dict as `user_device_resync`.
|
|
|
|
"""
|
|
|
|
# mark_failed_as_stale is not sent. Ensure this doesn't break expectations.
|
|
|
|
assert mark_failed_as_stale
|
|
|
|
|
|
|
|
if not user_ids:
|
|
|
|
# Shortcut empty requests
|
|
|
|
return {}
|
|
|
|
|
2023-04-21 05:06:39 -06:00
|
|
|
return await self._multi_user_device_resync_client(user_ids=user_ids)
|
2022-11-22 12:08:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceListUpdater(DeviceListWorkerUpdater):
|
2017-03-01 07:12:11 -07:00
|
|
|
"Handles incoming device list updates from federation and updates the DB"
|
|
|
|
|
2020-10-07 06:58:21 -06:00
|
|
|
def __init__(self, hs: "HomeServer", device_handler: DeviceHandler):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2018-03-13 07:26:52 -06:00
|
|
|
self.federation = hs.get_federation_client()
|
2017-03-01 07:12:11 -07:00
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.device_handler = device_handler
|
2023-01-22 14:10:11 -07:00
|
|
|
self._notifier = hs.get_notifier()
|
2017-03-01 07:12:11 -07:00
|
|
|
|
2017-03-03 08:31:57 -07:00
|
|
|
self._remote_edu_linearizer = Linearizer(name="remote_device_list")
|
2023-09-07 06:45:43 -06:00
|
|
|
self._resync_linearizer = Linearizer(name="remote_device_resync")
|
2017-03-01 07:12:11 -07:00
|
|
|
|
|
|
|
# user_id -> list of updates waiting to be handled.
|
2021-07-16 11:22:36 -06:00
|
|
|
self._pending_updates: Dict[
|
|
|
|
str, List[Tuple[str, str, Iterable[str], JsonDict]]
|
|
|
|
] = {}
|
2017-03-01 07:12:11 -07:00
|
|
|
|
|
|
|
# Recently seen stream ids. We don't bother keeping these in the DB,
|
|
|
|
# but they're useful to have them about to reduce the number of spurious
|
|
|
|
# resyncs.
|
2021-07-16 11:22:36 -06:00
|
|
|
self._seen_updates: ExpiringCache[str, Set[str]] = ExpiringCache(
|
2017-03-03 09:02:53 -07:00
|
|
|
cache_name="device_update_edu",
|
|
|
|
clock=self.clock,
|
|
|
|
max_len=10000,
|
|
|
|
expiry_ms=30 * 60 * 1000,
|
|
|
|
iterable=True,
|
2021-07-16 11:22:36 -06:00
|
|
|
)
|
2017-03-01 07:12:11 -07:00
|
|
|
|
2020-05-21 09:41:12 -06:00
|
|
|
# Attempt to resync out of sync device lists every 30s.
|
|
|
|
self._resync_retry_in_progress = False
|
|
|
|
self.clock.looping_call(
|
|
|
|
run_as_background_process,
|
|
|
|
30 * 1000,
|
|
|
|
func=self._maybe_retry_device_resync,
|
|
|
|
desc="_maybe_retry_device_resync",
|
|
|
|
)
|
|
|
|
|
2019-09-03 03:21:30 -06:00
|
|
|
@trace
|
2020-10-07 06:58:21 -06:00
|
|
|
async def incoming_device_list_update(
|
|
|
|
self, origin: str, edu_content: JsonDict
|
|
|
|
) -> None:
|
2017-03-01 07:12:11 -07:00
|
|
|
"""Called on incoming device list update from federation. Responsible
|
|
|
|
for parsing the EDU and adding to pending updates list.
|
|
|
|
"""
|
|
|
|
|
2019-09-03 03:21:30 -06:00
|
|
|
set_tag("origin", origin)
|
2022-07-21 06:01:52 -06:00
|
|
|
set_tag("edu_content", str(edu_content))
|
2017-03-01 07:12:11 -07:00
|
|
|
user_id = edu_content.pop("user_id")
|
|
|
|
device_id = edu_content.pop("device_id")
|
|
|
|
stream_id = str(edu_content.pop("stream_id")) # They may come as ints
|
|
|
|
prev_ids = edu_content.pop("prev_id", [])
|
2022-05-25 03:14:45 -06:00
|
|
|
if not isinstance(prev_ids, list):
|
|
|
|
raise SynapseError(
|
|
|
|
400, "Device list update had an invalid 'prev_ids' field"
|
|
|
|
)
|
2017-03-01 07:12:11 -07:00
|
|
|
prev_ids = [str(p) for p in prev_ids] # They may come as ints
|
2017-01-26 09:06:54 -07:00
|
|
|
|
|
|
|
if get_domain_from_id(user_id) != origin:
|
|
|
|
# TODO: Raise?
|
2019-03-07 08:53:14 -07:00
|
|
|
logger.warning(
|
|
|
|
"Got device list update edu for %r/%r from %r",
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
origin,
|
|
|
|
)
|
2019-09-03 03:21:30 -06:00
|
|
|
|
|
|
|
set_tag("error", True)
|
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"message": "Got a device list update edu from a user and "
|
|
|
|
"device which does not match the origin of the request.",
|
|
|
|
"user_id": user_id,
|
|
|
|
"device_id": device_id,
|
|
|
|
}
|
|
|
|
)
|
2017-01-26 09:06:54 -07:00
|
|
|
return
|
|
|
|
|
2022-09-28 07:42:43 -06:00
|
|
|
# Check if we are partially joining any rooms. If so we need to store
|
|
|
|
# all device list updates so that we can handle them correctly once we
|
|
|
|
# know who is in the room.
|
2022-12-05 06:07:55 -07:00
|
|
|
# TODO(faster_joins): this fetches and processes a bunch of data that we don't
|
2022-10-18 05:33:18 -06:00
|
|
|
# use. Could be replaced by a tighter query e.g.
|
|
|
|
# SELECT EXISTS(SELECT 1 FROM partial_state_rooms)
|
|
|
|
partial_rooms = await self.store.get_partial_state_room_resync_info()
|
2022-09-28 07:42:43 -06:00
|
|
|
if partial_rooms:
|
|
|
|
await self.store.add_remote_device_list_to_pending(
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
)
|
2023-01-22 14:10:11 -07:00
|
|
|
self._notifier.notify_replication()
|
2022-09-28 07:42:43 -06:00
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
room_ids = await self.store.get_rooms_for_user(user_id)
|
2017-03-16 05:51:46 -06:00
|
|
|
if not room_ids:
|
2017-01-26 09:39:33 -07:00
|
|
|
# We don't share any rooms with this user. Ignore update, as we
|
|
|
|
# probably won't get any further updates.
|
2019-09-03 03:21:30 -06:00
|
|
|
set_tag("error", True)
|
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"message": "Got an update from a user for which "
|
|
|
|
"we don't share any rooms",
|
|
|
|
"other user_id": user_id,
|
|
|
|
}
|
|
|
|
)
|
2019-03-07 08:53:14 -07:00
|
|
|
logger.warning(
|
|
|
|
"Got device list update edu for %r/%r, but don't share a room",
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
)
|
2017-01-26 09:39:33 -07:00
|
|
|
return
|
2017-01-26 09:06:54 -07:00
|
|
|
|
2019-03-07 08:53:14 -07:00
|
|
|
logger.debug("Received device list update for %r/%r", user_id, device_id)
|
|
|
|
|
2017-03-01 07:12:11 -07:00
|
|
|
self._pending_updates.setdefault(user_id, []).append(
|
|
|
|
(device_id, stream_id, prev_ids, edu_content)
|
|
|
|
)
|
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
await self._handle_device_updates(user_id)
|
2017-03-01 07:12:11 -07:00
|
|
|
|
|
|
|
@measure_func("_incoming_device_list_update")
|
2020-10-07 06:58:21 -06:00
|
|
|
async def _handle_device_updates(self, user_id: str) -> None:
|
2017-03-01 07:12:11 -07:00
|
|
|
"Actually handle pending updates."
|
|
|
|
|
2022-04-05 08:43:52 -06:00
|
|
|
async with self._remote_edu_linearizer.queue(user_id):
|
2017-03-01 07:12:11 -07:00
|
|
|
pending_updates = self._pending_updates.pop(user_id, [])
|
|
|
|
if not pending_updates:
|
|
|
|
# This can happen since we batch updates
|
|
|
|
return
|
|
|
|
|
2021-04-20 04:50:49 -06:00
|
|
|
for device_id, stream_id, prev_ids, _ in pending_updates:
|
2019-03-07 08:53:14 -07:00
|
|
|
logger.debug(
|
|
|
|
"Handling update %r/%r, ID: %r, prev: %r ",
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
stream_id,
|
|
|
|
prev_ids,
|
|
|
|
)
|
|
|
|
|
2017-05-05 03:59:32 -06:00
|
|
|
# Given a list of updates we check if we need to resync. This
|
|
|
|
# happens if we've missed updates.
|
2020-07-17 05:09:25 -06:00
|
|
|
resync = await self._need_to_do_resync(user_id, pending_updates)
|
2017-01-26 09:06:54 -07:00
|
|
|
|
2020-02-05 07:02:39 -07:00
|
|
|
if logger.isEnabledFor(logging.INFO):
|
|
|
|
logger.info(
|
|
|
|
"Received device list update for %s, requiring resync: %s. Devices: %s",
|
|
|
|
user_id,
|
|
|
|
resync,
|
|
|
|
", ".join(u[0] for u in pending_updates),
|
|
|
|
)
|
2019-03-07 08:53:14 -07:00
|
|
|
|
2017-01-26 09:06:54 -07:00
|
|
|
if resync:
|
2023-07-24 06:43:43 -06:00
|
|
|
# We mark as stale up front in case we get restarted.
|
|
|
|
await self.store.mark_remote_users_device_caches_as_stale([user_id])
|
|
|
|
run_as_background_process(
|
|
|
|
"_maybe_retry_device_resync",
|
|
|
|
self.multi_user_device_resync,
|
|
|
|
[user_id],
|
|
|
|
False,
|
|
|
|
)
|
2017-01-26 09:06:54 -07:00
|
|
|
else:
|
2017-01-26 09:30:37 -07:00
|
|
|
# Simply update the single device, since we know that is the only
|
2018-07-10 10:58:09 -06:00
|
|
|
# change (because of the single prev_id matching the current cache)
|
2021-04-20 04:50:49 -06:00
|
|
|
for device_id, stream_id, _, content in pending_updates:
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.store.update_remote_device_list_cache_entry(
|
2017-03-01 07:12:11 -07:00
|
|
|
user_id, device_id, content, stream_id
|
|
|
|
)
|
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.device_handler.notify_device_update(
|
2017-03-01 07:12:11 -07:00
|
|
|
user_id, [device_id for device_id, _, _, _ in pending_updates]
|
2017-01-26 09:06:54 -07:00
|
|
|
)
|
|
|
|
|
2019-03-07 09:04:24 -07:00
|
|
|
self._seen_updates.setdefault(user_id, set()).update(
|
|
|
|
stream_id for _, stream_id, _, _ in pending_updates
|
|
|
|
)
|
2017-01-26 09:06:54 -07:00
|
|
|
|
2020-10-07 06:58:21 -06:00
|
|
|
async def _need_to_do_resync(
|
|
|
|
self, user_id: str, updates: Iterable[Tuple[str, str, Iterable[str], JsonDict]]
|
|
|
|
) -> bool:
|
2017-03-01 07:12:11 -07:00
|
|
|
"""Given a list of updates for a user figure out if we need to do a full
|
|
|
|
resync, or whether we have enough data that we can just apply the delta.
|
|
|
|
"""
|
2021-07-16 11:22:36 -06:00
|
|
|
seen_updates: Set[str] = self._seen_updates.get(user_id, set())
|
2017-01-26 09:39:33 -07:00
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
extremity = await self.store.get_device_list_last_stream_id_for_remote(user_id)
|
2016-07-20 10:58:44 -06:00
|
|
|
|
2019-03-07 08:53:14 -07:00
|
|
|
logger.debug("Current extremity for %r: %r", user_id, extremity)
|
|
|
|
|
2017-03-01 07:12:11 -07:00
|
|
|
stream_id_in_updates = set() # stream_ids in updates list
|
|
|
|
for _, stream_id, prev_ids, _ in updates:
|
|
|
|
if not prev_ids:
|
|
|
|
# We always do a resync if there are no previous IDs
|
2019-07-23 07:00:55 -06:00
|
|
|
return True
|
2017-03-01 07:12:11 -07:00
|
|
|
|
|
|
|
for prev_id in prev_ids:
|
|
|
|
if prev_id == extremity:
|
|
|
|
continue
|
|
|
|
elif prev_id in seen_updates:
|
|
|
|
continue
|
|
|
|
elif prev_id in stream_id_in_updates:
|
|
|
|
continue
|
|
|
|
else:
|
2019-07-23 07:00:55 -06:00
|
|
|
return True
|
2017-03-01 07:12:11 -07:00
|
|
|
|
|
|
|
stream_id_in_updates.add(stream_id)
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return False
|
2019-07-29 09:34:44 -06:00
|
|
|
|
2020-06-15 09:42:21 -06:00
|
|
|
@trace
|
2020-10-07 06:58:21 -06:00
|
|
|
async def _maybe_retry_device_resync(self) -> None:
|
2020-05-21 09:41:12 -06:00
|
|
|
"""Retry to resync device lists that are out of sync, except if another retry is
|
|
|
|
in progress.
|
|
|
|
"""
|
|
|
|
if self._resync_retry_in_progress:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Prevent another call of this function to retry resyncing device lists so
|
|
|
|
# we don't send too many requests.
|
|
|
|
self._resync_retry_in_progress = True
|
|
|
|
# Get all of the users that need resyncing.
|
2020-07-17 05:09:25 -06:00
|
|
|
need_resync = await self.store.get_user_ids_requiring_device_list_resync()
|
2023-09-11 06:11:02 -06:00
|
|
|
|
|
|
|
# Filter out users whose host is marked as "down" up front.
|
|
|
|
hosts = await filter_destinations_by_retry_limiter(
|
|
|
|
{get_domain_from_id(u) for u in need_resync}, self.clock, self.store
|
|
|
|
)
|
|
|
|
hosts = set(hosts)
|
|
|
|
|
2020-05-21 09:41:12 -06:00
|
|
|
# Iterate over the set of user IDs.
|
|
|
|
for user_id in need_resync:
|
2023-09-11 06:11:02 -06:00
|
|
|
if get_domain_from_id(user_id) not in hosts:
|
|
|
|
continue
|
|
|
|
|
2020-06-01 04:55:14 -06:00
|
|
|
try:
|
|
|
|
# Try to resync the current user's devices list.
|
2023-04-21 05:06:39 -06:00
|
|
|
result = (await self.multi_user_device_resync([user_id], False))[
|
|
|
|
user_id
|
|
|
|
]
|
2020-06-01 04:55:14 -06:00
|
|
|
|
|
|
|
# user_device_resync only returns a result if it managed to
|
|
|
|
# successfully resync and update the database. Updating the table
|
|
|
|
# of users requiring resync isn't necessary here as
|
|
|
|
# user_device_resync already does it (through
|
|
|
|
# self.store.update_remote_device_list_cache).
|
|
|
|
if result:
|
|
|
|
logger.debug(
|
|
|
|
"Successfully resynced the device list for %s",
|
|
|
|
user_id,
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
# If there was an issue resyncing this user, e.g. if the remote
|
|
|
|
# server sent a malformed result, just log the error instead of
|
|
|
|
# aborting all the subsequent resyncs.
|
2020-05-21 09:41:12 -06:00
|
|
|
logger.debug(
|
2020-06-01 04:55:14 -06:00
|
|
|
"Could not resync the device list for %s: %s",
|
|
|
|
user_id,
|
|
|
|
e,
|
2020-05-21 09:41:12 -06:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
# Allow future calls to retry resyncinc out of sync device lists.
|
|
|
|
self._resync_retry_in_progress = False
|
|
|
|
|
2023-01-10 04:17:59 -07:00
|
|
|
async def multi_user_device_resync(
|
|
|
|
self, user_ids: List[str], mark_failed_as_stale: bool = True
|
2023-09-07 06:45:43 -06:00
|
|
|
) -> Dict[str, Optional[JsonMapping]]:
|
2023-01-10 04:17:59 -07:00
|
|
|
"""
|
|
|
|
Like `user_device_resync` but operates on multiple users **from the same origin**
|
|
|
|
at once.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict from User ID to the same Dict as `user_device_resync`.
|
|
|
|
"""
|
|
|
|
if not user_ids:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
origins = {UserID.from_string(user_id).domain for user_id in user_ids}
|
|
|
|
|
|
|
|
if len(origins) != 1:
|
|
|
|
raise InvalidAPICallError(f"Only one origin permitted, got {origins!r}")
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
failed = set()
|
|
|
|
# TODO(Perf): Actually batch these up
|
|
|
|
for user_id in user_ids:
|
2023-09-07 06:45:43 -06:00
|
|
|
async with self._resync_linearizer.queue(user_id):
|
|
|
|
(
|
|
|
|
user_result,
|
|
|
|
user_failed,
|
|
|
|
) = await self._user_device_resync_returning_failed(user_id)
|
2023-01-10 04:17:59 -07:00
|
|
|
result[user_id] = user_result
|
|
|
|
if user_failed:
|
|
|
|
failed.add(user_id)
|
|
|
|
|
|
|
|
if mark_failed_as_stale:
|
|
|
|
await self.store.mark_remote_users_device_caches_as_stale(failed)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
async def _user_device_resync_returning_failed(
|
|
|
|
self, user_id: str
|
2023-09-07 06:45:43 -06:00
|
|
|
) -> Tuple[Optional[JsonMapping], bool]:
|
2019-07-29 09:34:44 -06:00
|
|
|
"""Fetches all devices for a user and updates the device cache with them.
|
|
|
|
|
|
|
|
Args:
|
2020-07-17 05:09:25 -06:00
|
|
|
user_id: The user's id whose device_list will be updated.
|
2019-07-29 09:34:44 -06:00
|
|
|
Returns:
|
2023-01-10 04:17:59 -07:00
|
|
|
- A dict with device info as under the "devices" in the result of this
|
|
|
|
request:
|
|
|
|
https://matrix.org/docs/spec/server_server/r0.1.2#get-matrix-federation-v1-user-devices-userid
|
|
|
|
None when we weren't able to fetch the device info for some reason,
|
|
|
|
e.g. due to a connection problem.
|
|
|
|
- True iff the resync failed and the device list should be marked as stale.
|
2019-07-29 09:34:44 -06:00
|
|
|
"""
|
2023-09-07 06:45:43 -06:00
|
|
|
# Check that we haven't gone and fetched the devices since we last
|
|
|
|
# checked if we needed to resync these device lists.
|
|
|
|
if await self.store.get_users_whose_devices_are_cached([user_id]):
|
|
|
|
cached = await self.store.get_cached_devices_for_user(user_id)
|
|
|
|
return cached, False
|
|
|
|
|
2020-06-01 04:55:14 -06:00
|
|
|
logger.debug("Attempting to resync the device list for %s", user_id)
|
2019-09-03 03:21:30 -06:00
|
|
|
log_kv({"message": "Doing resync to update device list."})
|
2019-07-29 09:34:44 -06:00
|
|
|
# Fetch all devices for the user.
|
|
|
|
origin = get_domain_from_id(user_id)
|
|
|
|
try:
|
2020-07-17 05:09:25 -06:00
|
|
|
result = await self.federation.query_user_devices(origin, user_id)
|
2020-05-21 09:41:12 -06:00
|
|
|
except NotRetryingDestination:
|
2023-01-10 04:17:59 -07:00
|
|
|
return None, True
|
2020-05-21 09:41:12 -06:00
|
|
|
except (RequestSendFailed, HttpResponseException) as e:
|
|
|
|
logger.warning(
|
|
|
|
"Failed to handle device list update for %s: %s",
|
|
|
|
user_id,
|
|
|
|
e,
|
|
|
|
)
|
|
|
|
|
2019-07-29 09:34:44 -06:00
|
|
|
# We abort on exceptions rather than accepting the update
|
|
|
|
# as otherwise synapse will 'forget' that its device list
|
|
|
|
# is out of date. If we bail then we will retry the resync
|
|
|
|
# next time we get a device list update for this user_id.
|
|
|
|
# This makes it more likely that the device lists will
|
|
|
|
# eventually become consistent.
|
2023-01-10 04:17:59 -07:00
|
|
|
return None, True
|
2019-07-29 09:34:44 -06:00
|
|
|
except FederationDeniedError as e:
|
2019-09-03 03:21:30 -06:00
|
|
|
set_tag("error", True)
|
|
|
|
log_kv({"reason": "FederationDeniedError"})
|
2019-07-29 09:34:44 -06:00
|
|
|
logger.info(e)
|
2023-01-10 04:17:59 -07:00
|
|
|
return None, False
|
2019-09-03 03:21:30 -06:00
|
|
|
except Exception as e:
|
|
|
|
set_tag("error", True)
|
|
|
|
log_kv(
|
|
|
|
{"message": "Exception raised by federation request", "exception": e}
|
|
|
|
)
|
2019-07-29 09:34:44 -06:00
|
|
|
logger.exception("Failed to handle device list update for %s", user_id)
|
2020-05-21 09:41:12 -06:00
|
|
|
|
2023-01-10 04:17:59 -07:00
|
|
|
return None, True
|
2019-09-03 03:21:30 -06:00
|
|
|
log_kv({"result": result})
|
2019-07-29 09:34:44 -06:00
|
|
|
stream_id = result["stream_id"]
|
|
|
|
devices = result["devices"]
|
|
|
|
|
2020-06-01 09:47:30 -06:00
|
|
|
# Get the master key and the self-signing key for this user if provided in the
|
|
|
|
# response (None if not in the response).
|
|
|
|
# The response will not contain the user signing key, as this key is only used by
|
|
|
|
# its owner, thus it doesn't make sense to send it over federation.
|
|
|
|
master_key = result.get("master_key")
|
|
|
|
self_signing_key = result.get("self_signing_key")
|
|
|
|
|
2021-03-17 09:04:57 -06:00
|
|
|
ignore_devices = False
|
2019-07-29 09:34:44 -06:00
|
|
|
# If the remote server has more than ~1000 devices for this user
|
|
|
|
# we assume that something is going horribly wrong (e.g. a bot
|
|
|
|
# that logs in and creates a new device every time it tries to
|
|
|
|
# send a message). Maintaining lots of devices per user in the
|
|
|
|
# cache can cause serious performance issues as if this request
|
|
|
|
# takes more than 60s to complete, internal replication from the
|
|
|
|
# inbound federation worker to the synapse master may time out
|
|
|
|
# causing the inbound federation to fail and causing the remote
|
|
|
|
# server to retry, causing a DoS. So in this scenario we give
|
|
|
|
# up on storing the total list of devices and only handle the
|
|
|
|
# delta instead.
|
|
|
|
if len(devices) > 1000:
|
2019-10-31 04:23:24 -06:00
|
|
|
logger.warning(
|
2019-07-29 09:34:44 -06:00
|
|
|
"Ignoring device list snapshot for %s as it has >1K devs (%d)",
|
|
|
|
user_id,
|
|
|
|
len(devices),
|
|
|
|
)
|
|
|
|
devices = []
|
2021-03-17 09:04:57 -06:00
|
|
|
ignore_devices = True
|
|
|
|
else:
|
2022-01-05 06:33:28 -07:00
|
|
|
prev_stream_id = await self.store.get_device_list_last_stream_id_for_remote(
|
|
|
|
user_id
|
|
|
|
)
|
2021-03-17 09:04:57 -06:00
|
|
|
cached_devices = await self.store.get_cached_devices_for_user(user_id)
|
2022-01-05 06:33:28 -07:00
|
|
|
|
|
|
|
# To ensure that a user with no devices is cached, we skip the resync only
|
|
|
|
# if we have a stream_id from previously writing a cache entry.
|
|
|
|
if prev_stream_id is not None and cached_devices == {
|
|
|
|
d["device_id"]: d for d in devices
|
|
|
|
}:
|
2021-04-22 09:53:24 -06:00
|
|
|
logging.info(
|
|
|
|
"Skipping device list resync for %s, as our cache matches already",
|
|
|
|
user_id,
|
|
|
|
)
|
2021-03-17 09:04:57 -06:00
|
|
|
devices = []
|
|
|
|
ignore_devices = True
|
2019-07-29 09:34:44 -06:00
|
|
|
|
|
|
|
for device in devices:
|
|
|
|
logger.debug(
|
|
|
|
"Handling resync update %r/%r, ID: %r",
|
|
|
|
user_id,
|
|
|
|
device["device_id"],
|
|
|
|
stream_id,
|
|
|
|
)
|
|
|
|
|
2021-03-17 09:04:57 -06:00
|
|
|
if not ignore_devices:
|
|
|
|
await self.store.update_remote_device_list_cache(
|
|
|
|
user_id, devices, stream_id
|
|
|
|
)
|
2021-04-22 09:53:24 -06:00
|
|
|
# mark the cache as valid, whether or not we actually processed any device
|
|
|
|
# list updates.
|
|
|
|
await self.store.mark_remote_user_device_cache_as_valid(user_id)
|
2019-07-29 09:34:44 -06:00
|
|
|
device_ids = [device["device_id"] for device in devices]
|
2020-06-01 09:47:30 -06:00
|
|
|
|
|
|
|
# Handle cross-signing keys.
|
2020-07-17 05:09:25 -06:00
|
|
|
cross_signing_device_ids = await self.process_cross_signing_key_update(
|
2020-06-01 09:47:30 -06:00
|
|
|
user_id,
|
|
|
|
master_key,
|
|
|
|
self_signing_key,
|
|
|
|
)
|
|
|
|
device_ids = device_ids + cross_signing_device_ids
|
|
|
|
|
2021-03-17 09:04:57 -06:00
|
|
|
if device_ids:
|
|
|
|
await self.device_handler.notify_device_update(user_id, device_ids)
|
2019-07-29 09:34:44 -06:00
|
|
|
|
|
|
|
# We clobber the seen updates since we've re-synced from a given
|
|
|
|
# point.
|
2020-02-21 05:15:07 -07:00
|
|
|
self._seen_updates[user_id] = {stream_id}
|
2019-07-29 09:34:44 -06:00
|
|
|
|
2023-01-10 04:17:59 -07:00
|
|
|
return result, False
|
2020-06-01 09:47:30 -06:00
|
|
|
|
2020-07-17 05:09:25 -06:00
|
|
|
async def process_cross_signing_key_update(
|
2020-06-01 09:47:30 -06:00
|
|
|
self,
|
|
|
|
user_id: str,
|
2021-01-28 06:34:19 -07:00
|
|
|
master_key: Optional[JsonDict],
|
|
|
|
self_signing_key: Optional[JsonDict],
|
2020-10-07 06:58:21 -06:00
|
|
|
) -> List[str]:
|
2020-06-01 09:47:30 -06:00
|
|
|
"""Process the given new master and self-signing key for the given remote user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: The ID of the user these keys are for.
|
|
|
|
master_key: The dict of the cross-signing master key as returned by the
|
|
|
|
remote server.
|
|
|
|
self_signing_key: The dict of the cross-signing self-signing key as returned
|
|
|
|
by the remote server.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
The device IDs for the given keys.
|
|
|
|
"""
|
|
|
|
device_ids = []
|
|
|
|
|
2021-03-17 09:04:57 -06:00
|
|
|
current_keys_map = await self.store.get_e2e_cross_signing_keys_bulk([user_id])
|
|
|
|
current_keys = current_keys_map.get(user_id) or {}
|
|
|
|
|
|
|
|
if master_key and master_key != current_keys.get("master"):
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.store.set_e2e_cross_signing_key(user_id, "master", master_key)
|
2020-06-01 09:47:30 -06:00
|
|
|
_, verify_key = get_verify_key_from_cross_signing_key(master_key)
|
|
|
|
# verify_key is a VerifyKey from signedjson, which uses
|
|
|
|
# .version to denote the portion of the key ID after the
|
|
|
|
# algorithm and colon, which is the device ID
|
|
|
|
device_ids.append(verify_key.version)
|
2021-03-17 09:04:57 -06:00
|
|
|
if self_signing_key and self_signing_key != current_keys.get("self_signing"):
|
2020-07-17 05:09:25 -06:00
|
|
|
await self.store.set_e2e_cross_signing_key(
|
2020-06-01 09:47:30 -06:00
|
|
|
user_id, "self_signing", self_signing_key
|
|
|
|
)
|
|
|
|
_, verify_key = get_verify_key_from_cross_signing_key(self_signing_key)
|
|
|
|
device_ids.append(verify_key.version)
|
|
|
|
|
|
|
|
return device_ids
|
2022-09-28 07:42:43 -06:00
|
|
|
|
|
|
|
async def handle_room_un_partial_stated(self, room_id: str) -> None:
|
|
|
|
"""Handles sending appropriate device list updates in a room that has
|
|
|
|
gone from partial to full state.
|
|
|
|
"""
|
|
|
|
|
|
|
|
pending_updates = (
|
|
|
|
await self.store.get_pending_remote_device_list_updates_for_room(room_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
for user_id, device_id in pending_updates:
|
|
|
|
logger.info(
|
|
|
|
"Got pending device list update in room %s: %s / %s",
|
|
|
|
room_id,
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
)
|
|
|
|
position = await self.store.add_device_change_to_streams(
|
|
|
|
user_id,
|
|
|
|
[device_id],
|
|
|
|
room_ids=[room_id],
|
|
|
|
)
|
|
|
|
|
|
|
|
if not position:
|
|
|
|
# This should only happen if there are no updates, which
|
|
|
|
# shouldn't happen when we've passed in a non-empty set of
|
|
|
|
# device IDs.
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.device_handler.notifier.on_new_event(
|
|
|
|
StreamKeyType.DEVICE_LIST, position, rooms=[room_id]
|
|
|
|
)
|