summary refs log tree commit diff
path: root/synapse/rest
diff options
context:
space:
mode:
authorDaniel Wagner-Hall <daniel@matrix.org>2016-02-15 15:39:16 +0000
committerDaniel Wagner-Hall <daniel@matrix.org>2016-02-15 15:39:16 +0000
commite71095801fc376aac30ff9408ae7f0203684024d (patch)
treeee95d9524469d75bf5abd87b9e2c8ee1451399c3 /synapse/rest
parentMerge some room joining codepaths (diff)
downloadsynapse-e71095801fc376aac30ff9408ae7f0203684024d.tar.xz
Merge implementation of /join by alias or ID
This code is kind of rough (passing the remote servers down a long
chain), but is a step towards improvement.
Diffstat (limited to 'synapse/rest')
-rw-r--r--synapse/rest/client/v1/room.py68
1 files changed, 31 insertions, 37 deletions
diff --git a/synapse/rest/client/v1/room.py b/synapse/rest/client/v1/room.py
index 76025213dc..340c24635d 100644
--- a/synapse/rest/client/v1/room.py
+++ b/synapse/rest/client/v1/room.py
@@ -229,46 +229,40 @@ class JoinRoomAliasServlet(ClientV1RestServlet):
             allow_guest=True,
         )
 
-        # the identifier could be a room alias or a room id. Try one then the
-        # other if it fails to parse, without swallowing other valid
-        # SynapseErrors.
-
-        identifier = None
-        is_room_alias = False
-        try:
-            identifier = RoomAlias.from_string(room_identifier)
-            is_room_alias = True
-        except SynapseError:
-            identifier = RoomID.from_string(room_identifier)
+        if RoomID.is_valid(room_identifier):
+            room_id = room_identifier
+            room_hosts = None
+        elif RoomAlias.is_valid(room_identifier):
+            handler = self.handlers.room_member_handler
+            room_alias = RoomAlias.from_string(room_identifier)
+            room_id, room_hosts = yield handler.lookup_room_alias(room_alias)
+            room_id = room_id.to_string()
+        else:
+            raise SynapseError(400, "%s was not legal room ID or room alias" % (
+                room_identifier,
+            ))
 
-        # TODO: Support for specifying the home server to join with?
+        msg_handler = self.handlers.message_handler
+        content = {"membership": Membership.JOIN}
+        if requester.is_guest:
+            content["kind"] = "guest"
+        yield msg_handler.create_and_send_event(
+            {
+                "type": EventTypes.Member,
+                "content": content,
+                "room_id": room_id,
+                "sender": requester.user.to_string(),
+                "state_key": requester.user.to_string(),
 
-        if is_room_alias:
-            handler = self.handlers.room_member_handler
-            ret_dict = yield handler.join_room_alias(
-                requester,
-                identifier,
-            )
-            defer.returnValue((200, ret_dict))
-        else:  # room id
-            msg_handler = self.handlers.message_handler
-            content = {"membership": Membership.JOIN}
-            if requester.is_guest:
-                content["kind"] = "guest"
-            yield msg_handler.create_and_send_event(
-                {
-                    "type": EventTypes.Member,
-                    "content": content,
-                    "room_id": identifier.to_string(),
-                    "sender": requester.user.to_string(),
-                    "state_key": requester.user.to_string(),
-                },
-                token_id=requester.access_token_id,
-                txn_id=txn_id,
-                is_guest=requester.is_guest,
-            )
+                "membership": Membership.JOIN,  # For backwards compatibility
+            },
+            token_id=requester.access_token_id,
+            txn_id=txn_id,
+            is_guest=requester.is_guest,
+            room_hosts=room_hosts,
+        )
 
-            defer.returnValue((200, {"room_id": identifier.to_string()}))
+        defer.returnValue((200, {"room_id": room_id}))
 
     @defer.inlineCallbacks
     def on_PUT(self, request, room_identifier, txn_id):