Clean up room alias creation

This commit is contained in:
Erik Johnston 2018-10-18 16:14:24 +01:00
parent 6190abe8da
commit 74e7617083
3 changed files with 55 additions and 64 deletions

View File

@ -80,12 +80,34 @@ class DirectoryHandler(BaseHandler):
) )
@defer.inlineCallbacks @defer.inlineCallbacks
def create_association(self, user_id, room_alias, room_id, servers=None): def create_association(self, requester, room_alias, room_id, servers=None,
# association creation for human users send_event=True):
# TODO(erikj): Do user auth. """Attempt to create a new alias
if not self.spam_checker.user_may_create_room_alias(user_id, room_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
Returns:
Deferred
"""
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( 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", 403, "This user is not permitted to create this alias",
) )
@ -94,28 +116,24 @@ class DirectoryHandler(BaseHandler):
user_id=user_id user_id=user_id
) )
if not can_create: if not can_create:
raise SynapseError( raise AuthError(
400, "This alias is reserved by an application service.", 400, "This alias is reserved by an application service.",
errcode=Codes.EXCLUSIVE errcode=Codes.EXCLUSIVE
) )
yield self._create_association(room_alias, room_id, servers, creator=user_id)
@defer.inlineCallbacks yield self._create_association(room_alias, room_id, servers, creator=user_id)
def create_appservice_association(self, service, room_alias, room_id, if send_event:
servers=None): yield self.send_room_alias_update_event(
if not service.is_interested_in_alias(room_alias.to_string()): requester,
raise SynapseError( room_id
400, "This application service has not reserved"
" this kind of alias.", errcode=Codes.EXCLUSIVE
) )
# association creation for app services
yield self._create_association(room_alias, room_id, servers)
@defer.inlineCallbacks @defer.inlineCallbacks
def delete_association(self, requester, user_id, room_alias): def delete_association(self, requester, room_alias):
# association deletion for human users # association deletion for human users
user_id = requester.user.to_string()
try: try:
can_delete = yield self._user_can_delete_alias(room_alias, user_id) can_delete = yield self._user_can_delete_alias(room_alias, user_id)
except StoreError as e: except StoreError as e:
@ -143,7 +161,6 @@ class DirectoryHandler(BaseHandler):
try: try:
yield self.send_room_alias_update_event( yield self.send_room_alias_update_event(
requester, requester,
requester.user.to_string(),
room_id room_id
) )
@ -261,7 +278,7 @@ class DirectoryHandler(BaseHandler):
) )
@defer.inlineCallbacks @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) aliases = yield self.store.get_aliases_for_room(room_id)
yield self.event_creation_handler.create_and_send_nonmember_event( yield self.event_creation_handler.create_and_send_nonmember_event(
@ -270,7 +287,7 @@ class DirectoryHandler(BaseHandler):
"type": EventTypes.Aliases, "type": EventTypes.Aliases,
"state_key": self.hs.hostname, "state_key": self.hs.hostname,
"room_id": room_id, "room_id": room_id,
"sender": user_id, "sender": requester.user.to_string(),
"content": {"aliases": aliases}, "content": {"aliases": aliases},
}, },
ratelimit=False ratelimit=False

View File

@ -190,10 +190,11 @@ class RoomCreationHandler(BaseHandler):
if room_alias: if room_alias:
directory_handler = self.hs.get_handlers().directory_handler directory_handler = self.hs.get_handlers().directory_handler
yield directory_handler.create_association( yield directory_handler.create_association(
user_id=user_id, requester=requester,
room_id=room_id, room_id=room_id,
room_alias=room_alias, room_alias=room_alias,
servers=[self.hs.hostname], servers=[self.hs.hostname],
send_event=False,
) )
preset_config = config.get( preset_config = config.get(
@ -289,7 +290,7 @@ class RoomCreationHandler(BaseHandler):
if room_alias: if room_alias:
result["room_alias"] = room_alias.to_string() result["room_alias"] = room_alias.to_string()
yield directory_handler.send_room_alias_update_event( yield directory_handler.send_room_alias_update_event(
requester, user_id, room_id requester, room_id
) )
defer.returnValue(result) defer.returnValue(result)

View File

@ -74,37 +74,10 @@ class ClientDirectoryServer(ClientV1RestServlet):
if room is None: if room is None:
raise SynapseError(400, "Room does not exist") raise SynapseError(400, "Room does not exist")
dir_handler = self.handlers.directory_handler
try:
# try to auth as a user
requester = yield self.auth.get_user_by_req(request) requester = yield self.auth.get_user_by_req(request)
try:
user_id = requester.user.to_string() yield self.handlers.directory_handler.create_association(
yield dir_handler.create_association( requester, room_alias, room_id, servers
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
) )
defer.returnValue((200, {})) defer.returnValue((200, {}))
@ -135,7 +108,7 @@ class ClientDirectoryServer(ClientV1RestServlet):
room_alias = RoomAlias.from_string(room_alias) room_alias = RoomAlias.from_string(room_alias)
yield dir_handler.delete_association( yield dir_handler.delete_association(
requester, user.to_string(), room_alias requester, room_alias
) )
logger.info( logger.info(