Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

only add new filter when not existent prevoisly #2219

Merged
merged 6 commits into from
Jun 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion synapse/storage/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from synapse.api.errors import SynapseError, Codes
from synapse.util.caches.descriptors import cachedInlineCallbacks

from canonicaljson import encode_canonical_json
import simplejson as json


Expand Down Expand Up @@ -46,11 +47,20 @@ def get_user_filter(self, user_localpart, filter_id):
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 = ?"
Expand Down