Skip to content

Commit

Permalink
Convert federation handler to async/await. (matrix-org#7459)
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored May 11, 2020
1 parent be309d9 commit 8c8858e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
1 change: 1 addition & 0 deletions changelog.d/7459.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert the federation handler to async/await.
32 changes: 14 additions & 18 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2681,19 +2681,18 @@ async def on_exchange_third_party_invite_request(
member_handler = self.hs.get_room_member_handler()
await member_handler.send_membership_event(None, event, context)

@defer.inlineCallbacks
def add_display_name_to_third_party_invite(
async def add_display_name_to_third_party_invite(
self, room_version, event_dict, event, context
):
key = (
EventTypes.ThirdPartyInvite,
event.content["third_party_invite"]["signed"]["token"],
)
original_invite = None
prev_state_ids = yield context.get_prev_state_ids()
prev_state_ids = await context.get_prev_state_ids()
original_invite_id = prev_state_ids.get(key)
if original_invite_id:
original_invite = yield self.store.get_event(
original_invite = await self.store.get_event(
original_invite_id, allow_none=True
)
if original_invite:
Expand All @@ -2714,14 +2713,13 @@ def add_display_name_to_third_party_invite(

builder = self.event_builder_factory.new(room_version, event_dict)
EventValidator().validate_builder(builder)
event, context = yield self.event_creation_handler.create_new_client_event(
event, context = await self.event_creation_handler.create_new_client_event(
builder=builder
)
EventValidator().validate_new(event, self.config)
return (event, context)

@defer.inlineCallbacks
def _check_signature(self, event, context):
async def _check_signature(self, event, context):
"""
Checks that the signature in the event is consistent with its invite.
Expand All @@ -2738,12 +2736,12 @@ def _check_signature(self, event, context):
signed = event.content["third_party_invite"]["signed"]
token = signed["token"]

prev_state_ids = yield context.get_prev_state_ids()
prev_state_ids = await context.get_prev_state_ids()
invite_event_id = prev_state_ids.get((EventTypes.ThirdPartyInvite, token))

invite_event = None
if invite_event_id:
invite_event = yield self.store.get_event(invite_event_id, allow_none=True)
invite_event = await self.store.get_event(invite_event_id, allow_none=True)

if not invite_event:
raise AuthError(403, "Could not find invite")
Expand Down Expand Up @@ -2792,7 +2790,7 @@ def _check_signature(self, event, context):
raise
try:
if "key_validity_url" in public_key_object:
yield self._check_key_revocation(
await self._check_key_revocation(
public_key, public_key_object["key_validity_url"]
)
except Exception:
Expand All @@ -2806,8 +2804,7 @@ def _check_signature(self, event, context):
last_exception = e
raise last_exception

@defer.inlineCallbacks
def _check_key_revocation(self, public_key, url):
async def _check_key_revocation(self, public_key, url):
"""
Checks whether public_key has been revoked.
Expand All @@ -2821,7 +2818,7 @@ def _check_key_revocation(self, public_key, url):
for revocation.
"""
try:
response = yield self.http_client.get_json(url, {"public_key": public_key})
response = await self.http_client.get_json(url, {"public_key": public_key})
except Exception:
raise SynapseError(502, "Third party certificate could not be checked")
if "valid" not in response or not response["valid"]:
Expand Down Expand Up @@ -2916,8 +2913,7 @@ async def user_joined_room(self, user: UserID, room_id: str) -> None:
else:
user_joined_room(self.distributor, user, room_id)

@defer.inlineCallbacks
def get_room_complexity(self, remote_room_hosts, room_id):
async def get_room_complexity(self, remote_room_hosts, room_id):
"""
Fetch the complexity of a remote room over federation.
Expand All @@ -2931,12 +2927,12 @@ def get_room_complexity(self, remote_room_hosts, room_id):
"""

for host in remote_room_hosts:
res = yield self.federation_client.get_room_complexity(host, room_id)
res = await self.federation_client.get_room_complexity(host, room_id)

# We got a result, return it.
if res:
defer.returnValue(res)
return res

# We fell off the bottom, couldn't get the complexity from anyone. Oh
# well.
defer.returnValue(None)
return None
5 changes: 2 additions & 3 deletions synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,7 @@ def __init__(self, hs):
self.distributor.declare("user_joined_room")
self.distributor.declare("user_left_room")

@defer.inlineCallbacks
def _is_remote_room_too_complex(self, room_id, remote_room_hosts):
async def _is_remote_room_too_complex(self, room_id, remote_room_hosts):
"""
Check if complexity of a remote room is too great.
Expand All @@ -888,7 +887,7 @@ def _is_remote_room_too_complex(self, room_id, remote_room_hosts):
if unable to be fetched
"""
max_complexity = self.hs.config.limit_remote_rooms.complexity
complexity = yield self.federation_handler.get_room_complexity(
complexity = await self.federation_handler.get_room_complexity(
remote_room_hosts, room_id
)

Expand Down

0 comments on commit 8c8858e

Please sign in to comment.