Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rooms.required_state to Sliding Sync /sync #17342

Merged
merged 123 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 114 commits
Commits
Show all changes
123 commits
Select commit Hold shift + click to select a range
079194c
Return some room timeline data in Sliding Sync
MadLittleMods Jun 17, 2024
3e0f759
Strip invite/knock event itself and avoid mutating event `unsigned`
MadLittleMods Jun 17, 2024
5e2fd4e
Add changelog
MadLittleMods Jun 17, 2024
8ce06f1
Fix sort being lost
MadLittleMods Jun 17, 2024
aa5f54a
Start on required_state
MadLittleMods Jun 18, 2024
5c175d5
Add some notes from pairing
MadLittleMods Jun 18, 2024
9089bfe
Remove required_state for now
MadLittleMods Jun 18, 2024
9427991
Clean up knock_state comments
MadLittleMods Jun 18, 2024
19b2297
Calculate `num_live`
MadLittleMods Jun 18, 2024
81d36f3
Add tests for `limited`
MadLittleMods Jun 18, 2024
9791209
Add more tests
MadLittleMods Jun 18, 2024
70ecd4d
Fix lint
MadLittleMods Jun 19, 2024
71eabe5
Make room name optional
MadLittleMods Jun 19, 2024
39b4f10
Update comments
MadLittleMods Jun 19, 2024
9883b0f
Add bundled aggregations
MadLittleMods Jun 19, 2024
1c06153
Determine limited before filtering
MadLittleMods Jun 19, 2024
57ba033
Merge branch 'develop' into madlittlemods/sliding-sync-room-data
MadLittleMods Jun 19, 2024
c81f300
Add better support for leave/ban
MadLittleMods Jun 19, 2024
d801db0
Fix lints
MadLittleMods Jun 19, 2024
6942b64
Merge branch 'develop' into madlittlemods/sliding-sync-room-data
MadLittleMods Jun 19, 2024
884b448
Update some wording
MadLittleMods Jun 19, 2024
ef3f606
Start of combinining `RoomSyncConfig`
MadLittleMods Jun 20, 2024
bbc3eb5
Different better
MadLittleMods Jun 20, 2024
0eb0294
Remove unused `IncludeOldRooms` class
MadLittleMods Jun 20, 2024
5b17d70
Add `RoomSyncConfig` tests
MadLittleMods Jun 20, 2024
b6edd6b
Better `RoomSyncConfig` tests
MadLittleMods Jun 20, 2024
cddcf1f
Fetch required state
MadLittleMods Jun 20, 2024
ddbcd78
Add future note about other people we might care about when lazy-load…
MadLittleMods Jun 20, 2024
55ad559
Start of thinking to handle partial stated rooms
MadLittleMods Jun 20, 2024
41988a7
Allow the next room to fill its place if we exclude a partially-state…
MadLittleMods Jun 21, 2024
8dc4009
Add a test
MadLittleMods Jun 21, 2024
5fdbeff
Make sure we add some specific tests
MadLittleMods Jun 21, 2024
b1b4231
Merge branch 'develop' into madlittlemods/sliding-sync-room-data
MadLittleMods Jun 24, 2024
87fac19
Fix lints
MadLittleMods Jun 24, 2024
0e71a2f
Add TODO for filtering call invites in public rooms
MadLittleMods Jun 24, 2024
21ca02c
`newly_joined` vs `limited` already being tracked in a discussion
MadLittleMods Jun 24, 2024
3568311
Fix spelling typo
MadLittleMods Jun 25, 2024
7aea406
Just stripped_state for invite rooms
MadLittleMods Jun 25, 2024
e3e431f
Finish up stripped_state for invite rooms
MadLittleMods Jun 25, 2024
303d834
Add tracking discussion for not optional in the future
MadLittleMods Jun 25, 2024
4c22131
Start testing for the correct room membership (failing)
MadLittleMods Jun 25, 2024
83d6f76
Describe `current_state_delta_stream` better
MadLittleMods Jun 25, 2024
fbd92e1
Add `get_current_state_delta_membership_changes_for_user(...)` (using…
MadLittleMods Jun 26, 2024
6c791a8
WIP: Add back `newly_left`
MadLittleMods Jun 26, 2024
27d74b0
Iterate
MadLittleMods Jun 26, 2024
fb8fbd4
Just fetch full events for `get_current_state_delta_membership_change…
MadLittleMods Jun 26, 2024
d91aa00
Remove extras
MadLittleMods Jun 26, 2024
daa7e36
Add docstring
MadLittleMods Jun 26, 2024
cccbd15
Refactor back to not pulling out full events
MadLittleMods Jun 26, 2024
62c6a4e
Add `newly_joined` support to `get_sync_room_ids_for_user(...)`
MadLittleMods Jun 26, 2024
39259f6
Join both tables with stream_ordering
MadLittleMods Jun 26, 2024
5c21315
Merge branch 'develop' into madlittlemods/sliding-sync-room-data
MadLittleMods Jun 26, 2024
c60aca7
Fix clause change
MadLittleMods Jun 26, 2024
11db1be
Remove debug log
MadLittleMods Jun 26, 2024
7395e10
Fix `builtins.SyntaxError: EOL while scanning string literal (test_sy…
MadLittleMods Jun 26, 2024
2bf3923
Add some tests for `get_current_state_delta_membership_changes_for_us…
MadLittleMods Jun 26, 2024
ec2d8dc
Create events using helper
MadLittleMods Jun 26, 2024
0b9a903
Add test that remotely joins room
MadLittleMods Jun 27, 2024
48d0acf
Actually test `get_current_state_delta_membership_changes_for_user(..…
MadLittleMods Jun 27, 2024
2a944ff
Add state of the db in each situation
MadLittleMods Jun 27, 2024
8df39d1
Remove redundant `instance_name` column
MadLittleMods Jun 27, 2024
b7914e7
Add skipped test for state resets
MadLittleMods Jun 27, 2024
7eb1806
Fix lints
MadLittleMods Jun 27, 2024
935b98c
All `get_current_state_delta_membership_changes_for_user(...)` tests …
MadLittleMods Jun 27, 2024
f163fcf
Remove need for topological_ordering
MadLittleMods Jun 27, 2024
956f20e
(currently failing) Add test to make sure membership changes don't re…
MadLittleMods Jun 27, 2024
830e09d
Grab `prev_membership` to see whether the server left the room (fixes…
MadLittleMods Jun 27, 2024
15fcead
Slight clean-up
MadLittleMods Jun 27, 2024
81c06be
Detect state resets
MadLittleMods Jun 27, 2024
eb159c1
Don't worry about `state_reset` for now
MadLittleMods Jun 27, 2024
ba56350
Passing current tests
MadLittleMods Jun 27, 2024
f774032
Add better comments
MadLittleMods Jun 27, 2024
325856e
Inclusive ranges
MadLittleMods Jun 27, 2024
63c7b50
(doesn't work) Add test for batch persisting multiple member events f…
MadLittleMods Jun 27, 2024
1158058
Opt for tackling more batch scenarios in future PRs
MadLittleMods Jun 27, 2024
32b8b68
Add TODO to handle state resets
MadLittleMods Jun 27, 2024
6045e11
Merge branch 'develop' into madlittlemods/sliding-sync-room-data
MadLittleMods Jul 1, 2024
9e53336
Avoid fetching full events for `prev_event_ids`
MadLittleMods Jul 1, 2024
a4263bf
Update stream tests with prev event info
MadLittleMods Jul 1, 2024
10d78d6
Protect for non-existent prev events
MadLittleMods Jul 2, 2024
0061561
Merge branch 'develop' into madlittlemods/sliding-sync-room-data
MadLittleMods Jul 2, 2024
73f2903
Merge branch 'madlittlemods/sliding-sync-room-data' into madlittlemod…
MadLittleMods Jul 2, 2024
e419587
Protect from no timeline_events
MadLittleMods Jul 2, 2024
2bd6cba
Select `to_key if to_key else from_key`
MadLittleMods Jul 2, 2024
b8687e7
Select `to_key if to_key else from_key`
MadLittleMods Jul 2, 2024
7c9513c
Add missing test description
MadLittleMods Jul 2, 2024
dfb4324
Merge branch 'madlittlemods/sliding-sync-room-data' into madlittlemod…
MadLittleMods Jul 2, 2024
78df54a
Get state at leave/ban
MadLittleMods Jul 2, 2024
8b73185
Trigger CI again
MadLittleMods Jul 2, 2024
8aa2297
Merge branch 'madlittlemods/sliding-sync-room-data' into madlittlemod…
MadLittleMods Jul 2, 2024
939b9ce
room_state can be None
MadLittleMods Jul 2, 2024
126ce1e
Merge branch 'develop' into madlittlemods/sliding-sync-room-data
erikjohnston Jul 2, 2024
cae489b
Merge branch 'madlittlemods/sliding-sync-room-data' into madlittlemod…
MadLittleMods Jul 2, 2024
2175edf
Merge branch 'develop' into madlittlemods/sliding-sync-required-state
MadLittleMods Jul 2, 2024
01a4b43
Rename `get_state_at` -> `get_state_ids_at`
MadLittleMods Jul 2, 2024
77b8d9c
Add `get_state_at(...)` that returns full events
MadLittleMods Jul 2, 2024
a3ac718
Some clean-up
MadLittleMods Jul 2, 2024
c935d03
Merge branch 'develop' into madlittlemods/sliding-sync-required-state
MadLittleMods Jul 2, 2024
c71cf86
Handle wildcard merges in from_config
MadLittleMods Jul 2, 2024
dcad14b
Support multiple wildcard types
MadLittleMods Jul 2, 2024
ce503fc
Parameterize tests
MadLittleMods Jul 2, 2024
4eb82e3
Combine wildcard configs
MadLittleMods Jul 2, 2024
646b328
No need for `get_first_item_in_set(...)`
MadLittleMods Jul 2, 2024
f2e0c5e
Workaround wildcard event types
MadLittleMods Jul 2, 2024
859f9b0
Rename to `StateValues` because it can be used in the event_type or s…
MadLittleMods Jul 2, 2024
2c4eea7
Add `_assertIncludes(...)` and `_assertRequiredStateIncludes(...)`
MadLittleMods Jul 3, 2024
c1222c6
Add exact option
MadLittleMods Jul 3, 2024
75446e7
Add more tests
MadLittleMods Jul 3, 2024
aa59ead
Fix lints
MadLittleMods Jul 3, 2024
cc30a85
Add changelog
MadLittleMods Jul 3, 2024
5c8dd69
Either or
MadLittleMods Jul 3, 2024
ea78da5
Add explicit test for no required_state
MadLittleMods Jul 3, 2024
5023943
Bette as a fixme
MadLittleMods Jul 3, 2024
fbad6b0
Fix `\n` backslash usage forbidden in f-string
MadLittleMods Jul 3, 2024
b3ca35e
Hoist repetitive logic outside loop
MadLittleMods Jul 3, 2024
dc8bef0
Remove extra safety check
MadLittleMods Jul 3, 2024
f2174c1
Move comment to the right place
MadLittleMods Jul 3, 2024
e22a291
Refactor to map from event type to list of state keys
MadLittleMods Jul 3, 2024
fe4f2ea
Refactor usage
MadLittleMods Jul 3, 2024
52444d9
Better loop pattern
MadLittleMods Jul 3, 2024
7c133a8
Fix lint
MadLittleMods Jul 3, 2024
3cf9a77
Add line break between if-cases
MadLittleMods Jul 4, 2024
7ef822c
Merge branch 'develop' into madlittlemods/sliding-sync-required-state
MadLittleMods Jul 4, 2024
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/17342.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return "required state" in experimental [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575) Sliding Sync `/sync` endpoint.
470 changes: 417 additions & 53 deletions synapse/handlers/sliding_sync.py

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ async def _compute_state_delta_for_full_sync(
await_full_state = True
lazy_load_members = False

state_at_timeline_end = await self._state_storage_controller.get_state_at(
state_at_timeline_end = await self._state_storage_controller.get_state_ids_at(
room_id,
stream_position=end_token,
state_filter=state_filter,
Expand Down Expand Up @@ -1480,11 +1480,13 @@ async def _compute_state_delta_for_incremental_sync(
else:
# We can get here if the user has ignored the senders of all
# the recent events.
state_at_timeline_start = await self._state_storage_controller.get_state_at(
room_id,
stream_position=end_token,
state_filter=state_filter,
await_full_state=await_full_state,
state_at_timeline_start = (
await self._state_storage_controller.get_state_ids_at(
room_id,
stream_position=end_token,
state_filter=state_filter,
await_full_state=await_full_state,
)
)

if batch.limited:
Expand All @@ -1502,14 +1504,14 @@ async def _compute_state_delta_for_incremental_sync(
# about them).
state_filter = StateFilter.all()

state_at_previous_sync = await self._state_storage_controller.get_state_at(
state_at_previous_sync = await self._state_storage_controller.get_state_ids_at(
room_id,
stream_position=since_token,
state_filter=state_filter,
await_full_state=await_full_state,
)

state_at_timeline_end = await self._state_storage_controller.get_state_at(
state_at_timeline_end = await self._state_storage_controller.get_state_ids_at(
room_id,
stream_position=end_token,
state_filter=state_filter,
Expand Down Expand Up @@ -2508,7 +2510,7 @@ async def _get_room_changes_for_incremental_sync(
continue

if room_id in sync_result_builder.joined_room_ids or has_join:
old_state_ids = await self._state_storage_controller.get_state_at(
old_state_ids = await self._state_storage_controller.get_state_ids_at(
room_id,
since_token,
state_filter=StateFilter.from_types([(EventTypes.Member, user_id)]),
Expand Down Expand Up @@ -2539,7 +2541,7 @@ async def _get_room_changes_for_incremental_sync(
else:
if not old_state_ids:
old_state_ids = (
await self._state_storage_controller.get_state_at(
await self._state_storage_controller.get_state_ids_at(
room_id,
since_token,
state_filter=StateFilter.from_types(
Expand Down
10 changes: 5 additions & 5 deletions synapse/rest/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ async def encode_rooms(
if room_result.initial:
serialized_rooms[room_id]["initial"] = room_result.initial

# This will omitted for invite/knock rooms with `stripped_state`
# This will be omitted for invite/knock rooms with `stripped_state`
if room_result.required_state is not None:
serialized_required_state = (
await self.event_serializer.serialize_events(
Expand All @@ -1007,7 +1007,7 @@ async def encode_rooms(
)
serialized_rooms[room_id]["required_state"] = serialized_required_state

# This will omitted for invite/knock rooms with `stripped_state`
# This will be omitted for invite/knock rooms with `stripped_state`
if room_result.timeline_events is not None:
serialized_timeline = await self.event_serializer.serialize_events(
room_result.timeline_events,
Expand All @@ -1017,17 +1017,17 @@ async def encode_rooms(
)
serialized_rooms[room_id]["timeline"] = serialized_timeline

# This will omitted for invite/knock rooms with `stripped_state`
# This will be omitted for invite/knock rooms with `stripped_state`
if room_result.limited is not None:
serialized_rooms[room_id]["limited"] = room_result.limited

# This will omitted for invite/knock rooms with `stripped_state`
# This will be omitted for invite/knock rooms with `stripped_state`
if room_result.prev_batch is not None:
serialized_rooms[room_id]["prev_batch"] = (
await room_result.prev_batch.to_string(self.store)
)

# This will omitted for invite/knock rooms with `stripped_state`
# This will be omitted for invite/knock rooms with `stripped_state`
if room_result.num_live is not None:
serialized_rooms[room_id]["num_live"] = room_result.num_live

Expand Down
26 changes: 25 additions & 1 deletion synapse/storage/controllers/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ async def get_state_after_event(

return state_ids

async def get_state_at(
async def get_state_ids_at(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Renamed get_state_at(...) -> get_state_ids_at(...) to be accurate

Also add get_state_at(...) since we have a use now

self,
room_id: str,
stream_position: StreamToken,
Expand Down Expand Up @@ -460,6 +460,30 @@ async def get_state_at(
)
return state

@trace
@tag_args
async def get_state_at(
self,
room_id: str,
stream_position: StreamToken,
state_filter: Optional[StateFilter] = None,
await_full_state: bool = True,
) -> StateMap[EventBase]:
"""Same as `get_state_ids_at` but also fetches the events"""
state_map_ids = await self.get_state_ids_at(
room_id, stream_position, state_filter, await_full_state
)

event_map = await self.stores.main.get_events(list(state_map_ids.values()))

state_map = {}
for key, event_id in state_map_ids.items():
event = event_map.get(event_id)
if event:
state_map[key] = event

return state_map

@trace
@tag_args
async def get_state_for_groups(
Expand Down
24 changes: 12 additions & 12 deletions synapse/types/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class RoomResult:
avatar: Room avatar
heroes: List of stripped membership events (containing `user_id` and optionally
`avatar_url` and `displayname`) for the users used to calculate the room name.
is_dm: Flag to specify whether the room is a direct-message room (most likely
between two people).
initial: Flag which is set when this is the first time the server is sending this
data on this connection. Clients can use this flag to replace or update
their local state. When there is an update, servers MUST omit this flag
Expand All @@ -167,15 +169,20 @@ class RoomResult:
the timeline events above. This allows clients to show accurate reaction
counts (or edits, threads), even if some of the reaction events were skipped
over in a gappy sync.
is_dm: Flag to specify whether the room is a direct-message room (most likely
between two people).
stripped_state: Stripped state events (for rooms where the usre is
invited/knocked). Same as `rooms.invite.$room_id.invite_state` in sync v2,
absent on joined/left rooms
prev_batch: A token that can be passed as a start parameter to the
`/rooms/<room_id>/messages` API to retrieve earlier messages.
limited: True if their are more events than fit between the given position and now.
Sync again to get more.
num_live: The number of timeline events which have just occurred and are not historical.
The last N events are 'live' and should be treated as such. This is mostly
useful to determine whether a given @mention event should make a noise or not.
Clients cannot rely solely on the absence of `initial: true` to determine live
events because if a room not in the sliding window bumps into the window because
of an @mention it will have `initial: true` yet contain a single live event
(with potentially other old events in the timeline).
joined_count: The number of users with membership of join, including the client's
own user ID. (same as sync `v2 m.joined_member_count`)
invited_count: The number of users with membership of invite. (same as sync v2
Expand All @@ -184,37 +191,30 @@ class RoomResult:
as sync v2)
highlight_count: The number of unread notifications for this room with the highlight
flag set. (same as sync v2)
num_live: The number of timeline events which have just occurred and are not historical.
The last N events are 'live' and should be treated as such. This is mostly
useful to determine whether a given @mention event should make a noise or not.
Clients cannot rely solely on the absence of `initial: true` to determine live
events because if a room not in the sliding window bumps into the window because
of an @mention it will have `initial: true` yet contain a single live event
(with potentially other old events in the timeline).
"""

name: Optional[str]
avatar: Optional[str]
heroes: Optional[List[EventBase]]
is_dm: bool
initial: bool
# Only optional because it won't be included for invite/knock rooms with `stripped_state`
required_state: Optional[List[EventBase]]
# Only optional because it won't be included for invite/knock rooms with `stripped_state`
timeline_events: Optional[List[EventBase]]
bundled_aggregations: Optional[Dict[str, "BundledAggregations"]]
is_dm: bool
# Optional because it's only relevant to invite/knock rooms
stripped_state: Optional[List[JsonDict]]
# Only optional because it won't be included for invite/knock rooms with `stripped_state`
prev_batch: Optional[StreamToken]
# Only optional because it won't be included for invite/knock rooms with `stripped_state`
limited: Optional[bool]
# Only optional because it won't be included for invite/knock rooms with `stripped_state`
num_live: Optional[int]
joined_count: int
invited_count: int
notification_count: int
highlight_count: int
# Only optional because it won't be included for invite/knock rooms with `stripped_state`
num_live: Optional[int]

@attr.s(slots=True, frozen=True, auto_attribs=True)
class SlidingWindowList:
Expand Down
Loading
Loading