mirror of
https://mau.dev/maunium/synapse.git
synced 2024-12-13 17:23:21 +01:00
improve auto room join logic, comments and tests
This commit is contained in:
parent
ed82043efb
commit
a2bfb778c8
4 changed files with 37 additions and 10 deletions
|
@ -1 +1 @@
|
||||||
First user should autocreate autojoin rooms
|
Servers with auto join rooms, should autocreate those rooms when first user registers
|
||||||
|
|
|
@ -15,6 +15,8 @@
|
||||||
|
|
||||||
from distutils.util import strtobool
|
from distutils.util import strtobool
|
||||||
|
|
||||||
|
from synapse.config._base import ConfigError
|
||||||
|
from synapse.types import RoomAlias
|
||||||
from synapse.util.stringutils import random_string_with_symbols
|
from synapse.util.stringutils import random_string_with_symbols
|
||||||
|
|
||||||
from ._base import Config
|
from ._base import Config
|
||||||
|
@ -44,6 +46,9 @@ class RegistrationConfig(Config):
|
||||||
)
|
)
|
||||||
|
|
||||||
self.auto_join_rooms = config.get("auto_join_rooms", [])
|
self.auto_join_rooms = config.get("auto_join_rooms", [])
|
||||||
|
for room_alias in self.auto_join_rooms:
|
||||||
|
if not RoomAlias.is_valid(room_alias):
|
||||||
|
raise ConfigError('Invalid auto_join_rooms entry %s' % room_alias)
|
||||||
self.autocreate_auto_join_rooms = config.get("autocreate_auto_join_rooms", True)
|
self.autocreate_auto_join_rooms = config.get("autocreate_auto_join_rooms", True)
|
||||||
|
|
||||||
def default_config(self, **kwargs):
|
def default_config(self, **kwargs):
|
||||||
|
@ -100,7 +105,11 @@ class RegistrationConfig(Config):
|
||||||
#auto_join_rooms:
|
#auto_join_rooms:
|
||||||
# - "#example:example.com"
|
# - "#example:example.com"
|
||||||
|
|
||||||
# Have first user on server autocreate autojoin rooms
|
# Where auto_join_rooms are specified, setting this flag ensures that the
|
||||||
|
# the rooms exists by creating them when the first user on the
|
||||||
|
# homeserver registers.
|
||||||
|
# Setting to false means that if the rooms are not manually created,
|
||||||
|
# users cannot be auto joined since they do not exist.
|
||||||
autocreate_auto_join_rooms: true
|
autocreate_auto_join_rooms: true
|
||||||
""" % locals()
|
""" % locals()
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ from synapse.api.errors import (
|
||||||
RegistrationError,
|
RegistrationError,
|
||||||
SynapseError,
|
SynapseError,
|
||||||
)
|
)
|
||||||
|
from synapse.config._base import ConfigError
|
||||||
from synapse.http.client import CaptchaServerHttpClient
|
from synapse.http.client import CaptchaServerHttpClient
|
||||||
from synapse.types import RoomAlias, RoomID, UserID, create_requester
|
from synapse.types import RoomAlias, RoomID, UserID, create_requester
|
||||||
from synapse.util.async_helpers import Linearizer
|
from synapse.util.async_helpers import Linearizer
|
||||||
|
@ -222,14 +223,19 @@ class RegistrationHandler(BaseHandler):
|
||||||
fake_requester = create_requester(user_id)
|
fake_requester = create_requester(user_id)
|
||||||
|
|
||||||
# try to create the room if we're the first user on the server
|
# try to create the room if we're the first user on the server
|
||||||
|
should_auto_create_rooms = False
|
||||||
if self.hs.config.autocreate_auto_join_rooms:
|
if self.hs.config.autocreate_auto_join_rooms:
|
||||||
count = yield self.store.count_all_users()
|
count = yield self.store.count_all_users()
|
||||||
auto_create_rooms = count == 1
|
should_auto_create_rooms = count == 1
|
||||||
|
|
||||||
for r in self.hs.config.auto_join_rooms:
|
for r in self.hs.config.auto_join_rooms:
|
||||||
try:
|
try:
|
||||||
if auto_create_rooms and RoomAlias.is_valid(r):
|
if should_auto_create_rooms:
|
||||||
room_creation_handler = self.hs.get_room_creation_handler()
|
room_creation_handler = self.hs.get_room_creation_handler()
|
||||||
|
if self.hs.hostname != RoomAlias.from_string(r).domain:
|
||||||
|
raise ConfigError(
|
||||||
|
'Cannot create room alias %s, it does not match server domain'
|
||||||
|
)
|
||||||
# create room expects the localpart of the room alias
|
# create room expects the localpart of the room alias
|
||||||
room_alias_localpart = RoomAlias.from_string(r).localpart
|
room_alias_localpart = RoomAlias.from_string(r).localpart
|
||||||
yield room_creation_handler.create_room(
|
yield room_creation_handler.create_room(
|
||||||
|
@ -531,7 +537,6 @@ class RegistrationHandler(BaseHandler):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _join_user_to_room(self, requester, room_identifier):
|
def _join_user_to_room(self, requester, room_identifier):
|
||||||
|
|
||||||
room_id = None
|
room_id = None
|
||||||
room_member_handler = self.hs.get_room_member_handler()
|
room_member_handler = self.hs.get_room_member_handler()
|
||||||
if RoomID.is_valid(room_identifier):
|
if RoomID.is_valid(room_identifier):
|
||||||
|
|
|
@ -47,7 +47,6 @@ class RegistrationTestCase(unittest.TestCase):
|
||||||
generate_access_token=Mock(return_value='secret')
|
generate_access_token=Mock(return_value='secret')
|
||||||
)
|
)
|
||||||
self.hs.get_macaroon_generator = Mock(return_value=self.macaroon_generator)
|
self.hs.get_macaroon_generator = Mock(return_value=self.macaroon_generator)
|
||||||
# self.hs.handlers = RegistrationHandlers(self.hs)
|
|
||||||
self.handler = self.hs.get_handlers().registration_handler
|
self.handler = self.hs.get_handlers().registration_handler
|
||||||
self.store = self.hs.get_datastore()
|
self.store = self.hs.get_datastore()
|
||||||
self.hs.config.max_mau_value = 50
|
self.hs.config.max_mau_value = 50
|
||||||
|
@ -148,9 +147,7 @@ class RegistrationTestCase(unittest.TestCase):
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_auto_create_auto_join_rooms(self):
|
def test_auto_create_auto_join_rooms(self):
|
||||||
room_alias_str = "#room:test"
|
room_alias_str = "#room:test"
|
||||||
self.hs.config.autocreate_auto_join_rooms = True
|
|
||||||
self.hs.config.auto_join_rooms = [room_alias_str]
|
self.hs.config.auto_join_rooms = [room_alias_str]
|
||||||
|
|
||||||
res = yield self.handler.register(localpart='jeff')
|
res = yield self.handler.register(localpart='jeff')
|
||||||
rooms = yield self.store.get_rooms_for_user(res[0])
|
rooms = yield self.store.get_rooms_for_user(res[0])
|
||||||
|
|
||||||
|
@ -163,11 +160,27 @@ class RegistrationTestCase(unittest.TestCase):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_auto_create_auto_join_rooms_with_no_rooms(self):
|
def test_auto_create_auto_join_rooms_with_no_rooms(self):
|
||||||
self.hs.config.autocreate_auto_join_rooms = True
|
|
||||||
self.hs.config.auto_join_rooms = []
|
self.hs.config.auto_join_rooms = []
|
||||||
frank = UserID.from_string("@frank:test")
|
frank = UserID.from_string("@frank:test")
|
||||||
res = yield self.handler.register(frank.localpart)
|
res = yield self.handler.register(frank.localpart)
|
||||||
self.assertEqual(res[0], frank.to_string())
|
self.assertEqual(res[0], frank.to_string())
|
||||||
rooms = yield self.store.get_rooms_for_user(res[0])
|
rooms = yield self.store.get_rooms_for_user(res[0])
|
||||||
|
self.assertEqual(len(rooms), 0)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def test_auto_create_auto_join_where_room_is_another_domain(self):
|
||||||
|
self.hs.config.auto_join_rooms = ["#room:another"]
|
||||||
|
frank = UserID.from_string("@frank:test")
|
||||||
|
res = yield self.handler.register(frank.localpart)
|
||||||
|
self.assertEqual(res[0], frank.to_string())
|
||||||
|
rooms = yield self.store.get_rooms_for_user(res[0])
|
||||||
|
self.assertEqual(len(rooms), 0)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def test_auto_create_auto_join_where_auto_create_is_false(self):
|
||||||
|
self.hs.config.autocreate_auto_join_rooms = False
|
||||||
|
room_alias_str = "#room:test"
|
||||||
|
self.hs.config.auto_join_rooms = [room_alias_str]
|
||||||
|
res = yield self.handler.register(localpart='jeff')
|
||||||
|
rooms = yield self.store.get_rooms_for_user(res[0])
|
||||||
self.assertEqual(len(rooms), 0)
|
self.assertEqual(len(rooms), 0)
|
||||||
|
|
Loading…
Reference in a new issue