bulklookupservlet.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import absolute_import
  16. from twisted.web.resource import Resource
  17. from sydent.db.threepid_associations import GlobalAssociationStore
  18. import logging
  19. from sydent.http.servlets import get_args, jsonwrap, send_cors, MatrixRestError
  20. logger = logging.getLogger(__name__)
  21. class BulkLookupServlet(Resource):
  22. isLeaf = True
  23. def __init__(self, syd):
  24. self.sydent = syd
  25. @jsonwrap
  26. def render_POST(self, request):
  27. """
  28. Bulk-lookup for threepids.
  29. Params: 'threepids': list of threepids, each of which is a list of medium, address
  30. Returns: Object with key 'threepids', which is a list of results where each result
  31. is a 3 item list of medium, address, mxid
  32. Note that results are not streamed to the client.
  33. Threepids for which no mapping is found are omitted.
  34. """
  35. send_cors(request)
  36. args = get_args(request, ('threepids',))
  37. threepids = args['threepids']
  38. if not isinstance(threepids, list):
  39. raise MatrixRestError(400, 'M_INVALID_PARAM', 'threepids must be a list')
  40. logger.info("Bulk lookup of %d threepids", len(threepids))
  41. globalAssocStore = GlobalAssociationStore(self.sydent)
  42. results = globalAssocStore.getMxids(threepids)
  43. return {'threepids': results}
  44. def render_OPTIONS(self, request):
  45. send_cors(request)
  46. return b''