Simplify room creation code
This commit is contained in:
parent
8168341e9b
commit
1a2197d7bf
|
@ -76,13 +76,11 @@ class RoomCreationHandler(BaseHandler):
|
||||||
}
|
}
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def create_room(self, user_id, room_id, config):
|
def create_room(self, requester, config):
|
||||||
""" Creates a new room.
|
""" Creates a new room.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_id (str): The ID of the user creating the new room.
|
user_id (str): The ID of the user creating the new room.
|
||||||
room_id (str): The proposed ID for the new room. Can be None, in
|
|
||||||
which case one will be created for you.
|
|
||||||
config (dict) : A dict of configuration options.
|
config (dict) : A dict of configuration options.
|
||||||
Returns:
|
Returns:
|
||||||
The new room ID.
|
The new room ID.
|
||||||
|
@ -90,6 +88,8 @@ class RoomCreationHandler(BaseHandler):
|
||||||
SynapseError if the room ID was taken, couldn't be stored, or
|
SynapseError if the room ID was taken, couldn't be stored, or
|
||||||
something went horribly wrong.
|
something went horribly wrong.
|
||||||
"""
|
"""
|
||||||
|
user_id = requester.user.to_string()
|
||||||
|
|
||||||
self.ratelimit(user_id)
|
self.ratelimit(user_id)
|
||||||
|
|
||||||
if "room_alias_name" in config:
|
if "room_alias_name" in config:
|
||||||
|
@ -121,40 +121,28 @@ class RoomCreationHandler(BaseHandler):
|
||||||
|
|
||||||
is_public = config.get("visibility", None) == "public"
|
is_public = config.get("visibility", None) == "public"
|
||||||
|
|
||||||
if room_id:
|
# autogen room IDs and try to create it. We may clash, so just
|
||||||
# Ensure room_id is the correct type
|
# try a few times till one goes through, giving up eventually.
|
||||||
room_id_obj = RoomID.from_string(room_id)
|
attempts = 0
|
||||||
if not self.hs.is_mine(room_id_obj):
|
room_id = None
|
||||||
raise SynapseError(400, "Room id must be local")
|
while attempts < 5:
|
||||||
|
try:
|
||||||
yield self.store.store_room(
|
random_string = stringutils.random_string(18)
|
||||||
room_id=room_id,
|
gen_room_id = RoomID.create(
|
||||||
room_creator_user_id=user_id,
|
random_string,
|
||||||
is_public=is_public
|
self.hs.hostname,
|
||||||
)
|
)
|
||||||
else:
|
yield self.store.store_room(
|
||||||
# autogen room IDs and try to create it. We may clash, so just
|
room_id=gen_room_id.to_string(),
|
||||||
# try a few times till one goes through, giving up eventually.
|
room_creator_user_id=user_id,
|
||||||
attempts = 0
|
is_public=is_public
|
||||||
room_id = None
|
)
|
||||||
while attempts < 5:
|
room_id = gen_room_id.to_string()
|
||||||
try:
|
break
|
||||||
random_string = stringutils.random_string(18)
|
except StoreError:
|
||||||
gen_room_id = RoomID.create(
|
attempts += 1
|
||||||
random_string,
|
if not room_id:
|
||||||
self.hs.hostname,
|
raise StoreError(500, "Couldn't generate a room ID.")
|
||||||
)
|
|
||||||
yield self.store.store_room(
|
|
||||||
room_id=gen_room_id.to_string(),
|
|
||||||
room_creator_user_id=user_id,
|
|
||||||
is_public=is_public
|
|
||||||
)
|
|
||||||
room_id = gen_room_id.to_string()
|
|
||||||
break
|
|
||||||
except StoreError:
|
|
||||||
attempts += 1
|
|
||||||
if not room_id:
|
|
||||||
raise StoreError(500, "Couldn't generate a room ID.")
|
|
||||||
|
|
||||||
if room_alias:
|
if room_alias:
|
||||||
directory_handler = self.hs.get_handlers().directory_handler
|
directory_handler = self.hs.get_handlers().directory_handler
|
||||||
|
|
|
@ -63,24 +63,12 @@ class RoomCreateRestServlet(ClientV1RestServlet):
|
||||||
def on_POST(self, request):
|
def on_POST(self, request):
|
||||||
requester = yield self.auth.get_user_by_req(request)
|
requester = yield self.auth.get_user_by_req(request)
|
||||||
|
|
||||||
room_config = self.get_room_config(request)
|
|
||||||
info = yield self.make_room(
|
|
||||||
room_config,
|
|
||||||
requester.user,
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
room_config.update(info)
|
|
||||||
defer.returnValue((200, info))
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
|
||||||
def make_room(self, room_config, auth_user, room_id):
|
|
||||||
handler = self.handlers.room_creation_handler
|
handler = self.handlers.room_creation_handler
|
||||||
info = yield handler.create_room(
|
info = yield handler.create_room(
|
||||||
user_id=auth_user.to_string(),
|
requester, self.get_room_config(request)
|
||||||
room_id=room_id,
|
|
||||||
config=room_config
|
|
||||||
)
|
)
|
||||||
defer.returnValue(info)
|
|
||||||
|
defer.returnValue((200, info))
|
||||||
|
|
||||||
def get_room_config(self, request):
|
def get_room_config(self, request):
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue