2015-12-22 08:19:34 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2015 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.
|
|
|
|
|
|
|
|
import logging
|
2016-01-18 07:09:47 -07:00
|
|
|
import ujson as json
|
2015-12-22 08:19:34 -07:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2016-03-08 04:45:50 -07:00
|
|
|
from .baserules import list_with_base_rules
|
|
|
|
from .push_rule_evaluator import PushRuleEvaluatorForEvent
|
2016-01-18 07:09:47 -07:00
|
|
|
|
|
|
|
from synapse.api.constants import EventTypes
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2016-01-06 04:38:09 -07:00
|
|
|
|
2015-12-22 08:19:34 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def decode_rule_json(rule):
|
|
|
|
rule['conditions'] = json.loads(rule['conditions'])
|
|
|
|
rule['actions'] = json.loads(rule['actions'])
|
|
|
|
return rule
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-01-18 07:09:47 -07:00
|
|
|
def _get_rules(room_id, user_ids, store):
|
|
|
|
rules_by_user = yield store.bulk_get_push_rules(user_ids)
|
2016-01-22 07:58:19 -07:00
|
|
|
rules_enabled_by_user = yield store.bulk_get_push_rules_enabled(user_ids)
|
2016-01-19 09:01:05 -07:00
|
|
|
|
2015-12-22 08:19:34 -07:00
|
|
|
rules_by_user = {
|
2016-03-08 04:45:50 -07:00
|
|
|
uid: list_with_base_rules([
|
2016-01-18 03:09:14 -07:00
|
|
|
decode_rule_json(rule_list)
|
|
|
|
for rule_list in rules_by_user.get(uid, [])
|
|
|
|
])
|
2016-01-18 07:09:47 -07:00
|
|
|
for uid in user_ids
|
2015-12-22 08:19:34 -07:00
|
|
|
}
|
2016-01-22 07:58:19 -07:00
|
|
|
|
|
|
|
# We apply the rules-enabled map here: bulk_get_push_rules doesn't
|
|
|
|
# fetch disabled rules, but this won't account for any server default
|
|
|
|
# rules the user has disabled, so we need to do this too.
|
|
|
|
for uid in user_ids:
|
2016-01-22 08:13:44 -07:00
|
|
|
if uid not in rules_enabled_by_user:
|
|
|
|
continue
|
|
|
|
|
2016-01-22 07:58:19 -07:00
|
|
|
user_enabled_map = rules_enabled_by_user[uid]
|
|
|
|
|
2016-01-22 09:03:55 -07:00
|
|
|
for i, rule in enumerate(rules_by_user[uid]):
|
2016-01-22 07:58:19 -07:00
|
|
|
rule_id = rule['rule_id']
|
|
|
|
|
|
|
|
if rule_id in user_enabled_map:
|
2016-01-22 09:03:55 -07:00
|
|
|
if rule.get('enabled', True) != bool(user_enabled_map[rule_id]):
|
|
|
|
# Rules are cached across users.
|
|
|
|
rule = dict(rule)
|
|
|
|
rule['enabled'] = bool(user_enabled_map[rule_id])
|
|
|
|
rules_by_user[uid][i] = rule
|
2016-01-22 07:58:19 -07:00
|
|
|
|
2016-01-18 07:09:47 -07:00
|
|
|
defer.returnValue(rules_by_user)
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-01-19 09:01:05 -07:00
|
|
|
def evaluator_for_room_id(room_id, hs, store):
|
|
|
|
results = yield store.get_receipts_for_room(room_id, "m.read")
|
|
|
|
user_ids = [
|
|
|
|
row["user_id"] for row in results
|
|
|
|
if hs.is_mine_id(row["user_id"])
|
|
|
|
]
|
|
|
|
rules_by_user = yield _get_rules(room_id, user_ids, store)
|
2015-12-22 08:19:34 -07:00
|
|
|
|
|
|
|
defer.returnValue(BulkPushRuleEvaluator(
|
2016-01-19 09:01:05 -07:00
|
|
|
room_id, rules_by_user, user_ids, store
|
2015-12-22 08:19:34 -07:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
class BulkPushRuleEvaluator:
|
2015-12-22 10:19:22 -07:00
|
|
|
"""
|
|
|
|
Runs push rules for all users in a room.
|
|
|
|
This is faster than running PushRuleEvaluator for each user because it
|
|
|
|
fetches all the rules for all the users in one (batched) db query
|
2016-01-05 06:32:39 -07:00
|
|
|
rather than doing multiple queries per-user. It currently uses
|
2015-12-22 10:19:22 -07:00
|
|
|
the same logic to run the actual rules, but could be optimised further
|
|
|
|
(see https://matrix.org/jira/browse/SYN-562)
|
|
|
|
"""
|
2016-01-18 07:09:47 -07:00
|
|
|
def __init__(self, room_id, rules_by_user, users_in_room, store):
|
2015-12-22 08:19:34 -07:00
|
|
|
self.room_id = room_id
|
|
|
|
self.rules_by_user = rules_by_user
|
|
|
|
self.users_in_room = users_in_room
|
2016-01-06 04:38:09 -07:00
|
|
|
self.store = store
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2016-01-06 04:38:09 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-02-09 09:01:40 -07:00
|
|
|
def action_for_event_by_user(self, event, handler, current_state):
|
2015-12-22 08:19:34 -07:00
|
|
|
actions_by_user = {}
|
|
|
|
|
2016-01-18 07:09:47 -07:00
|
|
|
users_dict = yield self.store.are_guests(self.rules_by_user.keys())
|
|
|
|
|
2016-02-19 04:41:02 -07:00
|
|
|
filtered_by_user = yield handler.filter_events_for_clients(
|
2016-02-09 09:01:40 -07:00
|
|
|
users_dict.items(), [event], {event.event_id: current_state}
|
2016-01-18 07:09:47 -07:00
|
|
|
)
|
|
|
|
|
2016-03-22 07:52:45 -06:00
|
|
|
room_members = yield self.store.get_users_in_room(self.room_id)
|
|
|
|
|
|
|
|
evaluator = PushRuleEvaluatorForEvent(event, len(room_members))
|
2016-01-18 07:09:47 -07:00
|
|
|
|
|
|
|
condition_cache = {}
|
|
|
|
|
|
|
|
display_names = {}
|
2016-02-09 09:01:40 -07:00
|
|
|
for ev in current_state.values():
|
2016-01-18 07:09:47 -07:00
|
|
|
nm = ev.content.get("displayname", None)
|
|
|
|
if nm and ev.type == EventTypes.Member:
|
|
|
|
display_names[ev.state_key] = nm
|
|
|
|
|
2015-12-22 08:19:34 -07:00
|
|
|
for uid, rules in self.rules_by_user.items():
|
2016-01-18 07:09:47 -07:00
|
|
|
display_name = display_names.get(uid, None)
|
|
|
|
|
|
|
|
filtered = filtered_by_user[uid]
|
2016-01-06 04:38:09 -07:00
|
|
|
if len(filtered) == 0:
|
|
|
|
continue
|
|
|
|
|
2016-01-20 06:44:04 -07:00
|
|
|
if filtered[0].sender == uid:
|
2016-01-20 06:24:59 -07:00
|
|
|
continue
|
|
|
|
|
2015-12-22 08:19:34 -07:00
|
|
|
for rule in rules:
|
|
|
|
if 'enabled' in rule and not rule['enabled']:
|
|
|
|
continue
|
|
|
|
|
2016-01-18 07:09:47 -07:00
|
|
|
matches = _condition_checker(
|
2016-01-18 03:09:14 -07:00
|
|
|
evaluator, rule['conditions'], uid, display_name, condition_cache
|
2016-01-18 07:09:47 -07:00
|
|
|
)
|
|
|
|
if matches:
|
2015-12-22 08:19:34 -07:00
|
|
|
actions = [x for x in rule['actions'] if x != 'dont_notify']
|
2016-01-22 10:21:58 -07:00
|
|
|
if actions and 'notify' in actions:
|
2015-12-22 08:19:34 -07:00
|
|
|
actions_by_user[uid] = actions
|
|
|
|
break
|
2016-01-06 04:38:09 -07:00
|
|
|
defer.returnValue(actions_by_user)
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2016-01-18 07:09:47 -07:00
|
|
|
|
2016-01-18 03:09:14 -07:00
|
|
|
def _condition_checker(evaluator, conditions, uid, display_name, cache):
|
2016-01-18 07:09:47 -07:00
|
|
|
for cond in conditions:
|
|
|
|
_id = cond.get("_id", None)
|
|
|
|
if _id:
|
|
|
|
res = cache.get(_id, None)
|
|
|
|
if res is False:
|
2016-01-19 07:24:59 -07:00
|
|
|
return False
|
2016-01-18 07:09:47 -07:00
|
|
|
elif res is True:
|
|
|
|
continue
|
|
|
|
|
2016-02-18 09:05:13 -07:00
|
|
|
res = evaluator.matches(cond, uid, display_name)
|
2016-01-18 07:09:47 -07:00
|
|
|
if _id:
|
2016-01-19 09:01:05 -07:00
|
|
|
cache[_id] = bool(res)
|
2016-01-18 07:09:47 -07:00
|
|
|
|
2016-01-19 07:22:02 -07:00
|
|
|
if not res:
|
2016-01-18 07:09:47 -07:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|