diff options
author | Paul Evans <leonerd@leonerd.org.uk> | 2016-08-25 17:06:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-25 17:06:53 +0100 |
commit | d9dcb2ba3a6f9fbdc286581e772e7fa5ed9086fc (patch) | |
tree | 3686942d7fc944d528518102fc3276a8bac33587 /synapse/appservice/api.py | |
parent | Merge branch 'release-v0.17.1' of github.com:matrix-org/synapse into develop (diff) | |
parent | appease pep8 (diff) | |
download | synapse-d9dcb2ba3a6f9fbdc286581e772e7fa5ed9086fc.tar.xz |
Merge pull request #1041 from matrix-org/paul/third-party-lookup
Extend 3PE lookup APIs for metadata query
Diffstat (limited to 'synapse/appservice/api.py')
-rw-r--r-- | synapse/appservice/api.py | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/synapse/appservice/api.py b/synapse/appservice/api.py index 066127b666..d28a9dff0a 100644 --- a/synapse/appservice/api.py +++ b/synapse/appservice/api.py @@ -17,6 +17,7 @@ from twisted.internet import defer from synapse.api.errors import CodeMessageException from synapse.http.client import SimpleHttpClient from synapse.events.utils import serialize_event +from synapse.util.caches.response_cache import ResponseCache from synapse.types import ThirdPartyEntityKind import logging @@ -25,6 +26,9 @@ import urllib logger = logging.getLogger(__name__) +HOUR_IN_MS = 60 * 60 * 1000 + + def _is_valid_3pe_result(r, field): if not isinstance(r, dict): return False @@ -56,6 +60,8 @@ class ApplicationServiceApi(SimpleHttpClient): super(ApplicationServiceApi, self).__init__(hs) self.clock = hs.get_clock() + self.protocol_meta_cache = ResponseCache(hs, timeout_ms=HOUR_IN_MS) + @defer.inlineCallbacks def query_user(self, service, user_id): uri = service.url + ("/users/%s" % urllib.quote(user_id)) @@ -97,10 +103,10 @@ class ApplicationServiceApi(SimpleHttpClient): @defer.inlineCallbacks def query_3pe(self, service, kind, protocol, fields): if kind == ThirdPartyEntityKind.USER: - uri = "%s/3pu/%s" % (service.url, urllib.quote(protocol)) + uri = "%s/thirdparty/user/%s" % (service.url, urllib.quote(protocol)) required_field = "userid" elif kind == ThirdPartyEntityKind.LOCATION: - uri = "%s/3pl/%s" % (service.url, urllib.quote(protocol)) + uri = "%s/thirdparty/location/%s" % (service.url, urllib.quote(protocol)) required_field = "alias" else: raise ValueError( @@ -131,6 +137,22 @@ class ApplicationServiceApi(SimpleHttpClient): logger.warning("query_3pe to %s threw exception %s", uri, ex) defer.returnValue([]) + def get_3pe_protocol(self, service, protocol): + @defer.inlineCallbacks + def _get(): + uri = "%s/thirdparty/protocol/%s" % (service.url, urllib.quote(protocol)) + try: + defer.returnValue((yield self.get_json(uri, {}))) + except Exception as ex: + logger.warning("query_3pe_protocol to %s threw exception %s", + uri, ex) + defer.returnValue({}) + + key = (service.id, protocol) + return self.protocol_meta_cache.get(key) or ( + self.protocol_meta_cache.set(key, _get()) + ) + @defer.inlineCallbacks def push_bulk(self, service, events, txn_id=None): events = self._serialize(events) |