summary refs log tree commit diff
path: root/synapse/storage
diff options
context:
space:
mode:
authorErik Johnston <erikj@jki.re>2017-06-21 14:11:26 +0100
committerGitHub <noreply@github.com>2017-06-21 14:11:26 +0100
commit71995e1397419cf4dade36b1cbe7f392345e40eb (patch)
tree73975e960b5a80f7a466cbf62810639662093d4a /synapse/storage
parentMerge pull request #2292 from matrix-org/erikj/quarantine_media (diff)
parentMerge branch 'develop' into avoid_duplicate_filters (diff)
downloadsynapse-71995e1397419cf4dade36b1cbe7f392345e40eb.tar.xz
Merge pull request #2219 from krombel/avoid_duplicate_filters
only add new filter when not existent prevoisly
Diffstat (limited to 'synapse/storage')
-rw-r--r--synapse/storage/filtering.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/synapse/storage/filtering.py b/synapse/storage/filtering.py
index a2ccc66ea7..78b1e30945 100644
--- a/synapse/storage/filtering.py
+++ b/synapse/storage/filtering.py
@@ -19,6 +19,7 @@ from ._base import SQLBaseStore
 from synapse.api.errors import SynapseError, Codes
 from synapse.util.caches.descriptors import cachedInlineCallbacks
 
+from canonicaljson import encode_canonical_json
 import simplejson as json
 
 
@@ -46,12 +47,21 @@ class FilteringStore(SQLBaseStore):
         defer.returnValue(json.loads(str(def_json).decode("utf-8")))
 
     def add_user_filter(self, user_localpart, user_filter):
-        def_json = json.dumps(user_filter).encode("utf-8")
+        def_json = encode_canonical_json(user_filter)
 
         # Need an atomic transaction to SELECT the maximal ID so far then
         # INSERT a new one
         def _do_txn(txn):
             sql = (
+                "SELECT filter_id FROM user_filters "
+                "WHERE user_id = ? AND filter_json = ?"
+            )
+            txn.execute(sql, (user_localpart, def_json))
+            filter_id_response = txn.fetchone()
+            if filter_id_response is not None:
+                return filter_id_response[0]
+
+            sql = (
                 "SELECT MAX(filter_id) FROM user_filters "
                 "WHERE user_id = ?"
             )