summary refs log tree commit diff
path: root/synapse/storage
diff options
context:
space:
mode:
Diffstat (limited to 'synapse/storage')
-rw-r--r--synapse/storage/__init__.py12
-rw-r--r--synapse/storage/room.py89
-rw-r--r--synapse/storage/schema/im.sql20
3 files changed, 99 insertions, 22 deletions
diff --git a/synapse/storage/__init__.py b/synapse/storage/__init__.py
index 204a243560..3d5e5049fa 100644
--- a/synapse/storage/__init__.py
+++ b/synapse/storage/__init__.py
@@ -21,7 +21,8 @@ from synapse.api.events.room import (
     RoomNameEvent,
     RoomJoinRulesEvent,
     RoomPowerLevelsEvent,
-    RoomDefaultLevelEvent,
+    RoomAddStateLevelEvent,
+    RoomSendEventLevelEvent,
 )
 
 from synapse.util.logutils import log_function
@@ -125,7 +126,7 @@ class DataStore(RoomMemberStore, RoomStore,
         if event.type == RoomMemberEvent.TYPE:
             self._store_room_member_txn(txn, event)
         elif event.type == FeedbackEvent.TYPE:
-            self._store_feedback_txn(txn,event)
+            self._store_feedback_txn(txn, event)
 #        elif event.type == RoomConfigEvent.TYPE:
 #            self._store_room_config_txn(txn, event)
         elif event.type == RoomNameEvent.TYPE:
@@ -136,8 +137,10 @@ class DataStore(RoomMemberStore, RoomStore,
             self._store_join_rule(txn, event)
         elif event.type == RoomPowerLevelsEvent.TYPE:
             self._store_power_levels(txn, event)
-        elif event.type == RoomDefaultLevelEvent.TYPE:
-            self._store_default_level(txn, event)
+        elif event.type == RoomAddStateLevelEvent.TYPE:
+            self._store_add_state_level(txn, event)
+        elif event.type == RoomSendEventLevelEvent.TYPE:
+            self._store_send_event_level(txn, event)
 
         vals = {
             "topological_ordering": event.depth,
@@ -231,7 +234,6 @@ class DataStore(RoomMemberStore, RoomStore,
 
         defer.returnValue(self.min_token)
 
-
     def snapshot_room(self, room_id, user_id, state_type=None, state_key=None):
         """Snapshot the room for an update by a user
         Args:
diff --git a/synapse/storage/room.py b/synapse/storage/room.py
index 8946ce99f7..f8aa8bd2a1 100644
--- a/synapse/storage/room.py
+++ b/synapse/storage/room.py
@@ -174,6 +174,28 @@ class RoomStore(SQLBaseStore):
         else:
             defer.returnValue(None)
 
+    def get_add_state_level(self, room_id):
+        return self._get_level_from_table("room_add_state_levels", room_id)
+
+    def get_send_event_level(self, room_id):
+        return self._get_level_from_table("room_send_event_levels", room_id)
+
+    @defer.inlineCallbacks
+    def _get_level_from_table(self, table, room_id):
+        sql = (
+            "SELECT level FROM %(table)s as r "
+            "INNER JOIN current_state_events as c "
+            "ON r.event_id = c.event_id "
+            "WHERE c.room_id = ? "
+        ) % {"table": table}
+
+        rows = yield self._execute(None, sql, room_id)
+
+        if len(rows) == 1:
+            defer.returnValue(rows[0][0])
+        else:
+            defer.returnValue(None)
+
     def _store_room_topic_txn(self, txn, event):
         self._simple_insert_txn(
             txn,
@@ -196,38 +218,71 @@ class RoomStore(SQLBaseStore):
             }
         )
 
-    def _store_join_rule(txn, event):
+    def _store_join_rule(self, txn, event):
         self._simple_insert_txn(
             txn,
             "room_join_rules",
             {
                 "event_id": event.event_id,
                 "room_id": event.room_id,
-                "join_rule": event.join_rule,
+                "join_rule": event.content["join_rule"],
             },
         )
 
-    def _store_power_levels(txn, event):
-        for user_id, level in event.content:
-            self._simple_insert_txn(
-                txn,
-                "room_power_levels",
-                {
-                    "event_id": event.event_id,
-                    "room_id": event.room_id,
-                    "user_id": user_id,
-                    "level": level
-                },
-            )
-
-    def _store_default_level(txn, event):
+    def _store_power_levels(self, txn, event):
+        for user_id, level in event.content.items():
+            if user_id == "default":
+                self._simple_insert_txn(
+                    txn,
+                    "room_default_levels",
+                    {
+                        "event_id": event.event_id,
+                        "room_id": event.room_id,
+                        "level": level,
+                    },
+                )
+            else:
+                self._simple_insert_txn(
+                    txn,
+                    "room_power_levels",
+                    {
+                        "event_id": event.event_id,
+                        "room_id": event.room_id,
+                        "user_id": user_id,
+                        "level": level
+                    },
+                )
+
+    def _store_default_level(self, txn, event):
         self._simple_insert_txn(
             txn,
             "room_default_levels",
             {
                 "event_id": event.event_id,
                 "room_id": event.room_id,
-                "level": level
+                "level": event.content["default_level"],
+            },
+        )
+
+    def _store_add_state_level(self, txn, event):
+        self._simple_insert_txn(
+            txn,
+            "room_add_state_levels",
+            {
+                "event_id": event.event_id,
+                "room_id": event.room_id,
+                "level": event.content["level"],
+            },
+        )
+
+    def _store_send_event_level(self, txn, event):
+        self._simple_insert_txn(
+            txn,
+            "room_send_event_levels",
+            {
+                "event_id": event.event_id,
+                "room_id": event.room_id,
+                "level": event.content["level"],
             },
         )
 
diff --git a/synapse/storage/schema/im.sql b/synapse/storage/schema/im.sql
index c20516b7fa..447c1d29a2 100644
--- a/synapse/storage/schema/im.sql
+++ b/synapse/storage/schema/im.sql
@@ -126,6 +126,26 @@ CREATE INDEX IF NOT EXISTS room_default_levels_event_id ON room_default_levels(e
 CREATE INDEX IF NOT EXISTS room_default_levels_room_id ON room_default_levels(room_id);
 
 
+CREATE TABLE IF NOT EXISTS room_add_state_levels(
+    event_id TEXT NOT NULL,
+    room_id TEXT NOT NULL,
+    level INTEGER NOT NULL
+);
+
+CREATE INDEX IF NOT EXISTS room_add_state_levels_event_id ON room_add_state_levels(event_id);
+CREATE INDEX IF NOT EXISTS room_add_state_levels_room_id ON room_add_state_levels(room_id);
+
+
+CREATE TABLE IF NOT EXISTS room_send_event_levels(
+    event_id TEXT NOT NULL,
+    room_id TEXT NOT NULL,
+    level INTEGER NOT NULL
+);
+
+CREATE INDEX IF NOT EXISTS room_send_event_levels_event_id ON room_send_event_levels(event_id);
+CREATE INDEX IF NOT EXISTS room_send_event_levels_room_id ON room_send_event_levels(room_id);
+
+
 CREATE TABLE IF NOT EXISTS room_hosts(
     room_id TEXT NOT NULL,
     host TEXT NOT NULL,