summary refs log tree commit diff
diff options
context:
space:
mode:
authorMark Haines <mark.haines@matrix.org>2014-11-18 15:03:01 +0000
committerMark Haines <mark.haines@matrix.org>2014-11-18 15:03:13 +0000
commita5b88c489ebf23e076a99306e7c95e9b60e83864 (patch)
tree9e6d493cec26f95f3e92bc83e447589a638f81f7
parentwarn about memory (diff)
downloadsynapse-a5b88c489ebf23e076a99306e7c95e9b60e83864.tar.xz
Split out sending the room alias events from creating the alias so that we can do them in the right point when creating a room
-rw-r--r--synapse/handlers/directory.py18
-rw-r--r--synapse/handlers/room.py17
-rw-r--r--synapse/rest/directory.py4
-rw-r--r--synapse/storage/directory.py19
4 files changed, 31 insertions, 27 deletions
diff --git a/synapse/handlers/directory.py b/synapse/handlers/directory.py
index 164363cdc5..5d79c6690c 100644
--- a/synapse/handlers/directory.py
+++ b/synapse/handlers/directory.py
@@ -56,17 +56,11 @@ class DirectoryHandler(BaseHandler):
         if not servers:
             raise SynapseError(400, "Failed to get server list")
 
-        try:
-            yield self.store.create_room_alias_association(
-                room_alias,
-                room_id,
-                servers
-            )
-        except sqlite3.IntegrityError:
-            defer.returnValue("Already exists")
-
-        # TODO: Send the room event.
-        yield self._update_room_alias_events(user_id, room_id)
+        yield self.store.create_room_alias_association(
+            room_alias,
+            room_id,
+            servers
+        )
 
     @defer.inlineCallbacks
     def delete_association(self, user_id, room_alias):
@@ -136,7 +130,7 @@ class DirectoryHandler(BaseHandler):
         })
 
     @defer.inlineCallbacks
-    def _update_room_alias_events(self, user_id, room_id):
+    def send_room_alias_update_event(self, user_id, room_id):
         aliases = yield self.store.get_aliases_for_room(room_id)
 
         event = self.event_factory.create_event(
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
index cfe1061ed3..bfbd7f9783 100644
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -106,6 +106,15 @@ class RoomCreationHandler(BaseHandler):
             if not room_id:
                 raise StoreError(500, "Couldn't generate a room ID.")
 
+        if room_alias:
+            directory_handler = self.hs.get_handlers().directory_handler
+            yield directory_handler.create_association(
+                user_id=user_id,
+                room_id=room_id,
+                room_alias=room_alias,
+                servers=[self.hs.hostname],
+            )
+
         user = self.hs.parse_userid(user_id)
         creation_events = self._create_events_for_new_room(
             user, room_id, is_public=is_public
@@ -180,13 +189,7 @@ class RoomCreationHandler(BaseHandler):
 
         if room_alias:
             result["room_alias"] = room_alias.to_string()
-            directory_handler = self.hs.get_handlers().directory_handler
-            yield directory_handler.create_association(
-                user_id=user_id,
-                room_id=room_id,
-                room_alias=room_alias,
-                servers=[self.hs.hostname],
-            )
+            directory_handler.send_room_alias_update_event(user_id, room_id)
 
         defer.returnValue(result)
 
diff --git a/synapse/rest/directory.py b/synapse/rest/directory.py
index 6c260e7102..5aaac05e28 100644
--- a/synapse/rest/directory.py
+++ b/synapse/rest/directory.py
@@ -70,9 +70,11 @@ class ClientDirectoryServer(RestServlet):
         dir_handler = self.handlers.directory_handler
 
         try:
+            user_id = user.to_string()
             yield dir_handler.create_association(
-                user.to_string(), room_alias, room_id, servers
+                user_id, room_alias, room_id, servers
             )
+            yield dir_handler.send_room_alias_update_event(user_id, room_id)
         except SynapseError as e:
             raise e
         except:
diff --git a/synapse/storage/directory.py b/synapse/storage/directory.py
index d6a7113b9c..f2e895a5c2 100644
--- a/synapse/storage/directory.py
+++ b/synapse/storage/directory.py
@@ -75,13 +75,18 @@ class DirectoryStore(SQLBaseStore):
         Returns:
             Deferred
         """
-        yield self._simple_insert(
-            "room_aliases",
-            {
-                "room_alias": room_alias.to_string(),
-                "room_id": room_id,
-            },
-        )
+        try:
+            yield self._simple_insert(
+                "room_aliases",
+                {
+                    "room_alias": room_alias.to_string(),
+                    "room_id": room_id,
+                },
+            )
+        except sqlite3.IntegrityError:
+            raise SynapseError(
+                409, "Room alias %s already exists" % room_alias.to_string()
+            )
 
         for server in servers:
             # TODO(erikj): Fix this to bulk insert