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

Add config option for setting homeserver's default room version #5223

Merged
merged 16 commits into from May 23, 2019
1 change: 1 addition & 0 deletions changelog.d/5223.feature
@@ -0,0 +1 @@
Ability to configure default room version.
9 changes: 9 additions & 0 deletions docs/sample_config.yaml
Expand Up @@ -83,6 +83,15 @@ pid_file: DATADIR/homeserver.pid
#
#restrict_public_rooms_to_local_users: true

# The default room version for newly created rooms.
#
# Known room versions are listed here:
# https://matrix.org/docs/spec/#complete-list-of-room-versions
#
# For example, for room version 1, default_room_version should be set
# to "1".
#default_room_version: "1"

# The GC threshold parameters to pass to `gc.set_threshold`, if defined
#
#gc_thresholds: [700, 10, 10]
Expand Down
4 changes: 0 additions & 4 deletions synapse/api/room_versions.py
Expand Up @@ -85,10 +85,6 @@ class RoomVersions(object):
)


# the version we will give rooms which are created on this server
DEFAULT_ROOM_VERSION = RoomVersions.V1


KNOWN_ROOM_VERSIONS = {
v.identifier: v for v in (
RoomVersions.V1,
Expand Down
32 changes: 32 additions & 0 deletions synapse/config/server.py
Expand Up @@ -20,6 +20,7 @@

from netaddr import IPSet

from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
from synapse.http.endpoint import parse_and_validate_server_name
from synapse.python_dependencies import DependencyException, check_requirements

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

DEFAULT_ROOM_VERSION = "1"


class ServerConfig(Config):

Expand Down Expand Up @@ -88,6 +91,22 @@ def read_config(self, config):
"restrict_public_rooms_to_local_users", False,
)

default_room_version = config.get(
"default_room_version", DEFAULT_ROOM_VERSION,
)

# Ensure room version is a str
default_room_version = str(default_room_version)

if default_room_version not in KNOWN_ROOM_VERSIONS:
raise ConfigError(
"Unknown default_room_version: %s, known room versions: %s" %
(default_room_version, list(KNOWN_ROOM_VERSIONS.keys()))
)

# Get the actual room version object rather than just the identifier
self.default_room_version = KNOWN_ROOM_VERSIONS[default_room_version]

# whether to enable search. If disabled, new entries will not be inserted
# into the search tables and they will not be indexed. Users will receive
# errors when attempting to search for messages.
Expand Down Expand Up @@ -310,6 +329,10 @@ def default_config(self, server_name, data_dir_path, **kwargs):
unsecure_port = 8008

pid_file = os.path.join(data_dir_path, "homeserver.pid")

# Bring DEFAULT_ROOM_VERSION into the local-scope for use in the
# default config string
default_room_version = DEFAULT_ROOM_VERSION
return """\
## Server ##

Expand Down Expand Up @@ -384,6 +407,15 @@ def default_config(self, server_name, data_dir_path, **kwargs):
#
#restrict_public_rooms_to_local_users: true

# The default room version for newly created rooms.
#
# Known room versions are listed here:
# https://matrix.org/docs/spec/#complete-list-of-room-versions
#
# For example, for room version 1, default_room_version should be set
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, duh?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never hurts to be too user-friendly.

# to "1".
#default_room_version: "%(default_room_version)s"

# The GC threshold parameters to pass to `gc.set_threshold`, if defined
#
#gc_thresholds: [700, 10, 10]
Expand Down
9 changes: 7 additions & 2 deletions synapse/handlers/room.py
Expand Up @@ -27,7 +27,7 @@

from synapse.api.constants import EventTypes, JoinRules, RoomCreationPreset
from synapse.api.errors import AuthError, Codes, NotFoundError, StoreError, SynapseError
from synapse.api.room_versions import DEFAULT_ROOM_VERSION, KNOWN_ROOM_VERSIONS
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
from synapse.storage.state import StateFilter
from synapse.types import RoomAlias, RoomID, RoomStreamToken, StreamToken, UserID
from synapse.util import stringutils
Expand Down Expand Up @@ -70,6 +70,7 @@ def __init__(self, hs):
self.spam_checker = hs.get_spam_checker()
self.event_creation_handler = hs.get_event_creation_handler()
self.room_member_handler = hs.get_room_member_handler()
self.config = hs.config

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

room_version = config.get("room_version", DEFAULT_ROOM_VERSION.identifier)
room_version = config.get(
"room_version",
self.config.default_room_version.identifier,
)

if not isinstance(room_version, string_types):
raise SynapseError(
400,
Expand Down
5 changes: 3 additions & 2 deletions synapse/rest/client/v2_alpha/capabilities.py
Expand Up @@ -16,7 +16,7 @@

from twisted.internet import defer

from synapse.api.room_versions import DEFAULT_ROOM_VERSION, KNOWN_ROOM_VERSIONS
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
from synapse.http.servlet import RestServlet

from ._base import client_v2_patterns
Expand All @@ -36,6 +36,7 @@ def __init__(self, hs):
"""
super(CapabilitiesRestServlet, self).__init__()
self.hs = hs
self.config = hs.config
self.auth = hs.get_auth()
self.store = hs.get_datastore()

Expand All @@ -48,7 +49,7 @@ def on_GET(self, request):
response = {
"capabilities": {
"m.room_versions": {
"default": DEFAULT_ROOM_VERSION.identifier,
"default": self.config.default_room_version.identifier,
"available": {
v.identifier: v.disposition
for v in KNOWN_ROOM_VERSIONS.values()
Expand Down
30 changes: 30 additions & 0 deletions synapse/util/dictutils.py
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Copyright 2019 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def merge_dicts(*dicts):
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
"""Merge multiple dictionaries together and return the result.

Args:
dicts: Variable list of dictionary arguments.

Returns:
Dict: A single dictionary as a result of merging the provided
dictionaries.
"""
merged = {}
for d in dicts:
merged.update(d)
return merged
7 changes: 5 additions & 2 deletions tests/rest/client/v2_alpha/test_capabilities.py
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import synapse.rest.admin
from synapse.api.room_versions import DEFAULT_ROOM_VERSION, KNOWN_ROOM_VERSIONS
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
from synapse.rest.client.v1 import login
from synapse.rest.client.v2_alpha import capabilities

Expand All @@ -32,6 +32,7 @@ def make_homeserver(self, reactor, clock):
self.url = b"/_matrix/client/r0/capabilities"
hs = self.setup_test_homeserver()
self.store = hs.get_datastore()
self.config = hs.config
return hs

def test_check_auth_required(self):
Expand All @@ -51,8 +52,10 @@ def test_get_room_version_capabilities(self):
self.assertEqual(channel.code, 200)
for room_version in capabilities['m.room_versions']['available'].keys():
self.assertTrue(room_version in KNOWN_ROOM_VERSIONS, "" + room_version)

self.assertEqual(
DEFAULT_ROOM_VERSION.identifier, capabilities['m.room_versions']['default']
self.config.default_room_version.identifier,
capabilities['m.room_versions']['default'],
)

def test_get_change_password_capabilities(self):
Expand Down