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

Commit

Permalink
Neilj/fix autojoin (#4223)
Browse files Browse the repository at this point in the history
* Fix auto join failures for servers that require user consent

* Fix auto join failures for servers that require user consent
  • Loading branch information
neilisfragile authored and hawkowl committed Nov 28, 2018
1 parent 8ca53fb commit 7039ece
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/4223.bugfix
@@ -0,0 +1 @@
Fix auto join failures for servers that require user consent
23 changes: 21 additions & 2 deletions synapse/handlers/register.py
Expand Up @@ -217,7 +217,19 @@ def register(
user_id = None
token = None
attempts += 1
if not self.hs.config.user_consent_at_registration:
yield self._auto_join_rooms(user_id)

defer.returnValue((user_id, token))

@defer.inlineCallbacks
def _auto_join_rooms(self, user_id):
"""Automatically joins users to auto join rooms - creating the room in the first place
if the user is the first to be created.
Args:
user_id(str): The user to join
"""
# auto-join the user to any rooms we're supposed to dump them into
fake_requester = create_requester(user_id)

Expand All @@ -226,7 +238,6 @@ def register(
if self.hs.config.autocreate_auto_join_rooms:
count = yield self.store.count_all_users()
should_auto_create_rooms = count == 1

for r in self.hs.config.auto_join_rooms:
try:
if should_auto_create_rooms:
Expand Down Expand Up @@ -256,7 +267,15 @@ def register(
except Exception as e:
logger.error("Failed to join new user to %r: %r", r, e)

defer.returnValue((user_id, token))
@defer.inlineCallbacks
def post_consent_actions(self, user_id):
"""A series of registration actions that can only be carried out once consent
has been granted
Args:
user_id (str): The user to join
"""
yield self._auto_join_rooms(user_id)

@defer.inlineCallbacks
def appservice_register(self, user_localpart, as_token):
Expand Down
1 change: 1 addition & 0 deletions synapse/rest/client/v2_alpha/register.py
Expand Up @@ -457,6 +457,7 @@ def on_POST(self, request):
yield self.store.user_set_consent_version(
registered_user_id, self.hs.config.user_consent_version,
)
yield self.registration_handler.post_consent_actions(registered_user_id)

defer.returnValue((200, return_dict))

Expand Down
2 changes: 2 additions & 0 deletions synapse/rest/consent/consent_resource.py
Expand Up @@ -89,6 +89,7 @@ def __init__(self, hs):

self.hs = hs
self.store = hs.get_datastore()
self.registration_handler = hs.get_handlers().registration_handler

# this is required by the request_handler wrapper
self.clock = hs.get_clock()
Expand Down Expand Up @@ -199,6 +200,7 @@ def _async_render_POST(self, request):
if e.code != 404:
raise
raise NotFoundError("Unknown user")
yield self.registration_handler.post_consent_actions(qualified_user_id)

try:
self._render_template(request, "success.html")
Expand Down
12 changes: 11 additions & 1 deletion tests/handlers/test_register.py
Expand Up @@ -150,7 +150,6 @@ def test_auto_create_auto_join_rooms(self):
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])

directory_handler = self.hs.get_handlers().directory_handler
room_alias = RoomAlias.from_string(room_alias_str)
room_id = yield directory_handler.get_association(room_alias)
Expand Down Expand Up @@ -184,3 +183,14 @@ def test_auto_create_auto_join_where_auto_create_is_false(self):
res = yield self.handler.register(localpart='jeff')
rooms = yield self.store.get_rooms_for_user(res[0])
self.assertEqual(len(rooms), 0)

@defer.inlineCallbacks
def test_auto_create_auto_join_where_no_consent(self):
self.hs.config.user_consent_at_registration = True
self.hs.config.block_events_without_consent_error = "Error"
room_alias_str = "#room:test"
self.hs.config.auto_join_rooms = [room_alias_str]
res = yield self.handler.register(localpart='jeff')
yield self.handler.post_consent_actions(res[0])
rooms = yield self.store.get_rooms_for_user(res[0])
self.assertEqual(len(rooms), 0)

0 comments on commit 7039ece

Please sign in to comment.