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

Add tests for using _flatten_dict with an event. #15002

Merged
merged 4 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/15002.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add tests for `_flatten_dict`.
13 changes: 5 additions & 8 deletions synapse/push/bulk_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
Membership,
RelationTypes,
)
from synapse.api.room_versions import PushRuleRoomFlag, RoomVersion
from synapse.api.room_versions import PushRuleRoomFlag
from synapse.event_auth import auth_types_for_event, get_user_power_level
from synapse.events import EventBase, relation_from_event
from synapse.events.snapshot import EventContext
Expand Down Expand Up @@ -405,7 +405,7 @@ async def _action_for_event_by_user(
room_mention = mentions.get("room") is True

evaluator = PushRuleEvaluator(
_flatten_dict(event, room_version=event.room_version),
_flatten_dict(event),
has_mentions,
user_mentions,
room_mention,
Expand Down Expand Up @@ -491,7 +491,6 @@ async def _action_for_event_by_user(

def _flatten_dict(
d: Union[EventBase, Mapping[str, Any]],
room_version: Optional[RoomVersion] = None,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event has the room version in it; I think this change will mean that the related events stuff (which is also experimental) will start running the extensible events behavior too. Both of these are experimental and it is undefined how they should interact, but that seems OK to me.

prefix: Optional[List[str]] = None,
result: Optional[Dict[str, str]] = None,
) -> Dict[str, str]:
Expand All @@ -511,7 +510,6 @@ def _flatten_dict(

Args:
d: The event or content to continue flattening.
room_version: The room version object.
prefix: The key prefix (from outer dictionaries).
result: The result to mutate.

Expand All @@ -531,14 +529,13 @@ def _flatten_dict(

# `room_version` should only ever be set when looking at the top level of an event
if (
room_version is not None
and PushRuleRoomFlag.EXTENSIBLE_EVENTS in room_version.msc3931_push_features
and isinstance(d, EventBase)
isinstance(d, EventBase)
and PushRuleRoomFlag.EXTENSIBLE_EVENTS in d.room_version.msc3931_push_features
):
# Room supports extensible events: replace `content.body` with the plain text
# representation from `m.markup`, as per MSC1767.
markup = d.get("content").get("m.markup")
if room_version.identifier.startswith("org.matrix.msc1767."):
if d.room_version.identifier.startswith("org.matrix.msc1767."):
markup = d.get("content").get("org.matrix.msc1767.markup")
if markup is not None and isinstance(markup, list):
text = ""
Expand Down
63 changes: 62 additions & 1 deletion tests/push/test_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from synapse.api.constants import EventTypes, HistoryVisibility, Membership
from synapse.api.room_versions import RoomVersions
from synapse.appservice import ApplicationService
from synapse.events import FrozenEvent
from synapse.events import FrozenEvent, make_event_from_dict
from synapse.push.bulk_push_rule_evaluator import _flatten_dict
from synapse.push.httppusher import tweaks_for_actions
from synapse.rest import admin
Expand Down Expand Up @@ -60,6 +60,67 @@ def test_non_string(self) -> None:
}
self.assertEqual({"woo": "woo"}, _flatten_dict(input))

def test_event(self) -> None:
"""Events can also be flattened."""
event = make_event_from_dict(
{
"room_id": "!test:test",
"type": "m.room.message",
"sender": "@alice:test",
"content": {
"msgtype": "m.text",
"body": "Hello world!",
"format": "org.matrix.custom.html",
"formatted_body": "<h1>Hello world!</h1>",
},
},
room_version=RoomVersions.V8,
)
expected = {
"content.msgtype": "m.text",
"content.body": "hello world!",
"content.format": "org.matrix.custom.html",
"content.formatted_body": "<h1>hello world!</h1>",
"room_id": "!test:test",
"sender": "@alice:test",
"type": "m.room.message",
}
self.assertEqual(expected, _flatten_dict(event))

def test_extensible_events(self) -> None:
"""Extensible events has compatibility behaviour."""
event_dict = {
"room_id": "!test:test",
"type": "m.room.message",
"sender": "@alice:test",
"content": {
"org.matrix.msc1767.markup": [
{"mimetype": "text/plain", "body": "Hello world!"},
{"mimetype": "text/html", "body": "<h1>Hello world!</h1>"},
]
},
}

# For a current room version, there's no special behavior.
event = make_event_from_dict(event_dict, room_version=RoomVersions.V8)
expected = {
"room_id": "!test:test",
"sender": "@alice:test",
"type": "m.room.message",
}
self.assertEqual(expected, _flatten_dict(event))

# For a room version with extensible events, they parse out the text/plain
# to a content.body property.
event = make_event_from_dict(event_dict, room_version=RoomVersions.MSC1767v10)
expected = {
"content.body": "hello world!",
"room_id": "!test:test",
"sender": "@alice:test",
"type": "m.room.message",
}
self.assertEqual(expected, _flatten_dict(event))


class PushRuleEvaluatorTestCase(unittest.TestCase):
def _get_evaluator(
Expand Down