summary refs log tree commit diff
path: root/synapse/storage/room.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-09-01 16:15:34 +0100
committerErik Johnston <erik@matrix.org>2014-09-01 16:15:34 +0100
commitdb7109c43b298a0b647188dde45724bfc7300915 (patch)
treef723dc8bbda9446fa4728cacd59b06e887ec5663 /synapse/storage/room.py
parentImplement power level lists, default power levels and send_evnet_level/add_st... (diff)
downloadsynapse-db7109c43b298a0b647188dde45724bfc7300915.tar.xz
Add beginnings of ban support.
Diffstat (limited to 'synapse/storage/room.py')
-rw-r--r--synapse/storage/room.py57
1 files changed, 52 insertions, 5 deletions
diff --git a/synapse/storage/room.py b/synapse/storage/room.py
index f8aa8bd2a1..3b2d1a8ecd 100644
--- a/synapse/storage/room.py
+++ b/synapse/storage/room.py
@@ -27,6 +27,9 @@ import logging
 logger = logging.getLogger(__name__)
 
 
+OpsLevel = collections.namedtuple("OpsLevel", ("ban_level", "kick_level"))
+
+
 class RoomStore(SQLBaseStore):
 
     @defer.inlineCallbacks
@@ -145,8 +148,13 @@ class RoomStore(SQLBaseStore):
         else:
             defer.returnValue(None)
 
-    @defer.inlineCallbacks
     def get_power_level(self, room_id, user_id):
+        return self._db_pool.runInteraction(
+            self._get_power_level,
+            room_id, user_id,
+        )
+
+    def _get_power_level(self, txn, room_id, user_id):
         sql = (
             "SELECT level FROM room_power_levels as r "
             "INNER JOIN current_state_events as c "
@@ -154,7 +162,7 @@ class RoomStore(SQLBaseStore):
             "WHERE c.room_id = ? AND r.user_id = ? "
         )
 
-        rows = yield self._execute(None, sql, room_id, user_id)
+        rows = txn.execute(sql, (room_id, user_id,)).fetchall()
 
         if len(rows) == 1:
             defer.returnValue(rows[0][0])
@@ -167,12 +175,33 @@ class RoomStore(SQLBaseStore):
             "WHERE c.room_id = ? "
         )
 
-        rows = yield self._execute(None, sql, room_id)
+        rows = txn.execute(sql, (room_id,)).fetchall()
 
         if len(rows) == 1:
-            defer.returnValue(rows[0][0])
+            return rows[0][0]
         else:
-            defer.returnValue(None)
+            return None
+
+    def get_ops_levels(self, room_id):
+        return self._db_pool.runInteraction(
+            self._get_ops_levels,
+            room_id,
+        )
+
+    def _get_ops_levels(self, txn, room_id):
+        sql = (
+            "SELECT ban_level, kick_level FROM room_ops_levels as r "
+            "INNER JOIN current_state_events as c "
+            "ON r.event_id = c.event_id "
+            "WHERE c.room_id = ? "
+        )
+
+        rows = txn.execute(sql, (room_id,)).fetchall()
+
+        if len(rows) == 1:
+            return OpsLevel(rows[0][0], rows[0][1])
+        else:
+            return OpsLevel(None, None)
 
     def get_add_state_level(self, room_id):
         return self._get_level_from_table("room_add_state_levels", room_id)
@@ -286,6 +315,24 @@ class RoomStore(SQLBaseStore):
             },
         )
 
+    def _store_ops_level(self, txn, event):
+        content = {
+            "event_id": event.event_id,
+            "room_id": event.room_id,
+        }
+
+        if "kick_level" in event.content:
+            content["kick_level"] = event.content["kick_level"]
+
+        if "ban_level" in event.content:
+            content["ban_level"] = event.content["ban_level"]
+
+        self._simple_insert_txn(
+            txn,
+            "room_send_event_levels",
+            content,
+        )
+
 
 class RoomsTable(Table):
     table_name = "rooms"