summary refs log tree commit diff
path: root/synapse/storage/registration.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2016-03-11 20:16:25 +0000
committerErik Johnston <erik@matrix.org>2016-03-11 20:16:25 +0000
commit494d0c8e0244dc6c9c7c3422cbeccc3cbe9283d8 (patch)
tree641a9a66c62ee6dfac1111ac6147c8ca7f498c66 /synapse/storage/registration.py
parentMerge pull request #643 from matrix-org/markjh/parse_json_II (diff)
parentThats not how transactions work. (diff)
downloadsynapse-494d0c8e0244dc6c9c7c3422cbeccc3cbe9283d8.tar.xz
Merge pull request #642 from matrix-org/erikj/logout
Implement logout
Diffstat (limited to 'synapse/storage/registration.py')
-rw-r--r--synapse/storage/registration.py54
1 files changed, 39 insertions, 15 deletions
diff --git a/synapse/storage/registration.py b/synapse/storage/registration.py
index 5e7a4e371d..bd4eb88a92 100644
--- a/synapse/storage/registration.py
+++ b/synapse/storage/registration.py
@@ -195,24 +195,48 @@ class RegistrationStore(SQLBaseStore):
         })
 
     @defer.inlineCallbacks
-    def user_delete_access_tokens(self, user_id, except_token_ids):
+    def user_delete_access_tokens(self, user_id, except_token_ids=[]):
         def f(txn):
-            txn.execute(
-                "SELECT id, token FROM access_tokens "
-                "WHERE user_id = ? AND id NOT IN ? LIMIT 50",
-                (user_id, except_token_ids)
-            )
+            sql = "SELECT token FROM access_tokens WHERE user_id = ?"
+            clauses = [user_id]
+
+            if except_token_ids:
+                sql += " AND id NOT IN (%s)" % (
+                    ",".join(["?" for _ in except_token_ids]),
+                )
+                clauses += except_token_ids
+
+            txn.execute(sql, clauses)
+
             rows = txn.fetchall()
-            for r in rows:
-                txn.call_after(self.get_user_by_access_token.invalidate, (r[1],))
-            txn.execute(
-                "DELETE FROM access_tokens WHERE id in (%s)" % ",".join(
-                    ["?" for _ in rows]
-                ), [r[0] for r in rows]
+
+            n = 100
+            chunks = [rows[i:i + n] for i in xrange(0, len(rows), n)]
+            for chunk in chunks:
+                for row in chunk:
+                    txn.call_after(self.get_user_by_access_token.invalidate, (row[0],))
+
+                txn.execute(
+                    "DELETE FROM access_tokens WHERE token in (%s)" % (
+                        ",".join(["?" for _ in chunk]),
+                    ), [r[0] for r in chunk]
+                )
+
+        yield self.runInteraction("user_delete_access_tokens", f)
+
+    def delete_access_token(self, access_token):
+        def f(txn):
+            self._simple_delete_one_txn(
+                txn,
+                table="access_tokens",
+                keyvalues={
+                    "token": access_token
+                },
             )
-            return len(rows) == 50
-        while (yield self.runInteraction("user_delete_access_tokens", f)):
-            pass
+
+            txn.call_after(self.get_user_by_access_token.invalidate, (access_token,))
+
+        return self.runInteraction("delete_access_token", f)
 
     @cached()
     def get_user_by_access_token(self, token):