2016-02-23 08:22:07 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2016-02-23 08:22:07 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2021-03-30 07:12:44 -04:00
|
|
|
import logging
|
2022-12-12 19:54:46 -05:00
|
|
|
from typing import Any, Iterable, Optional, Tuple
|
2021-03-30 07:12:44 -04:00
|
|
|
|
2016-02-23 08:22:07 -05:00
|
|
|
from synapse.api.constants import EventTypes
|
2021-03-30 07:12:44 -04:00
|
|
|
from synapse.config._base import Config, ConfigError
|
|
|
|
from synapse.config._util import validate_config
|
|
|
|
from synapse.types import JsonDict
|
2022-12-12 19:54:46 -05:00
|
|
|
from synapse.types.state import StateFilter
|
2016-02-23 08:22:07 -05:00
|
|
|
|
2021-03-30 07:12:44 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2016-02-23 08:22:07 -05:00
|
|
|
|
|
|
|
class ApiConfig(Config):
|
2019-10-10 04:39:35 -04:00
|
|
|
section = "api"
|
|
|
|
|
2022-12-12 19:54:46 -05:00
|
|
|
room_prejoin_state: StateFilter
|
|
|
|
track_puppetted_users_ips: bool
|
|
|
|
|
2022-04-11 12:07:23 -04:00
|
|
|
def read_config(self, config: JsonDict, **kwargs: Any) -> None:
|
2021-03-30 07:12:44 -04:00
|
|
|
validate_config(_MAIN_SCHEMA, config, ())
|
2022-12-12 19:54:46 -05:00
|
|
|
self.room_prejoin_state = StateFilter.from_types(
|
|
|
|
self._get_prejoin_state_entries(config)
|
|
|
|
)
|
2022-01-12 11:09:36 -05:00
|
|
|
self.track_puppeted_user_ips = config.get("track_puppeted_user_ips", False)
|
2021-03-30 07:12:44 -04:00
|
|
|
|
2022-12-12 19:54:46 -05:00
|
|
|
def _get_prejoin_state_entries(
|
|
|
|
self, config: JsonDict
|
|
|
|
) -> Iterable[Tuple[str, Optional[str]]]:
|
|
|
|
"""Get the event types and state keys to include in the prejoin state."""
|
2021-03-30 07:12:44 -04:00
|
|
|
room_prejoin_state_config = config.get("room_prejoin_state") or {}
|
|
|
|
|
|
|
|
# backwards-compatibility support for room_invite_state_types
|
|
|
|
if "room_invite_state_types" in config:
|
|
|
|
# if both "room_invite_state_types" and "room_prejoin_state" are set, then
|
|
|
|
# we don't really know what to do.
|
|
|
|
if room_prejoin_state_config:
|
|
|
|
raise ConfigError(
|
|
|
|
"Can't specify both 'room_invite_state_types' and 'room_prejoin_state' "
|
|
|
|
"in config"
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.warning(_ROOM_INVITE_STATE_TYPES_WARNING)
|
|
|
|
|
2022-12-12 19:54:46 -05:00
|
|
|
for event_type in config["room_invite_state_types"]:
|
|
|
|
yield event_type, None
|
2021-03-30 07:12:44 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
if not room_prejoin_state_config.get("disable_default_event_types"):
|
2022-12-12 19:54:46 -05:00
|
|
|
yield from _DEFAULT_PREJOIN_STATE_TYPES_AND_STATE_KEYS
|
2021-03-30 07:12:44 -04:00
|
|
|
|
2022-12-12 19:54:46 -05:00
|
|
|
for entry in room_prejoin_state_config.get("additional_event_types", []):
|
|
|
|
if isinstance(entry, str):
|
|
|
|
yield entry, None
|
|
|
|
else:
|
|
|
|
yield entry
|
2021-03-30 07:12:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
_ROOM_INVITE_STATE_TYPES_WARNING = """\
|
|
|
|
WARNING: The 'room_invite_state_types' configuration setting is now deprecated,
|
|
|
|
and replaced with 'room_prejoin_state'. New features may not work correctly
|
2022-12-12 19:54:46 -05:00
|
|
|
unless 'room_invite_state_types' is removed. See the config documentation at
|
2023-12-13 11:15:22 -05:00
|
|
|
https://element-hq.github.io/synapse/latest/usage/configuration/config_documentation.html#room_prejoin_state
|
2022-12-12 19:54:46 -05:00
|
|
|
for details of 'room_prejoin_state'.
|
2021-03-30 07:12:44 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
"""
|
|
|
|
|
2022-12-12 19:54:46 -05:00
|
|
|
_DEFAULT_PREJOIN_STATE_TYPES_AND_STATE_KEYS = [
|
|
|
|
(EventTypes.JoinRules, ""),
|
|
|
|
(EventTypes.CanonicalAlias, ""),
|
|
|
|
(EventTypes.RoomAvatar, ""),
|
|
|
|
(EventTypes.RoomEncryption, ""),
|
|
|
|
(EventTypes.Name, ""),
|
2021-05-11 10:58:58 -04:00
|
|
|
# Per MSC1772.
|
2022-12-12 19:54:46 -05:00
|
|
|
(EventTypes.Create, ""),
|
2022-01-04 11:08:08 -05:00
|
|
|
# Per MSC3173.
|
2022-12-12 19:54:46 -05:00
|
|
|
(EventTypes.Topic, ""),
|
2021-03-30 07:12:44 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
# room_prejoin_state can either be None (as it is in the default config), or
|
|
|
|
# an object containing other config settings
|
|
|
|
_ROOM_PREJOIN_STATE_CONFIG_SCHEMA = {
|
|
|
|
"oneOf": [
|
|
|
|
{
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
|
|
|
"disable_default_event_types": {"type": "boolean"},
|
|
|
|
"additional_event_types": {
|
|
|
|
"type": "array",
|
2022-12-12 19:54:46 -05:00
|
|
|
"items": {
|
|
|
|
"oneOf": [
|
|
|
|
{"type": "string"},
|
|
|
|
{
|
|
|
|
"type": "array",
|
|
|
|
"items": {"type": "string"},
|
|
|
|
"minItems": 2,
|
|
|
|
"maxItems": 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-03-30 07:12:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"type": "null"},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
# the legacy room_invite_state_types setting
|
|
|
|
_ROOM_INVITE_STATE_TYPES_SCHEMA = {"type": "array", "items": {"type": "string"}}
|
|
|
|
|
|
|
|
_MAIN_SCHEMA = {
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
|
|
|
"room_prejoin_state": _ROOM_PREJOIN_STATE_CONFIG_SCHEMA,
|
|
|
|
"room_invite_state_types": _ROOM_INVITE_STATE_TYPES_SCHEMA,
|
2022-01-12 11:09:36 -05:00
|
|
|
"track_puppeted_user_ips": {
|
|
|
|
"type": "boolean",
|
|
|
|
},
|
2021-03-30 07:12:44 -04:00
|
|
|
},
|
|
|
|
}
|