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

Commit

Permalink
Merge pull request #6484 from matrix-org/erikj/port_sync_handler
Browse files Browse the repository at this point in the history
* commit 'a9b393340':
  Fixup functions to consistently return deferreds
  Newsfile
  Port SyncHandler to async/await
  • Loading branch information
anoadragon453 committed Mar 19, 2020
2 parents 60baffd + a9b3933 commit 2beae00
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 202 deletions.
1 change: 1 addition & 0 deletions changelog.d/6484.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port SyncHandler to async/await.
30 changes: 13 additions & 17 deletions synapse/handlers/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import logging
import random

from twisted.internet import defer

from synapse.api.constants import EventTypes, Membership
from synapse.api.errors import AuthError, SynapseError
from synapse.events import EventBase
Expand Down Expand Up @@ -50,9 +48,8 @@ def __init__(self, hs):
self._server_notices_sender = hs.get_server_notices_sender()
self._event_serializer = hs.get_event_client_serializer()

@defer.inlineCallbacks
@log_function
def get_stream(
async def get_stream(
self,
auth_user_id,
pagin_config,
Expand All @@ -69,17 +66,17 @@ def get_stream(
"""

if room_id:
blocked = yield self.store.is_room_blocked(room_id)
blocked = await self.store.is_room_blocked(room_id)
if blocked:
raise SynapseError(403, "This room has been blocked on this server")

# send any outstanding server notices to the user.
yield self._server_notices_sender.on_user_syncing(auth_user_id)
await self._server_notices_sender.on_user_syncing(auth_user_id)

auth_user = UserID.from_string(auth_user_id)
presence_handler = self.hs.get_presence_handler()

context = yield presence_handler.user_syncing(
context = await presence_handler.user_syncing(
auth_user_id, affect_presence=affect_presence
)
with context:
Expand All @@ -91,7 +88,7 @@ def get_stream(
# thundering herds on restart.
timeout = random.randint(int(timeout * 0.9), int(timeout * 1.1))

events, tokens = yield self.notifier.get_events_for(
events, tokens = await self.notifier.get_events_for(
auth_user,
pagin_config,
timeout,
Expand All @@ -112,14 +109,14 @@ def get_stream(
# Send down presence.
if event.state_key == auth_user_id:
# Send down presence for everyone in the room.
users = yield self.state.get_current_users_in_room(
users = await self.state.get_current_users_in_room(
event.room_id
)
states = yield presence_handler.get_states(users, as_event=True)
states = await presence_handler.get_states(users, as_event=True)
to_add.extend(states)
else:

ev = yield presence_handler.get_state(
ev = await presence_handler.get_state(
UserID.from_string(event.state_key), as_event=True
)
to_add.append(ev)
Expand All @@ -128,7 +125,7 @@ def get_stream(

time_now = self.clock.time_msec()

chunks = yield self._event_serializer.serialize_events(
chunks = await self._event_serializer.serialize_events(
events,
time_now,
as_client_event=as_client_event,
Expand All @@ -151,8 +148,7 @@ def __init__(self, hs):
super(EventHandler, self).__init__(hs)
self.storage = hs.get_storage()

@defer.inlineCallbacks
def get_event(self, user, room_id, event_id):
async def get_event(self, user, room_id, event_id):
"""Retrieve a single specified event.
Args:
Expand All @@ -167,15 +163,15 @@ def get_event(self, user, room_id, event_id):
AuthError if the user does not have the rights to inspect this
event.
"""
event = yield self.store.get_event(event_id, check_room_id=room_id)
event = await self.store.get_event(event_id, check_room_id=room_id)

if not event:
return None

users = yield self.store.get_users_in_room(event.room_id)
users = await self.store.get_users_in_room(event.room_id)
is_peeking = user.to_string() not in users

filtered = yield filter_events_for_client(
filtered = await filter_events_for_client(
self.storage, user.to_string(), [event], is_peeking=is_peeking
)

Expand Down
Loading

0 comments on commit 2beae00

Please sign in to comment.