Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f02663c

Browse files
authored
Replace room_invite_state_types with room_prejoin_state (#9700)
`room_invite_state_types` was inconvenient as a configuration setting, because anyone that ever set it would not receive any new types that were added to the defaults. Here, we deprecate the old setting, and replace it with a couple of new settings under `room_prejoin_state`.
1 parent 963f430 commit f02663c

File tree

8 files changed

+144
-43
lines changed

8 files changed

+144
-43
lines changed

changelog.d/9700.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace the `room_invite_state_types` configuration setting with `room_prejoin_state`.

docker/conf/homeserver.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,10 @@ report_stats: False
173173

174174
## API Configuration ##
175175

176-
room_invite_state_types:
177-
- "m.room.join_rules"
178-
- "m.room.canonical_alias"
179-
- "m.room.avatar"
180-
- "m.room.name"
181-
182176
{% if SYNAPSE_APPSERVICES %}
183177
app_service_config_files:
184178
{% for appservice in SYNAPSE_APPSERVICES %} - "{{ appservice }}"
185179
{% endfor %}
186-
{% else %}
187-
app_service_config_files: []
188180
{% endif %}
189181

190182
macaroon_secret_key: "{{ SYNAPSE_MACAROON_SECRET_KEY }}"

docs/code_style.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ Some guidelines follow:
128128
will be if no sub-options are enabled).
129129
- Lines should be wrapped at 80 characters.
130130
- Use two-space indents.
131+
- `true` and `false` are spelt thus (as opposed to `True`, etc.)
132+
- Use single quotes (`'`) rather than double-quotes (`"`) or backticks
133+
(`` ` ``) to refer to configuration options.
131134

132135
Example:
133136

docs/sample_config.yaml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,14 +1451,31 @@ metrics_flags:
14511451

14521452
## API Configuration ##
14531453

1454-
# A list of event types that will be included in the room_invite_state
1455-
#
1456-
#room_invite_state_types:
1457-
# - "m.room.join_rules"
1458-
# - "m.room.canonical_alias"
1459-
# - "m.room.avatar"
1460-
# - "m.room.encryption"
1461-
# - "m.room.name"
1454+
# Controls for the state that is shared with users who receive an invite
1455+
# to a room
1456+
#
1457+
room_prejoin_state:
1458+
# By default, the following state event types are shared with users who
1459+
# receive invites to the room:
1460+
#
1461+
# - m.room.join_rules
1462+
# - m.room.canonical_alias
1463+
# - m.room.avatar
1464+
# - m.room.encryption
1465+
# - m.room.name
1466+
#
1467+
# Uncomment the following to disable these defaults (so that only the event
1468+
# types listed in 'additional_event_types' are shared). Defaults to 'false'.
1469+
#
1470+
#disable_default_event_types: true
1471+
1472+
# Additional state event types to share with users when they are invited
1473+
# to a room.
1474+
#
1475+
# By default, this list is empty (so only the default event types are shared).
1476+
#
1477+
#additional_event_types:
1478+
# - org.example.custom.event.type
14621479

14631480

14641481
# A list of application service config files to use

synapse/config/api.py

Lines changed: 112 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015, 2016 OpenMarket Ltd
1+
# Copyright 2015-2021 The Matrix.org Foundation C.I.C.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,38 +12,127 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import logging
16+
from typing import Iterable
17+
1518
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
1622

17-
from ._base import Config
23+
logger = logging.getLogger(__name__)
1824

1925

2026
class ApiConfig(Config):
2127
section = "api"
2228

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
3336
)
3437

35-
def generate_config_section(cls, **kwargs):
3638
return """\
3739
## API Configuration ##
3840
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
4043
#
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+
}

synapse/handlers/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def __init__(self, hs: "HomeServer"):
385385
self._events_shard_config = self.config.worker.events_shard_config
386386
self._instance_name = hs.get_instance_name()
387387

388-
self.room_invite_state_types = self.hs.config.room_invite_state_types
388+
self.room_invite_state_types = self.hs.config.api.room_prejoin_state
389389

390390
self.membership_types_to_include_profile_data_in = (
391391
{Membership.JOIN, Membership.INVITE}

synapse/storage/databases/main/events_worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import logging
1717
import threading
1818
from collections import namedtuple
19-
from typing import Dict, Iterable, List, Optional, Tuple, overload
19+
from typing import Container, Dict, Iterable, List, Optional, Tuple, overload
2020

2121
from constantly import NamedConstant, Names
2222
from typing_extensions import Literal
@@ -544,7 +544,7 @@ def _get_events_from_cache(self, events, allow_rejected, update_metrics=True):
544544
async def get_stripped_room_state_from_event_context(
545545
self,
546546
context: EventContext,
547-
state_types_to_include: List[EventTypes],
547+
state_types_to_include: Container[str],
548548
membership_user_id: Optional[str] = None,
549549
) -> List[JsonDict]:
550550
"""

tests/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ def default_config(name, parse=False):
122122
"enable_registration_captcha": False,
123123
"macaroon_secret_key": "not even a little secret",
124124
"trusted_third_party_id_servers": [],
125-
"room_invite_state_types": [],
126125
"password_providers": [],
127126
"worker_replication_url": "",
128127
"worker_app": None,

0 commit comments

Comments
 (0)