Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

fix: Push notifications for invite over federation #13719

Merged
1 change: 1 addition & 0 deletions changelog.d/13719.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Send invite push notifications for invite over federation.
4 changes: 4 additions & 0 deletions synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ def is_historical(self) -> bool:
"""
return self._dict.get("historical", False)

def is_notifiable(self) -> bool:
"""Whether this event can trigger a push notification"""
return not self.is_outlier() or self.is_out_of_band_membership()


class EventBase(metaclass=abc.ABCMeta):
@property
Expand Down
13 changes: 10 additions & 3 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def __init__(self, hs: "HomeServer"):
self.http_client = hs.get_proxied_blacklisted_http_client()
self._replication = hs.get_replication_data_handler()
self._federation_event_handler = hs.get_federation_event_handler()
self._bulk_push_rule_evaluator = hs.get_bulk_push_rule_evaluator()

self._clean_room_for_join_client = ReplicationCleanRoomRestServlet.make_client(
hs
Expand Down Expand Up @@ -946,9 +947,15 @@ async def on_invite_request(
)

context = EventContext.for_outlier(self._storage_controllers)
await self._federation_event_handler.persist_events_and_notify(
event.room_id, [(event, context)]
)

await self._bulk_push_rule_evaluator.action_for_event_by_user(event, context)
try:
await self._federation_event_handler.persist_events_and_notify(
event.room_id, [(event, context)]
)
except Exception:
await self.store.remove_push_actions_from_staging(event.event_id)
raise

return event

Expand Down
1 change: 1 addition & 0 deletions synapse/handlers/federation_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,7 @@ async def persist_events_and_notify(
if instance != self._instance_name:
# Limit the number of events sent over replication. We choose 200
# here as that is what we default to in `max_request_body_size(..)`
result = {}
try:
for batch in batch_iter(event_and_contexts, 200):
result = await self._send_events(
Expand Down
10 changes: 7 additions & 3 deletions synapse/push/bulk_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ async def _get_rules_for_event(

async def _get_power_levels_and_sender_level(
self, event: EventBase, context: EventContext
) -> Tuple[dict, int]:
) -> Tuple[dict, Optional[int]]:
# There are no power levels and sender levels possible to get from outlier
if event.internal_metadata.is_outlier():
return {}, None

event_types = auth_types_for_event(event.room_version, event)
prev_state_ids = await context.get_prev_state_ids(
StateFilter.from_types(event_types)
Expand Down Expand Up @@ -250,8 +254,8 @@ async def action_for_event_by_user(
should increment the unread count, and insert the results into the
event_push_actions_staging table.
"""
if event.internal_metadata.is_outlier():
# This can happen due to out of band memberships
if not event.internal_metadata.is_notifiable():
# Push rules for events that aren't notifiable can't be processed by this
return

# Disable counting as unread unless the experimental configuration is
Expand Down
16 changes: 8 additions & 8 deletions synapse/push/push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@
INEQUALITY_EXPR = re.compile("^([=<>]*)([0-9]*)$")


def _room_member_count(
ev: EventBase, condition: Mapping[str, Any], room_member_count: int
) -> bool:
def _room_member_count(condition: Mapping[str, Any], room_member_count: int) -> bool:
return _test_ineq_condition(condition, room_member_count)


def _sender_notification_permission(
ev: EventBase,
condition: Mapping[str, Any],
sender_power_level: int,
sender_power_level: Optional[int],
power_levels: Dict[str, Union[int, Dict[str, int]]],
) -> bool:
if sender_power_level is None:
return False

notif_level_key = condition.get("key")
if notif_level_key is None:
return False
Expand Down Expand Up @@ -129,7 +129,7 @@ def __init__(
self,
event: EventBase,
room_member_count: int,
sender_power_level: int,
sender_power_level: Optional[int],
power_levels: Dict[str, Union[int, Dict[str, int]]],
relations: Dict[str, Set[Tuple[str, str]]],
relations_match_enabled: bool,
Expand Down Expand Up @@ -198,10 +198,10 @@ def matches(
elif condition["kind"] == "contains_display_name":
return self._contains_display_name(display_name)
elif condition["kind"] == "room_member_count":
return _room_member_count(self._event, condition, self._room_member_count)
return _room_member_count(condition, self._room_member_count)
elif condition["kind"] == "sender_notification_permission":
return _sender_notification_permission(
self._event, condition, self._sender_power_level, self._power_levels
condition, self._sender_power_level, self._power_levels
)
elif (
condition["kind"] == "org.matrix.msc3772.relation_match"
Expand Down
10 changes: 6 additions & 4 deletions synapse/storage/controllers/persist_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,18 @@ async def enqueue(
for d in ret_vals:
replaced_events.update(d)

events = []
persisted_events = []
for event, _ in events_and_contexts:
existing_event_id = replaced_events.get(event.event_id)
if existing_event_id:
events.append(await self.main_store.get_event(existing_event_id))
persisted_events.append(
await self.main_store.get_event(existing_event_id)
)
else:
events.append(event)
persisted_events.append(event)

return (
events,
persisted_events,
self.main_store.get_room_max_token(),
)

Expand Down
12 changes: 6 additions & 6 deletions synapse/storage/databases/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2180,13 +2180,13 @@ def _set_push_actions_for_event_and_users_txn(
appear in events_and_context.
"""

# Only non outlier events will have push actions associated with them,
# Only notifiable events will have push actions associated with them,
# so let's filter them out. (This makes joining large rooms faster, as
# these queries took seconds to process all the state events).
non_outlier_events = [
notifiable_events = [
event
for event, _ in events_and_contexts
if not event.internal_metadata.is_outlier()
if event.internal_metadata.is_notifiable()
]

sql = """
Expand All @@ -2199,7 +2199,7 @@ def _set_push_actions_for_event_and_users_txn(
WHERE event_id = ?
"""

if non_outlier_events:
if notifiable_events:
txn.execute_batch(
sql,
(
Expand All @@ -2209,12 +2209,12 @@ def _set_push_actions_for_event_and_users_txn(
event.depth,
event.event_id,
)
for event in non_outlier_events
for event in notifiable_events
),
)

room_to_event_ids: Dict[str, List[str]] = {}
for e in non_outlier_events:
for e in notifiable_events:
room_to_event_ids.setdefault(e.room_id, []).append(e.event_id)

for room_id, event_ids in room_to_event_ids.items():
Expand Down