2015-01-22 12:37:12 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2018-02-23 05:39:19 -05:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2015-01-22 12:37:12 -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-07-09 02:09:20 -04:00
|
|
|
import abc
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from canonicaljson import json
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from synapse.push.baserules import list_with_base_rules
|
2018-02-27 07:01:36 -05:00
|
|
|
from synapse.storage.appservice import ApplicationServiceWorkerStore
|
|
|
|
from synapse.storage.pusher import PusherWorkerStore
|
|
|
|
from synapse.storage.receipts import ReceiptsWorkerStore
|
|
|
|
from synapse.storage.roommember import RoomMemberWorkerStore
|
2016-05-31 08:46:58 -04:00
|
|
|
from synapse.util.caches.descriptors import cachedInlineCallbacks, cachedList
|
2018-02-21 05:39:27 -05:00
|
|
|
from synapse.util.caches.stream_change_cache import StreamChangeCache
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from ._base import SQLBaseStore
|
2015-01-22 12:37:12 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-06-01 09:27:07 -04:00
|
|
|
def _load_rules(rawrules, enabled_map):
|
|
|
|
ruleslist = []
|
|
|
|
for rawrule in rawrules:
|
|
|
|
rule = dict(rawrule)
|
|
|
|
rule["conditions"] = json.loads(rawrule["conditions"])
|
|
|
|
rule["actions"] = json.loads(rawrule["actions"])
|
|
|
|
ruleslist.append(rule)
|
|
|
|
|
|
|
|
# We're going to be mutating this a lot, so do a deep copy
|
|
|
|
rules = list(list_with_base_rules(ruleslist))
|
|
|
|
|
|
|
|
for i, rule in enumerate(rules):
|
2019-06-20 05:32:02 -04:00
|
|
|
rule_id = rule["rule_id"]
|
2016-06-01 09:27:07 -04:00
|
|
|
if rule_id in enabled_map:
|
2019-06-20 05:32:02 -04:00
|
|
|
if rule.get("enabled", True) != bool(enabled_map[rule_id]):
|
2016-06-01 09:27:07 -04:00
|
|
|
# Rules are cached across users.
|
|
|
|
rule = dict(rule)
|
2019-06-20 05:32:02 -04:00
|
|
|
rule["enabled"] = bool(enabled_map[rule_id])
|
2016-06-01 09:27:07 -04:00
|
|
|
rules[i] = rule
|
|
|
|
|
|
|
|
return rules
|
|
|
|
|
|
|
|
|
2019-04-03 05:07:29 -04:00
|
|
|
class PushRulesWorkerStore(
|
|
|
|
ApplicationServiceWorkerStore,
|
|
|
|
ReceiptsWorkerStore,
|
|
|
|
PusherWorkerStore,
|
|
|
|
RoomMemberWorkerStore,
|
|
|
|
SQLBaseStore,
|
|
|
|
):
|
2018-02-21 05:39:27 -05:00
|
|
|
"""This is an abstract base class where subclasses must implement
|
|
|
|
`get_max_push_rules_stream_id` which can be called in the initializer.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# This ABCMeta metaclass ensures that we cannot be instantiated without
|
|
|
|
# the abstract methods being implemented.
|
|
|
|
__metaclass__ = abc.ABCMeta
|
|
|
|
|
|
|
|
def __init__(self, db_conn, hs):
|
|
|
|
super(PushRulesWorkerStore, self).__init__(db_conn, hs)
|
|
|
|
|
|
|
|
push_rules_prefill, push_rules_id = self._get_cache_dict(
|
2019-04-03 05:07:29 -04:00
|
|
|
db_conn,
|
|
|
|
"push_rules_stream",
|
2018-02-21 05:39:27 -05:00
|
|
|
entity_column="user_id",
|
|
|
|
stream_column="stream_id",
|
|
|
|
max_value=self.get_max_push_rules_stream_id(),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.push_rules_stream_cache = StreamChangeCache(
|
2019-04-03 05:07:29 -04:00
|
|
|
"PushRulesStreamChangeCache",
|
|
|
|
push_rules_id,
|
2018-02-21 05:39:27 -05:00
|
|
|
prefilled_cache=push_rules_prefill,
|
|
|
|
)
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def get_max_push_rules_stream_id(self):
|
|
|
|
"""Get the position of the push rules stream.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2017-05-18 12:17:53 -04:00
|
|
|
@cachedInlineCallbacks(max_entries=5000)
|
2016-01-13 08:08:59 -05:00
|
|
|
def get_push_rules_for_user(self, user_id):
|
2015-05-11 06:22:54 -04:00
|
|
|
rows = yield self._simple_select_list(
|
2016-01-12 09:22:24 -05:00
|
|
|
table="push_rules",
|
2019-04-03 05:07:29 -04:00
|
|
|
keyvalues={"user_name": user_id},
|
2016-01-12 09:22:24 -05:00
|
|
|
retcols=(
|
2019-04-03 05:07:29 -04:00
|
|
|
"user_name",
|
|
|
|
"rule_id",
|
|
|
|
"priority_class",
|
|
|
|
"priority",
|
|
|
|
"conditions",
|
|
|
|
"actions",
|
2016-01-12 09:22:24 -05:00
|
|
|
),
|
2015-05-22 05:39:45 -04:00
|
|
|
desc="get_push_rules_enabled_for_user",
|
2015-01-22 12:37:12 -05:00
|
|
|
)
|
|
|
|
|
2019-04-03 05:07:29 -04:00
|
|
|
rows.sort(key=lambda row: (-int(row["priority_class"]), -int(row["priority"])))
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2016-06-01 09:27:07 -04:00
|
|
|
enabled_map = yield self.get_push_rules_enabled_for_user(user_id)
|
|
|
|
|
|
|
|
rules = _load_rules(rows, enabled_map)
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return rules
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2017-05-18 12:17:53 -04:00
|
|
|
@cachedInlineCallbacks(max_entries=5000)
|
2016-01-13 08:08:59 -05:00
|
|
|
def get_push_rules_enabled_for_user(self, user_id):
|
2015-02-25 14:17:07 -05:00
|
|
|
results = yield self._simple_select_list(
|
2016-01-12 09:22:24 -05:00
|
|
|
table="push_rules_enable",
|
2019-06-20 05:32:02 -04:00
|
|
|
keyvalues={"user_name": user_id},
|
2019-04-03 05:07:29 -04:00
|
|
|
retcols=("user_name", "rule_id", "enabled"),
|
2015-03-20 11:59:18 -04:00
|
|
|
desc="get_push_rules_enabled_for_user",
|
2015-02-25 14:17:07 -05:00
|
|
|
)
|
2019-07-23 09:00:55 -04:00
|
|
|
return {r["rule_id"]: False if r["enabled"] == 0 else True for r in results}
|
2015-02-25 14:17:07 -05:00
|
|
|
|
2018-02-21 05:39:27 -05:00
|
|
|
def have_push_rules_changed_for_user(self, user_id, last_id):
|
|
|
|
if not self.push_rules_stream_cache.has_entity_changed(user_id, last_id):
|
|
|
|
return defer.succeed(False)
|
|
|
|
else:
|
2019-04-03 05:07:29 -04:00
|
|
|
|
2018-02-21 05:39:27 -05:00
|
|
|
def have_push_rules_changed_txn(txn):
|
|
|
|
sql = (
|
|
|
|
"SELECT COUNT(stream_id) FROM push_rules_stream"
|
|
|
|
" WHERE user_id = ? AND ? < stream_id"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (user_id, last_id))
|
|
|
|
count, = txn.fetchone()
|
|
|
|
return bool(count)
|
2019-04-03 05:07:29 -04:00
|
|
|
|
2018-02-21 05:39:27 -05:00
|
|
|
return self.runInteraction(
|
|
|
|
"have_push_rules_changed", have_push_rules_changed_txn
|
|
|
|
)
|
|
|
|
|
2019-04-03 05:07:29 -04:00
|
|
|
@cachedList(
|
|
|
|
cached_method_name="get_push_rules_for_user",
|
|
|
|
list_name="user_ids",
|
|
|
|
num_args=1,
|
|
|
|
inlineCallbacks=True,
|
|
|
|
)
|
2015-12-22 10:19:34 -05:00
|
|
|
def bulk_get_push_rules(self, user_ids):
|
2016-01-13 06:37:17 -05:00
|
|
|
if not user_ids:
|
2019-07-23 09:00:55 -04:00
|
|
|
return {}
|
2016-01-13 06:37:17 -05:00
|
|
|
|
2019-04-03 05:07:29 -04:00
|
|
|
results = {user_id: [] for user_id in user_ids}
|
2015-12-22 10:19:34 -05:00
|
|
|
|
2016-01-25 08:36:02 -05: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 10:19:34 -05:00
|
|
|
|
2019-04-03 05:07:29 -04:00
|
|
|
rows.sort(key=lambda row: (-int(row["priority_class"]), -int(row["priority"])))
|
2016-01-25 08:36:02 -05:00
|
|
|
|
|
|
|
for row in rows:
|
2019-06-20 05:32:02 -04:00
|
|
|
results.setdefault(row["user_name"], []).append(row)
|
2016-06-01 09:27:07 -04:00
|
|
|
|
|
|
|
enabled_map_by_user = yield self.bulk_get_push_rules_enabled(user_ids)
|
|
|
|
|
|
|
|
for user_id, rules in results.items():
|
2019-04-03 05:07:29 -04:00
|
|
|
results[user_id] = _load_rules(rules, enabled_map_by_user.get(user_id, {}))
|
2016-06-01 09:27:07 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return results
|
2015-12-22 10:19:34 -05:00
|
|
|
|
2019-03-12 10:42:53 -04:00
|
|
|
@defer.inlineCallbacks
|
2019-04-03 05:07:29 -04:00
|
|
|
def move_push_rule_from_room_to_room(self, new_room_id, user_id, rule):
|
2019-03-12 10:42:53 -04:00
|
|
|
"""Move a single push rule from one room to another for a specific user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
new_room_id (str): ID of the new room.
|
|
|
|
user_id (str): ID of user the push rule belongs to.
|
|
|
|
rule (Dict): A push rule.
|
|
|
|
"""
|
|
|
|
# Create new rule id
|
2019-06-20 05:32:02 -04:00
|
|
|
rule_id_scope = "/".join(rule["rule_id"].split("/")[:-1])
|
2019-03-12 10:42:53 -04:00
|
|
|
new_rule_id = rule_id_scope + "/" + new_room_id
|
|
|
|
|
|
|
|
# Change room id in each condition
|
|
|
|
for condition in rule.get("conditions", []):
|
|
|
|
if condition.get("key") == "room_id":
|
|
|
|
condition["pattern"] = new_room_id
|
|
|
|
|
|
|
|
# Add the rule for the new room
|
|
|
|
yield self.add_push_rule(
|
|
|
|
user_id=user_id,
|
|
|
|
rule_id=new_rule_id,
|
|
|
|
priority_class=rule["priority_class"],
|
|
|
|
conditions=rule["conditions"],
|
|
|
|
actions=rule["actions"],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Delete push rule for the old room
|
|
|
|
yield self.delete_push_rule(user_id, rule["rule_id"])
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def move_push_rules_from_room_to_room_for_user(
|
2019-04-03 05:07:29 -04:00
|
|
|
self, old_room_id, new_room_id, user_id
|
2019-03-12 10:42:53 -04:00
|
|
|
):
|
|
|
|
"""Move all of the push rules from one room to another for a specific
|
|
|
|
user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
old_room_id (str): ID of the old room.
|
|
|
|
new_room_id (str): ID of the new room.
|
|
|
|
user_id (str): ID of user to copy push rules for.
|
|
|
|
"""
|
|
|
|
# Retrieve push rules for this user
|
|
|
|
user_push_rules = yield self.get_push_rules_for_user(user_id)
|
|
|
|
|
|
|
|
# Get rules relating to the old room, move them to the new room, then
|
|
|
|
# delete them from the old room
|
|
|
|
for rule in user_push_rules:
|
|
|
|
conditions = rule.get("conditions", [])
|
2019-04-03 05:07:29 -04:00
|
|
|
if any(
|
|
|
|
(c.get("key") == "room_id" and c.get("pattern") == old_room_id)
|
|
|
|
for c in conditions
|
|
|
|
):
|
2019-09-27 10:11:14 -04:00
|
|
|
yield self.move_push_rule_from_room_to_room(new_room_id, user_id, rule)
|
2019-03-12 10:42:53 -04:00
|
|
|
|
2018-07-23 08:00:22 -04:00
|
|
|
@defer.inlineCallbacks
|
2016-09-05 05:02:38 -04:00
|
|
|
def bulk_get_push_rules_for_room(self, event, context):
|
2016-08-25 12:32:22 -04:00
|
|
|
state_group = context.state_group
|
2016-08-19 09:07:27 -04:00
|
|
|
if not state_group:
|
|
|
|
# If state_group is None it means it has yet to be assigned a
|
|
|
|
# state group, i.e. we need to make sure that calls with a state_group
|
|
|
|
# of None don't hit previous cached calls with a None state_group.
|
|
|
|
# To do this we set the state_group to a new object as object() != object()
|
|
|
|
state_group = object()
|
|
|
|
|
2018-07-23 08:00:22 -04:00
|
|
|
current_state_ids = yield context.get_current_state_ids(self)
|
|
|
|
result = yield self._bulk_get_push_rules_for_room(
|
|
|
|
event.room_id, state_group, current_state_ids, event=event
|
2016-08-25 12:32:22 -04:00
|
|
|
)
|
2019-07-23 09:00:55 -04:00
|
|
|
return result
|
2016-08-19 09:07:27 -04:00
|
|
|
|
2016-08-19 10:02:38 -04:00
|
|
|
@cachedInlineCallbacks(num_args=2, cache_context=True)
|
2019-04-03 05:07:29 -04:00
|
|
|
def _bulk_get_push_rules_for_room(
|
|
|
|
self, room_id, state_group, current_state_ids, cache_context, event=None
|
|
|
|
):
|
2016-08-19 09:07:27 -04:00
|
|
|
# We don't use `state_group`, its there so that we can cache based
|
|
|
|
# on it. However, its important that its never None, since two current_state's
|
|
|
|
# with a state_group of None are likely to be different.
|
|
|
|
# See bulk_get_push_rules_for_room for how we work around this.
|
|
|
|
assert state_group is not None
|
|
|
|
|
|
|
|
# We also will want to generate notifs for other people in the room so
|
|
|
|
# their unread countss are correct in the event stream, but to avoid
|
|
|
|
# generating them for bot / AS users etc, we only do so for people who've
|
|
|
|
# sent a read receipt into the room.
|
2016-08-25 12:32:22 -04:00
|
|
|
|
2016-09-05 05:43:32 -04:00
|
|
|
users_in_room = yield self._get_joined_users_from_context(
|
2019-04-03 05:07:29 -04:00
|
|
|
room_id,
|
|
|
|
state_group,
|
|
|
|
current_state_ids,
|
2016-09-05 05:02:38 -04:00
|
|
|
on_invalidate=cache_context.invalidate,
|
|
|
|
event=event,
|
2016-08-19 09:07:27 -04:00
|
|
|
)
|
|
|
|
|
2016-11-23 06:01:01 -05:00
|
|
|
# We ignore app service users for now. This is so that we don't fill
|
|
|
|
# up the `get_if_users_have_pushers` cache with AS entries that we
|
|
|
|
# know don't have pushers, nor even read receipts.
|
|
|
|
local_users_in_room = set(
|
2019-04-03 05:07:29 -04:00
|
|
|
u
|
|
|
|
for u in users_in_room
|
2016-11-23 06:01:01 -05:00
|
|
|
if self.hs.is_mine_id(u)
|
2017-05-18 11:34:41 -04:00
|
|
|
and not self.get_if_app_services_interested_in_user(u)
|
2016-11-23 06:01:01 -05:00
|
|
|
)
|
2016-09-05 05:43:32 -04:00
|
|
|
|
2016-08-19 09:07:27 -04:00
|
|
|
# users in the room who have pushers need to get push rules run because
|
|
|
|
# that's how their pushers work
|
|
|
|
if_users_with_pushers = yield self.get_if_users_have_pushers(
|
2019-04-03 05:07:29 -04:00
|
|
|
local_users_in_room, on_invalidate=cache_context.invalidate
|
2016-08-19 09:07:27 -04:00
|
|
|
)
|
|
|
|
user_ids = set(
|
|
|
|
uid for uid, have_pusher in if_users_with_pushers.items() if have_pusher
|
|
|
|
)
|
|
|
|
|
|
|
|
users_with_receipts = yield self.get_users_with_read_receipts_in_room(
|
2019-04-03 05:07:29 -04:00
|
|
|
room_id, on_invalidate=cache_context.invalidate
|
2016-08-19 09:07:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# any users with pushers must be ours: they have pushers
|
|
|
|
for uid in users_with_receipts:
|
|
|
|
if uid in local_users_in_room:
|
|
|
|
user_ids.add(uid)
|
|
|
|
|
|
|
|
rules_by_user = yield self.bulk_get_push_rules(
|
2019-04-03 05:07:29 -04:00
|
|
|
user_ids, on_invalidate=cache_context.invalidate
|
2016-08-19 09:07:27 -04:00
|
|
|
)
|
|
|
|
|
2017-05-08 08:07:41 -04:00
|
|
|
rules_by_user = {k: v for k, v in rules_by_user.items() if v is not None}
|
2016-08-19 09:07:27 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return rules_by_user
|
2016-08-19 09:07:27 -04:00
|
|
|
|
2019-04-03 05:07:29 -04:00
|
|
|
@cachedList(
|
|
|
|
cached_method_name="get_push_rules_enabled_for_user",
|
|
|
|
list_name="user_ids",
|
|
|
|
num_args=1,
|
|
|
|
inlineCallbacks=True,
|
|
|
|
)
|
2016-01-22 09:58:19 -05:00
|
|
|
def bulk_get_push_rules_enabled(self, user_ids):
|
|
|
|
if not user_ids:
|
2019-07-23 09:00:55 -04:00
|
|
|
return {}
|
2016-01-22 09:58:19 -05:00
|
|
|
|
2019-04-03 05:07:29 -04:00
|
|
|
results = {user_id: {} for user_id in user_ids}
|
2016-01-22 09:58:19 -05:00
|
|
|
|
2016-01-25 08:36:02 -05:00
|
|
|
rows = yield self._simple_select_many_batch(
|
|
|
|
table="push_rules_enable",
|
|
|
|
column="user_name",
|
|
|
|
iterable=user_ids,
|
2019-04-03 05:07:29 -04:00
|
|
|
retcols=("user_name", "rule_id", "enabled"),
|
2016-01-25 08:36:02 -05:00
|
|
|
desc="bulk_get_push_rules_enabled",
|
|
|
|
)
|
|
|
|
for row in rows:
|
2019-06-20 05:32:02 -04:00
|
|
|
enabled = bool(row["enabled"])
|
|
|
|
results.setdefault(row["user_name"], {})[row["rule_id"]] = enabled
|
2019-07-23 09:00:55 -04:00
|
|
|
return results
|
2016-01-22 09:58:19 -05:00
|
|
|
|
2018-02-27 07:01:36 -05:00
|
|
|
|
|
|
|
class PushRuleStore(PushRulesWorkerStore):
|
2016-03-01 08:35:37 -05:00
|
|
|
@defer.inlineCallbacks
|
2016-02-16 10:53:38 -05:00
|
|
|
def add_push_rule(
|
2019-04-03 05:07:29 -04:00
|
|
|
self,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
priority_class,
|
|
|
|
conditions,
|
|
|
|
actions,
|
|
|
|
before=None,
|
|
|
|
after=None,
|
2016-02-16 10:53:38 -05:00
|
|
|
):
|
|
|
|
conditions_json = json.dumps(conditions)
|
|
|
|
actions_json = json.dumps(actions)
|
2016-03-04 11:15:23 -05:00
|
|
|
with self._push_rules_stream_id_gen.get_next() as ids:
|
|
|
|
stream_id, event_stream_ordering = ids
|
2016-03-01 08:35:37 -05:00
|
|
|
if before or after:
|
|
|
|
yield self.runInteraction(
|
|
|
|
"_add_push_rule_relative_txn",
|
|
|
|
self._add_push_rule_relative_txn,
|
2019-04-03 05:07:29 -04:00
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
priority_class,
|
|
|
|
conditions_json,
|
|
|
|
actions_json,
|
|
|
|
before,
|
|
|
|
after,
|
2016-03-01 08:35:37 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
yield self.runInteraction(
|
|
|
|
"_add_push_rule_highest_priority_txn",
|
|
|
|
self._add_push_rule_highest_priority_txn,
|
2019-04-03 05:07:29 -04:00
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
priority_class,
|
|
|
|
conditions_json,
|
|
|
|
actions_json,
|
2016-03-01 08:35:37 -05:00
|
|
|
)
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2016-02-16 10:53:38 -05:00
|
|
|
def _add_push_rule_relative_txn(
|
2019-04-03 05:07:29 -04:00
|
|
|
self,
|
|
|
|
txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
priority_class,
|
|
|
|
conditions_json,
|
|
|
|
actions_json,
|
|
|
|
before,
|
|
|
|
after,
|
2016-02-16 10:53:38 -05:00
|
|
|
):
|
|
|
|
# Lock the table since otherwise we'll have annoying races between the
|
|
|
|
# SELECT here and the UPSERT below.
|
|
|
|
self.database_engine.lock_table(txn, "push_rules")
|
|
|
|
|
2016-01-27 05:24:20 -05:00
|
|
|
relative_to_rule = before or after
|
2015-05-11 06:22:54 -04:00
|
|
|
|
|
|
|
res = self._simple_select_one_txn(
|
|
|
|
txn,
|
2016-01-12 09:22:24 -05:00
|
|
|
table="push_rules",
|
2019-04-03 05:07:29 -04:00
|
|
|
keyvalues={"user_name": user_id, "rule_id": relative_to_rule},
|
2015-05-11 06:22:54 -04:00
|
|
|
retcols=["priority_class", "priority"],
|
|
|
|
allow_none=True,
|
2015-01-22 12:37:12 -05:00
|
|
|
)
|
2015-05-11 06:22:54 -04:00
|
|
|
|
2015-01-22 12:37:12 -05:00
|
|
|
if not res:
|
2015-02-10 11:30:48 -05:00
|
|
|
raise RuleNotFoundException(
|
|
|
|
"before/after rule not found: %s" % (relative_to_rule,)
|
|
|
|
)
|
2015-05-11 06:22:54 -04:00
|
|
|
|
2016-02-16 10:53:38 -05:00
|
|
|
base_priority_class = res["priority_class"]
|
2015-05-11 06:22:54 -04:00
|
|
|
base_rule_priority = res["priority"]
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2016-02-16 10:53:38 -05:00
|
|
|
if base_priority_class != priority_class:
|
2015-01-22 12:37:12 -05:00
|
|
|
raise InconsistentRuleException(
|
|
|
|
"Given priority class does not match class of relative rule"
|
|
|
|
)
|
|
|
|
|
2016-02-16 10:53:38 -05:00
|
|
|
if before:
|
|
|
|
# Higher priority rules are executed first, So adding a rule before
|
|
|
|
# a rule means giving it a higher priority than that rule.
|
|
|
|
new_rule_priority = base_rule_priority + 1
|
2015-01-22 12:37:12 -05:00
|
|
|
else:
|
2016-02-16 10:53:38 -05:00
|
|
|
# We increment the priority of the existing rules to make space for
|
|
|
|
# the new rule. Therefore if we want this rule to appear after
|
|
|
|
# an existing rule we give it the priority of the existing rule,
|
|
|
|
# and then increment the priority of the existing rule.
|
|
|
|
new_rule_priority = base_rule_priority
|
2015-01-22 12:37:12 -05:00
|
|
|
|
|
|
|
sql = (
|
2016-02-16 10:53:38 -05:00
|
|
|
"UPDATE push_rules SET priority = priority + 1"
|
|
|
|
" WHERE user_name = ? AND priority_class = ? AND priority >= ?"
|
2015-01-22 12:37:12 -05:00
|
|
|
)
|
2016-02-16 10:53:38 -05:00
|
|
|
|
2016-01-13 08:08:59 -05:00
|
|
|
txn.execute(sql, (user_id, priority_class, new_rule_priority))
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2016-02-16 10:53:38 -05:00
|
|
|
self._upsert_push_rule_txn(
|
2019-04-03 05:07:29 -04:00
|
|
|
txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
priority_class,
|
|
|
|
new_rule_priority,
|
|
|
|
conditions_json,
|
|
|
|
actions_json,
|
2015-05-22 05:39:45 -04:00
|
|
|
)
|
|
|
|
|
2016-02-16 10:53:38 -05:00
|
|
|
def _add_push_rule_highest_priority_txn(
|
2019-04-03 05:07:29 -04:00
|
|
|
self,
|
|
|
|
txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
priority_class,
|
|
|
|
conditions_json,
|
|
|
|
actions_json,
|
2016-02-16 10:53:38 -05:00
|
|
|
):
|
|
|
|
# Lock the table since otherwise we'll have annoying races between the
|
|
|
|
# SELECT here and the UPSERT below.
|
|
|
|
self.database_engine.lock_table(txn, "push_rules")
|
2015-05-21 10:14:26 -04:00
|
|
|
|
2015-01-22 12:37:12 -05:00
|
|
|
# find the highest priority rule in that class
|
|
|
|
sql = (
|
2016-01-12 09:22:24 -05:00
|
|
|
"SELECT COUNT(*), MAX(priority) FROM push_rules"
|
2015-01-22 12:37:12 -05:00
|
|
|
" WHERE user_name = ? and priority_class = ?"
|
|
|
|
)
|
2016-01-13 08:08:59 -05:00
|
|
|
txn.execute(sql, (user_id, priority_class))
|
2015-01-22 12:37:12 -05:00
|
|
|
res = txn.fetchall()
|
|
|
|
(how_many, highest_prio) = res[0]
|
|
|
|
|
|
|
|
new_prio = 0
|
|
|
|
if how_many > 0:
|
|
|
|
new_prio = highest_prio + 1
|
|
|
|
|
2016-02-16 10:53:38 -05:00
|
|
|
self._upsert_push_rule_txn(
|
|
|
|
txn,
|
2019-04-03 05:07:29 -04:00
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
priority_class,
|
|
|
|
new_prio,
|
|
|
|
conditions_json,
|
|
|
|
actions_json,
|
2016-02-16 10:53:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
def _upsert_push_rule_txn(
|
2019-04-03 05:07:29 -04:00
|
|
|
self,
|
|
|
|
txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
priority_class,
|
|
|
|
priority,
|
|
|
|
conditions_json,
|
|
|
|
actions_json,
|
|
|
|
update_stream=True,
|
2016-02-16 10:53:38 -05:00
|
|
|
):
|
|
|
|
"""Specialised version of _simple_upsert_txn that picks a push_rule_id
|
|
|
|
using the _push_rule_id_gen if it needs to insert the rule. It assumes
|
|
|
|
that the "push_rules" table is locked"""
|
|
|
|
|
|
|
|
sql = (
|
|
|
|
"UPDATE push_rules"
|
|
|
|
" SET priority_class = ?, priority = ?, conditions = ?, actions = ?"
|
|
|
|
" WHERE user_name = ? AND rule_id = ?"
|
|
|
|
)
|
|
|
|
|
2019-04-03 05:07:29 -04:00
|
|
|
txn.execute(
|
|
|
|
sql,
|
|
|
|
(priority_class, priority, conditions_json, actions_json, user_id, rule_id),
|
|
|
|
)
|
2016-02-16 10:53:38 -05:00
|
|
|
|
|
|
|
if txn.rowcount == 0:
|
|
|
|
# We didn't update a row with the given rule_id so insert one
|
2016-03-01 09:32:56 -05:00
|
|
|
push_rule_id = self._push_rule_id_gen.get_next()
|
2016-02-16 10:53:38 -05:00
|
|
|
|
|
|
|
self._simple_insert_txn(
|
|
|
|
txn,
|
|
|
|
table="push_rules",
|
|
|
|
values={
|
|
|
|
"id": push_rule_id,
|
|
|
|
"user_name": user_id,
|
|
|
|
"rule_id": rule_id,
|
|
|
|
"priority_class": priority_class,
|
|
|
|
"priority": priority,
|
|
|
|
"conditions": conditions_json,
|
|
|
|
"actions": actions_json,
|
|
|
|
},
|
|
|
|
)
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2016-03-01 08:35:37 -05:00
|
|
|
if update_stream:
|
2016-03-04 09:44:01 -05:00
|
|
|
self._insert_push_rules_update_txn(
|
2019-04-03 05:07:29 -04:00
|
|
|
txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
2016-03-04 09:44:01 -05:00
|
|
|
op="ADD",
|
|
|
|
data={
|
2016-03-01 08:35:37 -05:00
|
|
|
"priority_class": priority_class,
|
|
|
|
"priority": priority,
|
|
|
|
"conditions": conditions_json,
|
|
|
|
"actions": actions_json,
|
2019-04-03 05:07:29 -04:00
|
|
|
},
|
2016-03-01 08:35:37 -05:00
|
|
|
)
|
|
|
|
|
2015-01-23 12:49:37 -05:00
|
|
|
@defer.inlineCallbacks
|
2016-01-13 08:08:59 -05:00
|
|
|
def delete_push_rule(self, user_id, rule_id):
|
2015-01-28 13:06:04 -05: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 08:08:59 -05:00
|
|
|
user_id (str): The matrix ID of the push rule owner
|
2015-01-28 13:06:04 -05:00
|
|
|
rule_id (str): The rule_id of the rule to be deleted
|
|
|
|
"""
|
2019-04-03 05:07:29 -04:00
|
|
|
|
2016-03-04 11:15:23 -05:00
|
|
|
def delete_push_rule_txn(txn, stream_id, event_stream_ordering):
|
2016-03-01 08:35:37 -05:00
|
|
|
self._simple_delete_one_txn(
|
2019-06-20 05:32:02 -04:00
|
|
|
txn, "push_rules", {"user_name": user_id, "rule_id": rule_id}
|
2016-03-01 08:35:37 -05:00
|
|
|
)
|
2016-03-04 09:44:01 -05:00
|
|
|
|
|
|
|
self._insert_push_rules_update_txn(
|
2019-04-03 05:07:29 -04:00
|
|
|
txn, stream_id, event_stream_ordering, user_id, rule_id, op="DELETE"
|
2016-03-01 08:35:37 -05:00
|
|
|
)
|
2015-05-22 05:39:45 -04:00
|
|
|
|
2016-03-04 11:15:23 -05:00
|
|
|
with self._push_rules_stream_id_gen.get_next() as ids:
|
|
|
|
stream_id, event_stream_ordering = ids
|
2016-03-01 08:35:37 -05:00
|
|
|
yield self.runInteraction(
|
2019-04-03 05:07:29 -04:00
|
|
|
"delete_push_rule",
|
|
|
|
delete_push_rule_txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
2016-03-01 08:35:37 -05:00
|
|
|
)
|
2015-01-22 12:53:30 -05:00
|
|
|
|
2015-02-26 13:07:44 -05:00
|
|
|
@defer.inlineCallbacks
|
2016-01-13 08:08:59 -05:00
|
|
|
def set_push_rule_enabled(self, user_id, rule_id, enabled):
|
2016-03-04 11:15:23 -05:00
|
|
|
with self._push_rules_stream_id_gen.get_next() as ids:
|
|
|
|
stream_id, event_stream_ordering = ids
|
2016-03-01 08:35:37 -05:00
|
|
|
yield self.runInteraction(
|
|
|
|
"_set_push_rule_enabled_txn",
|
|
|
|
self._set_push_rule_enabled_txn,
|
2019-04-03 05:07:29 -04:00
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
enabled,
|
2016-03-01 08:35:37 -05:00
|
|
|
)
|
2015-05-14 06:44:03 -04:00
|
|
|
|
2016-03-01 08:35:37 -05:00
|
|
|
def _set_push_rule_enabled_txn(
|
2016-03-04 11:15:23 -05:00
|
|
|
self, txn, stream_id, event_stream_ordering, user_id, rule_id, enabled
|
2016-03-01 08:35:37 -05:00
|
|
|
):
|
2016-03-01 09:32:56 -05:00
|
|
|
new_id = self._push_rules_enable_id_gen.get_next()
|
2015-05-14 06:44:03 -04:00
|
|
|
self._simple_upsert_txn(
|
|
|
|
txn,
|
2016-01-12 09:22:24 -05:00
|
|
|
"push_rules_enable",
|
2019-06-20 05:32:02 -04:00
|
|
|
{"user_name": user_id, "rule_id": rule_id},
|
|
|
|
{"enabled": 1 if enabled else 0},
|
|
|
|
{"id": new_id},
|
2015-03-10 13:26:25 -04:00
|
|
|
)
|
2016-03-01 08:35:37 -05:00
|
|
|
|
2016-03-04 09:44:01 -05:00
|
|
|
self._insert_push_rules_update_txn(
|
2019-04-03 05:07:29 -04:00
|
|
|
txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
op="ENABLE" if enabled else "DISABLE",
|
2015-05-22 05:39:45 -04:00
|
|
|
)
|
2015-01-28 09:51:01 -05:00
|
|
|
|
2016-03-01 08:35:37 -05:00
|
|
|
@defer.inlineCallbacks
|
2016-02-26 09:28:19 -05:00
|
|
|
def set_push_rule_actions(self, user_id, rule_id, actions, is_default_rule):
|
|
|
|
actions_json = json.dumps(actions)
|
|
|
|
|
2016-03-04 11:15:23 -05:00
|
|
|
def set_push_rule_actions_txn(txn, stream_id, event_stream_ordering):
|
2016-02-26 09:28:19 -05:00
|
|
|
if is_default_rule:
|
|
|
|
# Add a dummy rule to the rules table with the user specified
|
|
|
|
# actions.
|
|
|
|
priority_class = -1
|
|
|
|
priority = 1
|
|
|
|
self._upsert_push_rule_txn(
|
2019-04-03 05:07:29 -04:00
|
|
|
txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
priority_class,
|
|
|
|
priority,
|
|
|
|
"[]",
|
|
|
|
actions_json,
|
|
|
|
update_stream=False,
|
2016-02-26 09:28:19 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
self._simple_update_one_txn(
|
|
|
|
txn,
|
|
|
|
"push_rules",
|
2019-06-20 05:32:02 -04:00
|
|
|
{"user_name": user_id, "rule_id": rule_id},
|
|
|
|
{"actions": actions_json},
|
2016-02-26 09:28:19 -05:00
|
|
|
)
|
|
|
|
|
2016-03-04 09:44:01 -05:00
|
|
|
self._insert_push_rules_update_txn(
|
2019-04-03 05:07:29 -04:00
|
|
|
txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
|
|
|
user_id,
|
|
|
|
rule_id,
|
|
|
|
op="ACTIONS",
|
|
|
|
data={"actions": actions_json},
|
2016-03-01 08:35:37 -05:00
|
|
|
)
|
|
|
|
|
2016-03-04 11:15:23 -05:00
|
|
|
with self._push_rules_stream_id_gen.get_next() as ids:
|
|
|
|
stream_id, event_stream_ordering = ids
|
2016-03-01 08:35:37 -05:00
|
|
|
yield self.runInteraction(
|
2019-04-03 05:07:29 -04:00
|
|
|
"set_push_rule_actions",
|
|
|
|
set_push_rule_actions_txn,
|
|
|
|
stream_id,
|
|
|
|
event_stream_ordering,
|
2016-03-01 08:35:37 -05:00
|
|
|
)
|
|
|
|
|
2016-03-04 09:44:01 -05:00
|
|
|
def _insert_push_rules_update_txn(
|
2016-03-04 11:15:23 -05:00
|
|
|
self, txn, stream_id, event_stream_ordering, user_id, rule_id, op, data=None
|
2016-03-04 09:44:01 -05:00
|
|
|
):
|
|
|
|
values = {
|
|
|
|
"stream_id": stream_id,
|
2016-03-04 11:15:23 -05:00
|
|
|
"event_stream_ordering": event_stream_ordering,
|
2016-03-04 09:44:01 -05:00
|
|
|
"user_id": user_id,
|
|
|
|
"rule_id": rule_id,
|
|
|
|
"op": op,
|
|
|
|
}
|
|
|
|
if data is not None:
|
|
|
|
values.update(data)
|
|
|
|
|
|
|
|
self._simple_insert_txn(txn, "push_rules_stream", values=values)
|
|
|
|
|
2019-04-03 05:07:29 -04:00
|
|
|
txn.call_after(self.get_push_rules_for_user.invalidate, (user_id,))
|
|
|
|
txn.call_after(self.get_push_rules_enabled_for_user.invalidate, (user_id,))
|
2016-03-04 09:44:01 -05:00
|
|
|
txn.call_after(
|
|
|
|
self.push_rules_stream_cache.entity_has_changed, user_id, stream_id
|
|
|
|
)
|
|
|
|
|
2016-03-01 08:35:37 -05:00
|
|
|
def get_all_push_rule_updates(self, last_id, current_id, limit):
|
|
|
|
"""Get all the push rules changes that have happend on the server"""
|
2016-06-08 06:33:30 -04:00
|
|
|
if last_id == current_id:
|
|
|
|
return defer.succeed([])
|
|
|
|
|
2016-03-01 08:35:37 -05:00
|
|
|
def get_all_push_rule_updates_txn(txn):
|
|
|
|
sql = (
|
2016-03-04 11:15:23 -05:00
|
|
|
"SELECT stream_id, event_stream_ordering, user_id, rule_id,"
|
2016-03-01 08:35:37 -05:00
|
|
|
" op, priority_class, priority, conditions, actions"
|
|
|
|
" FROM push_rules_stream"
|
2016-03-04 09:44:01 -05:00
|
|
|
" WHERE ? < stream_id AND stream_id <= ?"
|
2016-03-01 08:35:37 -05:00
|
|
|
" ORDER BY stream_id ASC LIMIT ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (last_id, current_id, limit))
|
|
|
|
return txn.fetchall()
|
2019-04-03 05:07:29 -04:00
|
|
|
|
2016-02-26 09:28:19 -05:00
|
|
|
return self.runInteraction(
|
2016-03-01 08:35:37 -05:00
|
|
|
"get_all_push_rule_updates", get_all_push_rule_updates_txn
|
2016-02-26 09:28:19 -05:00
|
|
|
)
|
|
|
|
|
2016-03-02 12:26:20 -05:00
|
|
|
def get_push_rules_stream_token(self):
|
|
|
|
"""Get the position of the push rules stream.
|
|
|
|
Returns a pair of a stream id for the push_rules stream and the
|
|
|
|
room stream ordering it corresponds to."""
|
2016-04-01 08:29:05 -04:00
|
|
|
return self._push_rules_stream_id_gen.get_current_token()
|
2016-03-02 12:26:20 -05:00
|
|
|
|
2018-02-21 05:39:27 -05:00
|
|
|
def get_max_push_rules_stream_id(self):
|
|
|
|
return self.get_push_rules_stream_token()[0]
|
2016-03-04 09:44:01 -05:00
|
|
|
|
2015-03-02 10:58:12 -05:00
|
|
|
|
2015-01-22 12:37:12 -05:00
|
|
|
class RuleNotFoundException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InconsistentRuleException(Exception):
|
|
|
|
pass
|