2016-07-15 14:19:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2019-07-25 17:08:24 +02:00
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
2016-07-15 14:19:07 +02: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 08:09:20 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from six import iteritems, itervalues
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2016-07-20 18:58:44 +02:00
|
|
|
from synapse.api import errors
|
2017-02-01 18:33:16 +01:00
|
|
|
from synapse.api.constants import EventTypes
|
2019-02-18 16:24:13 +01:00
|
|
|
from synapse.api.errors import (
|
|
|
|
FederationDeniedError,
|
|
|
|
HttpResponseException,
|
|
|
|
RequestSendFailed,
|
|
|
|
)
|
2019-09-03 11:21:30 +02:00
|
|
|
from synapse.logging.opentracing import log_kv, set_tag, trace
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.types import RoomStreamToken, get_domain_from_id
|
2016-07-15 14:19:07 +02:00
|
|
|
from synapse.util import stringutils
|
2018-08-10 15:50:21 +02:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2017-03-03 17:02:53 +01:00
|
|
|
from synapse.util.caches.expiringcache import ExpiringCache
|
2017-02-02 19:36:17 +01:00
|
|
|
from synapse.util.metrics import measure_func
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.util.retryutils import NotRetryingDestination
|
2016-07-15 14:19:07 +02:00
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from ._base import BaseHandler
|
2018-04-28 13:19:12 +02:00
|
|
|
|
2016-07-15 14:19:07 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-03-04 19:24:32 +01:00
|
|
|
class DeviceWorkerHandler(BaseHandler):
|
2016-07-15 14:19:07 +02:00
|
|
|
def __init__(self, hs):
|
2019-03-04 19:24:32 +01:00
|
|
|
super(DeviceWorkerHandler, self).__init__(hs)
|
2016-07-15 14:19:07 +02:00
|
|
|
|
2017-01-26 17:06:54 +01:00
|
|
|
self.hs = hs
|
2017-01-25 15:27:27 +01:00
|
|
|
self.state = hs.get_state_handler()
|
2017-11-01 11:29:34 +01:00
|
|
|
self._auth_handler = hs.get_auth_handler()
|
2019-03-04 19:24:32 +01:00
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
@trace
|
2019-03-04 19:24:32 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_devices_by_user(self, user_id):
|
|
|
|
"""
|
|
|
|
Retrieve the given user's devices
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
|
|
|
Returns:
|
|
|
|
defer.Deferred: list[dict[str, X]]: info on each device
|
|
|
|
"""
|
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
set_tag("user_id", user_id)
|
2019-03-04 19:24:32 +01:00
|
|
|
device_map = yield self.store.get_devices_by_user(user_id)
|
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
ips = yield self.store.get_last_client_ip_by_device(user_id, device_id=None)
|
2019-03-04 19:24:32 +01:00
|
|
|
|
|
|
|
devices = list(device_map.values())
|
|
|
|
for device in devices:
|
|
|
|
_update_device_from_client_ips(device, ips)
|
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
log_kv(device_map)
|
2019-07-23 15:00:55 +02:00
|
|
|
return devices
|
2019-03-04 19:24:32 +01:00
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
@trace
|
2019-03-04 19:24:32 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_device(self, user_id, device_id):
|
|
|
|
""" Retrieve the given device
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
|
|
|
device_id (str):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
defer.Deferred: dict[str, X]: info on the device
|
|
|
|
Raises:
|
|
|
|
errors.NotFoundError: if the device was not found
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
device = yield self.store.get_device(user_id, device_id)
|
|
|
|
except errors.StoreError:
|
|
|
|
raise errors.NotFoundError
|
2019-06-20 11:32:02 +02:00
|
|
|
ips = yield self.store.get_last_client_ip_by_device(user_id, device_id)
|
2019-03-04 19:24:32 +01:00
|
|
|
_update_device_from_client_ips(device, ips)
|
2019-09-03 11:21:30 +02:00
|
|
|
|
|
|
|
set_tag("device", device)
|
|
|
|
set_tag("ips", ips)
|
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return device
|
2019-03-04 19:24:32 +01:00
|
|
|
|
|
|
|
@measure_func("device.get_user_ids_changed")
|
2019-09-03 11:21:30 +02:00
|
|
|
@trace
|
2019-03-04 19:24:32 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_user_ids_changed(self, user_id, from_token):
|
|
|
|
"""Get list of users that have had the devices updated, or have newly
|
|
|
|
joined a room, that `user_id` may be interested in.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str)
|
|
|
|
from_token (StreamToken)
|
|
|
|
"""
|
2019-09-03 11:21:30 +02:00
|
|
|
|
|
|
|
set_tag("user_id", user_id)
|
|
|
|
set_tag("from_token", from_token)
|
2019-03-04 19:09:06 +01:00
|
|
|
now_room_key = yield self.store.get_room_events_max_id()
|
2019-03-04 19:24:32 +01:00
|
|
|
|
|
|
|
room_ids = yield self.store.get_rooms_for_user(user_id)
|
|
|
|
|
2019-06-26 12:56:52 +02:00
|
|
|
# First we check if any devices have changed for users that we share
|
|
|
|
# rooms with.
|
|
|
|
users_who_share_room = yield self.store.get_users_who_share_room_with_user(
|
|
|
|
user_id
|
|
|
|
)
|
2019-06-26 20:09:10 +02:00
|
|
|
changed = yield self.store.get_users_whose_devices_changed(
|
2019-06-26 12:56:52 +02:00
|
|
|
from_token.device_list_key, users_who_share_room
|
2019-03-04 19:24:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Then work out if any users have since joined
|
|
|
|
rooms_changed = self.store.get_rooms_that_changed(room_ids, from_token.room_key)
|
|
|
|
|
|
|
|
member_events = yield self.store.get_membership_changes_for_user(
|
2019-06-20 11:32:02 +02:00
|
|
|
user_id, from_token.room_key, now_room_key
|
2019-03-04 19:24:32 +01:00
|
|
|
)
|
|
|
|
rooms_changed.update(event.room_id for event in member_events)
|
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
stream_ordering = RoomStreamToken.parse_stream_token(from_token.room_key).stream
|
2019-03-04 19:24:32 +01:00
|
|
|
|
|
|
|
possibly_changed = set(changed)
|
|
|
|
possibly_left = set()
|
|
|
|
for room_id in rooms_changed:
|
|
|
|
current_state_ids = yield self.store.get_current_state_ids(room_id)
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
for key, event_id in iteritems(current_state_ids):
|
|
|
|
etype, state_key = key
|
|
|
|
if etype != EventTypes.Member:
|
|
|
|
continue
|
|
|
|
possibly_left.add(state_key)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Fetch the current state at the time.
|
|
|
|
try:
|
|
|
|
event_ids = yield self.store.get_forward_extremeties_for_room(
|
|
|
|
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 11:21:30 +02:00
|
|
|
log_kv(
|
|
|
|
{"event": "encountered empty previous state", "room_id": room_id}
|
|
|
|
)
|
2019-03-04 19:24:32 +01:00
|
|
|
for key, event_id in iteritems(current_state_ids):
|
|
|
|
etype, state_key = key
|
|
|
|
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
|
|
|
|
prev_state_ids = yield self.store.get_state_ids_for_events(event_ids)
|
|
|
|
|
|
|
|
# Check if we've joined the room? If so we just blindly add all the users to
|
|
|
|
# the "possibly changed" users.
|
|
|
|
for state_dict in itervalues(prev_state_ids):
|
|
|
|
member_event = state_dict.get((EventTypes.Member, user_id), None)
|
|
|
|
if not member_event or member_event != current_member_id:
|
|
|
|
for key, event_id in iteritems(current_state_ids):
|
|
|
|
etype, state_key = key
|
|
|
|
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.
|
|
|
|
for key, event_id in iteritems(current_state_ids):
|
|
|
|
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.
|
|
|
|
for state_dict in itervalues(prev_state_ids):
|
|
|
|
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:
|
|
|
|
# Take the intersection of the users whose devices may have changed
|
|
|
|
# and those that actually still share a room with the user
|
|
|
|
possibly_joined = possibly_changed & users_who_share_room
|
|
|
|
possibly_left = (possibly_changed | possibly_left) - users_who_share_room
|
|
|
|
else:
|
|
|
|
possibly_joined = []
|
|
|
|
possibly_left = []
|
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
result = {"changed": list(possibly_joined), "left": list(possibly_left)}
|
|
|
|
|
|
|
|
log_kv(result)
|
|
|
|
|
|
|
|
return result
|
2019-03-04 19:24:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceHandler(DeviceWorkerHandler):
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(DeviceHandler, self).__init__(hs)
|
|
|
|
|
2017-01-26 17:06:54 +01:00
|
|
|
self.federation_sender = hs.get_federation_sender()
|
2017-03-01 15:12:11 +01:00
|
|
|
|
2019-07-29 17:34:44 +02:00
|
|
|
self.device_list_updater = DeviceListUpdater(hs, self)
|
2017-01-26 17:06:54 +01:00
|
|
|
|
2018-03-12 17:17:08 +01:00
|
|
|
federation_registry = hs.get_federation_registry()
|
|
|
|
|
|
|
|
federation_registry.register_edu_handler(
|
2019-07-29 17:34:44 +02:00
|
|
|
"m.device_list_update", self.device_list_updater.incoming_device_list_update
|
2017-01-26 17:06:54 +01:00
|
|
|
)
|
2018-03-12 17:17:08 +01:00
|
|
|
federation_registry.register_query_handler(
|
2019-06-20 11:32:02 +02:00
|
|
|
"user_devices", self.on_federation_query_user_devices
|
2017-01-26 17:06:54 +01:00
|
|
|
)
|
2017-01-25 15:27:27 +01:00
|
|
|
|
2017-01-26 17:39:33 +01:00
|
|
|
hs.get_distributor().observe("user_left_room", self.user_left_room)
|
|
|
|
|
2016-07-15 14:19:07 +02:00
|
|
|
@defer.inlineCallbacks
|
2019-06-20 11:32:02 +02:00
|
|
|
def check_device_registered(
|
|
|
|
self, user_id, device_id, initial_device_display_name=None
|
|
|
|
):
|
2016-07-15 14:19:07 +02: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:
|
|
|
|
user_id (str): @user:id
|
|
|
|
device_id (str | None): device id supplied by client
|
|
|
|
initial_device_display_name (str | None): device display name from
|
|
|
|
client
|
|
|
|
Returns:
|
|
|
|
str: device id (generated if none was supplied)
|
|
|
|
"""
|
|
|
|
if device_id is not None:
|
2017-01-25 15:27:27 +01:00
|
|
|
new_device = yield self.store.store_device(
|
2016-07-15 14:19:07 +02:00
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
initial_device_display_name=initial_device_display_name,
|
|
|
|
)
|
2017-01-25 15:27:27 +01:00
|
|
|
if new_device:
|
2017-01-26 17:06:54 +01:00
|
|
|
yield self.notify_device_update(user_id, [device_id])
|
2019-07-23 15:00:55 +02:00
|
|
|
return device_id
|
2016-07-15 14:19:07 +02: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:
|
2017-01-25 15:27:27 +01:00
|
|
|
device_id = stringutils.random_string(10).upper()
|
|
|
|
new_device = yield self.store.store_device(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
initial_device_display_name=initial_device_display_name,
|
|
|
|
)
|
|
|
|
if new_device:
|
2017-01-26 17:06:54 +01:00
|
|
|
yield self.notify_device_update(user_id, [device_id])
|
2019-07-23 15:00:55 +02:00
|
|
|
return device_id
|
2017-01-25 15:27:27 +01:00
|
|
|
attempts += 1
|
2016-07-15 14:19:07 +02:00
|
|
|
|
2016-07-20 18:58:44 +02:00
|
|
|
raise errors.StoreError(500, "Couldn't generate a device ID.")
|
2016-07-20 17:34:00 +02:00
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
@trace
|
2016-07-22 15:52:53 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def delete_device(self, user_id, device_id):
|
|
|
|
""" Delete the given device
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
|
|
|
device_id (str):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
defer.Deferred:
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield self.store.delete_device(user_id, device_id)
|
2018-03-15 16:11:17 +01:00
|
|
|
except errors.StoreError as e:
|
2016-07-22 15:52:53 +02:00
|
|
|
if e.code == 404:
|
|
|
|
# no match
|
2019-09-03 11:21:30 +02:00
|
|
|
set_tag("error", True)
|
|
|
|
log_kv(
|
|
|
|
{"reason": "User doesn't have device id.", "device_id": device_id}
|
|
|
|
)
|
2016-07-22 15:52:53 +02:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2017-11-01 11:29:34 +01:00
|
|
|
yield self._auth_handler.delete_access_tokens_for_user(
|
2019-06-20 11:32:02 +02:00
|
|
|
user_id, device_id=device_id
|
2016-07-26 12:09:47 +02:00
|
|
|
)
|
2016-07-22 15:52:53 +02:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
yield self.store.delete_e2e_keys_by_device(user_id=user_id, device_id=device_id)
|
2016-07-27 13:18:03 +02:00
|
|
|
|
2017-01-26 17:06:54 +01:00
|
|
|
yield self.notify_device_update(user_id, [device_id])
|
2017-01-25 15:27:27 +01:00
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
@trace
|
2017-11-29 16:44:59 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def delete_all_devices_for_user(self, user_id, except_device_id=None):
|
|
|
|
"""Delete all of the user's devices
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
|
|
|
except_device_id (str|None): optional device id which should not
|
|
|
|
be deleted
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
defer.Deferred:
|
|
|
|
"""
|
|
|
|
device_map = yield self.store.get_devices_by_user(user_id)
|
2018-05-31 11:03:47 +02:00
|
|
|
device_ids = list(device_map)
|
2017-11-29 16:44:59 +01:00
|
|
|
if except_device_id is not None:
|
|
|
|
device_ids = [d for d in device_ids if d != except_device_id]
|
|
|
|
yield self.delete_devices(user_id, device_ids)
|
|
|
|
|
2017-03-13 18:53:23 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def delete_devices(self, user_id, device_ids):
|
|
|
|
""" Delete several devices
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
2017-11-29 16:44:59 +01:00
|
|
|
device_ids (List[str]): The list of device IDs to delete
|
2017-03-13 18:53:23 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
defer.Deferred:
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield self.store.delete_devices(user_id, device_ids)
|
2018-03-15 16:11:17 +01:00
|
|
|
except errors.StoreError as e:
|
2017-03-13 18:53:23 +01:00
|
|
|
if e.code == 404:
|
|
|
|
# no match
|
2019-09-03 11:21:30 +02:00
|
|
|
set_tag("error", True)
|
|
|
|
set_tag("reason", "User doesn't have that device id.")
|
2017-03-13 18:53:23 +01:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Delete access tokens and e2e keys for each device. Not optimised as it is not
|
|
|
|
# considered as part of a critical path.
|
|
|
|
for device_id in device_ids:
|
2017-11-01 11:29:34 +01:00
|
|
|
yield self._auth_handler.delete_access_tokens_for_user(
|
2019-06-20 11:32:02 +02:00
|
|
|
user_id, device_id=device_id
|
2017-03-13 18:53:23 +01:00
|
|
|
)
|
|
|
|
yield self.store.delete_e2e_keys_by_device(
|
|
|
|
user_id=user_id, device_id=device_id
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.notify_device_update(user_id, device_ids)
|
|
|
|
|
2016-07-25 18:51:24 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def update_device(self, user_id, device_id, content):
|
|
|
|
""" Update the given device
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
|
|
|
device_id (str):
|
|
|
|
content (dict): body of update request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
defer.Deferred:
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield self.store.update_device(
|
2019-06-20 11:32:02 +02:00
|
|
|
user_id, device_id, new_display_name=content.get("display_name")
|
2016-07-25 18:51:24 +02:00
|
|
|
)
|
2017-01-26 17:06:54 +01:00
|
|
|
yield self.notify_device_update(user_id, [device_id])
|
2018-03-15 16:11:17 +01:00
|
|
|
except errors.StoreError as e:
|
2016-07-25 18:51:24 +02:00
|
|
|
if e.code == 404:
|
|
|
|
raise errors.NotFoundError()
|
|
|
|
else:
|
|
|
|
raise
|
2016-07-22 15:52:53 +02:00
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
@trace
|
2017-02-02 19:36:17 +01:00
|
|
|
@measure_func("notify_device_update")
|
2017-01-25 15:27:27 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-01-26 17:06:54 +01:00
|
|
|
def notify_device_update(self, user_id, device_ids):
|
2017-01-26 17:30:37 +01:00
|
|
|
"""Notify that a user's device(s) has changed. Pokes the notifier, and
|
|
|
|
remote servers if the user is local.
|
|
|
|
"""
|
2017-02-02 16:25:00 +01:00
|
|
|
users_who_share_room = yield self.store.get_users_who_share_room_with_user(
|
|
|
|
user_id
|
|
|
|
)
|
2017-01-25 15:27:27 +01:00
|
|
|
|
|
|
|
hosts = set()
|
2017-01-26 17:06:54 +01:00
|
|
|
if self.hs.is_mine_id(user_id):
|
2017-02-02 16:25:00 +01:00
|
|
|
hosts.update(get_domain_from_id(u) for u in users_who_share_room)
|
2017-01-26 17:06:54 +01:00
|
|
|
hosts.discard(self.server_name)
|
2017-01-25 15:27:27 +01:00
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
set_tag("target_hosts", hosts)
|
|
|
|
|
2017-01-25 15:27:27 +01:00
|
|
|
position = yield self.store.add_device_change_to_streams(
|
2017-01-26 17:06:54 +01:00
|
|
|
user_id, device_ids, list(hosts)
|
2017-01-25 15:27:27 +01:00
|
|
|
)
|
|
|
|
|
2019-03-07 16:53:14 +01:00
|
|
|
for device_id in device_ids:
|
|
|
|
logger.debug(
|
2019-06-20 11:32:02 +02:00
|
|
|
"Notifying about update %r/%r, ID: %r", user_id, device_id, position
|
2019-03-07 16:53:14 +01:00
|
|
|
)
|
|
|
|
|
2017-03-16 12:51:46 +01:00
|
|
|
room_ids = yield self.store.get_rooms_for_user(user_id)
|
2017-02-02 16:25:00 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
yield self.notifier.on_new_event("device_list_key", position, rooms=room_ids)
|
2017-01-25 15:27:27 +01:00
|
|
|
|
2017-01-26 17:30:37 +01:00
|
|
|
if hosts:
|
2019-06-20 11:32:02 +02:00
|
|
|
logger.info(
|
|
|
|
"Sending device list update notif for %r to: %r", user_id, hosts
|
|
|
|
)
|
2017-01-26 17:30:37 +01:00
|
|
|
for host in hosts:
|
|
|
|
self.federation_sender.send_device_messages(host)
|
2019-09-03 11:21:30 +02:00
|
|
|
log_kv({"message": "sent device update to host", "host": host})
|
2017-01-25 15:27:27 +01:00
|
|
|
|
2019-07-25 17:08:24 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def notify_user_signature_update(self, from_user_id, user_ids):
|
|
|
|
"""Notify a user that they have made new signatures of other users.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
from_user_id (str): the user who made the signature
|
|
|
|
user_ids (list[str]): the users IDs that have new signatures
|
|
|
|
"""
|
|
|
|
|
|
|
|
position = yield self.store.add_user_signature_change_to_streams(
|
|
|
|
from_user_id, user_ids
|
|
|
|
)
|
|
|
|
|
|
|
|
self.notifier.on_new_event("device_list_key", position, users=[from_user_id])
|
|
|
|
|
2017-01-26 17:06:54 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-03-01 15:12:11 +01:00
|
|
|
def on_federation_query_user_devices(self, user_id):
|
|
|
|
stream_id, devices = yield self.store.get_devices_with_keys_by_user(user_id)
|
2019-07-23 15:00:55 +02:00
|
|
|
return {"user_id": user_id, "stream_id": stream_id, "devices": devices}
|
2017-03-01 15:12:11 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def user_left_room(self, user, room_id):
|
|
|
|
user_id = user.to_string()
|
2017-03-16 12:51:46 +01:00
|
|
|
room_ids = yield self.store.get_rooms_for_user(user_id)
|
|
|
|
if not room_ids:
|
2017-03-01 15:12:11 +01:00
|
|
|
# We no longer share rooms with this user, so we'll no longer
|
|
|
|
# receive device updates. Mark this in DB.
|
|
|
|
yield self.store.mark_remote_user_device_list_as_unsubscribed(user_id)
|
|
|
|
|
|
|
|
|
|
|
|
def _update_device_from_client_ips(device, client_ips):
|
|
|
|
ip = client_ips.get((device["user_id"], device["device_id"]), {})
|
2019-06-20 11:32:02 +02:00
|
|
|
device.update({"last_seen_ts": ip.get("last_seen"), "last_seen_ip": ip.get("ip")})
|
2017-03-01 15:12:11 +01:00
|
|
|
|
|
|
|
|
2019-07-29 17:34:44 +02:00
|
|
|
class DeviceListUpdater(object):
|
2017-03-01 15:12:11 +01:00
|
|
|
"Handles incoming device list updates from federation and updates the DB"
|
|
|
|
|
|
|
|
def __init__(self, hs, device_handler):
|
|
|
|
self.store = hs.get_datastore()
|
2018-03-13 14:26:52 +01:00
|
|
|
self.federation = hs.get_federation_client()
|
2017-03-01 15:12:11 +01:00
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.device_handler = device_handler
|
|
|
|
|
2017-03-03 16:31:57 +01:00
|
|
|
self._remote_edu_linearizer = Linearizer(name="remote_device_list")
|
2017-03-01 15:12:11 +01:00
|
|
|
|
|
|
|
# user_id -> list of updates waiting to be handled.
|
|
|
|
self._pending_updates = {}
|
|
|
|
|
|
|
|
# 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.
|
2017-03-03 17:02:53 +01:00
|
|
|
self._seen_updates = ExpiringCache(
|
|
|
|
cache_name="device_update_edu",
|
|
|
|
clock=self.clock,
|
|
|
|
max_len=10000,
|
|
|
|
expiry_ms=30 * 60 * 1000,
|
|
|
|
iterable=True,
|
|
|
|
)
|
2017-03-01 15:12:11 +01:00
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
@trace
|
2017-03-01 15:12:11 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def incoming_device_list_update(self, origin, edu_content):
|
|
|
|
"""Called on incoming device list update from federation. Responsible
|
|
|
|
for parsing the EDU and adding to pending updates list.
|
|
|
|
"""
|
|
|
|
|
2019-09-03 11:21:30 +02:00
|
|
|
set_tag("origin", origin)
|
|
|
|
set_tag("edu_content", edu_content)
|
2017-03-01 15:12:11 +01: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", [])
|
2019-06-20 11:32:02 +02:00
|
|
|
prev_ids = [str(p) for p in prev_ids] # They may come as ints
|
2017-01-26 17:06:54 +01:00
|
|
|
|
|
|
|
if get_domain_from_id(user_id) != origin:
|
|
|
|
# TODO: Raise?
|
2019-03-07 16:53:14 +01:00
|
|
|
logger.warning(
|
|
|
|
"Got device list update edu for %r/%r from %r",
|
2019-06-20 11:32:02 +02:00
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
origin,
|
2019-03-07 16:53:14 +01:00
|
|
|
)
|
2019-09-03 11:21:30 +02: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 17:06:54 +01:00
|
|
|
return
|
|
|
|
|
2017-03-16 12:51:46 +01:00
|
|
|
room_ids = yield self.store.get_rooms_for_user(user_id)
|
|
|
|
if not room_ids:
|
2017-01-26 17:39:33 +01:00
|
|
|
# We don't share any rooms with this user. Ignore update, as we
|
|
|
|
# probably won't get any further updates.
|
2019-09-03 11:21:30 +02: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 16:53:14 +01:00
|
|
|
logger.warning(
|
|
|
|
"Got device list update edu for %r/%r, but don't share a room",
|
2019-06-20 11:32:02 +02:00
|
|
|
user_id,
|
|
|
|
device_id,
|
2019-03-07 16:53:14 +01:00
|
|
|
)
|
2017-01-26 17:39:33 +01:00
|
|
|
return
|
2017-01-26 17:06:54 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
logger.debug("Received device list update for %r/%r", user_id, device_id)
|
2019-03-07 16:53:14 +01:00
|
|
|
|
2017-03-01 15:12:11 +01:00
|
|
|
self._pending_updates.setdefault(user_id, []).append(
|
|
|
|
(device_id, stream_id, prev_ids, edu_content)
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self._handle_device_updates(user_id)
|
|
|
|
|
|
|
|
@measure_func("_incoming_device_list_update")
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _handle_device_updates(self, user_id):
|
|
|
|
"Actually handle pending updates."
|
|
|
|
|
2017-03-03 16:31:57 +01:00
|
|
|
with (yield self._remote_edu_linearizer.queue(user_id)):
|
2017-03-01 15:12:11 +01:00
|
|
|
pending_updates = self._pending_updates.pop(user_id, [])
|
|
|
|
if not pending_updates:
|
|
|
|
# This can happen since we batch updates
|
|
|
|
return
|
|
|
|
|
2019-03-07 16:53:14 +01:00
|
|
|
for device_id, stream_id, prev_ids, content in pending_updates:
|
|
|
|
logger.debug(
|
|
|
|
"Handling update %r/%r, ID: %r, prev: %r ",
|
2019-06-20 11:32:02 +02:00
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
stream_id,
|
|
|
|
prev_ids,
|
2019-03-07 16:53:14 +01:00
|
|
|
)
|
|
|
|
|
2017-05-05 11:59:32 +02:00
|
|
|
# Given a list of updates we check if we need to resync. This
|
|
|
|
# happens if we've missed updates.
|
2017-03-01 15:12:11 +01:00
|
|
|
resync = yield self._need_to_do_resync(user_id, pending_updates)
|
2017-01-26 17:06:54 +01:00
|
|
|
|
2019-03-07 16:53:14 +01:00
|
|
|
logger.debug("Need to re-sync devices for %r? %r", user_id, resync)
|
|
|
|
|
2017-01-26 17:06:54 +01:00
|
|
|
if resync:
|
2019-07-29 17:34:44 +02:00
|
|
|
yield self.user_device_resync(user_id)
|
2017-01-26 17:06:54 +01:00
|
|
|
else:
|
2017-01-26 17:30:37 +01:00
|
|
|
# Simply update the single device, since we know that is the only
|
2018-07-10 18:58:09 +02:00
|
|
|
# change (because of the single prev_id matching the current cache)
|
2017-03-01 15:12:11 +01:00
|
|
|
for device_id, stream_id, prev_ids, content in pending_updates:
|
|
|
|
yield self.store.update_remote_device_list_cache_entry(
|
2019-06-20 11:32:02 +02:00
|
|
|
user_id, device_id, content, stream_id
|
2017-03-01 15:12:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
yield self.device_handler.notify_device_update(
|
|
|
|
user_id, [device_id for device_id, _, _, _ in pending_updates]
|
2017-01-26 17:06:54 +01:00
|
|
|
)
|
|
|
|
|
2019-03-07 17:04:24 +01:00
|
|
|
self._seen_updates.setdefault(user_id, set()).update(
|
|
|
|
stream_id for _, stream_id, _, _ in pending_updates
|
|
|
|
)
|
2017-01-26 17:06:54 +01:00
|
|
|
|
2017-01-26 17:39:33 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-03-01 15:12:11 +01:00
|
|
|
def _need_to_do_resync(self, user_id, updates):
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
seen_updates = self._seen_updates.get(user_id, set())
|
2017-01-26 17:39:33 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
extremity = yield self.store.get_device_list_last_stream_id_for_remote(user_id)
|
2016-07-20 18:58:44 +02:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
logger.debug("Current extremity for %r: %r", user_id, extremity)
|
2019-03-07 16:53:14 +01:00
|
|
|
|
2017-03-01 15:12:11 +01: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 15:00:55 +02:00
|
|
|
return True
|
2017-03-01 15:12:11 +01: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 15:00:55 +02:00
|
|
|
return True
|
2017-03-01 15:12:11 +01:00
|
|
|
|
|
|
|
stream_id_in_updates.add(stream_id)
|
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return False
|
2019-07-29 17:34:44 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def user_device_resync(self, user_id):
|
|
|
|
"""Fetches all devices for a user and updates the device cache with them.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): The user's id whose device_list will be updated.
|
|
|
|
Returns:
|
|
|
|
Deferred[dict]: 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
|
|
|
|
"""
|
2019-09-03 11:21:30 +02:00
|
|
|
log_kv({"message": "Doing resync to update device list."})
|
2019-07-29 17:34:44 +02:00
|
|
|
# Fetch all devices for the user.
|
|
|
|
origin = get_domain_from_id(user_id)
|
|
|
|
try:
|
|
|
|
result = yield self.federation.query_user_devices(origin, user_id)
|
|
|
|
except (NotRetryingDestination, RequestSendFailed, HttpResponseException):
|
|
|
|
# TODO: Remember that we are now out of sync and try again
|
|
|
|
# later
|
|
|
|
logger.warn("Failed to handle device list update for %s", user_id)
|
|
|
|
# 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.
|
|
|
|
return
|
|
|
|
except FederationDeniedError as e:
|
2019-09-03 11:21:30 +02:00
|
|
|
set_tag("error", True)
|
|
|
|
log_kv({"reason": "FederationDeniedError"})
|
2019-07-29 17:34:44 +02:00
|
|
|
logger.info(e)
|
|
|
|
return
|
2019-09-03 11:21:30 +02:00
|
|
|
except Exception as e:
|
2019-07-29 17:34:44 +02:00
|
|
|
# TODO: Remember that we are now out of sync and try again
|
|
|
|
# later
|
2019-09-03 11:21:30 +02:00
|
|
|
set_tag("error", True)
|
|
|
|
log_kv(
|
|
|
|
{"message": "Exception raised by federation request", "exception": e}
|
|
|
|
)
|
2019-07-29 17:34:44 +02:00
|
|
|
logger.exception("Failed to handle device list update for %s", user_id)
|
|
|
|
return
|
2019-09-03 11:21:30 +02:00
|
|
|
log_kv({"result": result})
|
2019-07-29 17:34:44 +02:00
|
|
|
stream_id = result["stream_id"]
|
|
|
|
devices = result["devices"]
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
logger.warn(
|
|
|
|
"Ignoring device list snapshot for %s as it has >1K devs (%d)",
|
|
|
|
user_id,
|
|
|
|
len(devices),
|
|
|
|
)
|
|
|
|
devices = []
|
|
|
|
|
|
|
|
for device in devices:
|
|
|
|
logger.debug(
|
|
|
|
"Handling resync update %r/%r, ID: %r",
|
|
|
|
user_id,
|
|
|
|
device["device_id"],
|
|
|
|
stream_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.store.update_remote_device_list_cache(user_id, devices, stream_id)
|
|
|
|
device_ids = [device["device_id"] for device in devices]
|
|
|
|
yield self.device_handler.notify_device_update(user_id, device_ids)
|
|
|
|
|
|
|
|
# We clobber the seen updates since we've re-synced from a given
|
|
|
|
# point.
|
|
|
|
self._seen_updates[user_id] = set([stream_id])
|
|
|
|
|
|
|
|
defer.returnValue(result)
|