diff options
author | Kegan Dougal <kegan@matrix.org> | 2014-08-26 17:22:10 +0100 |
---|---|---|
committer | Kegan Dougal <kegan@matrix.org> | 2014-08-26 17:22:10 +0100 |
commit | 6f0bba1934d866ab21038761aacb27a12f66c4f4 (patch) | |
tree | 8fdd0868de29108aa3de5a9bb524b516c343e326 /synapse/rest/room.py | |
parent | Respect 'limit' param in initialSync api (diff) | |
parent | Removed MessageRestServlet, use RoomSendEventRestServlet instead. Updated cmd... (diff) | |
download | synapse-6f0bba1934d866ab21038761aacb27a12f66c4f4.tar.xz |
Merge branch 'client_server_url_rename' into develop
Diffstat (limited to 'synapse/rest/room.py')
-rw-r--r-- | synapse/rest/room.py | 97 |
1 files changed, 48 insertions, 49 deletions
diff --git a/synapse/rest/room.py b/synapse/rest/room.py index b031ad1bc4..e00f74cf19 100644 --- a/synapse/rest/room.py +++ b/synapse/rest/room.py @@ -168,72 +168,64 @@ class RoomStateEventRestServlet(RestServlet): defer.returnValue((200, "")) -class JoinRoomAliasServlet(RestServlet): - PATTERN = client_path_pattern("/join/(?P<room_alias>[^/]+)$") +class RoomSendEventRestServlet(RestServlet): + + def register(self, http_server): + # /rooms/$roomid/send/$event_type[/$txn_id] + PATTERN = ("/rooms/(?P<room_id>[^/]*)/send/(?P<event_type>[^/]*)") + register_txn_path(self, PATTERN, http_server, with_get=True) @defer.inlineCallbacks - def on_PUT(self, request, room_alias): + def on_POST(self, request, room_id, event_type): user = yield self.auth.get_user_by_req(request) + content = _parse_json(request) - if not user: - defer.returnValue((403, "Unrecognized user")) - - logger.debug("room_alias: %s", room_alias) - - room_alias = self.hs.parse_roomalias(urllib.unquote(room_alias)) - - handler = self.handlers.room_member_handler - ret_dict = yield handler.join_room_alias(user, room_alias) - - defer.returnValue((200, ret_dict)) + event = self.event_factory.create_event( + etype=event_type, + room_id=urllib.unquote(room_id), + user_id=user.to_string(), + content=content + ) + msg_handler = self.handlers.message_handler + yield msg_handler.send_message(event) -class MessageRestServlet(RestServlet): - PATTERN = client_path_pattern("/rooms/(?P<room_id>[^/]*)/messages/" - + "(?P<sender_id>[^/]*)/(?P<msg_id>[^/]*)$") + defer.returnValue((200, {"event_id": event.event_id})) - def get_event_type(self): - return MessageEvent.TYPE + def on_GET(self, request, room_id, event_type, txn_id): + return (200, "Not implemented") @defer.inlineCallbacks - def on_GET(self, request, room_id, sender_id, msg_id): - user = yield self.auth.get_user_by_req(request) + def on_PUT(self, request, room_id, event_type, txn_id): + try: + defer.returnValue(self.txns.get_client_transaction(request, txn_id)) + except KeyError: + pass - msg_handler = self.handlers.message_handler - msg = yield msg_handler.get_message(room_id=urllib.unquote(room_id), - sender_id=urllib.unquote(sender_id), - msg_id=msg_id, - user_id=user.to_string(), - ) + response = yield self.on_POST(request, room_id, event_type) + + self.txns.store_client_transaction(request, txn_id, response) + defer.returnValue(response) - if not msg: - raise SynapseError(404, "Message not found.", - errcode=Codes.NOT_FOUND) - defer.returnValue((200, json.loads(msg.content))) +class JoinRoomAliasServlet(RestServlet): + PATTERN = client_path_pattern("/join/(?P<room_alias>[^/]+)$") @defer.inlineCallbacks - def on_PUT(self, request, room_id, sender_id, msg_id): + def on_PUT(self, request, room_alias): user = yield self.auth.get_user_by_req(request) - if user.to_string() != urllib.unquote(sender_id): - raise SynapseError(403, "Must send messages as yourself.", - errcode=Codes.FORBIDDEN) + if not user: + defer.returnValue((403, "Unrecognized user")) - content = _parse_json(request) + logger.debug("room_alias: %s", room_alias) - event = self.event_factory.create_event( - etype=self.get_event_type(), - room_id=urllib.unquote(room_id), - user_id=user.to_string(), - msg_id=msg_id, - content=content - ) + room_alias = self.hs.parse_roomalias(urllib.unquote(room_alias)) - msg_handler = self.handlers.message_handler - yield msg_handler.send_message(event) + handler = self.handlers.room_member_handler + ret_dict = yield handler.join_room_alias(user, room_alias) - defer.returnValue((200, "")) + defer.returnValue((200, ret_dict)) class FeedbackRestServlet(RestServlet): @@ -402,7 +394,7 @@ class RoomMembershipRestServlet(RestServlet): def on_PUT(self, request, room_id, membership_action, txn_id): try: defer.returnValue(self.txns.get_client_transaction(request, txn_id)) - except: + except KeyError: pass response = yield self.on_POST(request, room_id, membership_action) @@ -422,7 +414,7 @@ def _parse_json(request): raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON) -def register_txn_path(servlet, regex_string, http_server): +def register_txn_path(servlet, regex_string, http_server, with_get=False): """Registers a transaction-based path. This registers two paths: @@ -433,6 +425,7 @@ def register_txn_path(servlet, regex_string, http_server): regex_string (str): The regex string to register. Must NOT have a trailing $ as this string will be appended to. http_server : The http_server to register paths with. + with_get: True to also register respective GET paths for the PUTs. """ http_server.register_path( "POST", @@ -444,11 +437,16 @@ def register_txn_path(servlet, regex_string, http_server): client_path_pattern(regex_string + "/(?P<txn_id>[^/]*)$"), servlet.on_PUT ) + if with_get: + http_server.register_path( + "GET", + client_path_pattern(regex_string + "/(?P<txn_id>[^/]*)$"), + servlet.on_GET + ) def register_servlets(hs, http_server): RoomStateEventRestServlet(hs).register(http_server) - MessageRestServlet(hs).register(http_server) FeedbackRestServlet(hs).register(http_server) RoomCreateRestServlet(hs).register(http_server) RoomMemberListRestServlet(hs).register(http_server) @@ -456,3 +454,4 @@ def register_servlets(hs, http_server): JoinRoomAliasServlet(hs).register(http_server) RoomTriggerBackfill(hs).register(http_server) RoomMembershipRestServlet(hs).register(http_server) + RoomSendEventRestServlet(hs).register(http_server) |