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

Commit

Permalink
Factor _generate_room_id out of create_room
Browse files Browse the repository at this point in the history
we're going to need this for room upgrades.
  • Loading branch information
richvdh committed Oct 25, 2018
1 parent cb53ce9 commit 871c4ab
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,7 @@ def create_room(self, requester, config, ratelimit=True,
visibility = config.get("visibility", None)
is_public = visibility == "public"

# autogen room IDs and try to create it. We may clash, so just
# try a few times till one goes through, giving up eventually.
attempts = 0
room_id = None
while attempts < 5:
try:
random_string = stringutils.random_string(18)
gen_room_id = RoomID(
random_string,
self.hs.hostname,
)
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.")
room_id = yield self._generate_room_id(creator_id=user_id, is_public=is_public)

if room_alias:
directory_handler = self.hs.get_handlers().directory_handler
Expand Down Expand Up @@ -427,6 +406,28 @@ def send(etype, content, **kwargs):
content=content,
)

@defer.inlineCallbacks
def _generate_room_id(self, creator_id, is_public):
# autogen room IDs and try to create it. We may clash, so just
# try a few times till one goes through, giving up eventually.
attempts = 0
while attempts < 5:
try:
random_string = stringutils.random_string(18)
gen_room_id = RoomID(
random_string,
self.hs.hostname,
).to_string()
yield self.store.store_room(
room_id=gen_room_id,
room_creator_user_id=creator_id,
is_public=is_public,
)
defer.returnValue(gen_room_id)
except StoreError:
attempts += 1
raise StoreError(500, "Couldn't generate a room ID.")


class RoomContextHandler(object):
def __init__(self, hs):
Expand Down

0 comments on commit 871c4ab

Please sign in to comment.