Skip to content

Commit d6c9007

Browse files
committed
Add configuration class with pydantic for parsing
1 parent fe34c3a commit d6c9007

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

synapse/config/_base.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ from synapse.config import ( # noqa: F401
3737
key,
3838
logger,
3939
mas,
40+
matrixrtc,
4041
metrics,
4142
modules,
4243
oembed,
@@ -126,6 +127,7 @@ class RootConfig:
126127
auto_accept_invites: auto_accept_invites.AutoAcceptInvitesConfig
127128
user_types: user_types.UserTypesConfig
128129
mas: mas.MasConfig
130+
matrix_rtc: matrixrtc.MatrixRtcConfig
129131

130132
config_classes: List[Type["Config"]] = ...
131133
config_files: List[str]

synapse/config/homeserver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from .key import KeyConfig
3838
from .logger import LoggingConfig
3939
from .mas import MasConfig
40+
from .matrixrtc import MatrixRtcConfig
4041
from .metrics import MetricsConfig
4142
from .modules import ModulesConfig
4243
from .oembed import OembedConfig
@@ -80,6 +81,7 @@ class HomeServerConfig(RootConfig):
8081
OembedConfig,
8182
CaptchaConfig,
8283
VoipConfig,
84+
MatrixRtcConfig,
8385
RegistrationConfig,
8486
AccountValidityConfig,
8587
MetricsConfig,

synapse/config/matrixrtc.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#
2+
# This file is licensed under the Affero General Public License (AGPL) version 3.
3+
#
4+
# Copyright (C) 2025 New Vector, Ltd
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Affero General Public License as
8+
# published by the Free Software Foundation, either version 3 of the
9+
# License, or (at your option) any later version.
10+
#
11+
# See the GNU Affero General Public License for more details:
12+
# <https://www.gnu.org/licenses/agpl-3.0.html>.
13+
#
14+
# [This file includes modifications made by New Vector Limited]
15+
#
16+
#
17+
18+
from typing import Any, Optional
19+
20+
from pydantic import ValidationError
21+
22+
from synapse._pydantic_compat import Field, StrictStr, validator
23+
from synapse.types import JsonDict
24+
from synapse.util.pydantic_models import ParseModel
25+
26+
from ._base import Config, ConfigError
27+
28+
29+
class TransportConfigModel(ParseModel):
30+
type: StrictStr
31+
32+
livekit_service_url: Optional[StrictStr] = Field(default=None)
33+
"""An optional livekit service URL. Only required if type is "livekit"."""
34+
35+
@validator("livekit_service_url", always=True)
36+
def validate_livekit_service_url(cls, v: Any, values: dict) -> Any:
37+
if values.get("type") == "livekit" and not v:
38+
raise ValueError(
39+
"You must set a `livekit_service_url` when using the 'livekit' transport."
40+
)
41+
42+
return v
43+
44+
45+
class MatrixRtcConfigModel(ParseModel):
46+
transports: list = []
47+
48+
49+
class MatrixRtcConfig(Config):
50+
section = "matrix_rtc"
51+
52+
def read_config(
53+
self, config: JsonDict, allow_secrets_in_config: bool, **kwargs: Any
54+
) -> None:
55+
matrix_rtc = config.get("matrix_rtc", {})
56+
if matrix_rtc is None:
57+
matrix_rtc = {}
58+
59+
try:
60+
parsed = MatrixRtcConfigModel(**matrix_rtc)
61+
except ValidationError as e:
62+
raise ConfigError(
63+
"Could not validate matrix_rtc config",
64+
("matrix_rtc",),
65+
) from e
66+
67+
self.transports = parsed.transports

0 commit comments

Comments
 (0)