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

Commit 6368150

Browse files
Add config option for setting homeserver's default room version (#5223)
Replaces DEFAULT_ROOM_VERSION constant with a method that first checks the config, then returns a hardcoded value if the option is not present. That hardcoded value is now located in the server.py config file.
1 parent b75537b commit 6368150

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

changelog.d/5223.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ability to configure default room version.

docs/sample_config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ pid_file: DATADIR/homeserver.pid
8383
#
8484
#restrict_public_rooms_to_local_users: true
8585

86+
# The default room version for newly created rooms.
87+
#
88+
# Known room versions are listed here:
89+
# https://matrix.org/docs/spec/#complete-list-of-room-versions
90+
#
91+
# For example, for room version 1, default_room_version should be set
92+
# to "1".
93+
#default_room_version: "1"
94+
8695
# The GC threshold parameters to pass to `gc.set_threshold`, if defined
8796
#
8897
#gc_thresholds: [700, 10, 10]

synapse/api/room_versions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ class RoomVersions(object):
8585
)
8686

8787

88-
# the version we will give rooms which are created on this server
89-
DEFAULT_ROOM_VERSION = RoomVersions.V1
90-
91-
9288
KNOWN_ROOM_VERSIONS = {
9389
v.identifier: v for v in (
9490
RoomVersions.V1,

synapse/config/server.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from netaddr import IPSet
2222

23+
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
2324
from synapse.http.endpoint import parse_and_validate_server_name
2425
from synapse.python_dependencies import DependencyException, check_requirements
2526

@@ -35,6 +36,8 @@
3536
# in the list.
3637
DEFAULT_BIND_ADDRESSES = ['::', '0.0.0.0']
3738

39+
DEFAULT_ROOM_VERSION = "1"
40+
3841

3942
class ServerConfig(Config):
4043

@@ -88,6 +91,22 @@ def read_config(self, config):
8891
"restrict_public_rooms_to_local_users", False,
8992
)
9093

94+
default_room_version = config.get(
95+
"default_room_version", DEFAULT_ROOM_VERSION,
96+
)
97+
98+
# Ensure room version is a str
99+
default_room_version = str(default_room_version)
100+
101+
if default_room_version not in KNOWN_ROOM_VERSIONS:
102+
raise ConfigError(
103+
"Unknown default_room_version: %s, known room versions: %s" %
104+
(default_room_version, list(KNOWN_ROOM_VERSIONS.keys()))
105+
)
106+
107+
# Get the actual room version object rather than just the identifier
108+
self.default_room_version = KNOWN_ROOM_VERSIONS[default_room_version]
109+
91110
# whether to enable search. If disabled, new entries will not be inserted
92111
# into the search tables and they will not be indexed. Users will receive
93112
# errors when attempting to search for messages.
@@ -310,6 +329,10 @@ def default_config(self, server_name, data_dir_path, **kwargs):
310329
unsecure_port = 8008
311330

312331
pid_file = os.path.join(data_dir_path, "homeserver.pid")
332+
333+
# Bring DEFAULT_ROOM_VERSION into the local-scope for use in the
334+
# default config string
335+
default_room_version = DEFAULT_ROOM_VERSION
313336
return """\
314337
## Server ##
315338
@@ -384,6 +407,15 @@ def default_config(self, server_name, data_dir_path, **kwargs):
384407
#
385408
#restrict_public_rooms_to_local_users: true
386409
410+
# The default room version for newly created rooms.
411+
#
412+
# Known room versions are listed here:
413+
# https://matrix.org/docs/spec/#complete-list-of-room-versions
414+
#
415+
# For example, for room version 1, default_room_version should be set
416+
# to "1".
417+
#default_room_version: "%(default_room_version)s"
418+
387419
# The GC threshold parameters to pass to `gc.set_threshold`, if defined
388420
#
389421
#gc_thresholds: [700, 10, 10]

synapse/handlers/room.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from synapse.api.constants import EventTypes, JoinRules, RoomCreationPreset
2929
from synapse.api.errors import AuthError, Codes, NotFoundError, StoreError, SynapseError
30-
from synapse.api.room_versions import DEFAULT_ROOM_VERSION, KNOWN_ROOM_VERSIONS
30+
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
3131
from synapse.storage.state import StateFilter
3232
from synapse.types import RoomAlias, RoomID, RoomStreamToken, StreamToken, UserID
3333
from synapse.util import stringutils
@@ -70,6 +70,7 @@ def __init__(self, hs):
7070
self.spam_checker = hs.get_spam_checker()
7171
self.event_creation_handler = hs.get_event_creation_handler()
7272
self.room_member_handler = hs.get_room_member_handler()
73+
self.config = hs.config
7374

7475
# linearizer to stop two upgrades happening at once
7576
self._upgrade_linearizer = Linearizer("room_upgrade_linearizer")
@@ -475,7 +476,11 @@ def create_room(self, requester, config, ratelimit=True,
475476
if ratelimit:
476477
yield self.ratelimit(requester)
477478

478-
room_version = config.get("room_version", DEFAULT_ROOM_VERSION.identifier)
479+
room_version = config.get(
480+
"room_version",
481+
self.config.default_room_version.identifier,
482+
)
483+
479484
if not isinstance(room_version, string_types):
480485
raise SynapseError(
481486
400,

synapse/rest/client/v2_alpha/capabilities.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from twisted.internet import defer
1818

19-
from synapse.api.room_versions import DEFAULT_ROOM_VERSION, KNOWN_ROOM_VERSIONS
19+
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
2020
from synapse.http.servlet import RestServlet
2121

2222
from ._base import client_v2_patterns
@@ -36,6 +36,7 @@ def __init__(self, hs):
3636
"""
3737
super(CapabilitiesRestServlet, self).__init__()
3838
self.hs = hs
39+
self.config = hs.config
3940
self.auth = hs.get_auth()
4041
self.store = hs.get_datastore()
4142

@@ -48,7 +49,7 @@ def on_GET(self, request):
4849
response = {
4950
"capabilities": {
5051
"m.room_versions": {
51-
"default": DEFAULT_ROOM_VERSION.identifier,
52+
"default": self.config.default_room_version.identifier,
5253
"available": {
5354
v.identifier: v.disposition
5455
for v in KNOWN_ROOM_VERSIONS.values()

tests/rest/client/v2_alpha/test_capabilities.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import synapse.rest.admin
16-
from synapse.api.room_versions import DEFAULT_ROOM_VERSION, KNOWN_ROOM_VERSIONS
16+
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
1717
from synapse.rest.client.v1 import login
1818
from synapse.rest.client.v2_alpha import capabilities
1919

@@ -32,6 +32,7 @@ def make_homeserver(self, reactor, clock):
3232
self.url = b"/_matrix/client/r0/capabilities"
3333
hs = self.setup_test_homeserver()
3434
self.store = hs.get_datastore()
35+
self.config = hs.config
3536
return hs
3637

3738
def test_check_auth_required(self):
@@ -51,8 +52,10 @@ def test_get_room_version_capabilities(self):
5152
self.assertEqual(channel.code, 200)
5253
for room_version in capabilities['m.room_versions']['available'].keys():
5354
self.assertTrue(room_version in KNOWN_ROOM_VERSIONS, "" + room_version)
55+
5456
self.assertEqual(
55-
DEFAULT_ROOM_VERSION.identifier, capabilities['m.room_versions']['default']
57+
self.config.default_room_version.identifier,
58+
capabilities['m.room_versions']['default'],
5659
)
5760

5861
def test_get_change_password_capabilities(self):

0 commit comments

Comments
 (0)