-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Conversation
synapse/handlers/register.py
Outdated
# try to create the room if we're the first user on the server | ||
if self.hs.config.autocreate_auto_join_rooms: | ||
count = yield self.store.count_all_users() | ||
if count == 1 and RoomAlias.is_valid(room_identifier): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it feels very random to be doing this at this point. (Which is when the first user is registered? when they log in?). what if two users register at once? what if the first user didn't qualify for autojoining for some reason?
Can we not do this when the HS first starts, somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the only way to do it at start would be if we also autocreated an admin user, which in turn then means shared-secret registering them and then setting up their profile and 3pid and passwords etc correctly. agreed this would be cleaner, but this is intended as a fast fix to avoid having to do that in the provisioning process, on the basis that the first user who signs in is 99% always going to be the server admin, and if it isn't, then you can always repoint the alias.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, fair enough, but still, having _join_user_to_room
magically do something different if there is now exactly one registered user feels very magical.
can we pull out the is this the first user
logic to register
(where we only have to do it once per user, rather than once per user per room)? and have a separate _create_auto_join_room
method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and have a separate _create_auto_join_room method
or just call room_creation_handler.create_room
directly from the for r in self.hs.config.auto_join_rooms
loop in register
.
synapse/handlers/register.py
Outdated
) | ||
room_id = info["room_id"] | ||
|
||
directory_handler = self.hs.get_handlers().directory_handler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we not do the alias bits by passing room_alias_name
to create_room
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, possibly.
Also: we're not happy with this going in with zero tests to make sure it doesn't suddenly break in the future. It's going to be hard to sytest, but please can it have some unit tests to run the code? |
before I go rewrite this, it'd be good to get aligned on the proposed behaviour in general; the current implementation was a quick fix i suggested to @neilisfragile so we could get this feature out quickly rather than getting entangled with complicated provisioning/auth rejigs. |
What is |
HHS === modular.im |
Need to think about gdpr consent and ability to create rooms, could be dependent on element-hq/element-web#7168 |
Blocked on #4004 |
Blocked how? This and #4004 are unrelated changes |
It's currently not possible to create the room prior to consent, so the room is never created on GDPR compliant servers. I suppose you could instead tie room creation into the first user to consent, but given you are already working on moving consent earlier in the flow it seems sensible just to wait for it to land then this PR will magically work. |
changelog.d/3975.feature
Outdated
@@ -0,0 +1 @@ | |||
First user should autocreate autojoin rooms |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you expand on this a bit so that it makes sense for users, please?
synapse/config/registration.py
Outdated
@@ -98,6 +99,9 @@ def default_config(self, **kwargs): | |||
# to these rooms | |||
#auto_join_rooms: | |||
# - "#example:example.com" | |||
|
|||
# Have first user on server autocreate autojoin rooms |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe explain what will happen if it is false, and conversely what happens if it is true?
synapse/handlers/register.py
Outdated
for r in self.hs.config.auto_join_rooms: | ||
try: | ||
if auto_create_rooms and RoomAlias.is_valid(r): | ||
room_creation_handler = self.hs.get_room_creation_handler() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you put this in the constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first part needs to be on a request basis - I guess the self.hs.get_room_creation_handler() could but this is the only place it is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's the only place it's used, but we prefer to get the handlers in the constructors where possible, so that we detect problems earlier.
synapse/handlers/register.py
Outdated
for r in self.hs.config.auto_join_rooms: | ||
try: | ||
if auto_create_rooms and RoomAlias.is_valid(r): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the alias is invalid, we silently ignore it? If you're going to sanity check it, put the sanity check in the config module, and raise a ConfigError if it's wrong.
synapse/handlers/register.py
Outdated
# try to create the room if we're the first user on the server | ||
if self.hs.config.autocreate_auto_join_rooms: | ||
count = yield self.store.count_all_users() | ||
auto_create_rooms = count == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to initialise auto_create_rooms
for the case where autocreate_auto_join_rooms
is False.
synapse/handlers/register.py
Outdated
@@ -513,6 +531,7 @@ def get_or_register_3pid_guest(self, medium, address, inviter_user_id): | |||
|
|||
@defer.inlineCallbacks | |||
def _join_user_to_room(self, requester, room_identifier): | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please no
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was oversight I promise!
tests/handlers/test_register.py
Outdated
) | ||
self.macaroon_generator = Mock( | ||
generate_access_token=Mock(return_value='secret') | ||
) | ||
self.hs.get_macaroon_generator = Mock(return_value=self.macaroon_generator) | ||
self.hs.handlers = RegistrationHandlers(self.hs) | ||
# self.hs.handlers = RegistrationHandlers(self.hs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it's redundant, remove it, don't comment it out
tests/handlers/test_register.py
Outdated
@defer.inlineCallbacks | ||
def test_auto_create_auto_join_rooms(self): | ||
room_alias_str = "#room:test" | ||
self.hs.config.autocreate_auto_join_rooms = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems to be the default; you could argue it should be set here for certainty but in that case let's have a comment. else just remove it
self.assertEqual(len(rooms), 1) | ||
|
||
@defer.inlineCallbacks | ||
def test_auto_create_auto_join_rooms_with_no_rooms(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also have a UT with autocreate_auto_join_rooms=False
please?
Could portions of this be used to auto-create a room for each user? Useful for creating notes channels, etc. for workflow-specific applications. |
changelog.d/3975.feature
Outdated
@@ -1 +1 @@ | |||
First user should autocreate autojoin rooms | |||
Servers with auto join rooms, should autocreate those rooms when first user registers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/auto join/auto-join/
s/should/will now/
s/first user/the first user/
possibly, s/autocreate/automatically create/
synapse/config/registration.py
Outdated
@@ -15,6 +15,8 @@ | |||
|
|||
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 ._base import Config |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it'd be nice to merge this into line 18 while you're at it (absolute imports are preferred over relative ones)
synapse/config/registration.py
Outdated
@@ -44,6 +46,9 @@ def read_config(self, config): | |||
) | |||
|
|||
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generally prefer % (room_alias, )
in case room_alias
turns out to be a tuple
synapse/config/registration.py
Outdated
@@ -100,7 +105,11 @@ def default_config(self, **kwargs): | |||
#auto_join_rooms: | |||
# - "#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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/exists/exist/
synapse/config/registration.py
Outdated
# 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/auto joined/auto-joined/
synapse/handlers/register.py
Outdated
for r in self.hs.config.auto_join_rooms: | ||
try: | ||
if auto_create_rooms and RoomAlias.is_valid(r): | ||
room_creation_handler = self.hs.get_room_creation_handler() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's the only place it's used, but we prefer to get the handlers in the constructors where possible, so that we detect problems earlier.
synapse/handlers/register.py
Outdated
@@ -26,6 +26,7 @@ | |||
RegistrationError, | |||
SynapseError, | |||
) | |||
from synapse.config._base import ConfigError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you import this from synapse.config
rather than the private _base
module, please? (not that it's really appropriate to be raising ConfigErrors at runtime imho)
synapse/handlers/register.py
Outdated
room_creation_handler = self.hs.get_room_creation_handler() | ||
if self.hs.hostname != RoomAlias.from_string(r).domain: | ||
raise ConfigError( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and if we can't check it during startup, it's probably more appropriate to warn and move on than to 500 reject the whole registration.
synapse/handlers/register.py
Outdated
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' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is missing its argument too.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
erm, why does this not throw an exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bare except in the try statement
Have since removed the exception as per comments above
changelog.d/3975.feature
Outdated
@@ -1 +1 @@ | |||
Servers with auto join rooms, should autocreate those rooms when first user registers | |||
Servers with auto-join rooms, should automatically create those rooms when first user registers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still some problems here:
s/, should/will now/
s/first user/the first user/
synapse/config/registration.py
Outdated
from synapse.types import RoomAlias | ||
from synapse.util.stringutils import random_string_with_symbols | ||
|
||
from ._base import Config | ||
from ._base import Config, ConfigError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, absolute imports are preferred over relative ones. Please s/._base/synapse.config._base/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, misunderstood you
synapse/handlers/register.py
Outdated
'Cannot create room alias %s, ' | ||
'it does not match server domain' % (r,) | ||
) | ||
raise SynapseError() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we raising an exception here (and why does it have no message, but that's a side-issue)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is brain dead, sorry
synapse/handlers/register.py
Outdated
if self.hs.hostname != RoomAlias.from_string(r).domain: | ||
raise ConfigError( | ||
'Cannot create room alias %s, it does not match server domain' | ||
logger.warn( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fwiw (just a nit fyi): logger.warn
is deprecated, prefer logger.warning
synapse/handlers/register.py
Outdated
'Cannot create room alias %s, it does not match server domain' | ||
logger.warn( | ||
'Cannot create room alias %s, ' | ||
'it does not match server domain' % (r,) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the logger methods automatically interpolate their arguments (with the benefit of doing it after checking if logging is enabled), so, you can write:
logger.warn('message with %s', r)
rather than using % explicitly
synapse/handlers/register.py
Outdated
@@ -231,21 +231,23 @@ def register( | |||
for r in self.hs.config.auto_join_rooms: | |||
try: | |||
if should_auto_create_rooms: | |||
room_creation_handler = self.hs.get_room_creation_handler() | |||
if self.hs.hostname != RoomAlias.from_string(r).domain: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be nice to do RoomAlias.from_string(r)
once and use a local var, rather than twice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm other than the nits below. Also your CI is failing.
Co-Authored-By: neilisfragile <[email protected]>
Co-Authored-By: neilisfragile <[email protected]>
…autocreate_autojoin
Needed for HHS.