capabilities.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector
  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. import logging
  16. from twisted.internet import defer
  17. from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
  18. from synapse.http.servlet import RestServlet
  19. from ._base import client_patterns
  20. logger = logging.getLogger(__name__)
  21. class CapabilitiesRestServlet(RestServlet):
  22. """End point to expose the capabilities of the server."""
  23. PATTERNS = client_patterns("/capabilities$")
  24. def __init__(self, hs):
  25. """
  26. Args:
  27. hs (synapse.server.HomeServer): server
  28. """
  29. super(CapabilitiesRestServlet, self).__init__()
  30. self.hs = hs
  31. self.config = hs.config
  32. self.auth = hs.get_auth()
  33. self.store = hs.get_datastore()
  34. @defer.inlineCallbacks
  35. def on_GET(self, request):
  36. requester = yield self.auth.get_user_by_req(request, allow_guest=True)
  37. user = yield self.store.get_user_by_id(requester.user.to_string())
  38. change_password = bool(user["password_hash"])
  39. response = {
  40. "capabilities": {
  41. "m.room_versions": {
  42. "default": self.config.default_room_version.identifier,
  43. "available": {
  44. v.identifier: v.disposition
  45. for v in KNOWN_ROOM_VERSIONS.values()
  46. },
  47. },
  48. "m.change_password": {"enabled": change_password},
  49. }
  50. }
  51. return (200, response)
  52. def register_servlets(hs, http_server):
  53. CapabilitiesRestServlet(hs).register(http_server)