2015-01-22 17:37:12 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2014 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# 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-05-21 14:14:26 +00:00
|
|
|
from ._base import SQLBaseStore, cached
|
2015-01-22 17:37:12 +00:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
import logging
|
2015-02-11 14:23:10 +00:00
|
|
|
import simplejson as json
|
2015-01-22 17:37:12 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class PushRuleStore(SQLBaseStore):
|
|
|
|
@defer.inlineCallbacks
|
2015-03-02 18:17:19 +00:00
|
|
|
def get_push_rules_for_user(self, user_name):
|
2015-05-11 10:22:54 +00:00
|
|
|
rows = yield self._simple_select_list(
|
|
|
|
table=PushRuleTable.table_name,
|
|
|
|
keyvalues={
|
|
|
|
"user_name": user_name,
|
|
|
|
},
|
|
|
|
retcols=PushRuleTable.fields,
|
2015-01-22 17:37:12 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 10:22:54 +00:00
|
|
|
rows.sort(
|
|
|
|
key=lambda row: (-int(row["priority_class"]), -int(row["priority"]))
|
|
|
|
)
|
2015-01-22 17:37:12 +00:00
|
|
|
|
2015-05-11 10:22:54 +00:00
|
|
|
defer.returnValue(rows)
|
2015-01-22 17:37:12 +00:00
|
|
|
|
2015-05-21 14:14:26 +00:00
|
|
|
@cached()
|
2015-02-25 19:17:07 +00:00
|
|
|
@defer.inlineCallbacks
|
2015-03-02 18:17:19 +00:00
|
|
|
def get_push_rules_enabled_for_user(self, user_name):
|
2015-02-25 19:17:07 +00:00
|
|
|
results = yield self._simple_select_list(
|
2015-05-11 10:22:54 +00:00
|
|
|
table=PushRuleEnableTable.table_name,
|
|
|
|
keyvalues={
|
|
|
|
'user_name': user_name
|
|
|
|
},
|
|
|
|
retcols=PushRuleEnableTable.fields,
|
2015-03-20 15:59:18 +00:00
|
|
|
desc="get_push_rules_enabled_for_user",
|
2015-02-25 19:17:07 +00:00
|
|
|
)
|
2015-05-11 10:22:54 +00:00
|
|
|
defer.returnValue({
|
|
|
|
r['rule_id']: False if r['enabled'] == 0 else True for r in results
|
|
|
|
})
|
2015-02-25 19:17:07 +00:00
|
|
|
|
2015-01-22 17:37:12 +00:00
|
|
|
@defer.inlineCallbacks
|
2015-01-23 10:28:25 +00:00
|
|
|
def add_push_rule(self, before, after, **kwargs):
|
2015-05-11 10:22:54 +00:00
|
|
|
vals = kwargs
|
2015-01-22 17:37:12 +00:00
|
|
|
if 'conditions' in vals:
|
|
|
|
vals['conditions'] = json.dumps(vals['conditions'])
|
|
|
|
if 'actions' in vals:
|
|
|
|
vals['actions'] = json.dumps(vals['actions'])
|
2015-05-11 10:22:54 +00:00
|
|
|
|
2015-01-22 17:37:12 +00: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 10:22:54 +00:00
|
|
|
vals.pop("id", None)
|
2015-01-22 17:37:12 +00:00
|
|
|
|
2015-01-23 10:28:25 +00:00
|
|
|
if before or after:
|
2015-01-22 17:37:12 +00:00
|
|
|
ret = yield self.runInteraction(
|
|
|
|
"_add_push_rule_relative_txn",
|
|
|
|
self._add_push_rule_relative_txn,
|
2015-01-23 10:28:25 +00:00
|
|
|
before=before,
|
|
|
|
after=after,
|
2015-01-22 17:37:12 +00: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)
|
|
|
|
|
|
|
|
def _add_push_rule_relative_txn(self, txn, user_name, **kwargs):
|
2015-05-11 10:22:54 +00:00
|
|
|
after = kwargs.pop("after", None)
|
|
|
|
relative_to_rule = kwargs.pop("before", after)
|
|
|
|
|
|
|
|
res = self._simple_select_one_txn(
|
|
|
|
txn,
|
|
|
|
table=PushRuleTable.table_name,
|
|
|
|
keyvalues={
|
|
|
|
"user_name": user_name,
|
|
|
|
"rule_id": relative_to_rule,
|
|
|
|
},
|
|
|
|
retcols=["priority_class", "priority"],
|
|
|
|
allow_none=True,
|
2015-01-22 17:37:12 +00:00
|
|
|
)
|
2015-05-11 10:22:54 +00:00
|
|
|
|
2015-01-22 17:37:12 +00:00
|
|
|
if not res:
|
2015-02-10 16:30:48 +00:00
|
|
|
raise RuleNotFoundException(
|
|
|
|
"before/after rule not found: %s" % (relative_to_rule,)
|
|
|
|
)
|
2015-05-11 10:22:54 +00:00
|
|
|
|
|
|
|
priority_class = res["priority_class"]
|
|
|
|
base_rule_priority = res["priority"]
|
2015-01-22 17:37:12 +00: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 10:22:54 +00:00
|
|
|
new_rule = kwargs
|
|
|
|
new_rule.pop("before", None)
|
|
|
|
new_rule.pop("after", None)
|
2015-01-22 17:37:12 +00:00
|
|
|
new_rule['priority_class'] = priority_class
|
|
|
|
new_rule['user_name'] = user_name
|
2015-05-11 09:58:36 +00:00
|
|
|
new_rule['id'] = self._push_rule_id_gen.get_next_txn(txn)
|
2015-01-22 17:37:12 +00: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 = (
|
2015-01-29 16:10:35 +00:00
|
|
|
"SELECT COUNT(*) FROM " + PushRuleTable.table_name +
|
2015-01-22 17:37:12 +00:00
|
|
|
" WHERE user_name = ? AND priority_class = ? AND priority = ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (user_name, priority_class, new_rule_priority))
|
|
|
|
res = txn.fetchall()
|
|
|
|
num_conflicting = res[0][0]
|
|
|
|
|
|
|
|
# if there are conflicting rules, bump everything
|
|
|
|
if num_conflicting:
|
|
|
|
sql = "UPDATE "+PushRuleTable.table_name+" SET priority = priority "
|
|
|
|
if after:
|
|
|
|
sql += "-1"
|
|
|
|
else:
|
|
|
|
sql += "+1"
|
|
|
|
sql += " WHERE user_name = ? AND priority_class = ? AND priority "
|
|
|
|
if after:
|
|
|
|
sql += "<= ?"
|
|
|
|
else:
|
|
|
|
sql += ">= ?"
|
|
|
|
|
|
|
|
txn.execute(sql, (user_name, priority_class, new_rule_priority))
|
|
|
|
|
2015-05-21 14:14:26 +00:00
|
|
|
txn.call_after(
|
|
|
|
self.get_push_rules_enabled_for_user.invalidate, user_name
|
|
|
|
)
|
|
|
|
|
2015-05-11 09:58:36 +00:00
|
|
|
self._simple_insert_txn(
|
|
|
|
txn,
|
|
|
|
table=PushRuleTable.table_name,
|
|
|
|
values=new_rule,
|
|
|
|
)
|
2015-01-22 17:37:12 +00:00
|
|
|
|
2015-01-29 16:10:35 +00:00
|
|
|
def _add_push_rule_highest_priority_txn(self, txn, user_name,
|
|
|
|
priority_class, **kwargs):
|
2015-01-22 17:37:12 +00:00
|
|
|
# find the highest priority rule in that class
|
|
|
|
sql = (
|
2015-01-29 16:10:35 +00:00
|
|
|
"SELECT COUNT(*), MAX(priority) FROM " + PushRuleTable.table_name +
|
2015-01-22 17:37:12 +00:00
|
|
|
" WHERE user_name = ? and priority_class = ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (user_name, priority_class))
|
|
|
|
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 10:22:54 +00:00
|
|
|
new_rule = kwargs
|
2015-05-11 09:58:36 +00:00
|
|
|
new_rule['id'] = self._push_rule_id_gen.get_next_txn(txn)
|
2015-01-22 17:37:12 +00:00
|
|
|
new_rule['user_name'] = user_name
|
|
|
|
new_rule['priority_class'] = priority_class
|
|
|
|
new_rule['priority'] = new_prio
|
|
|
|
|
2015-05-21 14:14:26 +00:00
|
|
|
txn.call_after(
|
|
|
|
self.get_push_rules_enabled_for_user.invalidate, user_name
|
|
|
|
)
|
|
|
|
|
2015-05-11 09:58:36 +00:00
|
|
|
self._simple_insert_txn(
|
|
|
|
txn,
|
|
|
|
table=PushRuleTable.table_name,
|
|
|
|
values=new_rule,
|
|
|
|
)
|
2015-01-22 17:37:12 +00:00
|
|
|
|
2015-01-23 17:49:37 +00:00
|
|
|
@defer.inlineCallbacks
|
2015-02-05 15:11:38 +00:00
|
|
|
def delete_push_rule(self, user_name, rule_id):
|
2015-01-28 18:06:04 +00: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:
|
|
|
|
user_name (str): The matrix ID of the push rule owner
|
|
|
|
rule_id (str): The rule_id of the rule to be deleted
|
|
|
|
"""
|
2015-02-05 15:11:38 +00:00
|
|
|
yield self._simple_delete_one(
|
|
|
|
PushRuleTable.table_name,
|
2015-03-20 15:59:18 +00:00
|
|
|
{'user_name': user_name, 'rule_id': rule_id},
|
|
|
|
desc="delete_push_rule",
|
2015-02-05 15:11:38 +00:00
|
|
|
)
|
2015-05-21 14:14:26 +00:00
|
|
|
self.get_push_rules_enabled_for_user.invalidate(user_name)
|
2015-01-22 17:53:30 +00:00
|
|
|
|
2015-02-26 18:07:44 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def set_push_rule_enabled(self, user_name, rule_id, enabled):
|
2015-03-10 17:26:25 +00:00
|
|
|
yield self._simple_upsert(
|
|
|
|
PushRuleEnableTable.table_name,
|
|
|
|
{'user_name': user_name, 'rule_id': rule_id},
|
2015-05-10 09:50:51 +00:00
|
|
|
{'enabled': 1 if enabled else 0},
|
2015-03-20 15:59:18 +00:00
|
|
|
desc="set_push_rule_enabled",
|
2015-03-10 17:26:25 +00:00
|
|
|
)
|
2015-05-21 14:14:26 +00:00
|
|
|
txn.call_after(
|
|
|
|
self.get_push_rules_enabled_for_user.invalidate, user_name
|
|
|
|
)
|
2015-01-28 14:51:01 +00:00
|
|
|
|
2015-03-02 15:58:12 +00:00
|
|
|
|
2015-01-22 17:37:12 +00:00
|
|
|
class RuleNotFoundException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InconsistentRuleException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-05-21 14:14:26 +00:00
|
|
|
class PushRuleTable(object):
|
2015-01-22 17:37:12 +00:00
|
|
|
table_name = "push_rules"
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"user_name",
|
|
|
|
"rule_id",
|
|
|
|
"priority_class",
|
|
|
|
"priority",
|
|
|
|
"conditions",
|
|
|
|
"actions",
|
|
|
|
]
|
|
|
|
|
2015-02-26 13:43:05 +00:00
|
|
|
|
2015-05-21 14:14:26 +00:00
|
|
|
class PushRuleEnableTable(object):
|
2015-02-25 19:17:07 +00:00
|
|
|
table_name = "push_rules_enable"
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
"user_name",
|
|
|
|
"rule_id",
|
|
|
|
"enabled"
|
2015-02-26 13:43:05 +00:00
|
|
|
]
|