Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit ac4f3fb

Browse files
committed
create device and store keys in same call
1 parent 96529c4 commit ac4f3fb

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

synapse/handlers/device.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
run_as_background_process,
4242
wrap_as_background_process,
4343
)
44+
from synapse.replication.http.devices import ReplicationUploadKeysForUserRestServlet
4445
from synapse.types import (
4546
JsonDict,
4647
StrCollection,
@@ -656,15 +657,17 @@ async def store_dehydrated_device(
656657
device_id: Optional[str],
657658
device_data: JsonDict,
658659
initial_device_display_name: Optional[str] = None,
660+
device_keys: Optional[JsonDict] = None,
659661
) -> str:
660-
"""Store a dehydrated device for a user. If the user had a previous
661-
dehydrated device, it is removed.
662+
"""Store a dehydrated device for a user, optionally storing the keys associated with
663+
it as well. If the user had a previous dehydrated device, it is removed.
662664
663665
Args:
664666
user_id: the user that we are storing the device for
665667
device_id: device id supplied by client
666668
device_data: the dehydrated device information
667669
initial_device_display_name: The display name to use for the device
670+
device_keys: keys for the dehydrated device
668671
Returns:
669672
device id of the dehydrated device
670673
"""
@@ -678,6 +681,18 @@ async def store_dehydrated_device(
678681
)
679682
if old_device_id is not None:
680683
await self.delete_devices(user_id, [old_device_id])
684+
685+
# we do this here to avoid a circular import
686+
if self.hs.config.worker.worker_app is None:
687+
# if main process
688+
key_uploader = self.hs.get_e2e_keys_handler().upload_keys_for_user
689+
else:
690+
# if worker process
691+
key_uploader = ReplicationUploadKeysForUserRestServlet.make_client(self.hs)
692+
693+
# if keys are provided store them
694+
if device_keys:
695+
await key_uploader(user_id=user_id, device_id=device_id, keys=device_keys)
681696
return device_id
682697

683698
async def rehydrate_device(

synapse/rest/client/devices.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,6 @@ class Config:
536536
async def on_PUT(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
537537
submission = parse_and_validate_json_object_from_request(request, self.PutBody)
538538
requester = await self.auth.get_user_by_req(request)
539-
user_id = requester.user.to_string()
540539

541540
device_info = submission.dict()
542541
if "device_keys" not in device_info.keys():
@@ -545,18 +544,12 @@ async def on_PUT(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
545544
"Device key(s) not found, these must be provided.",
546545
)
547546

548-
# TODO: Those two operations, creating a device and storing the
549-
# device's keys should be atomic.
550547
device_id = await self.device_handler.store_dehydrated_device(
551548
requester.user.to_string(),
552549
submission.device_id,
553550
submission.device_data.dict(),
554551
submission.initial_device_display_name,
555-
)
556-
557-
# TODO: Do we need to do something with the result here?
558-
await self.key_uploader(
559-
user_id=user_id, device_id=submission.device_id, keys=submission.dict()
552+
device_info,
560553
)
561554

562555
return 200, {"device_id": device_id}

0 commit comments

Comments
 (0)