mirror of
https://github.com/element-hq/synapse
synced 2024-08-19 02:10:55 +00:00
Clean up room alias creation
This commit is contained in:
parent
6190abe8da
commit
74e7617083
3 changed files with 55 additions and 64 deletions
|
@ -80,42 +80,60 @@ 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:
|
||||||
raise SynapseError(
|
requester (Requester)
|
||||||
403, "This user is not permitted to create this alias",
|
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(
|
Returns:
|
||||||
room_alias,
|
Deferred
|
||||||
user_id=user_id
|
"""
|
||||||
)
|
|
||||||
if not can_create:
|
user_id = requester.user.to_string()
|
||||||
raise SynapseError(
|
|
||||||
400, "This alias is reserved by an application service.",
|
service = requester.app_service
|
||||||
errcode=Codes.EXCLUSIVE
|
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
|
||||||
|
)
|
||||||
|
|
||||||
yield self._create_association(room_alias, room_id, servers, creator=user_id)
|
yield self._create_association(room_alias, room_id, servers, creator=user_id)
|
||||||
|
if send_event:
|
||||||
@defer.inlineCallbacks
|
yield self.send_room_alias_update_event(
|
||||||
def create_appservice_association(self, service, room_alias, room_id,
|
requester,
|
||||||
servers=None):
|
room_id
|
||||||
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
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# 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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -74,38 +74,11 @@ 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
|
requester = yield self.auth.get_user_by_req(request)
|
||||||
|
|
||||||
try:
|
yield self.handlers.directory_handler.create_association(
|
||||||
# try to auth as a user
|
requester, room_alias, room_id, servers
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
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(
|
||||||
|
|
Loading…
Reference in a new issue