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.
pull/5249/head
Andrew Morgan 2019-05-23 15:00:20 +01:00 committed by GitHub
parent b75537beaf
commit 6368150a74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 10 deletions

1
changelog.d/5223.feature Normal file
View File

@ -0,0 +1 @@
Ability to configure default room version.

View File

@ -83,6 +83,15 @@ pid_file: DATADIR/homeserver.pid
# #
#restrict_public_rooms_to_local_users: true #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 # The GC threshold parameters to pass to `gc.set_threshold`, if defined
# #
#gc_thresholds: [700, 10, 10] #gc_thresholds: [700, 10, 10]

View File

@ -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 = { KNOWN_ROOM_VERSIONS = {
v.identifier: v for v in ( v.identifier: v for v in (
RoomVersions.V1, RoomVersions.V1,

View File

@ -20,6 +20,7 @@ import os.path
from netaddr import IPSet 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.http.endpoint import parse_and_validate_server_name
from synapse.python_dependencies import DependencyException, check_requirements from synapse.python_dependencies import DependencyException, check_requirements
@ -35,6 +36,8 @@ logger = logging.Logger(__name__)
# in the list. # in the list.
DEFAULT_BIND_ADDRESSES = ['::', '0.0.0.0'] DEFAULT_BIND_ADDRESSES = ['::', '0.0.0.0']
DEFAULT_ROOM_VERSION = "1"
class ServerConfig(Config): class ServerConfig(Config):
@ -88,6 +91,22 @@ class ServerConfig(Config):
"restrict_public_rooms_to_local_users", False, "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 # 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 # into the search tables and they will not be indexed. Users will receive
# errors when attempting to search for messages. # errors when attempting to search for messages.
@ -310,6 +329,10 @@ class ServerConfig(Config):
unsecure_port = 8008 unsecure_port = 8008
pid_file = os.path.join(data_dir_path, "homeserver.pid") 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 """\ return """\
## Server ## ## Server ##
@ -384,6 +407,15 @@ class ServerConfig(Config):
# #
#restrict_public_rooms_to_local_users: true #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: "%(default_room_version)s"
# The GC threshold parameters to pass to `gc.set_threshold`, if defined # The GC threshold parameters to pass to `gc.set_threshold`, if defined
# #
#gc_thresholds: [700, 10, 10] #gc_thresholds: [700, 10, 10]

View File

@ -27,7 +27,7 @@ from twisted.internet import defer
from synapse.api.constants import EventTypes, JoinRules, RoomCreationPreset from synapse.api.constants import EventTypes, JoinRules, RoomCreationPreset
from synapse.api.errors import AuthError, Codes, NotFoundError, StoreError, SynapseError 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.storage.state import StateFilter
from synapse.types import RoomAlias, RoomID, RoomStreamToken, StreamToken, UserID from synapse.types import RoomAlias, RoomID, RoomStreamToken, StreamToken, UserID
from synapse.util import stringutils from synapse.util import stringutils
@ -70,6 +70,7 @@ class RoomCreationHandler(BaseHandler):
self.spam_checker = hs.get_spam_checker() self.spam_checker = hs.get_spam_checker()
self.event_creation_handler = hs.get_event_creation_handler() self.event_creation_handler = hs.get_event_creation_handler()
self.room_member_handler = hs.get_room_member_handler() self.room_member_handler = hs.get_room_member_handler()
self.config = hs.config
# linearizer to stop two upgrades happening at once # linearizer to stop two upgrades happening at once
self._upgrade_linearizer = Linearizer("room_upgrade_linearizer") self._upgrade_linearizer = Linearizer("room_upgrade_linearizer")
@ -475,7 +476,11 @@ class RoomCreationHandler(BaseHandler):
if ratelimit: if ratelimit:
yield self.ratelimit(requester) 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): if not isinstance(room_version, string_types):
raise SynapseError( raise SynapseError(
400, 400,

View File

@ -16,7 +16,7 @@ import logging
from twisted.internet import defer 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 synapse.http.servlet import RestServlet
from ._base import client_v2_patterns from ._base import client_v2_patterns
@ -36,6 +36,7 @@ class CapabilitiesRestServlet(RestServlet):
""" """
super(CapabilitiesRestServlet, self).__init__() super(CapabilitiesRestServlet, self).__init__()
self.hs = hs self.hs = hs
self.config = hs.config
self.auth = hs.get_auth() self.auth = hs.get_auth()
self.store = hs.get_datastore() self.store = hs.get_datastore()
@ -48,7 +49,7 @@ class CapabilitiesRestServlet(RestServlet):
response = { response = {
"capabilities": { "capabilities": {
"m.room_versions": { "m.room_versions": {
"default": DEFAULT_ROOM_VERSION.identifier, "default": self.config.default_room_version.identifier,
"available": { "available": {
v.identifier: v.disposition v.identifier: v.disposition
for v in KNOWN_ROOM_VERSIONS.values() for v in KNOWN_ROOM_VERSIONS.values()

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import synapse.rest.admin 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.v1 import login
from synapse.rest.client.v2_alpha import capabilities from synapse.rest.client.v2_alpha import capabilities
@ -32,6 +32,7 @@ class CapabilitiesTestCase(unittest.HomeserverTestCase):
self.url = b"/_matrix/client/r0/capabilities" self.url = b"/_matrix/client/r0/capabilities"
hs = self.setup_test_homeserver() hs = self.setup_test_homeserver()
self.store = hs.get_datastore() self.store = hs.get_datastore()
self.config = hs.config
return hs return hs
def test_check_auth_required(self): def test_check_auth_required(self):
@ -51,8 +52,10 @@ class CapabilitiesTestCase(unittest.HomeserverTestCase):
self.assertEqual(channel.code, 200) self.assertEqual(channel.code, 200)
for room_version in capabilities['m.room_versions']['available'].keys(): for room_version in capabilities['m.room_versions']['available'].keys():
self.assertTrue(room_version in KNOWN_ROOM_VERSIONS, "" + room_version) self.assertTrue(room_version in KNOWN_ROOM_VERSIONS, "" + room_version)
self.assertEqual( 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): def test_get_change_password_capabilities(self):