diff --git a/synapse/appservice/api.py b/synapse/appservice/api.py
index bc90605324..b0eb0c6d9d 100644
--- a/synapse/appservice/api.py
+++ b/synapse/appservice/api.py
@@ -14,9 +14,11 @@
# limitations under the License.
from twisted.internet import defer
+from synapse.api.constants import ThirdPartyEntityKind
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
import logging
import urllib
@@ -24,6 +26,42 @@ import urllib
logger = logging.getLogger(__name__)
+HOUR_IN_MS = 60 * 60 * 1000
+
+
+APP_SERVICE_PREFIX = "/_matrix/app/unstable"
+
+
+def _is_valid_3pe_metadata(info):
+ if "instances" not in info:
+ return False
+ if not isinstance(info["instances"], list):
+ return False
+ return True
+
+
+def _is_valid_3pe_result(r, field):
+ if not isinstance(r, dict):
+ return False
+
+ for k in (field, "protocol"):
+ if k not in r:
+ return False
+ if not isinstance(r[k], str):
+ return False
+
+ if "fields" not in r:
+ return False
+ fields = r["fields"]
+ if not isinstance(fields, dict):
+ return False
+ for k in fields.keys():
+ if not isinstance(fields[k], str):
+ return False
+
+ return True
+
+
class ApplicationServiceApi(SimpleHttpClient):
"""This class manages HS -> AS communications, including querying and
pushing.
@@ -33,8 +71,12 @@ 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):
+ if service.url is None:
+ defer.returnValue(False)
uri = service.url + ("/users/%s" % urllib.quote(user_id))
response = None
try:
@@ -54,6 +96,8 @@ class ApplicationServiceApi(SimpleHttpClient):
@defer.inlineCallbacks
def query_alias(self, service, alias):
+ if service.url is None:
+ defer.returnValue(False)
uri = service.url + ("/rooms/%s" % urllib.quote(alias))
response = None
try:
@@ -72,7 +116,83 @@ class ApplicationServiceApi(SimpleHttpClient):
defer.returnValue(False)
@defer.inlineCallbacks
+ def query_3pe(self, service, kind, protocol, fields):
+ if kind == ThirdPartyEntityKind.USER:
+ required_field = "userid"
+ elif kind == ThirdPartyEntityKind.LOCATION:
+ required_field = "alias"
+ else:
+ raise ValueError(
+ "Unrecognised 'kind' argument %r to query_3pe()", kind
+ )
+ if service.url is None:
+ defer.returnValue([])
+
+ uri = "%s%s/thirdparty/%s/%s" % (
+ service.url,
+ APP_SERVICE_PREFIX,
+ kind,
+ urllib.quote(protocol)
+ )
+ try:
+ response = yield self.get_json(uri, fields)
+ if not isinstance(response, list):
+ logger.warning(
+ "query_3pe to %s returned an invalid response %r",
+ uri, response
+ )
+ defer.returnValue([])
+
+ ret = []
+ for r in response:
+ if _is_valid_3pe_result(r, field=required_field):
+ ret.append(r)
+ else:
+ logger.warning(
+ "query_3pe to %s returned an invalid result %r",
+ uri, r
+ )
+
+ defer.returnValue(ret)
+ except Exception as ex:
+ logger.warning("query_3pe to %s threw exception %s", uri, ex)
+ defer.returnValue([])
+
+ def get_3pe_protocol(self, service, protocol):
+ if service.url is None:
+ defer.returnValue({})
+
+ @defer.inlineCallbacks
+ def _get():
+ uri = "%s%s/thirdparty/protocol/%s" % (
+ service.url,
+ APP_SERVICE_PREFIX,
+ urllib.quote(protocol)
+ )
+ try:
+ info = yield self.get_json(uri, {})
+
+ if not _is_valid_3pe_metadata(info):
+ logger.warning("query_3pe_protocol to %s did not return a"
+ " valid result", uri)
+ defer.returnValue(None)
+
+ defer.returnValue(info)
+ except Exception as ex:
+ logger.warning("query_3pe_protocol to %s threw exception %s",
+ uri, ex)
+ defer.returnValue(None)
+
+ 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):
+ if service.url is None:
+ defer.returnValue(True)
+
events = self._serialize(events)
if txn_id is None:
@@ -100,11 +220,6 @@ class ApplicationServiceApi(SimpleHttpClient):
logger.warning("push_bulk to %s threw exception %s", uri, ex)
defer.returnValue(False)
- @defer.inlineCallbacks
- def push(self, service, event, txn_id=None):
- response = yield self.push_bulk(service, [event], txn_id)
- defer.returnValue(response)
-
def _serialize(self, events):
time_now = self.clock.time_msec()
return [
|