use a device replication thingy
parent
84528e4fb2
commit
d95252c01f
|
@ -0,0 +1,64 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright 2019 New Vector Ltd
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from twisted.internet import defer
|
||||||
|
|
||||||
|
from synapse.http.servlet import parse_json_object_from_request
|
||||||
|
from synapse.replication.http._base import ReplicationEndpoint
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class CheckDeviceRegisteredServlet(ReplicationEndpoint):
|
||||||
|
"""
|
||||||
|
Check a device is registered.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
NAME = "device_check_registered"
|
||||||
|
PATH_ARGS = ("user_id")
|
||||||
|
|
||||||
|
def __init__(self, hs):
|
||||||
|
super(CheckDeviceRegisteredServlet, self).__init__(hs)
|
||||||
|
self.device_handler = hs.get_device_handler()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _serialize_payload(user_id, device_id, initial_display_name):
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
"device_id": device_id,
|
||||||
|
"initial_display_name": initial_display_name,
|
||||||
|
}
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def _handle_request(self, request, user_id):
|
||||||
|
content = parse_json_object_from_request(request)
|
||||||
|
|
||||||
|
device_id = content["device_id"]
|
||||||
|
initial_display_name = content["initial_display_name"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
device_id = yield self.device_handler.check_device_registered(user_id, device_id)
|
||||||
|
except Exception as e:
|
||||||
|
defer.returnValue((400, str(e))
|
||||||
|
|
||||||
|
defer.returnValue((200, {"device_id": device_id}))
|
||||||
|
|
||||||
|
|
||||||
|
def register_servlets(hs, http_server):
|
||||||
|
CheckDeviceRegisteredServlet(hs).register(http_server)
|
|
@ -1,5 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Copyright 2018 New Vector Ltd
|
# Copyright 2019 New Vector Ltd
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
# you may not use this file except in compliance with the License.
|
# you may not use this file except in compliance with the License.
|
||||||
|
|
|
@ -33,6 +33,7 @@ from synapse.http.servlet import (
|
||||||
parse_json_object_from_request,
|
parse_json_object_from_request,
|
||||||
parse_string,
|
parse_string,
|
||||||
)
|
)
|
||||||
|
from synapse.replication.http.device import CheckDeviceRegisteredServlet
|
||||||
from synapse.replication.http.registration import (
|
from synapse.replication.http.registration import (
|
||||||
RegistrationUserCacheInvalidationServlet,
|
RegistrationUserCacheInvalidationServlet,
|
||||||
)
|
)
|
||||||
|
@ -193,12 +194,19 @@ class RegisterRestServlet(RestServlet):
|
||||||
self.registration_handler = hs.get_handlers().registration_handler
|
self.registration_handler = hs.get_handlers().registration_handler
|
||||||
self.identity_handler = hs.get_handlers().identity_handler
|
self.identity_handler = hs.get_handlers().identity_handler
|
||||||
self.room_member_handler = hs.get_room_member_handler()
|
self.room_member_handler = hs.get_room_member_handler()
|
||||||
self.device_handler = hs.get_device_handler()
|
|
||||||
self.macaroon_gen = hs.get_macaroon_generator()
|
self.macaroon_gen = hs.get_macaroon_generator()
|
||||||
|
|
||||||
self._invalidate_caches_client = (
|
if self.hs.config.worker_app:
|
||||||
RegistrationUserCacheInvalidationServlet.make_client(hs)
|
|
||||||
)
|
self._invalidate_caches_client = (
|
||||||
|
RegistrationUserCacheInvalidationServlet.make_client(hs)
|
||||||
|
)
|
||||||
|
self._device_check_registered_client = (
|
||||||
|
CheckDeviceRegisteredServlet.make_client(hs)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.device_handler = hs.get_device_handler()
|
||||||
|
|
||||||
|
|
||||||
@interactive_auth_handler
|
@interactive_auth_handler
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
|
@ -664,6 +672,17 @@ class RegisterRestServlet(RestServlet):
|
||||||
})
|
})
|
||||||
defer.returnValue(result)
|
defer.returnValue(result)
|
||||||
|
|
||||||
|
def _check_device_registered(self, user_id, device_id, initial_display_name):
|
||||||
|
|
||||||
|
if self.hs.config.worker_app:
|
||||||
|
return self._device_check_registered_client(
|
||||||
|
user_id, device_id, initial_display_name
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return self.device_handler.check_device_registered(
|
||||||
|
user_id, device_id, initial_display_name
|
||||||
|
)
|
||||||
|
|
||||||
def _register_device(self, user_id, params):
|
def _register_device(self, user_id, params):
|
||||||
"""Register a device for a user.
|
"""Register a device for a user.
|
||||||
|
|
||||||
|
@ -680,7 +699,7 @@ class RegisterRestServlet(RestServlet):
|
||||||
# register the user's device
|
# register the user's device
|
||||||
device_id = params.get("device_id")
|
device_id = params.get("device_id")
|
||||||
initial_display_name = params.get("initial_device_display_name")
|
initial_display_name = params.get("initial_device_display_name")
|
||||||
return self.device_handler.check_device_registered(
|
return self._check_device_registered(
|
||||||
user_id, device_id, initial_display_name
|
user_id, device_id, initial_display_name
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -697,7 +716,7 @@ class RegisterRestServlet(RestServlet):
|
||||||
# we have nowhere to store it.
|
# we have nowhere to store it.
|
||||||
device_id = synapse.api.auth.GUEST_DEVICE_ID
|
device_id = synapse.api.auth.GUEST_DEVICE_ID
|
||||||
initial_display_name = params.get("initial_device_display_name")
|
initial_display_name = params.get("initial_device_display_name")
|
||||||
yield self.device_handler.check_device_registered(
|
yield self._check_device_registered(
|
||||||
user_id, device_id, initial_display_name
|
user_id, device_id, initial_display_name
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue