2015-12-22 16:19:34 +01: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
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2016-03-08 12:45:50 +01:00
|
|
|
from .push_rule_evaluator import PushRuleEvaluatorForEvent
|
2016-01-18 15:09:47 +01:00
|
|
|
|
2016-08-25 18:32:22 +02:00
|
|
|
from synapse.api.constants import EventTypes
|
|
|
|
from synapse.visibility import filter_events_for_clients_context
|
2015-12-22 16:19:34 +01:00
|
|
|
|
2016-01-06 12:38:09 +01:00
|
|
|
|
2015-12-22 16:19:34 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-01-18 15:09:47 +01:00
|
|
|
def _get_rules(room_id, user_ids, store):
|
|
|
|
rules_by_user = yield store.bulk_get_push_rules(user_ids)
|
2016-01-19 17:01:05 +01:00
|
|
|
|
2016-05-31 14:46:58 +02:00
|
|
|
rules_by_user = {k: v for k, v in rules_by_user.items() if v is not None}
|
|
|
|
|
2016-01-18 15:09:47 +01:00
|
|
|
defer.returnValue(rules_by_user)
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-08-25 18:32:22 +02:00
|
|
|
def evaluator_for_event(event, hs, store, context):
|
2016-08-19 15:07:27 +02:00
|
|
|
rules_by_user = yield store.bulk_get_push_rules_for_room(
|
2016-09-05 11:02:38 +02:00
|
|
|
event, context
|
2016-06-01 12:08:45 +02:00
|
|
|
)
|
|
|
|
|
2016-04-08 16:29:59 +02:00
|
|
|
# if this event is an invite event, we may need to run rules for the user
|
|
|
|
# who's been invited, otherwise they won't get told they've been invited
|
|
|
|
if event.type == 'm.room.member' and event.content['membership'] == 'invite':
|
|
|
|
invited_user = event.state_key
|
|
|
|
if invited_user and hs.is_mine_id(invited_user):
|
|
|
|
has_pusher = yield store.user_has_pusher(invited_user)
|
|
|
|
if has_pusher:
|
2016-08-19 15:07:27 +02:00
|
|
|
rules_by_user[invited_user] = yield store.get_push_rules_for_user(
|
|
|
|
invited_user
|
|
|
|
)
|
2015-12-22 16:19:34 +01:00
|
|
|
|
|
|
|
defer.returnValue(BulkPushRuleEvaluator(
|
2016-08-19 15:07:27 +02:00
|
|
|
event.room_id, rules_by_user, store
|
2015-12-22 16:19:34 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
class BulkPushRuleEvaluator:
|
2015-12-22 18:19:22 +01: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 14:32:39 +01:00
|
|
|
rather than doing multiple queries per-user. It currently uses
|
2015-12-22 18:19:22 +01:00
|
|
|
the same logic to run the actual rules, but could be optimised further
|
|
|
|
(see https://matrix.org/jira/browse/SYN-562)
|
|
|
|
"""
|
2016-08-19 15:07:27 +02:00
|
|
|
def __init__(self, room_id, rules_by_user, store):
|
2015-12-22 16:19:34 +01:00
|
|
|
self.room_id = room_id
|
|
|
|
self.rules_by_user = rules_by_user
|
2016-01-06 12:38:09 +01:00
|
|
|
self.store = store
|
2015-12-22 16:19:34 +01:00
|
|
|
|
2016-01-06 12:38:09 +01:00
|
|
|
@defer.inlineCallbacks
|
2016-08-25 18:32:22 +02:00
|
|
|
def action_for_event_by_user(self, event, context):
|
2015-12-22 16:19:34 +01:00
|
|
|
actions_by_user = {}
|
|
|
|
|
2016-04-06 16:42:15 +02:00
|
|
|
# None of these users can be peeking since this list of users comes
|
|
|
|
# from the set of users in the room, so we know for sure they're all
|
|
|
|
# actually in the room.
|
|
|
|
user_tuples = [
|
|
|
|
(u, False) for u in self.rules_by_user.keys()
|
|
|
|
]
|
2016-01-18 15:09:47 +01:00
|
|
|
|
2016-08-25 18:32:22 +02:00
|
|
|
filtered_by_user = yield filter_events_for_clients_context(
|
|
|
|
self.store, user_tuples, [event], {event.event_id: context}
|
2016-01-18 15:09:47 +01:00
|
|
|
)
|
|
|
|
|
2016-08-25 18:32:22 +02:00
|
|
|
room_members = yield self.store.get_joined_users_from_context(
|
2016-08-31 14:55:02 +02:00
|
|
|
event, context
|
2016-06-01 12:08:45 +02:00
|
|
|
)
|
2016-03-22 14:52:45 +01:00
|
|
|
|
|
|
|
evaluator = PushRuleEvaluatorForEvent(event, len(room_members))
|
2016-01-18 15:09:47 +01:00
|
|
|
|
|
|
|
condition_cache = {}
|
|
|
|
|
2015-12-22 16:19:34 +01:00
|
|
|
for uid, rules in self.rules_by_user.items():
|
2016-08-25 18:32:22 +02:00
|
|
|
display_name = None
|
|
|
|
member_ev_id = context.current_state_ids.get((EventTypes.Member, uid))
|
|
|
|
if member_ev_id:
|
|
|
|
member_ev = yield self.store.get_event(member_ev_id, allow_none=True)
|
|
|
|
if member_ev:
|
|
|
|
display_name = member_ev.content.get("displayname", None)
|
2016-01-18 15:09:47 +01:00
|
|
|
|
|
|
|
filtered = filtered_by_user[uid]
|
2016-01-06 12:38:09 +01:00
|
|
|
if len(filtered) == 0:
|
|
|
|
continue
|
|
|
|
|
2016-01-20 14:44:04 +01:00
|
|
|
if filtered[0].sender == uid:
|
2016-01-20 14:24:59 +01:00
|
|
|
continue
|
|
|
|
|
2015-12-22 16:19:34 +01:00
|
|
|
for rule in rules:
|
|
|
|
if 'enabled' in rule and not rule['enabled']:
|
|
|
|
continue
|
|
|
|
|
2016-01-18 15:09:47 +01:00
|
|
|
matches = _condition_checker(
|
2016-01-18 11:09:14 +01:00
|
|
|
evaluator, rule['conditions'], uid, display_name, condition_cache
|
2016-01-18 15:09:47 +01:00
|
|
|
)
|
|
|
|
if matches:
|
2015-12-22 16:19:34 +01:00
|
|
|
actions = [x for x in rule['actions'] if x != 'dont_notify']
|
2016-01-22 18:21:58 +01:00
|
|
|
if actions and 'notify' in actions:
|
2015-12-22 16:19:34 +01:00
|
|
|
actions_by_user[uid] = actions
|
|
|
|
break
|
2016-01-06 12:38:09 +01:00
|
|
|
defer.returnValue(actions_by_user)
|
2015-12-22 16:19:34 +01:00
|
|
|
|
2016-01-18 15:09:47 +01:00
|
|
|
|
2016-01-18 11:09:14 +01:00
|
|
|
def _condition_checker(evaluator, conditions, uid, display_name, cache):
|
2016-01-18 15:09:47 +01:00
|
|
|
for cond in conditions:
|
|
|
|
_id = cond.get("_id", None)
|
|
|
|
if _id:
|
|
|
|
res = cache.get(_id, None)
|
|
|
|
if res is False:
|
2016-01-19 15:24:59 +01:00
|
|
|
return False
|
2016-01-18 15:09:47 +01:00
|
|
|
elif res is True:
|
|
|
|
continue
|
|
|
|
|
2016-02-18 17:05:13 +01:00
|
|
|
res = evaluator.matches(cond, uid, display_name)
|
2016-01-18 15:09:47 +01:00
|
|
|
if _id:
|
2016-01-19 17:01:05 +01:00
|
|
|
cache[_id] = bool(res)
|
2016-01-18 15:09:47 +01:00
|
|
|
|
2016-01-19 15:22:02 +01:00
|
|
|
if not res:
|
2016-01-18 15:09:47 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|