api.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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
  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: Any) -> None:
  24. validate_config(_MAIN_SCHEMA, config, ())
  25. self.room_prejoin_state = list(self._get_prejoin_state_types(config))
  26. self.track_puppeted_user_ips = config.get("track_puppeted_user_ips", False)
  27. def _get_prejoin_state_types(self, config: JsonDict) -> Iterable[str]:
  28. """Get the event types to include in the prejoin state
  29. Parses the config and returns an iterable of the event types to be included.
  30. """
  31. room_prejoin_state_config = config.get("room_prejoin_state") or {}
  32. # backwards-compatibility support for room_invite_state_types
  33. if "room_invite_state_types" in config:
  34. # if both "room_invite_state_types" and "room_prejoin_state" are set, then
  35. # we don't really know what to do.
  36. if room_prejoin_state_config:
  37. raise ConfigError(
  38. "Can't specify both 'room_invite_state_types' and 'room_prejoin_state' "
  39. "in config"
  40. )
  41. logger.warning(_ROOM_INVITE_STATE_TYPES_WARNING)
  42. yield from config["room_invite_state_types"]
  43. return
  44. if not room_prejoin_state_config.get("disable_default_event_types"):
  45. yield from _DEFAULT_PREJOIN_STATE_TYPES
  46. yield from room_prejoin_state_config.get("additional_event_types", [])
  47. _ROOM_INVITE_STATE_TYPES_WARNING = """\
  48. WARNING: The 'room_invite_state_types' configuration setting is now deprecated,
  49. and replaced with 'room_prejoin_state'. New features may not work correctly
  50. unless 'room_invite_state_types' is removed. See the sample configuration file for
  51. details of 'room_prejoin_state'.
  52. --------------------------------------------------------------------------------
  53. """
  54. _DEFAULT_PREJOIN_STATE_TYPES = [
  55. EventTypes.JoinRules,
  56. EventTypes.CanonicalAlias,
  57. EventTypes.RoomAvatar,
  58. EventTypes.RoomEncryption,
  59. EventTypes.Name,
  60. # Per MSC1772.
  61. EventTypes.Create,
  62. # Per MSC3173.
  63. EventTypes.Topic,
  64. ]
  65. # room_prejoin_state can either be None (as it is in the default config), or
  66. # an object containing other config settings
  67. _ROOM_PREJOIN_STATE_CONFIG_SCHEMA = {
  68. "oneOf": [
  69. {
  70. "type": "object",
  71. "properties": {
  72. "disable_default_event_types": {"type": "boolean"},
  73. "additional_event_types": {
  74. "type": "array",
  75. "items": {"type": "string"},
  76. },
  77. },
  78. },
  79. {"type": "null"},
  80. ]
  81. }
  82. # the legacy room_invite_state_types setting
  83. _ROOM_INVITE_STATE_TYPES_SCHEMA = {"type": "array", "items": {"type": "string"}}
  84. _MAIN_SCHEMA = {
  85. "type": "object",
  86. "properties": {
  87. "room_prejoin_state": _ROOM_PREJOIN_STATE_CONFIG_SCHEMA,
  88. "room_invite_state_types": _ROOM_INVITE_STATE_TYPES_SCHEMA,
  89. "track_puppeted_user_ips": {
  90. "type": "boolean",
  91. },
  92. },
  93. }