|
1 | | -# Copyright 2015, 2016 OpenMarket Ltd |
| 1 | +# Copyright 2015-2021 The Matrix.org Foundation C.I.C. |
2 | 2 | # |
3 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | # you may not use this file except in compliance with the License. |
|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import logging |
| 16 | +from typing import Iterable |
| 17 | + |
15 | 18 | from synapse.api.constants import EventTypes |
| 19 | +from synapse.config._base import Config, ConfigError |
| 20 | +from synapse.config._util import validate_config |
| 21 | +from synapse.types import JsonDict |
16 | 22 |
|
17 | | -from ._base import Config |
| 23 | +logger = logging.getLogger(__name__) |
18 | 24 |
|
19 | 25 |
|
20 | 26 | class ApiConfig(Config): |
21 | 27 | section = "api" |
22 | 28 |
|
23 | | - def read_config(self, config, **kwargs): |
24 | | - self.room_invite_state_types = config.get( |
25 | | - "room_invite_state_types", |
26 | | - [ |
27 | | - EventTypes.JoinRules, |
28 | | - EventTypes.CanonicalAlias, |
29 | | - EventTypes.RoomAvatar, |
30 | | - EventTypes.RoomEncryption, |
31 | | - EventTypes.Name, |
32 | | - ], |
| 29 | + def read_config(self, config: JsonDict, **kwargs): |
| 30 | + validate_config(_MAIN_SCHEMA, config, ()) |
| 31 | + self.room_prejoin_state = list(self._get_prejoin_state_types(config)) |
| 32 | + |
| 33 | + def generate_config_section(cls, **kwargs) -> str: |
| 34 | + formatted_default_state_types = "\n".join( |
| 35 | + " # - %s" % (t,) for t in _DEFAULT_PREJOIN_STATE_TYPES |
33 | 36 | ) |
34 | 37 |
|
35 | | - def generate_config_section(cls, **kwargs): |
36 | 38 | return """\ |
37 | 39 | ## API Configuration ## |
38 | 40 |
|
39 | | - # A list of event types that will be included in the room_invite_state |
| 41 | + # Controls for the state that is shared with users who receive an invite |
| 42 | + # to a room |
40 | 43 | # |
41 | | - #room_invite_state_types: |
42 | | - # - "{JoinRules}" |
43 | | - # - "{CanonicalAlias}" |
44 | | - # - "{RoomAvatar}" |
45 | | - # - "{RoomEncryption}" |
46 | | - # - "{Name}" |
47 | | - """.format( |
48 | | - **vars(EventTypes) |
49 | | - ) |
| 44 | + room_prejoin_state: |
| 45 | + # By default, the following state event types are shared with users who |
| 46 | + # receive invites to the room: |
| 47 | + # |
| 48 | +%(formatted_default_state_types)s |
| 49 | + # |
| 50 | + # Uncomment the following to disable these defaults (so that only the event |
| 51 | + # types listed in 'additional_event_types' are shared). Defaults to 'false'. |
| 52 | + # |
| 53 | + #disable_default_event_types: true |
| 54 | +
|
| 55 | + # Additional state event types to share with users when they are invited |
| 56 | + # to a room. |
| 57 | + # |
| 58 | + # By default, this list is empty (so only the default event types are shared). |
| 59 | + # |
| 60 | + #additional_event_types: |
| 61 | + # - org.example.custom.event.type |
| 62 | + """ % { |
| 63 | + "formatted_default_state_types": formatted_default_state_types |
| 64 | + } |
| 65 | + |
| 66 | + def _get_prejoin_state_types(self, config: JsonDict) -> Iterable[str]: |
| 67 | + """Get the event types to include in the prejoin state |
| 68 | +
|
| 69 | + Parses the config and returns an iterable of the event types to be included. |
| 70 | + """ |
| 71 | + room_prejoin_state_config = config.get("room_prejoin_state") or {} |
| 72 | + |
| 73 | + # backwards-compatibility support for room_invite_state_types |
| 74 | + if "room_invite_state_types" in config: |
| 75 | + # if both "room_invite_state_types" and "room_prejoin_state" are set, then |
| 76 | + # we don't really know what to do. |
| 77 | + if room_prejoin_state_config: |
| 78 | + raise ConfigError( |
| 79 | + "Can't specify both 'room_invite_state_types' and 'room_prejoin_state' " |
| 80 | + "in config" |
| 81 | + ) |
| 82 | + |
| 83 | + logger.warning(_ROOM_INVITE_STATE_TYPES_WARNING) |
| 84 | + |
| 85 | + yield from config["room_invite_state_types"] |
| 86 | + return |
| 87 | + |
| 88 | + if not room_prejoin_state_config.get("disable_default_event_types"): |
| 89 | + yield from _DEFAULT_PREJOIN_STATE_TYPES |
| 90 | + |
| 91 | + yield from room_prejoin_state_config.get("additional_event_types", []) |
| 92 | + |
| 93 | + |
| 94 | +_ROOM_INVITE_STATE_TYPES_WARNING = """\ |
| 95 | +WARNING: The 'room_invite_state_types' configuration setting is now deprecated, |
| 96 | +and replaced with 'room_prejoin_state'. New features may not work correctly |
| 97 | +unless 'room_invite_state_types' is removed. See the sample configuration file for |
| 98 | +details of 'room_prejoin_state'. |
| 99 | +-------------------------------------------------------------------------------- |
| 100 | +""" |
| 101 | + |
| 102 | +_DEFAULT_PREJOIN_STATE_TYPES = [ |
| 103 | + EventTypes.JoinRules, |
| 104 | + EventTypes.CanonicalAlias, |
| 105 | + EventTypes.RoomAvatar, |
| 106 | + EventTypes.RoomEncryption, |
| 107 | + EventTypes.Name, |
| 108 | +] |
| 109 | + |
| 110 | + |
| 111 | +# room_prejoin_state can either be None (as it is in the default config), or |
| 112 | +# an object containing other config settings |
| 113 | +_ROOM_PREJOIN_STATE_CONFIG_SCHEMA = { |
| 114 | + "oneOf": [ |
| 115 | + { |
| 116 | + "type": "object", |
| 117 | + "properties": { |
| 118 | + "disable_default_event_types": {"type": "boolean"}, |
| 119 | + "additional_event_types": { |
| 120 | + "type": "array", |
| 121 | + "items": {"type": "string"}, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + {"type": "null"}, |
| 126 | + ] |
| 127 | +} |
| 128 | + |
| 129 | +# the legacy room_invite_state_types setting |
| 130 | +_ROOM_INVITE_STATE_TYPES_SCHEMA = {"type": "array", "items": {"type": "string"}} |
| 131 | + |
| 132 | +_MAIN_SCHEMA = { |
| 133 | + "type": "object", |
| 134 | + "properties": { |
| 135 | + "room_prejoin_state": _ROOM_PREJOIN_STATE_CONFIG_SCHEMA, |
| 136 | + "room_invite_state_types": _ROOM_INVITE_STATE_TYPES_SCHEMA, |
| 137 | + }, |
| 138 | +} |
0 commit comments