Skip to content

Commit

Permalink
Sliding Sync: Update filters to be robust against remote invite rooms (
Browse files Browse the repository at this point in the history
…#17450)

Update `filters.is_encrypted` and `filters.types`/`filters.not_types` to
be robust when dealing with remote invite rooms in Sliding Sync.

Part of
[MSC3575](matrix-org/matrix-spec-proposals#3575):
Sliding Sync

Follow-up to #17434

We now take into account current state, fallback to stripped state
for invite/knock rooms, then historical state. If we can't determine
the info needed to filter a room (either from state or stripped state),
it is filtered out.
  • Loading branch information
MadLittleMods authored Jul 30, 2024
1 parent b221f0b commit 46de0ee
Show file tree
Hide file tree
Showing 11 changed files with 1,598 additions and 110 deletions.
1 change: 1 addition & 0 deletions changelog.d/17450.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update experimental [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575) Sliding Sync `/sync` endpoint to handle invite/knock rooms when filtering.
8 changes: 8 additions & 0 deletions synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ class EventContentFields:
# This is deprecated in MSC2175.
ROOM_CREATOR: Final = "creator"

# The version of the room for `m.room.create` events.
ROOM_VERSION: Final = "room_version"

ROOM_NAME: Final = "name"

# Used in m.room.guest_access events.
GUEST_ACCESS: Final = "guest_access"

Expand All @@ -237,6 +242,9 @@ class EventContentFields:
# an unspecced field added to to-device messages to identify them uniquely-ish
TO_DEVICE_MSGID: Final = "org.matrix.msgid"

# `m.room.encryption`` algorithm field
ENCRYPTION_ALGORITHM: Final = "algorithm"


class EventUnsignedContentFields:
"""Fields found inside the 'unsigned' data on events"""
Expand Down
19 changes: 19 additions & 0 deletions synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,22 @@ def relation_from_event(event: EventBase) -> Optional[_EventRelation]:
aggregation_key = None

return _EventRelation(parent_id, rel_type, aggregation_key)


@attr.s(slots=True, frozen=True, auto_attribs=True)
class StrippedStateEvent:
"""
A stripped down state event. Usually used for remote invite/knocks so the user can
make an informed decision on whether they want to join.
Attributes:
type: Event `type`
state_key: Event `state_key`
sender: Event `sender`
content: Event `content`
"""

type: str
state_key: str
sender: str
content: Dict[str, Any]
29 changes: 28 additions & 1 deletion synapse/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from synapse.api.room_versions import RoomVersion
from synapse.types import JsonDict, Requester

from . import EventBase, make_event_from_dict
from . import EventBase, StrippedStateEvent, make_event_from_dict

if TYPE_CHECKING:
from synapse.handlers.relations import BundledAggregations
Expand Down Expand Up @@ -854,3 +854,30 @@ def strip_event(event: EventBase) -> JsonDict:
"content": event.content,
"sender": event.sender,
}


def parse_stripped_state_event(raw_stripped_event: Any) -> Optional[StrippedStateEvent]:
"""
Given a raw value from an event's `unsigned` field, attempt to parse it into a
`StrippedStateEvent`.
"""
if isinstance(raw_stripped_event, dict):
# All of these fields are required
type = raw_stripped_event.get("type")
state_key = raw_stripped_event.get("state_key")
sender = raw_stripped_event.get("sender")
content = raw_stripped_event.get("content")
if (
isinstance(type, str)
and isinstance(state_key, str)
and isinstance(sender, str)
and isinstance(content, dict)
):
return StrippedStateEvent(
type=type,
state_key=state_key,
sender=sender,
content=content,
)

return None
Loading

0 comments on commit 46de0ee

Please sign in to comment.