internalinfoservlet.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector 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 twisted.web.resource import Resource
  16. import logging
  17. import json
  18. from sydent.db.invite_tokens import JoinTokenStore
  19. from sydent.http.servlets import get_args, jsonwrap, send_cors
  20. logger = logging.getLogger(__name__)
  21. class InternalInfoServlet(Resource):
  22. """Maps a threepid to the responsible HS domain, and gives invitation status.
  23. For use by homeserver instances.
  24. :param syd: A sydent instance.
  25. :type syd: Sydent
  26. :param info: An instance of Info.
  27. :type info: Sydent.http.Info
  28. """
  29. isLeaf = True
  30. def __init__(self, syd, info):
  31. self.sydent = syd
  32. self.info = info
  33. @jsonwrap
  34. def render_GET(self, request):
  35. """
  36. Returns: { hs: ..., [shadow_hs: ...], invited: true/false, requires_invite: true/false }
  37. """
  38. send_cors(request)
  39. args = get_args(request, ('medium', 'address'))
  40. medium = args['medium']
  41. address = args['address']
  42. # Find an entry in the info file matching this user's ID
  43. result = self.info.match_user_id(medium, address)
  44. joinTokenStore = JoinTokenStore(self.sydent)
  45. pendingJoinTokens = joinTokenStore.getTokens(medium, address)
  46. # Report whether this user has been invited to a room
  47. result['invited'] = True if pendingJoinTokens else False
  48. return result
  49. @jsonwrap
  50. def render_OPTIONS(self, request):
  51. send_cors(request)
  52. request.setResponseCode(200)
  53. return {}