2015-12-22 10:19:34 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2015 OpenMarket Ltd
|
2017-10-10 06:47:10 -04:00
|
|
|
# Copyright 2017 New Vector Ltd
|
2015-12-22 10:19:34 -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.
|
|
|
|
|
|
|
|
import logging
|
2018-07-09 02:09:20 -04:00
|
|
|
from collections import namedtuple
|
2015-12-22 10:19:34 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from six import iteritems, itervalues
|
2015-12-22 10:19:34 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from prometheus_client import Counter
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
2016-01-18 09:09:47 -05:00
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
from synapse.api.constants import EventTypes, Membership
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.event_auth import get_user_power_level
|
|
|
|
from synapse.state import POWER_KEY
|
2018-08-10 09:50:21 -04:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2018-05-21 20:48:57 -04:00
|
|
|
from synapse.util.caches import register_cache
|
2017-05-02 05:46:01 -04:00
|
|
|
from synapse.util.caches.descriptors import cached
|
2015-12-22 10:19:34 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from .push_rule_evaluator import PushRuleEvaluatorForEvent
|
2016-01-06 06:38:09 -05:00
|
|
|
|
2015-12-22 10:19:34 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
rules_by_room = {}
|
2015-12-22 10:19:34 -05:00
|
|
|
|
2017-07-13 06:37:09 -04:00
|
|
|
|
2018-05-22 18:32:57 -04:00
|
|
|
push_rules_invalidation_counter = Counter(
|
2019-06-20 05:32:02 -04:00
|
|
|
"synapse_push_bulk_push_rule_evaluator_push_rules_invalidation_counter", ""
|
|
|
|
)
|
2018-05-22 18:32:57 -04:00
|
|
|
push_rules_state_size_counter = Counter(
|
2019-06-20 05:32:02 -04:00
|
|
|
"synapse_push_bulk_push_rule_evaluator_push_rules_state_size_counter", ""
|
|
|
|
)
|
2017-07-13 06:37:09 -04:00
|
|
|
|
|
|
|
# Measures whether we use the fast path of using state deltas, or if we have to
|
|
|
|
# recalculate from scratch
|
2018-05-21 20:48:57 -04:00
|
|
|
push_rules_delta_state_cache_metric = register_cache(
|
2017-07-13 06:37:09 -04:00
|
|
|
"cache",
|
2018-05-21 20:48:57 -04:00
|
|
|
"push_rules_delta_state_cache_metric",
|
|
|
|
cache=[], # Meaningless size, as this isn't a cache that stores values
|
2017-07-13 06:37:09 -04:00
|
|
|
)
|
|
|
|
|
2015-12-22 10:19:34 -05:00
|
|
|
|
2017-05-18 13:17:40 -04:00
|
|
|
class BulkPushRuleEvaluator(object):
|
2017-05-02 05:46:01 -04:00
|
|
|
"""Calculates the outcome of push rules for an event for all users in the
|
|
|
|
room at once.
|
2015-12-22 12:19:22 -05:00
|
|
|
"""
|
2017-05-02 05:46:01 -04:00
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.hs = hs
|
|
|
|
self.store = hs.get_datastore()
|
2017-10-05 07:39:18 -04:00
|
|
|
self.auth = hs.get_auth()
|
2017-05-02 05:46:01 -04:00
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
self.room_push_rule_cache_metrics = register_cache(
|
2017-07-13 06:37:09 -04:00
|
|
|
"cache",
|
2018-05-21 20:48:57 -04:00
|
|
|
"room_push_rule_cache",
|
|
|
|
cache=[], # Meaningless size, as this isn't a cache that stores values
|
2017-07-13 06:37:09 -04:00
|
|
|
)
|
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _get_rules_for_event(self, event, context):
|
|
|
|
"""This gets the rules for all users in the room at the time of the event,
|
|
|
|
as well as the push rules for the invitee if the event is an invite.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict of user_id -> push_rules
|
|
|
|
"""
|
|
|
|
room_id = event.room_id
|
2019-10-28 09:33:04 -04:00
|
|
|
rules_for_room = yield self._get_rules_for_room(room_id)
|
2017-05-02 05:46:01 -04:00
|
|
|
|
2017-05-22 10:02:12 -04:00
|
|
|
rules_by_user = yield rules_for_room.get_rules(event, context)
|
2017-05-02 05:46:01 -04: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
|
2019-06-20 05:32:02 -04:00
|
|
|
if event.type == "m.room.member" and event.content["membership"] == "invite":
|
2017-05-02 05:46:01 -04:00
|
|
|
invited = event.state_key
|
|
|
|
if invited and self.hs.is_mine_id(invited):
|
|
|
|
has_pusher = yield self.store.user_has_pusher(invited)
|
|
|
|
if has_pusher:
|
|
|
|
rules_by_user = dict(rules_by_user)
|
|
|
|
rules_by_user[invited] = yield self.store.get_push_rules_for_user(
|
|
|
|
invited
|
|
|
|
)
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return rules_by_user
|
2017-05-02 05:46:01 -04:00
|
|
|
|
2017-05-18 11:17:23 -04:00
|
|
|
@cached()
|
2017-05-02 05:46:01 -04:00
|
|
|
def _get_rules_for_room(self, room_id):
|
|
|
|
"""Get the current RulesForRoom object for the given room id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
RulesForRoom
|
|
|
|
"""
|
2017-05-18 06:45:56 -04:00
|
|
|
# It's important that RulesForRoom gets added to self._get_rules_for_room.cache
|
|
|
|
# before any lookup methods get called on it as otherwise there may be
|
|
|
|
# a race if invalidate_all gets called (which assumes its in the cache)
|
2017-07-13 06:37:09 -04:00
|
|
|
return RulesForRoom(
|
2019-06-20 05:32:02 -04:00
|
|
|
self.hs,
|
|
|
|
room_id,
|
|
|
|
self._get_rules_for_room.cache,
|
2017-07-13 06:37:09 -04:00
|
|
|
self.room_push_rule_cache_metrics,
|
|
|
|
)
|
2015-12-22 10:19:34 -05:00
|
|
|
|
2017-10-05 07:39:18 -04:00
|
|
|
@defer.inlineCallbacks
|
2017-10-10 10:23:00 -04:00
|
|
|
def _get_power_levels_and_sender_level(self, event, context):
|
2018-07-23 08:00:22 -04:00
|
|
|
prev_state_ids = yield context.get_prev_state_ids(self.store)
|
|
|
|
pl_event_id = prev_state_ids.get(POWER_KEY)
|
2017-10-05 08:27:12 -04:00
|
|
|
if pl_event_id:
|
2017-10-05 08:20:22 -04:00
|
|
|
# fastpath: if there's a power level event, that's all we need, and
|
|
|
|
# not having a power level event is an extreme edge case
|
2017-10-10 06:38:31 -04:00
|
|
|
pl_event = yield self.store.get_event(pl_event_id)
|
2017-10-10 10:53:34 -04:00
|
|
|
auth_events = {POWER_KEY: pl_event}
|
2017-10-05 08:20:22 -04:00
|
|
|
else:
|
|
|
|
auth_events_ids = yield self.auth.compute_auth_events(
|
2019-06-20 05:32:02 -04:00
|
|
|
event, prev_state_ids, for_verification=False
|
2017-10-05 08:20:22 -04:00
|
|
|
)
|
2017-10-10 06:38:31 -04:00
|
|
|
auth_events = yield self.store.get_events(auth_events_ids)
|
2019-06-20 05:32:02 -04:00
|
|
|
auth_events = {(e.type, e.state_key): e for e in itervalues(auth_events)}
|
2017-10-10 06:38:31 -04:00
|
|
|
|
2017-10-10 10:23:00 -04:00
|
|
|
sender_level = get_user_power_level(event.sender, auth_events)
|
|
|
|
|
2017-10-10 10:34:05 -04:00
|
|
|
pl_event = auth_events.get(POWER_KEY)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return pl_event.content if pl_event else {}, sender_level
|
2017-10-05 07:39:18 -04:00
|
|
|
|
2016-01-06 06:38:09 -05:00
|
|
|
@defer.inlineCallbacks
|
2016-08-25 12:32:22 -04:00
|
|
|
def action_for_event_by_user(self, event, context):
|
2018-02-15 10:18:37 -05:00
|
|
|
"""Given an event and context, evaluate the push rules and insert the
|
|
|
|
results into the event_push_actions_staging table.
|
2017-05-02 05:46:01 -04:00
|
|
|
|
|
|
|
Returns:
|
2018-02-15 10:18:37 -05:00
|
|
|
Deferred
|
2017-05-02 05:46:01 -04:00
|
|
|
"""
|
|
|
|
rules_by_user = yield self._get_rules_for_event(event, context)
|
2018-02-20 06:30:54 -05:00
|
|
|
actions_by_user = {}
|
2015-12-22 10:19:34 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
room_members = yield self.store.get_joined_users_from_context(event, context)
|
2016-03-22 09:52:45 -04:00
|
|
|
|
2019-10-31 11:43:24 -04:00
|
|
|
(
|
|
|
|
power_levels,
|
|
|
|
sender_power_level,
|
|
|
|
) = yield self._get_power_levels_and_sender_level(event, context)
|
2017-10-05 07:39:18 -04:00
|
|
|
|
|
|
|
evaluator = PushRuleEvaluatorForEvent(
|
2019-06-20 05:32:02 -04:00
|
|
|
event, len(room_members), sender_power_level, power_levels
|
2017-10-05 07:39:18 -04:00
|
|
|
)
|
2016-01-18 09:09:47 -05:00
|
|
|
|
|
|
|
condition_cache = {}
|
|
|
|
|
2018-04-28 07:19:12 -04:00
|
|
|
for uid, rules in iteritems(rules_by_user):
|
2017-07-07 09:04:40 -04:00
|
|
|
if event.sender == uid:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not event.is_state():
|
|
|
|
is_ignored = yield self.store.is_ignored_by(event.sender, uid)
|
|
|
|
if is_ignored:
|
|
|
|
continue
|
|
|
|
|
2017-04-25 09:38:51 -04:00
|
|
|
display_name = None
|
2017-04-25 10:22:59 -04:00
|
|
|
profile_info = room_members.get(uid)
|
2017-04-25 09:38:51 -04:00
|
|
|
if profile_info:
|
|
|
|
display_name = profile_info.display_name
|
2017-04-25 10:39:19 -04:00
|
|
|
|
|
|
|
if not display_name:
|
2016-12-08 08:32:05 -05:00
|
|
|
# Handle the case where we are pushing a membership event to
|
|
|
|
# that user, as they might not be already joined.
|
|
|
|
if event.type == EventTypes.Member and event.state_key == uid:
|
|
|
|
display_name = event.content.get("displayname", None)
|
2016-01-18 09:09:47 -05:00
|
|
|
|
2015-12-22 10:19:34 -05:00
|
|
|
for rule in rules:
|
2019-06-20 05:32:02 -04:00
|
|
|
if "enabled" in rule and not rule["enabled"]:
|
2015-12-22 10:19:34 -05:00
|
|
|
continue
|
|
|
|
|
2016-01-18 09:09:47 -05:00
|
|
|
matches = _condition_checker(
|
2019-06-20 05:32:02 -04:00
|
|
|
evaluator, rule["conditions"], uid, display_name, condition_cache
|
2016-01-18 09:09:47 -05:00
|
|
|
)
|
|
|
|
if matches:
|
2019-06-20 05:32:02 -04:00
|
|
|
actions = [x for x in rule["actions"] if x != "dont_notify"]
|
|
|
|
if actions and "notify" in actions:
|
2018-02-21 06:29:49 -05:00
|
|
|
# Push rules say we should notify the user of this event
|
2018-02-20 06:30:54 -05:00
|
|
|
actions_by_user[uid] = actions
|
2015-12-22 10:19:34 -05:00
|
|
|
break
|
|
|
|
|
2018-02-21 06:29:49 -05:00
|
|
|
# Mark in the DB staging area the push actions for users who should be
|
|
|
|
# notified for this event. (This will then get handled when we persist
|
|
|
|
# the event)
|
2019-06-20 05:32:02 -04:00
|
|
|
yield self.store.add_push_actions_to_staging(event.event_id, actions_by_user)
|
2018-02-20 06:30:54 -05:00
|
|
|
|
2016-01-18 09:09:47 -05:00
|
|
|
|
2016-01-18 05:09:14 -05:00
|
|
|
def _condition_checker(evaluator, conditions, uid, display_name, cache):
|
2016-01-18 09:09:47 -05:00
|
|
|
for cond in conditions:
|
|
|
|
_id = cond.get("_id", None)
|
|
|
|
if _id:
|
|
|
|
res = cache.get(_id, None)
|
|
|
|
if res is False:
|
2016-01-19 09:24:59 -05:00
|
|
|
return False
|
2016-01-18 09:09:47 -05:00
|
|
|
elif res is True:
|
|
|
|
continue
|
|
|
|
|
2016-02-18 11:05:13 -05:00
|
|
|
res = evaluator.matches(cond, uid, display_name)
|
2016-01-18 09:09:47 -05:00
|
|
|
if _id:
|
2016-01-19 11:01:05 -05:00
|
|
|
cache[_id] = bool(res)
|
2016-01-18 09:09:47 -05:00
|
|
|
|
2016-01-19 09:22:02 -05:00
|
|
|
if not res:
|
2016-01-18 09:09:47 -05:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2017-05-02 05:46:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
class RulesForRoom(object):
|
|
|
|
"""Caches push rules for users in a room.
|
|
|
|
|
|
|
|
This efficiently handles users joining/leaving the room by not invalidating
|
|
|
|
the entire cache for the room.
|
|
|
|
"""
|
|
|
|
|
2017-07-13 06:37:09 -04:00
|
|
|
def __init__(self, hs, room_id, rules_for_room_cache, room_push_rule_cache_metrics):
|
2017-05-02 05:46:01 -04:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
hs (HomeServer)
|
|
|
|
room_id (str)
|
|
|
|
rules_for_room_cache(Cache): The cache object that caches these
|
|
|
|
RoomsForUser objects.
|
2017-07-13 06:37:09 -04:00
|
|
|
room_push_rule_cache_metrics (CacheMetric)
|
2017-05-02 05:46:01 -04:00
|
|
|
"""
|
|
|
|
self.room_id = room_id
|
|
|
|
self.is_mine_id = hs.is_mine_id
|
|
|
|
self.store = hs.get_datastore()
|
2017-07-13 06:37:09 -04:00
|
|
|
self.room_push_rule_cache_metrics = room_push_rule_cache_metrics
|
2017-05-02 05:46:01 -04:00
|
|
|
|
|
|
|
self.linearizer = Linearizer(name="rules_for_room")
|
|
|
|
|
|
|
|
self.member_map = {} # event_id -> (user_id, state)
|
|
|
|
self.rules_by_user = {} # user_id -> rules
|
|
|
|
|
|
|
|
# The last state group we updated the caches for. If the state_group of
|
|
|
|
# a new event comes along, we know that we can just return the cached
|
|
|
|
# result.
|
|
|
|
# On invalidation of the rules themselves (if the user changes them),
|
|
|
|
# we invalidate everything and set state_group to `object()`
|
|
|
|
self.state_group = object()
|
|
|
|
|
|
|
|
# A sequence number to keep track of when we're allowed to update the
|
|
|
|
# cache. We bump the sequence number when we invalidate the cache. If
|
|
|
|
# the sequence number changes while we're calculating stuff we should
|
|
|
|
# not update the cache with it.
|
|
|
|
self.sequence = 0
|
|
|
|
|
2017-05-19 11:51:05 -04:00
|
|
|
# A cache of user_ids that we *know* aren't interesting, e.g. user_ids
|
2017-05-22 09:59:27 -04:00
|
|
|
# owned by AS's, or remote users, etc. (I.e. users we will never need to
|
|
|
|
# calculate push for)
|
|
|
|
# These never need to be invalidated as we will never set up push for
|
|
|
|
# them.
|
2017-05-19 11:51:05 -04:00
|
|
|
self.uninteresting_user_set = set()
|
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
# We need to be clever on the invalidating caches callbacks, as
|
|
|
|
# otherwise the invalidation callback holds a reference to the object,
|
|
|
|
# potentially causing it to leak.
|
|
|
|
# To get around this we pass a function that on invalidations looks ups
|
|
|
|
# the RoomsForUser entry in the cache, rather than keeping a reference
|
|
|
|
# to self around in the callback.
|
2017-05-19 06:44:11 -04:00
|
|
|
self.invalidate_all_cb = _Invalidation(rules_for_room_cache, room_id)
|
2017-05-02 05:46:01 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2017-05-22 10:02:12 -04:00
|
|
|
def get_rules(self, event, context):
|
2017-05-02 05:46:01 -04:00
|
|
|
"""Given an event context return the rules for all users who are
|
|
|
|
currently in the room.
|
|
|
|
"""
|
|
|
|
state_group = context.state_group
|
|
|
|
|
2017-07-13 06:23:53 -04:00
|
|
|
if state_group and self.state_group == state_group:
|
|
|
|
logger.debug("Using cached rules for %r", self.room_id)
|
2017-07-13 06:37:09 -04:00
|
|
|
self.room_push_rule_cache_metrics.inc_hits()
|
2019-07-23 09:00:55 -04:00
|
|
|
return self.rules_by_user
|
2017-07-13 06:23:53 -04:00
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
with (yield self.linearizer.queue(())):
|
|
|
|
if state_group and self.state_group == state_group:
|
2017-05-22 10:01:36 -04:00
|
|
|
logger.debug("Using cached rules for %r", self.room_id)
|
2017-07-13 06:37:09 -04:00
|
|
|
self.room_push_rule_cache_metrics.inc_hits()
|
2019-07-23 09:00:55 -04:00
|
|
|
return self.rules_by_user
|
2017-05-02 05:46:01 -04:00
|
|
|
|
2017-07-13 06:37:09 -04:00
|
|
|
self.room_push_rule_cache_metrics.inc_misses()
|
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
ret_rules_by_user = {}
|
|
|
|
missing_member_event_ids = {}
|
|
|
|
if state_group and self.state_group == context.prev_group:
|
|
|
|
# If we have a simple delta then we can reuse most of the previous
|
|
|
|
# results.
|
|
|
|
ret_rules_by_user = self.rules_by_user
|
|
|
|
current_state_ids = context.delta_ids
|
2017-07-13 06:37:09 -04:00
|
|
|
|
|
|
|
push_rules_delta_state_cache_metric.inc_hits()
|
2017-05-02 05:46:01 -04:00
|
|
|
else:
|
2018-07-23 08:00:22 -04:00
|
|
|
current_state_ids = yield context.get_current_state_ids(self.store)
|
2017-07-13 06:37:09 -04:00
|
|
|
push_rules_delta_state_cache_metric.inc_misses()
|
|
|
|
|
2018-05-21 20:48:57 -04:00
|
|
|
push_rules_state_size_counter.inc(len(current_state_ids))
|
2017-05-02 05:46:01 -04:00
|
|
|
|
2017-05-22 10:01:36 -04:00
|
|
|
logger.debug(
|
|
|
|
"Looking for member changes in %r %r", state_group, current_state_ids
|
|
|
|
)
|
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
# Loop through to see which member events we've seen and have rules
|
|
|
|
# for and which we need to fetch
|
2017-05-19 11:51:05 -04:00
|
|
|
for key in current_state_ids:
|
2017-05-22 09:58:22 -04:00
|
|
|
typ, user_id = key
|
|
|
|
if typ != EventTypes.Member:
|
2017-05-02 05:46:01 -04:00
|
|
|
continue
|
|
|
|
|
2017-05-19 11:51:05 -04:00
|
|
|
if user_id in self.uninteresting_user_set:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not self.is_mine_id(user_id):
|
|
|
|
self.uninteresting_user_set.add(user_id)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if self.store.get_if_app_services_interested_in_user(user_id):
|
|
|
|
self.uninteresting_user_set.add(user_id)
|
|
|
|
continue
|
|
|
|
|
|
|
|
event_id = current_state_ids[key]
|
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
res = self.member_map.get(event_id, None)
|
|
|
|
if res:
|
|
|
|
user_id, state = res
|
|
|
|
if state == Membership.JOIN:
|
|
|
|
rules = self.rules_by_user.get(user_id, None)
|
|
|
|
if rules:
|
|
|
|
ret_rules_by_user[user_id] = rules
|
|
|
|
continue
|
|
|
|
|
|
|
|
# If a user has left a room we remove their push rule. If they
|
|
|
|
# joined then we readd it later in _update_rules_with_member_event_ids
|
|
|
|
ret_rules_by_user.pop(user_id, None)
|
|
|
|
missing_member_event_ids[user_id] = event_id
|
|
|
|
|
|
|
|
if missing_member_event_ids:
|
|
|
|
# If we have some memebr events we haven't seen, look them up
|
|
|
|
# and fetch push rules for them if appropriate.
|
2017-05-22 10:01:36 -04:00
|
|
|
logger.debug("Found new member events %r", missing_member_event_ids)
|
2017-05-02 05:46:01 -04:00
|
|
|
yield self._update_rules_with_member_event_ids(
|
2017-05-22 10:02:12 -04:00
|
|
|
ret_rules_by_user, missing_member_event_ids, state_group, event
|
2017-05-02 05:46:01 -04:00
|
|
|
)
|
2017-07-14 11:11:26 -04:00
|
|
|
else:
|
|
|
|
# The push rules didn't change but lets update the cache anyway
|
|
|
|
self.update_cache(
|
|
|
|
self.sequence,
|
|
|
|
members={}, # There were no membership changes
|
|
|
|
rules_by_user=ret_rules_by_user,
|
2019-06-20 05:32:02 -04:00
|
|
|
state_group=state_group,
|
2017-07-14 11:11:26 -04:00
|
|
|
)
|
2017-05-02 05:46:01 -04:00
|
|
|
|
2017-05-22 10:01:36 -04:00
|
|
|
if logger.isEnabledFor(logging.DEBUG):
|
|
|
|
logger.debug(
|
2019-06-20 05:32:02 -04:00
|
|
|
"Returning push rules for %r %r", self.room_id, ret_rules_by_user.keys()
|
2017-05-22 10:01:36 -04:00
|
|
|
)
|
2019-07-23 09:00:55 -04:00
|
|
|
return ret_rules_by_user
|
2017-05-02 05:46:01 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2019-06-20 05:32:02 -04:00
|
|
|
def _update_rules_with_member_event_ids(
|
|
|
|
self, ret_rules_by_user, member_event_ids, state_group, event
|
|
|
|
):
|
2017-05-02 05:46:01 -04:00
|
|
|
"""Update the partially filled rules_by_user dict by fetching rules for
|
|
|
|
any newly joined users in the `member_event_ids` list.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ret_rules_by_user (dict): Partiallly filled dict of push rules. Gets
|
|
|
|
updated with any new rules.
|
|
|
|
member_event_ids (list): List of event ids for membership events that
|
|
|
|
have happened since the last time we filled rules_by_user
|
|
|
|
state_group: The state group we are currently computing push rules
|
|
|
|
for. Used when updating the cache.
|
|
|
|
"""
|
|
|
|
sequence = self.sequence
|
|
|
|
|
|
|
|
rows = yield self.store._simple_select_many_batch(
|
|
|
|
table="room_memberships",
|
|
|
|
column="event_id",
|
|
|
|
iterable=member_event_ids.values(),
|
2019-06-20 05:32:02 -04:00
|
|
|
retcols=("user_id", "membership", "event_id"),
|
2017-05-02 05:46:01 -04:00
|
|
|
keyvalues={},
|
|
|
|
batch_size=500,
|
|
|
|
desc="_get_rules_for_member_event_ids",
|
|
|
|
)
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
members = {row["event_id"]: (row["user_id"], row["membership"]) for row in rows}
|
2017-05-02 05:46:01 -04:00
|
|
|
|
2017-05-22 11:22:04 -04:00
|
|
|
# If the event is a join event then it will be in current state evnts
|
|
|
|
# map but not in the DB, so we have to explicitly insert it.
|
2017-05-22 10:02:12 -04:00
|
|
|
if event.type == EventTypes.Member:
|
2018-04-28 07:19:12 -04:00
|
|
|
for event_id in itervalues(member_event_ids):
|
2017-05-22 10:02:12 -04:00
|
|
|
if event_id == event.event_id:
|
|
|
|
members[event_id] = (event.state_key, event.membership)
|
|
|
|
|
2017-05-22 10:01:36 -04:00
|
|
|
if logger.isEnabledFor(logging.DEBUG):
|
|
|
|
logger.debug("Found members %r: %r", self.room_id, members.values())
|
|
|
|
|
2017-05-18 12:48:36 -04:00
|
|
|
interested_in_user_ids = set(
|
2019-06-20 05:32:02 -04:00
|
|
|
user_id
|
|
|
|
for user_id, membership in itervalues(members)
|
2017-05-18 12:48:36 -04:00
|
|
|
if membership == Membership.JOIN
|
|
|
|
)
|
2017-05-02 05:46:01 -04:00
|
|
|
|
2017-05-22 11:21:06 -04:00
|
|
|
logger.debug("Joined: %r", interested_in_user_ids)
|
2017-05-22 10:01:36 -04:00
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
if_users_with_pushers = yield self.store.get_if_users_have_pushers(
|
2019-06-20 05:32:02 -04:00
|
|
|
interested_in_user_ids, on_invalidate=self.invalidate_all_cb
|
2017-05-02 05:46:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
user_ids = set(
|
2018-04-28 07:19:12 -04:00
|
|
|
uid for uid, have_pusher in iteritems(if_users_with_pushers) if have_pusher
|
2017-05-02 05:46:01 -04:00
|
|
|
)
|
|
|
|
|
2017-05-22 11:21:06 -04:00
|
|
|
logger.debug("With pushers: %r", user_ids)
|
2017-05-22 10:01:36 -04:00
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
users_with_receipts = yield self.store.get_users_with_read_receipts_in_room(
|
2019-06-20 05:32:02 -04:00
|
|
|
self.room_id, on_invalidate=self.invalidate_all_cb
|
2017-05-02 05:46:01 -04:00
|
|
|
)
|
|
|
|
|
2017-05-22 11:21:06 -04:00
|
|
|
logger.debug("With receipts: %r", users_with_receipts)
|
2017-05-22 10:01:36 -04:00
|
|
|
|
2017-05-02 05:46:01 -04:00
|
|
|
# any users with pushers must be ours: they have pushers
|
|
|
|
for uid in users_with_receipts:
|
|
|
|
if uid in interested_in_user_ids:
|
|
|
|
user_ids.add(uid)
|
|
|
|
|
|
|
|
rules_by_user = yield self.store.bulk_get_push_rules(
|
2019-06-20 05:32:02 -04:00
|
|
|
user_ids, on_invalidate=self.invalidate_all_cb
|
2017-05-02 05:46:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
ret_rules_by_user.update(
|
2018-04-28 07:19:12 -04:00
|
|
|
item for item in iteritems(rules_by_user) if item[0] is not None
|
2017-05-02 05:46:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
self.update_cache(sequence, members, ret_rules_by_user, state_group)
|
|
|
|
|
|
|
|
def invalidate_all(self):
|
|
|
|
# Note: Don't hand this function directly to an invalidation callback
|
|
|
|
# as it keeps a reference to self and will stop this instance from being
|
|
|
|
# GC'd if it gets dropped from the rules_to_user cache. Instead use
|
|
|
|
# `self.invalidate_all_cb`
|
2017-05-22 10:01:36 -04:00
|
|
|
logger.debug("Invalidating RulesForRoom for %r", self.room_id)
|
2017-05-02 05:46:01 -04:00
|
|
|
self.sequence += 1
|
|
|
|
self.state_group = object()
|
|
|
|
self.member_map = {}
|
|
|
|
self.rules_by_user = {}
|
2017-07-13 06:37:09 -04:00
|
|
|
push_rules_invalidation_counter.inc()
|
2017-05-02 05:46:01 -04:00
|
|
|
|
|
|
|
def update_cache(self, sequence, members, rules_by_user, state_group):
|
|
|
|
if sequence == self.sequence:
|
|
|
|
self.member_map.update(members)
|
|
|
|
self.rules_by_user = rules_by_user
|
|
|
|
self.state_group = state_group
|
2017-05-19 06:44:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
class _Invalidation(namedtuple("_Invalidation", ("cache", "room_id"))):
|
|
|
|
# We rely on _CacheContext implementing __eq__ and __hash__ sensibly,
|
|
|
|
# which namedtuple does for us (i.e. two _CacheContext are the same if
|
|
|
|
# their caches and keys match). This is important in particular to
|
|
|
|
# dedupe when we add callbacks to lru cache nodes, otherwise the number
|
|
|
|
# of callbacks would grow.
|
|
|
|
def __call__(self):
|
|
|
|
rules = self.cache.get(self.room_id, None, update_metrics=False)
|
|
|
|
if rules:
|
|
|
|
rules.invalidate_all()
|