summary refs log tree commit diff
path: root/synapse/handlers
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <paul@matrix.org>2014-08-13 17:12:50 +0100
committerPaul "LeoNerd" Evans <paul@matrix.org>2014-08-13 17:23:49 +0100
commit505917cb975e521f9095194366b7acc3e18ce85a (patch)
tree10a655f22bcae1149b386620b094a6a8517880f3 /synapse/handlers
parentDefine the concept of a 'federation Query'; creating API for making and handl... (diff)
downloadsynapse-505917cb975e521f9095194366b7acc3e18ce85a.tar.xz
Use new Federation Query API to implement HS->HS fetching of remote users' profile information instead of (ab)using the client-side REST API
Diffstat (limited to 'synapse/handlers')
-rw-r--r--synapse/handlers/profile.py66
1 files changed, 41 insertions, 25 deletions
diff --git a/synapse/handlers/profile.py b/synapse/handlers/profile.py
index 976b8cfcdc..950648cc12 100644
--- a/synapse/handlers/profile.py
+++ b/synapse/handlers/profile.py
@@ -26,15 +26,16 @@ import logging
 
 logger = logging.getLogger(__name__)
 
-PREFIX = "/matrix/client/api/v1"
-
 
 class ProfileHandler(BaseHandler):
 
     def __init__(self, hs):
         super(ProfileHandler, self).__init__(hs)
 
-        self.client = hs.get_http_client()
+        self.federation = hs.get_replication_layer()
+        self.federation.register_query_handler(
+            "profile", self.on_profile_query
+        )
 
         distributor = hs.get_distributor()
         self.distributor = distributor
@@ -57,17 +58,14 @@ class ProfileHandler(BaseHandler):
 
             defer.returnValue(displayname)
         elif not local_only:
-            # TODO(paul): This should use the server-server API to ask another
-            # HS. For now we'll just have it use the http client to talk to the
-            # other HS's REST client API
-            path = PREFIX + "/profile/%s/displayname?local_only=1" % (
-                target_user.to_string()
-            )
-
             try:
-                result = yield self.client.get_json(
+                result = yield self.federation.make_query(
                     destination=target_user.domain,
-                    path=path
+                    query_type="profile",
+                    args={
+                        "user_id": target_user.to_string(),
+                        "field": "displayname",
+                    }
                 )
             except CodeMessageException as e:
                 if e.code != 404:
@@ -76,8 +74,8 @@ class ProfileHandler(BaseHandler):
                 raise
             except:
                 logger.exception("Failed to get displayname")
-
-            defer.returnValue(result["displayname"])
+            else:
+                defer.returnValue(result["displayname"])
         else:
             raise SynapseError(400, "User is not hosted on this Home Server")
 
@@ -110,18 +108,14 @@ class ProfileHandler(BaseHandler):
 
             defer.returnValue(avatar_url)
         elif not local_only:
-            # TODO(paul): This should use the server-server API to ask another
-            # HS. For now we'll just have it use the http client to talk to the
-            # other HS's REST client API
-            destination = target_user.domain
-            path = PREFIX + "/profile/%s/avatar_url?local_only=1" % (
-                target_user.to_string(),
-            )
-
             try:
-                result = yield self.client.get_json(
-                    destination=destination,
-                    path=path
+                result = yield self.federation.make_query(
+                    destination=target_user.domain,
+                    query_type="profile",
+                    args={
+                        "user_id": target_user.to_string(),
+                        "field": "avatar_url",
+                    }
                 )
             except CodeMessageException as e:
                 if e.code != 404:
@@ -168,3 +162,25 @@ class ProfileHandler(BaseHandler):
         state["avatar_url"] = avatar_url
 
         defer.returnValue(None)
+
+    @defer.inlineCallbacks
+    def on_profile_query(self, args):
+        user = self.hs.parse_userid(args["user_id"])
+        if not user.is_mine:
+            raise SynapseError(400, "User is not hosted on this Home Server")
+
+        just_field = args.get("field", None)
+
+        response = {}
+
+        if just_field is None or just_field == "displayname":
+            response["displayname"] = yield self.store.get_profile_displayname(
+                user.localpart
+            )
+
+        if just_field is None or just_field == "avatar_url":
+            response["avatar_url"] = yield self.store.get_profile_avatar_url(
+                user.localpart
+            )
+
+        defer.returnValue(response)