2015-01-22 10:37:12 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2015-01-22 10:37:12 -07: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.
|
|
|
|
|
2015-08-11 10:59:32 -06:00
|
|
|
from ._base import SQLBaseStore
|
|
|
|
from synapse.util.caches.descriptors import cachedInlineCallbacks
|
2015-01-22 10:37:12 -07:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
import logging
|
2015-02-11 07:23:10 -07:00
|
|
|
import simplejson as json
|
2015-01-22 10:37:12 -07:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class PushRuleStore(SQLBaseStore):
|
2015-07-27 06:57:29 -06:00
|
|
|
@cachedInlineCallbacks()
|
2016-01-13 06:08:59 -07:00
|
|
|
def get_push_rules_for_user(self, user_id):
|
2015-05-11 04:22:54 -06:00
|
|
|
rows = yield self._simple_select_list(
|
2016-01-12 07:22:24 -07:00
|
|
|
table="push_rules",
|
2015-05-11 04:22:54 -06:00
|
|
|
keyvalues={
|
2016-01-13 06:08:59 -07:00
|
|
|
"user_name": user_id,
|
2015-05-11 04:22:54 -06:00
|
|
|
},
|
2016-01-12 07:22:24 -07:00
|
|
|
retcols=(
|
|
|
|
"user_name", "rule_id", "priority_class", "priority",
|
|
|
|
"conditions", "actions",
|
|
|
|
),
|
2015-05-22 03:39:45 -06:00
|
|
|
desc="get_push_rules_enabled_for_user",
|
2015-01-22 10:37:12 -07:00
|
|
|
)
|
|
|
|
|
2015-05-11 04:22:54 -06:00
|
|
|
rows.sort(
|
|
|
|
key=lambda row: (-int(row["priority_class"]), -int(row["priority"]))
|
|
|
|
)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2015-05-11 04:22:54 -06:00
|
|
|
defer.returnValue(rows)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2015-07-27 06:57:29 -06:00
|
|
|
@cachedInlineCallbacks()
|
2016-01-13 06:08:59 -07:00
|
|
|
def get_push_rules_enabled_for_user(self, user_id):
|
2015-02-25 12:17:07 -07:00
|
|
|
results = yield self._simple_select_list(
|
2016-01-12 07:22:24 -07:00
|
|
|
table="push_rules_enable",
|
2015-05-11 04:22:54 -06:00
|
|
|
keyvalues={
|
2016-01-13 06:08:59 -07:00
|
|
|
'user_name': user_id
|
2015-05-11 04:22:54 -06:00
|
|
|
},
|
2016-01-12 07:22:24 -07:00
|
|
|
retcols=(
|
|
|
|
"user_name", "rule_id", "enabled",
|
|
|
|
),
|
2015-03-20 09:59:18 -06:00
|
|
|
desc="get_push_rules_enabled_for_user",
|
2015-02-25 12:17:07 -07:00
|
|
|
)
|
2015-05-11 04:22:54 -06:00
|
|
|
defer.returnValue({
|
|
|
|
r['rule_id']: False if r['enabled'] == 0 else True for r in results
|
|
|
|
})
|
2015-02-25 12:17:07 -07:00
|
|
|
|
2015-12-22 08:19:34 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def bulk_get_push_rules(self, user_ids):
|
2016-01-13 04:37:17 -07:00
|
|
|
if not user_ids:
|
|
|
|
defer.returnValue({})
|
|
|
|
|
2015-12-22 08:19:34 -07:00
|
|
|
results = {}
|
|
|
|
|
2016-01-25 06:36:02 -07:00
|
|
|
rows = yield self._simple_select_many_batch(
|
|
|
|
table="push_rules",
|
|
|
|
column="user_name",
|
|
|
|
iterable=user_ids,
|
|
|
|
retcols=("*",),
|
|
|
|
desc="bulk_get_push_rules",
|
|
|
|
)
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2016-01-25 06:36:02 -07:00
|
|
|
rows.sort(key=lambda e: (-e["priority_class"], -e["priority"]))
|
|
|
|
|
|
|
|
for row in rows:
|
|
|
|
results.setdefault(row['user_name'], []).append(row)
|
2015-12-22 08:19:34 -07:00
|
|
|
defer.returnValue(results)
|
|
|
|
|
2016-01-22 07:58:19 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def bulk_get_push_rules_enabled(self, user_ids):
|
|
|
|
if not user_ids:
|
|
|
|
defer.returnValue({})
|
|
|
|
|
|
|
|
results = {}
|
|
|
|
|
2016-01-25 06:36:02 -07:00
|
|
|
rows = yield self._simple_select_many_batch(
|
|
|
|
table="push_rules_enable",
|
|
|
|
column="user_name",
|
|
|
|
iterable=user_ids,
|
|
|
|
retcols=("user_name", "rule_id", "enabled",),
|
|
|
|
desc="bulk_get_push_rules_enabled",
|
|
|
|
)
|
|
|
|
for row in rows:
|
|
|
|
results.setdefault(row['user_name'], {})[row['rule_id']] = row['enabled']
|
2016-01-22 07:58:19 -07:00
|
|
|
defer.returnValue(results)
|
|
|
|
|
2015-01-22 10:37:12 -07:00
|
|
|
@defer.inlineCallbacks
|
2015-01-23 03:28:25 -07:00
|
|
|
def add_push_rule(self, before, after, **kwargs):
|
2015-05-11 04:22:54 -06:00
|
|
|
vals = kwargs
|
2015-01-22 10:37:12 -07:00
|
|
|
if 'conditions' in vals:
|
|
|
|
vals['conditions'] = json.dumps(vals['conditions'])
|
|
|
|
if 'actions' in vals:
|
|
|
|
vals['actions'] = json.dumps(vals['actions'])
|
2015-05-11 04:22:54 -06:00
|
|
|
|
2015-01-22 10:37:12 -07:00
|
|
|
# we could check the rest of the keys are valid column names
|
|
|
|
# but sqlite will do that anyway so I think it's just pointless.
|
2015-05-11 04:22:54 -06:00
|
|
|
vals.pop("id", None)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2015-01-23 03:28:25 -07:00
|
|
|
if before or after:
|
2015-01-22 10:37:12 -07:00
|
|
|
ret = yield self.runInteraction(
|
|
|
|
"_add_push_rule_relative_txn",
|
|
|
|
self._add_push_rule_relative_txn,
|
2015-01-23 03:28:25 -07:00
|
|
|
before=before,
|
|
|
|
after=after,
|
2015-01-22 10:37:12 -07:00
|
|
|
**vals
|
|
|
|
)
|
|
|
|
defer.returnValue(ret)
|
|
|
|
else:
|
|
|
|
ret = yield self.runInteraction(
|
|
|
|
"_add_push_rule_highest_priority_txn",
|
|
|
|
self._add_push_rule_highest_priority_txn,
|
|
|
|
**vals
|
|
|
|
)
|
|
|
|
defer.returnValue(ret)
|
|
|
|
|
2016-01-13 06:08:59 -07:00
|
|
|
def _add_push_rule_relative_txn(self, txn, user_id, **kwargs):
|
2015-05-11 04:22:54 -06:00
|
|
|
after = kwargs.pop("after", None)
|
2016-01-27 03:24:20 -07:00
|
|
|
before = kwargs.pop("before", None)
|
|
|
|
relative_to_rule = before or after
|
2015-05-11 04:22:54 -06:00
|
|
|
|
|
|
|
res = self._simple_select_one_txn(
|
|
|
|
txn,
|
2016-01-12 07:22:24 -07:00
|
|
|
table="push_rules",
|
2015-05-11 04:22:54 -06:00
|
|
|
keyvalues={
|
2016-01-13 06:08:59 -07:00
|
|
|
"user_name": user_id,
|
2015-05-11 04:22:54 -06:00
|
|
|
"rule_id": relative_to_rule,
|
|
|
|
},
|
|
|
|
retcols=["priority_class", "priority"],
|
|
|
|
allow_none=True,
|
2015-01-22 10:37:12 -07:00
|
|
|
)
|
2015-05-11 04:22:54 -06:00
|
|
|
|
2015-01-22 10:37:12 -07:00
|
|
|
if not res:
|
2015-02-10 09:30:48 -07:00
|
|
|
raise RuleNotFoundException(
|
|
|
|
"before/after rule not found: %s" % (relative_to_rule,)
|
|
|
|
)
|
2015-05-11 04:22:54 -06:00
|
|
|
|
|
|
|
priority_class = res["priority_class"]
|
|
|
|
base_rule_priority = res["priority"]
|
2015-01-22 10:37:12 -07:00
|
|
|
|
|
|
|
if 'priority_class' in kwargs and kwargs['priority_class'] != priority_class:
|
|
|
|
raise InconsistentRuleException(
|
|
|
|
"Given priority class does not match class of relative rule"
|
|
|
|
)
|
|
|
|
|
2015-05-11 04:22:54 -06:00
|
|
|
new_rule = kwargs
|
|
|
|
new_rule.pop("before", None)
|
|
|
|
new_rule.pop("after", None)
|
2015-01-22 10:37:12 -07:00
|
|
|
new_rule['priority_class'] = priority_class
|
2016-01-13 06:08:59 -07:00
|
|
|
new_rule['user_name'] = user_id
|
2015-05-11 03:58:36 -06:00
|
|
|
new_rule['id'] = self._push_rule_id_gen.get_next_txn(txn)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
|
|
|
# check if the priority before/after is free
|
|
|
|
new_rule_priority = base_rule_priority
|
|
|
|
if after:
|
|
|
|
new_rule_priority -= 1
|
|
|
|
else:
|
|
|
|
new_rule_priority += 1
|
|
|
|
|
|
|
|
new_rule['priority'] = new_rule_priority
|
|
|
|
|
|
|
|
sql = (
|
2016-01-12 07:22:24 -07:00
|
|
|
"SELECT COUNT(*) FROM push_rules"
|
2015-01-22 10:37:12 -07:00
|
|
|
" WHERE user_name = ? AND priority_class = ? AND priority = ?"
|
|
|
|
)
|
2016-01-13 06:08:59 -07:00
|
|
|
txn.execute(sql, (user_id, priority_class, new_rule_priority))
|
2015-01-22 10:37:12 -07:00
|
|
|
res = txn.fetchall()
|
|
|
|
num_conflicting = res[0][0]
|
|
|
|
|
|
|
|
# if there are conflicting rules, bump everything
|
|
|
|
if num_conflicting:
|
2016-01-12 07:22:24 -07:00
|
|
|
sql = "UPDATE push_rules SET priority = priority "
|
2015-01-22 10:37:12 -07:00
|
|
|
if after:
|
|
|
|
sql += "-1"
|
|
|
|
else:
|
|
|
|
sql += "+1"
|
|
|
|
sql += " WHERE user_name = ? AND priority_class = ? AND priority "
|
|
|
|
if after:
|
|
|
|
sql += "<= ?"
|
|
|
|
else:
|
|
|
|
sql += ">= ?"
|
|
|
|
|
2016-01-13 06:08:59 -07:00
|
|
|
txn.execute(sql, (user_id, priority_class, new_rule_priority))
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2015-05-22 03:39:45 -06:00
|
|
|
txn.call_after(
|
2016-01-13 06:08:59 -07:00
|
|
|
self.get_push_rules_for_user.invalidate, (user_id,)
|
2015-05-22 03:39:45 -06:00
|
|
|
)
|
|
|
|
|
2015-05-21 08:14:26 -06:00
|
|
|
txn.call_after(
|
2016-01-13 06:08:59 -07:00
|
|
|
self.get_push_rules_enabled_for_user.invalidate, (user_id,)
|
2015-05-21 08:14:26 -06:00
|
|
|
)
|
|
|
|
|
2015-05-11 03:58:36 -06:00
|
|
|
self._simple_insert_txn(
|
|
|
|
txn,
|
2016-01-12 07:22:24 -07:00
|
|
|
table="push_rules",
|
2015-05-11 03:58:36 -06:00
|
|
|
values=new_rule,
|
|
|
|
)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2016-01-13 06:08:59 -07:00
|
|
|
def _add_push_rule_highest_priority_txn(self, txn, user_id,
|
2015-01-29 09:10:35 -07:00
|
|
|
priority_class, **kwargs):
|
2015-01-22 10:37:12 -07:00
|
|
|
# find the highest priority rule in that class
|
|
|
|
sql = (
|
2016-01-12 07:22:24 -07:00
|
|
|
"SELECT COUNT(*), MAX(priority) FROM push_rules"
|
2015-01-22 10:37:12 -07:00
|
|
|
" WHERE user_name = ? and priority_class = ?"
|
|
|
|
)
|
2016-01-13 06:08:59 -07:00
|
|
|
txn.execute(sql, (user_id, priority_class))
|
2015-01-22 10:37:12 -07:00
|
|
|
res = txn.fetchall()
|
|
|
|
(how_many, highest_prio) = res[0]
|
|
|
|
|
|
|
|
new_prio = 0
|
|
|
|
if how_many > 0:
|
|
|
|
new_prio = highest_prio + 1
|
|
|
|
|
|
|
|
# and insert the new rule
|
2015-05-11 04:22:54 -06:00
|
|
|
new_rule = kwargs
|
2015-05-11 03:58:36 -06:00
|
|
|
new_rule['id'] = self._push_rule_id_gen.get_next_txn(txn)
|
2016-01-13 06:08:59 -07:00
|
|
|
new_rule['user_name'] = user_id
|
2015-01-22 10:37:12 -07:00
|
|
|
new_rule['priority_class'] = priority_class
|
|
|
|
new_rule['priority'] = new_prio
|
|
|
|
|
2015-05-22 03:39:45 -06:00
|
|
|
txn.call_after(
|
2016-01-13 06:08:59 -07:00
|
|
|
self.get_push_rules_for_user.invalidate, (user_id,)
|
2015-05-22 03:39:45 -06:00
|
|
|
)
|
2015-05-21 08:14:26 -06:00
|
|
|
txn.call_after(
|
2016-01-13 06:08:59 -07:00
|
|
|
self.get_push_rules_enabled_for_user.invalidate, (user_id,)
|
2015-05-21 08:14:26 -06:00
|
|
|
)
|
|
|
|
|
2015-05-11 03:58:36 -06:00
|
|
|
self._simple_insert_txn(
|
|
|
|
txn,
|
2016-01-12 07:22:24 -07:00
|
|
|
table="push_rules",
|
2015-05-11 03:58:36 -06:00
|
|
|
values=new_rule,
|
|
|
|
)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2015-01-23 10:49:37 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-01-13 06:08:59 -07:00
|
|
|
def delete_push_rule(self, user_id, rule_id):
|
2015-01-28 11:06:04 -07:00
|
|
|
"""
|
|
|
|
Delete a push rule. Args specify the row to be deleted and can be
|
|
|
|
any of the columns in the push_rule table, but below are the
|
|
|
|
standard ones
|
|
|
|
|
|
|
|
Args:
|
2016-01-13 06:08:59 -07:00
|
|
|
user_id (str): The matrix ID of the push rule owner
|
2015-01-28 11:06:04 -07:00
|
|
|
rule_id (str): The rule_id of the rule to be deleted
|
|
|
|
"""
|
2015-02-05 08:11:38 -07:00
|
|
|
yield self._simple_delete_one(
|
2016-01-12 07:22:24 -07:00
|
|
|
"push_rules",
|
2016-01-13 06:08:59 -07:00
|
|
|
{'user_name': user_id, 'rule_id': rule_id},
|
2015-03-20 09:59:18 -06:00
|
|
|
desc="delete_push_rule",
|
2015-02-05 08:11:38 -07:00
|
|
|
)
|
2015-05-22 03:39:45 -06:00
|
|
|
|
2016-01-13 06:08:59 -07:00
|
|
|
self.get_push_rules_for_user.invalidate((user_id,))
|
|
|
|
self.get_push_rules_enabled_for_user.invalidate((user_id,))
|
2015-01-22 10:53:30 -07:00
|
|
|
|
2015-02-26 11:07:44 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-01-13 06:08:59 -07:00
|
|
|
def set_push_rule_enabled(self, user_id, rule_id, enabled):
|
2015-05-14 04:44:03 -06:00
|
|
|
ret = yield self.runInteraction(
|
|
|
|
"_set_push_rule_enabled_txn",
|
|
|
|
self._set_push_rule_enabled_txn,
|
2016-01-13 06:08:59 -07:00
|
|
|
user_id, rule_id, enabled
|
2015-05-14 04:44:03 -06:00
|
|
|
)
|
|
|
|
defer.returnValue(ret)
|
|
|
|
|
2016-01-13 06:08:59 -07:00
|
|
|
def _set_push_rule_enabled_txn(self, txn, user_id, rule_id, enabled):
|
2015-05-14 04:44:03 -06:00
|
|
|
new_id = self._push_rules_enable_id_gen.get_next_txn(txn)
|
|
|
|
self._simple_upsert_txn(
|
|
|
|
txn,
|
2016-01-12 07:22:24 -07:00
|
|
|
"push_rules_enable",
|
2016-01-13 06:08:59 -07:00
|
|
|
{'user_name': user_id, 'rule_id': rule_id},
|
2015-05-10 03:50:51 -06:00
|
|
|
{'enabled': 1 if enabled else 0},
|
2015-05-14 04:44:03 -06:00
|
|
|
{'id': new_id},
|
2015-03-10 11:26:25 -06:00
|
|
|
)
|
2015-05-22 03:39:45 -06:00
|
|
|
txn.call_after(
|
2016-01-13 06:08:59 -07:00
|
|
|
self.get_push_rules_for_user.invalidate, (user_id,)
|
2015-05-22 03:39:45 -06:00
|
|
|
)
|
|
|
|
txn.call_after(
|
2016-01-13 06:08:59 -07:00
|
|
|
self.get_push_rules_enabled_for_user.invalidate, (user_id,)
|
2015-05-22 03:39:45 -06:00
|
|
|
)
|
2015-01-28 07:51:01 -07:00
|
|
|
|
2015-03-02 08:58:12 -07:00
|
|
|
|
2015-01-22 10:37:12 -07:00
|
|
|
class RuleNotFoundException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InconsistentRuleException(Exception):
|
|
|
|
pass
|