2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-01-27 12:48:13 -05:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2018-08-30 10:19:58 -04:00
|
|
|
from canonicaljson import encode_canonical_json
|
2018-07-09 02:09:20 -04:00
|
|
|
|
|
|
|
from synapse.api.errors import Codes, SynapseError
|
2019-10-21 07:56:42 -04:00
|
|
|
from synapse.storage._base import SQLBaseStore, db_to_json
|
2020-09-01 08:39:04 -04:00
|
|
|
from synapse.types import JsonDict
|
2020-08-12 09:28:48 -04:00
|
|
|
from synapse.util.caches.descriptors import cached
|
2015-01-27 12:48:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
class FilteringStore(SQLBaseStore):
|
2020-08-12 09:28:48 -04:00
|
|
|
@cached(num_args=2)
|
|
|
|
async def get_user_filter(self, user_localpart, filter_id):
|
2016-11-21 08:13:55 -05:00
|
|
|
# filter_id is BIGINT UNSIGNED, so if it isn't a number, fail
|
|
|
|
# with a coherent error message rather than 500 M_UNKNOWN.
|
|
|
|
try:
|
|
|
|
int(filter_id)
|
|
|
|
except ValueError:
|
|
|
|
raise SynapseError(400, "Invalid filter ID", Codes.INVALID_PARAM)
|
|
|
|
|
2020-08-12 09:28:48 -04:00
|
|
|
def_json = await self.db_pool.simple_select_one_onecol(
|
2015-01-27 13:46:03 -05:00
|
|
|
table="user_filters",
|
2019-04-03 05:07:29 -04:00
|
|
|
keyvalues={"user_id": user_localpart, "filter_id": filter_id},
|
2015-01-28 12:42:19 -05:00
|
|
|
retcol="filter_json",
|
2015-01-27 13:46:03 -05:00
|
|
|
allow_none=False,
|
2015-03-20 11:59:18 -04:00
|
|
|
desc="get_user_filter",
|
2015-01-27 13:46:03 -05:00
|
|
|
)
|
2015-01-27 12:48:13 -05:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return db_to_json(def_json)
|
2015-01-27 12:48:13 -05:00
|
|
|
|
2020-09-01 08:39:04 -04:00
|
|
|
async def add_user_filter(self, user_localpart: str, user_filter: JsonDict) -> str:
|
2017-06-21 08:48:12 -04:00
|
|
|
def_json = encode_canonical_json(user_filter)
|
2015-01-27 13:46:03 -05:00
|
|
|
|
|
|
|
# Need an atomic transaction to SELECT the maximal ID so far then
|
|
|
|
# INSERT a new one
|
|
|
|
def _do_txn(txn):
|
2017-05-11 10:05:30 -04:00
|
|
|
sql = (
|
|
|
|
"SELECT filter_id FROM user_filters "
|
|
|
|
"WHERE user_id = ? AND filter_json = ?"
|
|
|
|
)
|
2019-10-08 11:21:17 -04:00
|
|
|
txn.execute(sql, (user_localpart, bytearray(def_json)))
|
2017-05-15 12:36:37 -04:00
|
|
|
filter_id_response = txn.fetchone()
|
|
|
|
if filter_id_response is not None:
|
|
|
|
return filter_id_response[0]
|
2017-05-11 10:05:30 -04:00
|
|
|
|
2019-11-21 07:00:14 -05:00
|
|
|
sql = "SELECT MAX(filter_id) FROM user_filters WHERE user_id = ?"
|
2015-01-27 13:46:03 -05:00
|
|
|
txn.execute(sql, (user_localpart,))
|
|
|
|
max_id = txn.fetchone()[0]
|
|
|
|
if max_id is None:
|
|
|
|
filter_id = 0
|
|
|
|
else:
|
|
|
|
filter_id = max_id + 1
|
|
|
|
|
|
|
|
sql = (
|
2015-01-28 12:42:19 -05:00
|
|
|
"INSERT INTO user_filters (user_id, filter_id, filter_json)"
|
2015-01-27 13:46:03 -05:00
|
|
|
"VALUES(?, ?, ?)"
|
|
|
|
)
|
2019-10-08 11:21:17 -04:00
|
|
|
txn.execute(sql, (user_localpart, filter_id, bytearray(def_json)))
|
2015-01-27 12:48:13 -05:00
|
|
|
|
2015-01-27 13:46:03 -05:00
|
|
|
return filter_id
|
2015-01-27 12:48:13 -05:00
|
|
|
|
2020-09-01 08:39:04 -04:00
|
|
|
return await self.db_pool.runInteraction("add_user_filter", _do_txn)
|