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

Clean up room alias creation #4063

Merged
merged 2 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4063.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor room alias creation code
77 changes: 47 additions & 30 deletions synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,42 +80,60 @@ def _create_association(self, room_alias, room_id, servers=None, creator=None):
)

@defer.inlineCallbacks
def create_association(self, user_id, room_alias, room_id, servers=None):
# association creation for human users
# TODO(erikj): Do user auth.
def create_association(self, requester, room_alias, room_id, servers=None,
send_event=True):
"""Attempt to create a new alias

if not self.spam_checker.user_may_create_room_alias(user_id, room_alias):
raise SynapseError(
403, "This user is not permitted to create this alias",
)
Args:
requester (Requester)
room_alias (RoomAlias)
room_id (str)
servers (list[str]|None): List of servers that others servers
should try and join via
send_event (bool): Whether to send an updated m.room.aliases event

can_create = yield self.can_modify_alias(
room_alias,
user_id=user_id
)
if not can_create:
raise SynapseError(
400, "This alias is reserved by an application service.",
errcode=Codes.EXCLUSIVE
)
yield self._create_association(room_alias, room_id, servers, creator=user_id)
Returns:
Deferred
"""

@defer.inlineCallbacks
def create_appservice_association(self, service, room_alias, room_id,
servers=None):
if not service.is_interested_in_alias(room_alias.to_string()):
raise SynapseError(
400, "This application service has not reserved"
" this kind of alias.", errcode=Codes.EXCLUSIVE
user_id = requester.user.to_string()

service = requester.app_service
if service:
if not service.is_interested_in_alias(room_alias.to_string()):
raise SynapseError(
400, "This application service has not reserved"
" this kind of alias.", errcode=Codes.EXCLUSIVE
)
else:
if not self.spam_checker.user_may_create_room_alias(user_id, room_alias):
raise AuthError(
403, "This user is not permitted to create this alias",
)

can_create = yield self.can_modify_alias(
room_alias,
user_id=user_id
)
if not can_create:
raise AuthError(
400, "This alias is reserved by an application service.",
errcode=Codes.EXCLUSIVE
)

# association creation for app services
yield self._create_association(room_alias, room_id, servers)
yield self._create_association(room_alias, room_id, servers, creator=user_id)
if send_event:
Copy link
Member

Choose a reason for hiding this comment

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

I'm not entirely convinced that having this in here is helpful, but whatever

yield self.send_room_alias_update_event(
requester,
room_id
)

@defer.inlineCallbacks
def delete_association(self, requester, user_id, room_alias):
def delete_association(self, requester, room_alias):
# association deletion for human users

user_id = requester.user.to_string()

try:
can_delete = yield self._user_can_delete_alias(room_alias, user_id)
except StoreError as e:
Expand Down Expand Up @@ -143,7 +161,6 @@ def delete_association(self, requester, user_id, room_alias):
try:
yield self.send_room_alias_update_event(
requester,
requester.user.to_string(),
room_id
)

Expand Down Expand Up @@ -261,7 +278,7 @@ def on_directory_query(self, args):
)

@defer.inlineCallbacks
def send_room_alias_update_event(self, requester, user_id, room_id):
def send_room_alias_update_event(self, requester, room_id):
aliases = yield self.store.get_aliases_for_room(room_id)

yield self.event_creation_handler.create_and_send_nonmember_event(
Expand All @@ -270,7 +287,7 @@ def send_room_alias_update_event(self, requester, user_id, room_id):
"type": EventTypes.Aliases,
"state_key": self.hs.hostname,
"room_id": room_id,
"sender": user_id,
"sender": requester.user.to_string(),
"content": {"aliases": aliases},
},
ratelimit=False
Expand Down
5 changes: 3 additions & 2 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ def create_room(self, requester, config, ratelimit=True,
if room_alias:
directory_handler = self.hs.get_handlers().directory_handler
yield directory_handler.create_association(
user_id=user_id,
requester=requester,
room_id=room_id,
room_alias=room_alias,
servers=[self.hs.hostname],
send_event=False,
)

preset_config = config.get(
Expand Down Expand Up @@ -289,7 +290,7 @@ def create_room(self, requester, config, ratelimit=True,
if room_alias:
result["room_alias"] = room_alias.to_string()
yield directory_handler.send_room_alias_update_event(
requester, user_id, room_id
requester, room_id
)

defer.returnValue(result)
Expand Down
37 changes: 5 additions & 32 deletions synapse/rest/client/v1/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,11 @@ def on_PUT(self, request, room_alias):
if room is None:
raise SynapseError(400, "Room does not exist")

dir_handler = self.handlers.directory_handler
requester = yield self.auth.get_user_by_req(request)

try:
# try to auth as a user
requester = yield self.auth.get_user_by_req(request)
try:
user_id = requester.user.to_string()
yield dir_handler.create_association(
user_id, room_alias, room_id, servers
)
yield dir_handler.send_room_alias_update_event(
requester,
user_id,
room_id
)
except SynapseError as e:
raise e
except Exception:
logger.exception("Failed to create association")
raise
except AuthError:
# try to auth as an application service
service = yield self.auth.get_appservice_by_req(request)
yield dir_handler.create_appservice_association(
service, room_alias, room_id, servers
)
logger.info(
"Application service at %s created alias %s pointing to %s",
service.url,
room_alias.to_string(),
room_id
)
yield self.handlers.directory_handler.create_association(
requester, room_alias, room_id, servers
)

defer.returnValue((200, {}))

Expand Down Expand Up @@ -135,7 +108,7 @@ def on_DELETE(self, request, room_alias):
room_alias = RoomAlias.from_string(room_alias)

yield dir_handler.delete_association(
requester, user.to_string(), room_alias
requester, room_alias
)

logger.info(
Expand Down