bulklookupservlet.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. from sydent.http.auth import authIfV2
  21. logger = logging.getLogger(__name__)
  22. class BulkLookupServlet(Resource):
  23. isLeaf = True
  24. def __init__(self, syd):
  25. self.sydent = syd
  26. @jsonwrap
  27. def render_POST(self, request):
  28. """
  29. Bulk-lookup for threepids.
  30. Params: 'threepids': list of threepids, each of which is a list of medium, address
  31. Returns: Object with key 'threepids', which is a list of results where each result
  32. is a 3 item list of medium, address, mxid
  33. Note that results are not streamed to the client.
  34. Threepids for which no mapping is found are omitted.
  35. """
  36. send_cors(request)
  37. authIfV2(self.sydent, request)
  38. args = get_args(request, ('threepids',))
  39. threepids = args['threepids']
  40. if not isinstance(threepids, list):
  41. raise MatrixRestError(400, 'M_INVALID_PARAM', 'threepids must be a list')
  42. logger.info("Bulk lookup of %d threepids", len(threepids))
  43. globalAssocStore = GlobalAssociationStore(self.sydent)
  44. results = globalAssocStore.getMxids(threepids)
  45. return {'threepids': results}
  46. def render_OPTIONS(self, request):
  47. send_cors(request)
  48. return b''