api.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. self.track_puppeted_user_ips = config.get("track_puppeted_user_ips", False)
  27. def generate_config_section(cls, **kwargs) -> str:
  28. formatted_default_state_types = "\n".join(
  29. " # - %s" % (t,) for t in _DEFAULT_PREJOIN_STATE_TYPES
  30. )
  31. return """\
  32. ## API Configuration ##
  33. # Controls for the state that is shared with users who receive an invite
  34. # to a room
  35. #
  36. room_prejoin_state:
  37. # By default, the following state event types are shared with users who
  38. # receive invites to the room:
  39. #
  40. %(formatted_default_state_types)s
  41. #
  42. # Uncomment the following to disable these defaults (so that only the event
  43. # types listed in 'additional_event_types' are shared). Defaults to 'false'.
  44. #
  45. #disable_default_event_types: true
  46. # Additional state event types to share with users when they are invited
  47. # to a room.
  48. #
  49. # By default, this list is empty (so only the default event types are shared).
  50. #
  51. #additional_event_types:
  52. # - org.example.custom.event.type
  53. # We record the IP address of clients used to access the API for various
  54. # reasons, including displaying it to the user in the "Where you're signed in"
  55. # dialog.
  56. #
  57. # By default, when puppeting another user via the admin API, the client IP
  58. # address is recorded against the user who created the access token (ie, the
  59. # admin user), and *not* the puppeted user.
  60. #
  61. # Uncomment the following to also record the IP address against the puppeted
  62. # user. (This also means that the puppeted user will count as an "active" user
  63. # for the purpose of monthly active user tracking - see 'limit_usage_by_mau' etc
  64. # above.)
  65. #
  66. #track_puppeted_user_ips: true
  67. """ % {
  68. "formatted_default_state_types": formatted_default_state_types
  69. }
  70. def _get_prejoin_state_types(self, config: JsonDict) -> Iterable[str]:
  71. """Get the event types to include in the prejoin state
  72. Parses the config and returns an iterable of the event types to be included.
  73. """
  74. room_prejoin_state_config = config.get("room_prejoin_state") or {}
  75. # backwards-compatibility support for room_invite_state_types
  76. if "room_invite_state_types" in config:
  77. # if both "room_invite_state_types" and "room_prejoin_state" are set, then
  78. # we don't really know what to do.
  79. if room_prejoin_state_config:
  80. raise ConfigError(
  81. "Can't specify both 'room_invite_state_types' and 'room_prejoin_state' "
  82. "in config"
  83. )
  84. logger.warning(_ROOM_INVITE_STATE_TYPES_WARNING)
  85. yield from config["room_invite_state_types"]
  86. return
  87. if not room_prejoin_state_config.get("disable_default_event_types"):
  88. yield from _DEFAULT_PREJOIN_STATE_TYPES
  89. yield from room_prejoin_state_config.get("additional_event_types", [])
  90. _ROOM_INVITE_STATE_TYPES_WARNING = """\
  91. WARNING: The 'room_invite_state_types' configuration setting is now deprecated,
  92. and replaced with 'room_prejoin_state'. New features may not work correctly
  93. unless 'room_invite_state_types' is removed. See the sample configuration file for
  94. details of 'room_prejoin_state'.
  95. --------------------------------------------------------------------------------
  96. """
  97. _DEFAULT_PREJOIN_STATE_TYPES = [
  98. EventTypes.JoinRules,
  99. EventTypes.CanonicalAlias,
  100. EventTypes.RoomAvatar,
  101. EventTypes.RoomEncryption,
  102. EventTypes.Name,
  103. # Per MSC1772.
  104. EventTypes.Create,
  105. # Per MSC3173.
  106. EventTypes.Topic,
  107. ]
  108. # room_prejoin_state can either be None (as it is in the default config), or
  109. # an object containing other config settings
  110. _ROOM_PREJOIN_STATE_CONFIG_SCHEMA = {
  111. "oneOf": [
  112. {
  113. "type": "object",
  114. "properties": {
  115. "disable_default_event_types": {"type": "boolean"},
  116. "additional_event_types": {
  117. "type": "array",
  118. "items": {"type": "string"},
  119. },
  120. },
  121. },
  122. {"type": "null"},
  123. ]
  124. }
  125. # the legacy room_invite_state_types setting
  126. _ROOM_INVITE_STATE_TYPES_SCHEMA = {"type": "array", "items": {"type": "string"}}
  127. _MAIN_SCHEMA = {
  128. "type": "object",
  129. "properties": {
  130. "room_prejoin_state": _ROOM_PREJOIN_STATE_CONFIG_SCHEMA,
  131. "room_invite_state_types": _ROOM_INVITE_STATE_TYPES_SCHEMA,
  132. "track_puppeted_user_ips": {
  133. "type": "boolean",
  134. },
  135. },
  136. }