2015-12-22 08:19:34 -07:00
|
|
|
# Copyright 2015 OpenMarket Ltd
|
2017-10-10 04:47:10 -06:00
|
|
|
# Copyright 2017 New Vector Ltd
|
2015-12-22 08:19:34 -07: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
|
2020-12-11 09:43:53 -07:00
|
|
|
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Union
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2020-10-30 05:43:17 -06:00
|
|
|
import attr
|
2018-07-09 00:09:20 -06:00
|
|
|
from prometheus_client import Counter
|
|
|
|
|
2020-09-02 10:19:37 -06:00
|
|
|
from synapse.api.constants import EventTypes, Membership, RelationTypes
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.event_auth import get_user_power_level
|
2020-09-02 10:19:37 -06:00
|
|
|
from synapse.events import EventBase
|
|
|
|
from synapse.events.snapshot import EventContext
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.state import POWER_KEY
|
2022-03-25 08:58:56 -06:00
|
|
|
from synapse.storage.databases.main.roommember import EventIdMembership
|
2018-08-10 07:50:21 -06:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2020-12-11 09:43:53 -07:00
|
|
|
from synapse.util.caches import CacheMetric, register_cache
|
2020-10-30 05:43:17 -06:00
|
|
|
from synapse.util.caches.descriptors import lru_cache
|
|
|
|
from synapse.util.caches.lrucache import LruCache
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
from .push_rule_evaluator import PushRuleEvaluatorForEvent
|
2016-01-06 04:38:09 -07:00
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 05:12:48 -06:00
|
|
|
from synapse.server import HomeServer
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2017-07-13 04:37:09 -06:00
|
|
|
|
2018-05-22 16:32:57 -06:00
|
|
|
push_rules_invalidation_counter = Counter(
|
2018-05-23 12:03:51 -06:00
|
|
|
"synapse_push_bulk_push_rule_evaluator_push_rules_invalidation_counter", ""
|
|
|
|
)
|
2018-05-22 16:32:57 -06:00
|
|
|
push_rules_state_size_counter = Counter(
|
2018-05-23 12:03:51 -06:00
|
|
|
"synapse_push_bulk_push_rule_evaluator_push_rules_state_size_counter", ""
|
|
|
|
)
|
2017-07-13 04:37:09 -06:00
|
|
|
|
|
|
|
# Measures whether we use the fast path of using state deltas, or if we have to
|
|
|
|
# recalculate from scratch
|
2018-05-21 18:48:57 -06:00
|
|
|
push_rules_delta_state_cache_metric = register_cache(
|
2017-07-13 04:37:09 -06:00
|
|
|
"cache",
|
2018-05-21 18:48:57 -06:00
|
|
|
"push_rules_delta_state_cache_metric",
|
|
|
|
cache=[], # Meaningless size, as this isn't a cache that stores values
|
2020-05-11 11:45:23 -06:00
|
|
|
resizable=False,
|
2017-07-13 04:37:09 -06:00
|
|
|
)
|
|
|
|
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2020-09-02 10:19:37 -06:00
|
|
|
STATE_EVENT_TYPES_TO_MARK_UNREAD = {
|
|
|
|
EventTypes.Topic,
|
|
|
|
EventTypes.Name,
|
|
|
|
EventTypes.RoomAvatar,
|
|
|
|
EventTypes.Tombstone,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _should_count_as_unread(event: EventBase, context: EventContext) -> bool:
|
|
|
|
# Exclude rejected and soft-failed events.
|
|
|
|
if context.rejected or event.internal_metadata.is_soft_failed():
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Exclude notices.
|
|
|
|
if (
|
|
|
|
not event.is_state()
|
|
|
|
and event.type == EventTypes.Message
|
|
|
|
and event.content.get("msgtype") == "m.notice"
|
|
|
|
):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Exclude edits.
|
|
|
|
relates_to = event.content.get("m.relates_to", {})
|
|
|
|
if relates_to.get("rel_type") == RelationTypes.REPLACE:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Mark events that have a non-empty string body as unread.
|
|
|
|
body = event.content.get("body")
|
|
|
|
if isinstance(body, str) and body:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Mark some state events as unread.
|
|
|
|
if event.is_state() and event.type in STATE_EVENT_TYPES_TO_MARK_UNREAD:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Mark encrypted events as unread.
|
|
|
|
if not event.is_state() and event.type == EventTypes.Encrypted:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-09-04 04:54:56 -06:00
|
|
|
class BulkPushRuleEvaluator:
|
2017-05-02 03:46:01 -06:00
|
|
|
"""Calculates the outcome of push rules for an event for all users in the
|
|
|
|
room at once.
|
2015-12-22 10:19:22 -07:00
|
|
|
"""
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2017-05-02 03:46:01 -06:00
|
|
|
self.hs = hs
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2021-07-01 12:25:37 -06:00
|
|
|
self._event_auth_handler = hs.get_event_auth_handler()
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2021-04-23 04:47:07 -06:00
|
|
|
# Used by `RulesForRoom` to ensure only one thing mutates the cache at a
|
|
|
|
# time. Keyed off room_id.
|
|
|
|
self._rules_linearizer = Linearizer(name="rules_for_room")
|
|
|
|
|
2018-05-21 18:48:57 -06:00
|
|
|
self.room_push_rule_cache_metrics = register_cache(
|
2017-07-13 04:37:09 -06:00
|
|
|
"cache",
|
2018-05-21 18:48:57 -06:00
|
|
|
"room_push_rule_cache",
|
2020-05-11 11:45:23 -06:00
|
|
|
cache=[], # Meaningless size, as this isn't a cache that stores values,
|
|
|
|
resizable=False,
|
2017-07-13 04:37:09 -06:00
|
|
|
)
|
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
async def _get_rules_for_event(
|
|
|
|
self, event: EventBase, context: EventContext
|
|
|
|
) -> Dict[str, List[Dict[str, Any]]]:
|
2017-05-02 03:46:01 -06:00
|
|
|
"""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
|
2021-04-23 04:47:07 -06:00
|
|
|
|
|
|
|
rules_for_room_data = self._get_rules_for_room(room_id)
|
|
|
|
rules_for_room = RulesForRoom(
|
|
|
|
hs=self.hs,
|
|
|
|
room_id=room_id,
|
|
|
|
rules_for_room_cache=self._get_rules_for_room.cache,
|
|
|
|
room_push_rule_cache_metrics=self.room_push_rule_cache_metrics,
|
|
|
|
linearizer=self._rules_linearizer,
|
|
|
|
cached_data=rules_for_room_data,
|
|
|
|
)
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2020-07-27 10:21:34 -06:00
|
|
|
rules_by_user = await rules_for_room.get_rules(event, context)
|
2017-05-02 03:46:01 -06: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 = event.state_key
|
|
|
|
if invited and self.hs.is_mine_id(invited):
|
2020-07-27 10:21:34 -06:00
|
|
|
has_pusher = await self.store.user_has_pusher(invited)
|
2017-05-02 03:46:01 -06:00
|
|
|
if has_pusher:
|
|
|
|
rules_by_user = dict(rules_by_user)
|
2020-07-27 10:21:34 -06:00
|
|
|
rules_by_user[invited] = await self.store.get_push_rules_for_user(
|
2017-05-02 03:46:01 -06:00
|
|
|
invited
|
|
|
|
)
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return rules_by_user
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2020-10-30 05:43:17 -06:00
|
|
|
@lru_cache()
|
2021-04-23 04:47:07 -06:00
|
|
|
def _get_rules_for_room(self, room_id: str) -> "RulesForRoomData":
|
|
|
|
"""Get the current RulesForRoomData object for the given room id"""
|
|
|
|
# It's important that the RulesForRoomData object gets added to self._get_rules_for_room.cache
|
2017-05-18 04:45:56 -06:00
|
|
|
# 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)
|
2021-04-23 04:47:07 -06:00
|
|
|
return RulesForRoomData()
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
async def _get_power_levels_and_sender_level(
|
|
|
|
self, event: EventBase, context: EventContext
|
|
|
|
) -> Tuple[dict, int]:
|
2020-07-27 10:21:34 -06:00
|
|
|
prev_state_ids = await context.get_prev_state_ids()
|
2018-07-23 06:00:22 -06:00
|
|
|
pl_event_id = prev_state_ids.get(POWER_KEY)
|
2017-10-05 06:27:12 -06:00
|
|
|
if pl_event_id:
|
2017-10-05 06:20:22 -06: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
|
2020-12-11 09:43:53 -07:00
|
|
|
auth_events = {POWER_KEY: await self.store.get_event(pl_event_id)}
|
2017-10-05 06:20:22 -06:00
|
|
|
else:
|
2021-07-01 12:25:37 -06:00
|
|
|
auth_events_ids = self._event_auth_handler.compute_auth_events(
|
2018-07-23 06:00:22 -06:00
|
|
|
event, prev_state_ids, for_verification=False
|
2017-10-05 06:20:22 -06:00
|
|
|
)
|
2020-12-11 09:43:53 -07:00
|
|
|
auth_events_dict = await self.store.get_events(auth_events_ids)
|
|
|
|
auth_events = {(e.type, e.state_key): e for e in auth_events_dict.values()}
|
2017-10-10 04:38:31 -06:00
|
|
|
|
2017-10-10 08:23:00 -06:00
|
|
|
sender_level = get_user_power_level(event.sender, auth_events)
|
|
|
|
|
2017-10-10 08:34:05 -06:00
|
|
|
pl_event = auth_events.get(POWER_KEY)
|
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return pl_event.content if pl_event else {}, sender_level
|
2017-10-05 05:39:18 -06:00
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
async def action_for_event_by_user(
|
|
|
|
self, event: EventBase, context: EventContext
|
|
|
|
) -> None:
|
2020-09-02 10:19:37 -06:00
|
|
|
"""Given an event and context, evaluate the push rules, check if the message
|
|
|
|
should increment the unread count, and insert the results into the
|
|
|
|
event_push_actions_staging table.
|
2017-05-02 03:46:01 -06:00
|
|
|
"""
|
2020-09-02 10:19:37 -06:00
|
|
|
count_as_unread = _should_count_as_unread(event, context)
|
|
|
|
|
2020-07-27 10:21:34 -06:00
|
|
|
rules_by_user = await self._get_rules_for_event(event, context)
|
2021-07-15 04:02:43 -06:00
|
|
|
actions_by_user: Dict[str, List[Union[dict, str]]] = {}
|
2015-12-22 08:19:34 -07:00
|
|
|
|
2020-07-27 10:21:34 -06:00
|
|
|
room_members = await self.store.get_joined_users_from_context(event, context)
|
2016-03-22 07:52:45 -06:00
|
|
|
|
2019-10-31 09:43:24 -06:00
|
|
|
(
|
|
|
|
power_levels,
|
|
|
|
sender_power_level,
|
2020-07-27 10:21:34 -06:00
|
|
|
) = await self._get_power_levels_and_sender_level(event, context)
|
2017-10-05 05:39:18 -06:00
|
|
|
|
|
|
|
evaluator = PushRuleEvaluatorForEvent(
|
2017-10-10 08:23:00 -06:00
|
|
|
event, len(room_members), sender_power_level, power_levels
|
2017-10-05 05:39:18 -06:00
|
|
|
)
|
2016-01-18 07:09:47 -07:00
|
|
|
|
2021-01-07 06:03:38 -07:00
|
|
|
# If the event is not a state event check if any users ignore the sender.
|
|
|
|
if not event.is_state():
|
|
|
|
ignorers = await self.store.ignored_by(event.sender)
|
|
|
|
else:
|
2022-03-15 12:06:05 -06:00
|
|
|
ignorers = frozenset()
|
2021-01-07 06:03:38 -07:00
|
|
|
|
2020-06-15 05:03:36 -06:00
|
|
|
for uid, rules in rules_by_user.items():
|
2017-07-07 07:04:40 -06:00
|
|
|
if event.sender == uid:
|
|
|
|
continue
|
|
|
|
|
2021-01-07 06:03:38 -07:00
|
|
|
if uid in ignorers:
|
|
|
|
continue
|
2017-07-07 07:04:40 -06:00
|
|
|
|
2017-04-25 07:38:51 -06:00
|
|
|
display_name = None
|
2017-04-25 08:22:59 -06:00
|
|
|
profile_info = room_members.get(uid)
|
2017-04-25 07:38:51 -06:00
|
|
|
if profile_info:
|
|
|
|
display_name = profile_info.display_name
|
2017-04-25 08:39:19 -06:00
|
|
|
|
|
|
|
if not display_name:
|
2016-12-08 06:32:05 -07: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)
|
2021-11-02 07:55:52 -06:00
|
|
|
if not isinstance(display_name, str):
|
|
|
|
display_name = None
|
2016-01-18 07:09:47 -07:00
|
|
|
|
2020-09-07 09:56:27 -06:00
|
|
|
if count_as_unread:
|
|
|
|
# Add an element for the current user if the event needs to be marked as
|
|
|
|
# unread, so that add_push_actions_to_staging iterates over it.
|
|
|
|
# If the event shouldn't be marked as unread but should notify the
|
|
|
|
# current user, it'll be added to the dict later.
|
|
|
|
actions_by_user[uid] = []
|
2020-09-02 10:19:37 -06:00
|
|
|
|
2015-12-22 08:19:34 -07:00
|
|
|
for rule in rules:
|
|
|
|
if "enabled" in rule and not rule["enabled"]:
|
|
|
|
continue
|
|
|
|
|
2022-05-10 05:54:30 -06:00
|
|
|
matches = evaluator.check_conditions(
|
|
|
|
rule["conditions"], uid, display_name
|
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"]
|
2020-07-01 04:08:25 -06:00
|
|
|
if actions and "notify" in actions:
|
|
|
|
# Push rules say we should notify the user of this event
|
2020-06-12 08:11:01 -06:00
|
|
|
actions_by_user[uid] = actions
|
2015-12-22 08:19:34 -07:00
|
|
|
break
|
|
|
|
|
2018-02-21 04:29:49 -07: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)
|
2020-09-02 10:19:37 -06:00
|
|
|
await self.store.add_push_actions_to_staging(
|
|
|
|
event.event_id,
|
|
|
|
actions_by_user,
|
|
|
|
count_as_unread,
|
|
|
|
)
|
2018-02-20 04:30:54 -07:00
|
|
|
|
2016-01-18 07:09:47 -07:00
|
|
|
|
2022-03-25 08:58:56 -06:00
|
|
|
MemberMap = Dict[str, Optional[EventIdMembership]]
|
2021-10-11 10:42:10 -06:00
|
|
|
Rule = Dict[str, dict]
|
|
|
|
RulesByUser = Dict[str, List[Rule]]
|
|
|
|
StateGroup = Union[object, int]
|
|
|
|
|
|
|
|
|
2022-01-13 06:49:28 -07:00
|
|
|
@attr.s(slots=True, auto_attribs=True)
|
2021-04-23 04:47:07 -06:00
|
|
|
class RulesForRoomData:
|
|
|
|
"""The data stored in the cache by `RulesForRoom`.
|
|
|
|
|
|
|
|
We don't store `RulesForRoom` directly in the cache as we want our caches to
|
|
|
|
*only* include data, and not references to e.g. the data stores.
|
|
|
|
"""
|
|
|
|
|
2022-03-25 08:58:56 -06:00
|
|
|
# event_id -> EventIdMembership
|
2022-01-13 06:49:28 -07:00
|
|
|
member_map: MemberMap = attr.Factory(dict)
|
2021-04-23 04:47:07 -06:00
|
|
|
# user_id -> rules
|
2022-01-13 06:49:28 -07:00
|
|
|
rules_by_user: RulesByUser = attr.Factory(dict)
|
2021-04-23 04:47:07 -06:00
|
|
|
|
|
|
|
# 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()`
|
2022-01-13 06:49:28 -07:00
|
|
|
state_group: StateGroup = attr.Factory(object)
|
2021-04-23 04:47:07 -06:00
|
|
|
|
|
|
|
# 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.
|
2022-01-13 06:49:28 -07:00
|
|
|
sequence: int = 0
|
2021-04-23 04:47:07 -06:00
|
|
|
|
|
|
|
# A cache of user_ids that we *know* aren't interesting, e.g. user_ids
|
|
|
|
# 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.
|
2022-01-13 06:49:28 -07:00
|
|
|
uninteresting_user_set: Set[str] = attr.Factory(set)
|
2021-04-23 04:47:07 -06:00
|
|
|
|
|
|
|
|
2020-09-04 04:54:56 -06:00
|
|
|
class RulesForRoom:
|
2017-05-02 03:46:01 -06:00
|
|
|
"""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.
|
2021-04-23 04:47:07 -06:00
|
|
|
|
|
|
|
A new instance is constructed for each call to
|
|
|
|
`BulkPushRuleEvaluator._get_rules_for_event`, with the cached data from
|
|
|
|
previous calls passed in.
|
2017-05-02 03:46:01 -06:00
|
|
|
"""
|
|
|
|
|
2020-10-30 05:43:17 -06:00
|
|
|
def __init__(
|
2020-12-11 09:43:53 -07:00
|
|
|
self,
|
|
|
|
hs: "HomeServer",
|
|
|
|
room_id: str,
|
|
|
|
rules_for_room_cache: LruCache,
|
|
|
|
room_push_rule_cache_metrics: CacheMetric,
|
2021-04-23 04:47:07 -06:00
|
|
|
linearizer: Linearizer,
|
|
|
|
cached_data: RulesForRoomData,
|
2020-10-30 05:43:17 -06:00
|
|
|
):
|
2017-05-02 03:46:01 -06:00
|
|
|
"""
|
|
|
|
Args:
|
2020-12-11 09:43:53 -07:00
|
|
|
hs: The HomeServer object.
|
|
|
|
room_id: The room ID.
|
2020-10-30 05:43:17 -06:00
|
|
|
rules_for_room_cache: The cache object that caches these
|
2017-05-02 03:46:01 -06:00
|
|
|
RoomsForUser objects.
|
2020-12-11 09:43:53 -07:00
|
|
|
room_push_rule_cache_metrics: The metrics object
|
2021-04-23 04:47:07 -06:00
|
|
|
linearizer: The linearizer used to ensure only one thing mutates
|
|
|
|
the cache at a time. Keyed off room_id
|
|
|
|
cached_data: Cached data from previous calls to `self.get_rules`,
|
|
|
|
can be mutated.
|
2017-05-02 03:46:01 -06:00
|
|
|
"""
|
|
|
|
self.room_id = room_id
|
|
|
|
self.is_mine_id = hs.is_mine_id
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2017-07-13 04:37:09 -06:00
|
|
|
self.room_push_rule_cache_metrics = room_push_rule_cache_metrics
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2021-04-23 04:47:07 -06:00
|
|
|
# Used to ensure only one thing mutates the cache at a time. Keyed off
|
|
|
|
# room_id.
|
|
|
|
self.linearizer = linearizer
|
|
|
|
|
|
|
|
self.data = cached_data
|
2017-05-19 09:51:05 -06:00
|
|
|
|
2017-05-02 03:46:01 -06: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 04:44:11 -06:00
|
|
|
self.invalidate_all_cb = _Invalidation(rules_for_room_cache, room_id)
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
async def get_rules(
|
|
|
|
self, event: EventBase, context: EventContext
|
|
|
|
) -> Dict[str, List[Dict[str, dict]]]:
|
2017-05-02 03:46:01 -06:00
|
|
|
"""Given an event context return the rules for all users who are
|
|
|
|
currently in the room.
|
|
|
|
"""
|
|
|
|
state_group = context.state_group
|
|
|
|
|
2021-04-23 04:47:07 -06:00
|
|
|
if state_group and self.data.state_group == state_group:
|
2017-07-13 04:23:53 -06:00
|
|
|
logger.debug("Using cached rules for %r", self.room_id)
|
2017-07-13 04:37:09 -06:00
|
|
|
self.room_push_rule_cache_metrics.inc_hits()
|
2021-04-23 04:47:07 -06:00
|
|
|
return self.data.rules_by_user
|
2017-07-13 04:23:53 -06:00
|
|
|
|
2022-04-05 08:43:52 -06:00
|
|
|
async with self.linearizer.queue(self.room_id):
|
2021-04-23 04:47:07 -06:00
|
|
|
if state_group and self.data.state_group == state_group:
|
2017-05-22 08:01:36 -06:00
|
|
|
logger.debug("Using cached rules for %r", self.room_id)
|
2017-07-13 04:37:09 -06:00
|
|
|
self.room_push_rule_cache_metrics.inc_hits()
|
2021-04-23 04:47:07 -06:00
|
|
|
return self.data.rules_by_user
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2017-07-13 04:37:09 -06:00
|
|
|
self.room_push_rule_cache_metrics.inc_misses()
|
|
|
|
|
2017-05-02 03:46:01 -06:00
|
|
|
ret_rules_by_user = {}
|
|
|
|
missing_member_event_ids = {}
|
2021-04-23 04:47:07 -06:00
|
|
|
if state_group and self.data.state_group == context.prev_group:
|
2017-05-02 03:46:01 -06:00
|
|
|
# If we have a simple delta then we can reuse most of the previous
|
|
|
|
# results.
|
2021-04-23 04:47:07 -06:00
|
|
|
ret_rules_by_user = self.data.rules_by_user
|
2017-05-02 03:46:01 -06:00
|
|
|
current_state_ids = context.delta_ids
|
2017-07-13 04:37:09 -06:00
|
|
|
|
|
|
|
push_rules_delta_state_cache_metric.inc_hits()
|
2017-05-02 03:46:01 -06:00
|
|
|
else:
|
2020-07-27 10:21:34 -06:00
|
|
|
current_state_ids = await context.get_current_state_ids()
|
2017-07-13 04:37:09 -06:00
|
|
|
push_rules_delta_state_cache_metric.inc_misses()
|
2020-12-11 09:43:53 -07:00
|
|
|
# Ensure the state IDs exist.
|
|
|
|
assert current_state_ids is not None
|
2017-07-13 04:37:09 -06:00
|
|
|
|
2018-05-21 18:48:57 -06:00
|
|
|
push_rules_state_size_counter.inc(len(current_state_ids))
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2017-05-22 08:01:36 -06:00
|
|
|
logger.debug(
|
|
|
|
"Looking for member changes in %r %r", state_group, current_state_ids
|
|
|
|
)
|
|
|
|
|
2017-05-02 03:46:01 -06:00
|
|
|
# Loop through to see which member events we've seen and have rules
|
|
|
|
# for and which we need to fetch
|
2017-05-19 09:51:05 -06:00
|
|
|
for key in current_state_ids:
|
2017-05-22 07:58:22 -06:00
|
|
|
typ, user_id = key
|
|
|
|
if typ != EventTypes.Member:
|
2017-05-02 03:46:01 -06:00
|
|
|
continue
|
|
|
|
|
2021-04-23 04:47:07 -06:00
|
|
|
if user_id in self.data.uninteresting_user_set:
|
2017-05-19 09:51:05 -06:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not self.is_mine_id(user_id):
|
2021-04-23 04:47:07 -06:00
|
|
|
self.data.uninteresting_user_set.add(user_id)
|
2017-05-19 09:51:05 -06:00
|
|
|
continue
|
|
|
|
|
|
|
|
if self.store.get_if_app_services_interested_in_user(user_id):
|
2021-04-23 04:47:07 -06:00
|
|
|
self.data.uninteresting_user_set.add(user_id)
|
2017-05-19 09:51:05 -06:00
|
|
|
continue
|
|
|
|
|
|
|
|
event_id = current_state_ids[key]
|
|
|
|
|
2021-04-23 04:47:07 -06:00
|
|
|
res = self.data.member_map.get(event_id, None)
|
2017-05-02 03:46:01 -06:00
|
|
|
if res:
|
2022-03-25 08:58:56 -06:00
|
|
|
if res.membership == Membership.JOIN:
|
|
|
|
rules = self.data.rules_by_user.get(res.user_id, None)
|
2017-05-02 03:46:01 -06:00
|
|
|
if rules:
|
2022-03-25 08:58:56 -06:00
|
|
|
ret_rules_by_user[res.user_id] = rules
|
2017-05-02 03:46:01 -06:00
|
|
|
continue
|
|
|
|
|
|
|
|
# If a user has left a room we remove their push rule. If they
|
2020-10-23 10:38:40 -06:00
|
|
|
# joined then we re-add it later in _update_rules_with_member_event_ids
|
2017-05-02 03:46:01 -06:00
|
|
|
ret_rules_by_user.pop(user_id, None)
|
|
|
|
missing_member_event_ids[user_id] = event_id
|
|
|
|
|
|
|
|
if missing_member_event_ids:
|
2020-10-23 10:38:40 -06:00
|
|
|
# If we have some member events we haven't seen, look them up
|
2017-05-02 03:46:01 -06:00
|
|
|
# and fetch push rules for them if appropriate.
|
2017-05-22 08:01:36 -06:00
|
|
|
logger.debug("Found new member events %r", missing_member_event_ids)
|
2020-07-27 10:21:34 -06:00
|
|
|
await self._update_rules_with_member_event_ids(
|
2017-05-22 08:02:12 -06:00
|
|
|
ret_rules_by_user, missing_member_event_ids, state_group, event
|
2017-05-02 03:46:01 -06:00
|
|
|
)
|
2017-07-14 09:11:26 -06:00
|
|
|
else:
|
|
|
|
# The push rules didn't change but lets update the cache anyway
|
|
|
|
self.update_cache(
|
2021-04-23 04:47:07 -06:00
|
|
|
self.data.sequence,
|
2017-07-14 09:11:26 -06:00
|
|
|
members={}, # There were no membership changes
|
|
|
|
rules_by_user=ret_rules_by_user,
|
|
|
|
state_group=state_group,
|
|
|
|
)
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2017-05-22 08:01:36 -06:00
|
|
|
if logger.isEnabledFor(logging.DEBUG):
|
|
|
|
logger.debug(
|
|
|
|
"Returning push rules for %r %r", self.room_id, ret_rules_by_user.keys()
|
|
|
|
)
|
2019-07-23 07:00:55 -06:00
|
|
|
return ret_rules_by_user
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2020-07-27 10:21:34 -06:00
|
|
|
async def _update_rules_with_member_event_ids(
|
2020-12-11 09:43:53 -07:00
|
|
|
self,
|
|
|
|
ret_rules_by_user: Dict[str, list],
|
|
|
|
member_event_ids: Dict[str, str],
|
|
|
|
state_group: Optional[int],
|
|
|
|
event: EventBase,
|
|
|
|
) -> None:
|
2017-05-02 03:46:01 -06:00
|
|
|
"""Update the partially filled rules_by_user dict by fetching rules for
|
|
|
|
any newly joined users in the `member_event_ids` list.
|
|
|
|
|
|
|
|
Args:
|
2020-12-11 09:43:53 -07:00
|
|
|
ret_rules_by_user: Partially filled dict of push rules. Gets
|
2017-05-02 03:46:01 -06:00
|
|
|
updated with any new rules.
|
2020-12-11 09:43:53 -07:00
|
|
|
member_event_ids: Dict of user id to event id for membership events
|
2020-09-02 10:19:37 -06:00
|
|
|
that have happened since the last time we filled rules_by_user
|
2017-05-02 03:46:01 -06:00
|
|
|
state_group: The state group we are currently computing push rules
|
|
|
|
for. Used when updating the cache.
|
2020-12-11 09:43:53 -07:00
|
|
|
event: The event we are currently computing push rules for.
|
2017-05-02 03:46:01 -06:00
|
|
|
"""
|
2021-04-23 04:47:07 -06:00
|
|
|
sequence = self.data.sequence
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2022-03-25 08:58:56 -06:00
|
|
|
members = await self.store.get_membership_from_event_ids(
|
|
|
|
member_event_ids.values()
|
|
|
|
)
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2022-03-25 08:58:56 -06:00
|
|
|
# If the event is a join event then it will be in current state events
|
2017-05-22 09:22:04 -06:00
|
|
|
# map but not in the DB, so we have to explicitly insert it.
|
2017-05-22 08:02:12 -06:00
|
|
|
if event.type == EventTypes.Member:
|
2020-06-15 05:03:36 -06:00
|
|
|
for event_id in member_event_ids.values():
|
2017-05-22 08:02:12 -06:00
|
|
|
if event_id == event.event_id:
|
2022-03-25 08:58:56 -06:00
|
|
|
members[event_id] = EventIdMembership(
|
|
|
|
user_id=event.state_key, membership=event.membership
|
|
|
|
)
|
2017-05-22 08:02:12 -06:00
|
|
|
|
2017-05-22 08:01:36 -06:00
|
|
|
if logger.isEnabledFor(logging.DEBUG):
|
|
|
|
logger.debug("Found members %r: %r", self.room_id, members.values())
|
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
joined_user_ids = {
|
2022-03-25 08:58:56 -06:00
|
|
|
entry.user_id
|
|
|
|
for entry in members.values()
|
|
|
|
if entry and entry.membership == Membership.JOIN
|
2020-02-21 05:15:07 -07:00
|
|
|
}
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
logger.debug("Joined: %r", joined_user_ids)
|
2017-05-22 08:01:36 -06:00
|
|
|
|
2020-09-02 10:19:37 -06:00
|
|
|
# Previously we only considered users with pushers or read receipts in that
|
|
|
|
# room. We can't do this anymore because we use push actions to calculate unread
|
|
|
|
# counts, which don't rely on the user having pushers or sent a read receipt into
|
|
|
|
# the room. Therefore we just need to filter for local users here.
|
2020-12-11 09:43:53 -07:00
|
|
|
user_ids = list(filter(self.is_mine_id, joined_user_ids))
|
2017-05-02 03:46:01 -06:00
|
|
|
|
2020-07-27 10:21:34 -06:00
|
|
|
rules_by_user = await self.store.bulk_get_push_rules(
|
2017-05-02 03:46:01 -06:00
|
|
|
user_ids, on_invalidate=self.invalidate_all_cb
|
|
|
|
)
|
|
|
|
|
|
|
|
ret_rules_by_user.update(
|
2020-06-15 05:03:36 -06:00
|
|
|
item for item in rules_by_user.items() if item[0] is not None
|
2017-05-02 03:46:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
self.update_cache(sequence, members, ret_rules_by_user, state_group)
|
|
|
|
|
2021-10-11 10:42:10 -06:00
|
|
|
def update_cache(
|
|
|
|
self,
|
|
|
|
sequence: int,
|
|
|
|
members: MemberMap,
|
|
|
|
rules_by_user: RulesByUser,
|
|
|
|
state_group: StateGroup,
|
|
|
|
) -> None:
|
2021-04-23 04:47:07 -06:00
|
|
|
if sequence == self.data.sequence:
|
|
|
|
self.data.member_map.update(members)
|
|
|
|
self.data.rules_by_user = rules_by_user
|
|
|
|
self.data.state_group = state_group
|
2017-05-19 04:44:11 -06:00
|
|
|
|
|
|
|
|
2022-01-13 06:49:28 -07:00
|
|
|
@attr.attrs(slots=True, frozen=True, auto_attribs=True)
|
2020-10-30 05:43:17 -06:00
|
|
|
class _Invalidation:
|
|
|
|
# _Invalidation is passed as an `on_invalidate` callback to bulk_get_push_rules,
|
|
|
|
# which means that it it is stored on the bulk_get_push_rules cache entry. In order
|
2021-02-12 09:01:48 -07:00
|
|
|
# to ensure that we don't accumulate lots of redundant callbacks on the cache entry,
|
2020-10-30 05:43:17 -06:00
|
|
|
# we need to ensure that two _Invalidation objects are "equal" if they refer to the
|
|
|
|
# same `cache` and `room_id`.
|
|
|
|
#
|
|
|
|
# attrs provides suitable __hash__ and __eq__ methods, provided we remember to
|
|
|
|
# set `frozen=True`.
|
|
|
|
|
2022-01-13 06:49:28 -07:00
|
|
|
cache: LruCache
|
|
|
|
room_id: str
|
2020-10-30 05:43:17 -06:00
|
|
|
|
2020-12-11 09:43:53 -07:00
|
|
|
def __call__(self) -> None:
|
2021-04-23 04:47:07 -06:00
|
|
|
rules_data = self.cache.get(self.room_id, None, update_metrics=False)
|
|
|
|
if rules_data:
|
|
|
|
rules_data.sequence += 1
|
|
|
|
rules_data.state_group = object()
|
|
|
|
rules_data.member_map = {}
|
|
|
|
rules_data.rules_by_user = {}
|
|
|
|
push_rules_invalidation_counter.inc()
|