summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--synapse/handlers/directory.py48
-rw-r--r--synapse/rest/directory.py8
2 files changed, 29 insertions, 27 deletions
diff --git a/synapse/handlers/directory.py b/synapse/handlers/directory.py
index 3cc6348906..df98e39f69 100644
--- a/synapse/handlers/directory.py
+++ b/synapse/handlers/directory.py
@@ -35,9 +35,11 @@ class DirectoryHandler(BaseHandler):
 
     def __init__(self, hs):
         super(DirectoryHandler, self).__init__(hs)
-        self.hs = hs
-        self.http_client = hs.get_http_client()
-        self.clock = hs.get_clock()
+
+        self.federation = hs.get_replication_layer()
+        self.federation.register_query_handler(
+            "directory", self.on_directory_query
+        )
 
     @defer.inlineCallbacks
     def create_association(self, room_alias, room_id, servers):
@@ -58,9 +60,7 @@ class DirectoryHandler(BaseHandler):
         )
 
     @defer.inlineCallbacks
-    def get_association(self, room_alias, local_only=False):
-        # TODO(erikj): Do auth
-
+    def get_association(self, room_alias):
         room_id = None
         if room_alias.is_mine:
             result = yield self.store.get_association_from_room_alias(
@@ -70,22 +70,13 @@ class DirectoryHandler(BaseHandler):
             if result:
                 room_id = result.room_id
                 servers = result.servers
-        elif not local_only:
-            path = "%s/ds/room/%s?local_only=1" % (
-                PREFIX,
-                urllib.quote(room_alias.to_string())
+        else:
+            result = yield self.federation.make_query(
+                destination=room_alias.domain,
+                query_type="directory",
+                args={"room_alias": room_alias.to_string()},
             )
 
-            result = None
-            try:
-                result = yield self.http_client.get_json(
-                    destination=room_alias.domain,
-                    path=path,
-                )
-            except:
-                # TODO(erikj): Handle this better?
-                logger.exception("Failed to get remote room alias")
-
             if result and "room_id" in result and "servers" in result:
                 room_id = result["room_id"]
                 servers = result["servers"]
@@ -99,3 +90,20 @@ class DirectoryHandler(BaseHandler):
             "servers": servers,
         })
         return
+
+    @defer.inlineCallbacks
+    def on_directory_query(self, args):
+        room_alias = self.hs.parse_roomalias(args["room_alias"])
+        if not room_alias.is_mine:
+            raise SynapseError(
+                400, "Room Alias is not hosted on this Home Server"
+            )
+
+        result = yield self.store.get_association_from_room_alias(
+            room_alias
+        )
+
+        defer.returnValue({
+            "room_id": result.room_id,
+            "servers": result.servers,
+        })
diff --git a/synapse/rest/directory.py b/synapse/rest/directory.py
index 362f76c6ca..be9a3f5f9f 100644
--- a/synapse/rest/directory.py
+++ b/synapse/rest/directory.py
@@ -35,16 +35,10 @@ class ClientDirectoryServer(RestServlet):
 
     @defer.inlineCallbacks
     def on_GET(self, request, room_alias):
-        # TODO(erikj): Handle request
-        local_only = "local_only" in request.args
-
         room_alias = self.hs.parse_roomalias(urllib.unquote(room_alias))
 
         dir_handler = self.handlers.directory_handler
-        res = yield dir_handler.get_association(
-            room_alias,
-            local_only=local_only
-        )
+        res = yield dir_handler.get_association(room_alias)
 
         defer.returnValue((200, res))