Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

135 righe
4.7 KiB

  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 Any, Iterable, Optional, Tuple
  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. from synapse.types.state import StateFilter
  21. logger = logging.getLogger(__name__)
  22. class ApiConfig(Config):
  23. section = "api"
  24. room_prejoin_state: StateFilter
  25. track_puppetted_users_ips: bool
  26. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  27. validate_config(_MAIN_SCHEMA, config, ())
  28. self.room_prejoin_state = StateFilter.from_types(
  29. self._get_prejoin_state_entries(config)
  30. )
  31. self.track_puppeted_user_ips = config.get("track_puppeted_user_ips", False)
  32. def _get_prejoin_state_entries(
  33. self, config: JsonDict
  34. ) -> Iterable[Tuple[str, Optional[str]]]:
  35. """Get the event types and state keys to include in the prejoin state."""
  36. room_prejoin_state_config = config.get("room_prejoin_state") or {}
  37. # backwards-compatibility support for room_invite_state_types
  38. if "room_invite_state_types" in config:
  39. # if both "room_invite_state_types" and "room_prejoin_state" are set, then
  40. # we don't really know what to do.
  41. if room_prejoin_state_config:
  42. raise ConfigError(
  43. "Can't specify both 'room_invite_state_types' and 'room_prejoin_state' "
  44. "in config"
  45. )
  46. logger.warning(_ROOM_INVITE_STATE_TYPES_WARNING)
  47. for event_type in config["room_invite_state_types"]:
  48. yield event_type, None
  49. return
  50. if not room_prejoin_state_config.get("disable_default_event_types"):
  51. yield from _DEFAULT_PREJOIN_STATE_TYPES_AND_STATE_KEYS
  52. for entry in room_prejoin_state_config.get("additional_event_types", []):
  53. if isinstance(entry, str):
  54. yield entry, None
  55. else:
  56. yield entry
  57. _ROOM_INVITE_STATE_TYPES_WARNING = """\
  58. WARNING: The 'room_invite_state_types' configuration setting is now deprecated,
  59. and replaced with 'room_prejoin_state'. New features may not work correctly
  60. unless 'room_invite_state_types' is removed. See the config documentation at
  61. https://matrix-org.github.io/synapse/latest/usage/configuration/config_documentation.html#room_prejoin_state
  62. for details of 'room_prejoin_state'.
  63. --------------------------------------------------------------------------------
  64. """
  65. _DEFAULT_PREJOIN_STATE_TYPES_AND_STATE_KEYS = [
  66. (EventTypes.JoinRules, ""),
  67. (EventTypes.CanonicalAlias, ""),
  68. (EventTypes.RoomAvatar, ""),
  69. (EventTypes.RoomEncryption, ""),
  70. (EventTypes.Name, ""),
  71. # Per MSC1772.
  72. (EventTypes.Create, ""),
  73. # Per MSC3173.
  74. (EventTypes.Topic, ""),
  75. ]
  76. # room_prejoin_state can either be None (as it is in the default config), or
  77. # an object containing other config settings
  78. _ROOM_PREJOIN_STATE_CONFIG_SCHEMA = {
  79. "oneOf": [
  80. {
  81. "type": "object",
  82. "properties": {
  83. "disable_default_event_types": {"type": "boolean"},
  84. "additional_event_types": {
  85. "type": "array",
  86. "items": {
  87. "oneOf": [
  88. {"type": "string"},
  89. {
  90. "type": "array",
  91. "items": {"type": "string"},
  92. "minItems": 2,
  93. "maxItems": 2,
  94. },
  95. ],
  96. },
  97. },
  98. },
  99. },
  100. {"type": "null"},
  101. ]
  102. }
  103. # the legacy room_invite_state_types setting
  104. _ROOM_INVITE_STATE_TYPES_SCHEMA = {"type": "array", "items": {"type": "string"}}
  105. _MAIN_SCHEMA = {
  106. "type": "object",
  107. "properties": {
  108. "room_prejoin_state": _ROOM_PREJOIN_STATE_CONFIG_SCHEMA,
  109. "room_invite_state_types": _ROOM_INVITE_STATE_TYPES_SCHEMA,
  110. "track_puppeted_user_ips": {
  111. "type": "boolean",
  112. },
  113. },
  114. }