summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2018-10-25 17:40:41 +0100
committerRichard van der Hoff <richard@matrix.org>2018-10-25 18:23:09 +0100
commit871c4abfecfd14acda13e3f25c7d040f848a9a32 (patch)
tree78e4f7c307c1eb3f6d9725148284f9dd1c1d697b
parentRefactor state group lookup to reduce DB hits (#4011) (diff)
downloadsynapse-871c4abfecfd14acda13e3f25c7d040f848a9a32.tar.xz
Factor _generate_room_id out of create_room
we're going to need this for room upgrades.
Diffstat (limited to '')
-rw-r--r--synapse/handlers/room.py45
1 files changed, 23 insertions, 22 deletions
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
index 3ba92bdb4c..000a22b07c 100644
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -165,28 +165,7 @@ class RoomCreationHandler(BaseHandler):
         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
@@ -427,6 +406,28 @@ class RoomCreationHandler(BaseHandler):
                 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):