api.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright 2015-2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from typing import Iterable
  16. from synapse.api.constants import EventTypes
  17. from synapse.config._base import Config, ConfigError
  18. from synapse.config._util import validate_config
  19. from synapse.types import JsonDict
  20. logger = logging.getLogger(__name__)
  21. class ApiConfig(Config):
  22. section = "api"
  23. def read_config(self, config: JsonDict, **kwargs):
  24. validate_config(_MAIN_SCHEMA, config, ())
  25. self.room_prejoin_state = list(self._get_prejoin_state_types(config))
  26. def generate_config_section(cls, **kwargs) -> str:
  27. formatted_default_state_types = "\n".join(
  28. " # - %s" % (t,) for t in _DEFAULT_PREJOIN_STATE_TYPES
  29. )
  30. return """\
  31. ## API Configuration ##
  32. # Controls for the state that is shared with users who receive an invite
  33. # to a room
  34. #
  35. room_prejoin_state:
  36. # By default, the following state event types are shared with users who
  37. # receive invites to the room:
  38. #
  39. %(formatted_default_state_types)s
  40. #
  41. # Uncomment the following to disable these defaults (so that only the event
  42. # types listed in 'additional_event_types' are shared). Defaults to 'false'.
  43. #
  44. #disable_default_event_types: true
  45. # Additional state event types to share with users when they are invited
  46. # to a room.
  47. #
  48. # By default, this list is empty (so only the default event types are shared).
  49. #
  50. #additional_event_types:
  51. # - org.example.custom.event.type
  52. """ % {
  53. "formatted_default_state_types": formatted_default_state_types
  54. }
  55. def _get_prejoin_state_types(self, config: JsonDict) -> Iterable[str]:
  56. """Get the event types to include in the prejoin state
  57. Parses the config and returns an iterable of the event types to be included.
  58. """
  59. room_prejoin_state_config = config.get("room_prejoin_state") or {}
  60. # backwards-compatibility support for room_invite_state_types
  61. if "room_invite_state_types" in config:
  62. # if both "room_invite_state_types" and "room_prejoin_state" are set, then
  63. # we don't really know what to do.
  64. if room_prejoin_state_config:
  65. raise ConfigError(
  66. "Can't specify both 'room_invite_state_types' and 'room_prejoin_state' "
  67. "in config"
  68. )
  69. logger.warning(_ROOM_INVITE_STATE_TYPES_WARNING)
  70. yield from config["room_invite_state_types"]
  71. return
  72. if not room_prejoin_state_config.get("disable_default_event_types"):
  73. yield from _DEFAULT_PREJOIN_STATE_TYPES
  74. if self.spaces_enabled:
  75. # MSC1772 suggests adding m.room.create to the prejoin state
  76. yield EventTypes.Create
  77. yield from room_prejoin_state_config.get("additional_event_types", [])
  78. _ROOM_INVITE_STATE_TYPES_WARNING = """\
  79. WARNING: The 'room_invite_state_types' configuration setting is now deprecated,
  80. and replaced with 'room_prejoin_state'. New features may not work correctly
  81. unless 'room_invite_state_types' is removed. See the sample configuration file for
  82. details of 'room_prejoin_state'.
  83. --------------------------------------------------------------------------------
  84. """
  85. _DEFAULT_PREJOIN_STATE_TYPES = [
  86. EventTypes.JoinRules,
  87. EventTypes.CanonicalAlias,
  88. EventTypes.RoomAvatar,
  89. EventTypes.RoomEncryption,
  90. EventTypes.Name,
  91. ]
  92. # room_prejoin_state can either be None (as it is in the default config), or
  93. # an object containing other config settings
  94. _ROOM_PREJOIN_STATE_CONFIG_SCHEMA = {
  95. "oneOf": [
  96. {
  97. "type": "object",
  98. "properties": {
  99. "disable_default_event_types": {"type": "boolean"},
  100. "additional_event_types": {
  101. "type": "array",
  102. "items": {"type": "string"},
  103. },
  104. },
  105. },
  106. {"type": "null"},
  107. ]
  108. }
  109. # the legacy room_invite_state_types setting
  110. _ROOM_INVITE_STATE_TYPES_SCHEMA = {"type": "array", "items": {"type": "string"}}
  111. _MAIN_SCHEMA = {
  112. "type": "object",
  113. "properties": {
  114. "room_prejoin_state": _ROOM_PREJOIN_STATE_CONFIG_SCHEMA,
  115. "room_invite_state_types": _ROOM_INVITE_STATE_TYPES_SCHEMA,
  116. },
  117. }