summaryrefslogtreecommitdiff
path: root/synapse/appservice/api.py
diff options
context:
space:
mode:
authorErik Johnston <erikj@matrix.org>2016-08-24 15:05:56 +0100
committerErik Johnston <erikj@matrix.org>2016-08-24 15:05:56 +0100
commitc1c15ad12f8bda0d65778bd03543ad1f14a1cfc2 (patch)
tree1c843a49d3d5168ff998a54f50d30cdc3814f104 /synapse/appservice/api.py
parentcfb5c3f91265d2b9423b47cec2b555b39c46bc4b (diff)
Imported Upstream version 0.17.1
Diffstat (limited to 'synapse/appservice/api.py')
-rw-r--r--synapse/appservice/api.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/synapse/appservice/api.py b/synapse/appservice/api.py
index 6da6a1b6..066127b6 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.types import ThirdPartyEntityKind
import logging
import urllib
@@ -24,6 +25,28 @@ import urllib
logger = logging.getLogger(__name__)
+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.
@@ -72,6 +95,43 @@ class ApplicationServiceApi(SimpleHttpClient):
defer.returnValue(False)
@defer.inlineCallbacks
+ def query_3pe(self, service, kind, protocol, fields):
+ if kind == ThirdPartyEntityKind.USER:
+ uri = "%s/3pu/%s" % (service.url, urllib.quote(protocol))
+ required_field = "userid"
+ elif kind == ThirdPartyEntityKind.LOCATION:
+ uri = "%s/3pl/%s" % (service.url, urllib.quote(protocol))
+ required_field = "alias"
+ else:
+ raise ValueError(
+ "Unrecognised 'kind' argument %r to query_3pe()", kind
+ )
+
+ 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([])
+
+ @defer.inlineCallbacks
def push_bulk(self, service, events, txn_id=None):
events = self._serialize(events)