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 #6482 from matrix-org/erikj/port_rest_v1
Browse files Browse the repository at this point in the history
* commit 'af5d0ebc7':
  Newsfile
  Fixup tests
  Port rest/v1 to async/await
  • Loading branch information
anoadragon453 committed Mar 19, 2020
2 parents 4522fbf + af5d0eb commit 9120098
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 175 deletions.
1 change: 1 addition & 0 deletions changelog.d/6482.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port synapse.rest.client.v1 to async/await.
2 changes: 1 addition & 1 deletion synapse/app/synchrotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def send_stop_syncing(self):

def set_state(self, user, state, ignore_status_msg=False):
# TODO Hows this supposed to work?
pass
return defer.succeed(None)

get_states = __func__(PresenceHandler.get_states)
get_state = __func__(PresenceHandler.get_state)
Expand Down
53 changes: 22 additions & 31 deletions synapse/rest/client/v1/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import logging

from twisted.internet import defer

from synapse.api.errors import (
AuthError,
Codes,
Expand Down Expand Up @@ -47,17 +45,15 @@ def __init__(self, hs):
self.handlers = hs.get_handlers()
self.auth = hs.get_auth()

@defer.inlineCallbacks
def on_GET(self, request, room_alias):
async def on_GET(self, request, room_alias):
room_alias = RoomAlias.from_string(room_alias)

dir_handler = self.handlers.directory_handler
res = yield dir_handler.get_association(room_alias)
res = await dir_handler.get_association(room_alias)

return 200, res

@defer.inlineCallbacks
def on_PUT(self, request, room_alias):
async def on_PUT(self, request, room_alias):
room_alias = RoomAlias.from_string(room_alias)

content = parse_json_object_from_request(request)
Expand All @@ -77,26 +73,25 @@ def on_PUT(self, request, room_alias):

# TODO(erikj): Check types.

room = yield self.store.get_room(room_id)
room = await self.store.get_room(room_id)
if room is None:
raise SynapseError(400, "Room does not exist")

requester = yield self.auth.get_user_by_req(request)
requester = await self.auth.get_user_by_req(request)

yield self.handlers.directory_handler.create_association(
await self.handlers.directory_handler.create_association(
requester, room_alias, room_id, servers
)

return 200, {}

@defer.inlineCallbacks
def on_DELETE(self, request, room_alias):
async def on_DELETE(self, request, room_alias):
dir_handler = self.handlers.directory_handler

try:
service = yield self.auth.get_appservice_by_req(request)
service = await self.auth.get_appservice_by_req(request)
room_alias = RoomAlias.from_string(room_alias)
yield dir_handler.delete_appservice_association(service, room_alias)
await dir_handler.delete_appservice_association(service, room_alias)
logger.info(
"Application service at %s deleted alias %s",
service.url,
Expand All @@ -107,12 +102,12 @@ def on_DELETE(self, request, room_alias):
# fallback to default user behaviour if they aren't an AS
pass

requester = yield self.auth.get_user_by_req(request)
requester = await self.auth.get_user_by_req(request)
user = requester.user

room_alias = RoomAlias.from_string(room_alias)

yield dir_handler.delete_association(requester, room_alias)
await dir_handler.delete_association(requester, room_alias)

logger.info(
"User %s deleted alias %s", user.to_string(), room_alias.to_string()
Expand All @@ -130,32 +125,29 @@ def __init__(self, hs):
self.handlers = hs.get_handlers()
self.auth = hs.get_auth()

@defer.inlineCallbacks
def on_GET(self, request, room_id):
room = yield self.store.get_room(room_id)
async def on_GET(self, request, room_id):
room = await self.store.get_room(room_id)
if room is None:
raise NotFoundError("Unknown room")

return 200, {"visibility": "public" if room["is_public"] else "private"}

@defer.inlineCallbacks
def on_PUT(self, request, room_id):
requester = yield self.auth.get_user_by_req(request)
async def on_PUT(self, request, room_id):
requester = await self.auth.get_user_by_req(request)

content = parse_json_object_from_request(request)
visibility = content.get("visibility", "public")

yield self.handlers.directory_handler.edit_published_room_list(
await self.handlers.directory_handler.edit_published_room_list(
requester, room_id, visibility
)

return 200, {}

@defer.inlineCallbacks
def on_DELETE(self, request, room_id):
requester = yield self.auth.get_user_by_req(request)
async def on_DELETE(self, request, room_id):
requester = await self.auth.get_user_by_req(request)

yield self.handlers.directory_handler.edit_published_room_list(
await self.handlers.directory_handler.edit_published_room_list(
requester, room_id, "private"
)

Expand All @@ -181,15 +173,14 @@ def on_PUT(self, request, network_id, room_id):
def on_DELETE(self, request, network_id, room_id):
return self._edit(request, network_id, room_id, "private")

@defer.inlineCallbacks
def _edit(self, request, network_id, room_id, visibility):
requester = yield self.auth.get_user_by_req(request)
async def _edit(self, request, network_id, room_id, visibility):
requester = await self.auth.get_user_by_req(request)
if not requester.app_service:
raise AuthError(
403, "Only appservices can edit the appservice published room list"
)

yield self.handlers.directory_handler.edit_published_appservice_room_list(
await self.handlers.directory_handler.edit_published_appservice_room_list(
requester.app_service.id, network_id, room_id, visibility
)

Expand Down
18 changes: 7 additions & 11 deletions synapse/rest/client/v1/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"""This module contains REST servlets to do with event streaming, /events."""
import logging

from twisted.internet import defer

from synapse.api.errors import SynapseError
from synapse.http.servlet import RestServlet
from synapse.rest.client.v2_alpha._base import client_patterns
Expand All @@ -36,9 +34,8 @@ def __init__(self, hs):
self.event_stream_handler = hs.get_event_stream_handler()
self.auth = hs.get_auth()

@defer.inlineCallbacks
def on_GET(self, request):
requester = yield self.auth.get_user_by_req(request, allow_guest=True)
async def on_GET(self, request):
requester = await self.auth.get_user_by_req(request, allow_guest=True)
is_guest = requester.is_guest
room_id = None
if is_guest:
Expand All @@ -57,7 +54,7 @@ def on_GET(self, request):

as_client_event = b"raw" not in request.args

chunk = yield self.event_stream_handler.get_stream(
chunk = await self.event_stream_handler.get_stream(
requester.user.to_string(),
pagin_config,
timeout=timeout,
Expand All @@ -83,14 +80,13 @@ def __init__(self, hs):
self.event_handler = hs.get_event_handler()
self._event_serializer = hs.get_event_client_serializer()

@defer.inlineCallbacks
def on_GET(self, request, event_id):
requester = yield self.auth.get_user_by_req(request)
event = yield self.event_handler.get_event(requester.user, None, event_id)
async def on_GET(self, request, event_id):
requester = await self.auth.get_user_by_req(request)
event = await self.event_handler.get_event(requester.user, None, event_id)

time_now = self.clock.time_msec()
if event:
event = yield self._event_serializer.serialize_event(event, time_now)
event = await self._event_serializer.serialize_event(event, time_now)
return 200, event
else:
return 404, "Event not found."
Expand Down
8 changes: 3 additions & 5 deletions synapse/rest/client/v1/initial_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet import defer

from synapse.http.servlet import RestServlet, parse_boolean
from synapse.rest.client.v2_alpha._base import client_patterns
Expand All @@ -29,13 +28,12 @@ def __init__(self, hs):
self.initial_sync_handler = hs.get_initial_sync_handler()
self.auth = hs.get_auth()

@defer.inlineCallbacks
def on_GET(self, request):
requester = yield self.auth.get_user_by_req(request)
async def on_GET(self, request):
requester = await self.auth.get_user_by_req(request)
as_client_event = b"raw" not in request.args
pagination_config = PaginationConfig.from_request(request)
include_archived = parse_boolean(request, "archived", default=False)
content = yield self.initial_sync_handler.snapshot_all_rooms(
content = await self.initial_sync_handler.snapshot_all_rooms(
user_id=requester.user.to_string(),
pagin_config=pagination_config,
as_client_event=as_client_event,
Expand Down
Loading

0 comments on commit 9120098

Please sign in to comment.