-
Notifications
You must be signed in to change notification settings - Fork 405
Add experimental support for MSC4360: Sliding Sync Threads Extension #19005
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
base: develop
Are you sure you want to change the base?
Changes from 8 commits
cd4f422
6c460b3
4602b56
24b3873
9ef4ca1
79ea4be
6e69338
4dd82e5
4c51247
ab7e5a2
4d7826b
4cb0eea
c757969
bf594a2
a3c7b3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add experimental support for MSC4360: Sliding Sync Threads Extension. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,8 +109,6 @@ async def get_relations( | |
) -> JsonDict: | ||
"""Get related events of a event, ordered by topological ordering. | ||
|
||
TODO Accept a PaginationConfig instead of individual pagination parameters. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been done already, the comment just wasn't updated. |
||
|
||
Args: | ||
requester: The user requesting the relations. | ||
event_id: Fetch events that relate to this event ID. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
from synapse.api.presence import UserPresenceState | ||
from synapse.api.ratelimiting import Ratelimiter | ||
from synapse.events.utils import ( | ||
EventClientSerializer, | ||
SerializeEventConfig, | ||
format_event_for_client_v2_without_room_id, | ||
format_event_raw, | ||
|
@@ -648,6 +649,7 @@ class SlidingSyncRestServlet(RestServlet): | |
- receipts (MSC3960) | ||
- account data (MSC3959) | ||
- thread subscriptions (MSC4308) | ||
- threads (MSC4360) | ||
|
||
Request query parameters: | ||
timeout: How long to wait for new events in milliseconds. | ||
|
@@ -851,7 +853,10 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]: | |
logger.info("Client has disconnected; not serializing response.") | ||
return 200, {} | ||
|
||
response_content = await self.encode_response(requester, sliding_sync_results) | ||
time_now = self.clock.time_msec() | ||
response_content = await self.encode_response( | ||
requester, sliding_sync_results, time_now | ||
) | ||
Comment on lines
+857
to
+860
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call on using the same (consistency) |
||
|
||
return 200, response_content | ||
|
||
|
@@ -860,6 +865,7 @@ async def encode_response( | |
self, | ||
requester: Requester, | ||
sliding_sync_result: SlidingSyncResult, | ||
time_now: int, | ||
) -> JsonDict: | ||
response: JsonDict = defaultdict(dict) | ||
|
||
|
@@ -868,10 +874,10 @@ async def encode_response( | |
if serialized_lists: | ||
response["lists"] = serialized_lists | ||
response["rooms"] = await self.encode_rooms( | ||
requester, sliding_sync_result.rooms | ||
requester, sliding_sync_result.rooms, time_now | ||
) | ||
response["extensions"] = await self.encode_extensions( | ||
requester, sliding_sync_result.extensions | ||
requester, sliding_sync_result.extensions, time_now | ||
) | ||
|
||
return response | ||
|
@@ -903,9 +909,8 @@ async def encode_rooms( | |
self, | ||
requester: Requester, | ||
rooms: Dict[str, SlidingSyncResult.RoomResult], | ||
time_now: int, | ||
) -> JsonDict: | ||
time_now = self.clock.time_msec() | ||
|
||
serialize_options = SerializeEventConfig( | ||
event_format=format_event_for_client_v2_without_room_id, | ||
requester=requester, | ||
|
@@ -1021,7 +1026,10 @@ async def encode_rooms( | |
|
||
@trace_with_opname("sliding_sync.encode_extensions") | ||
async def encode_extensions( | ||
self, requester: Requester, extensions: SlidingSyncResult.Extensions | ||
self, | ||
requester: Requester, | ||
extensions: SlidingSyncResult.Extensions, | ||
time_now: int, | ||
) -> JsonDict: | ||
serialized_extensions: JsonDict = {} | ||
|
||
|
@@ -1091,6 +1099,16 @@ async def encode_extensions( | |
_serialise_thread_subscriptions(extensions.thread_subscriptions) | ||
) | ||
|
||
# excludes both None and falsy `threads` | ||
if extensions.threads: | ||
serialized_extensions[ | ||
"io.element.msc4360.threads" | ||
] = await _serialise_threads( | ||
self.event_serializer, | ||
time_now, | ||
extensions.threads, | ||
) | ||
|
||
return serialized_extensions | ||
|
||
|
||
|
@@ -1127,6 +1145,70 @@ def _serialise_thread_subscriptions( | |
return out | ||
|
||
|
||
async def _serialise_threads( | ||
event_serializer: EventClientSerializer, | ||
time_now: int, | ||
threads: SlidingSyncResult.Extensions.ThreadsExtension, | ||
) -> JsonDict: | ||
""" | ||
Serialize the threads extension response for sliding sync. | ||
|
||
Args: | ||
event_serializer: The event serializer to use for serializing thread root events. | ||
time_now: The current time in milliseconds, used for event serialization. | ||
threads: The threads extension data containing thread updates and pagination tokens. | ||
|
||
Returns: | ||
A JSON-serializable dict containing: | ||
- "updates": A nested dict mapping room_id -> thread_root_id -> thread update. | ||
Each thread update may contain: | ||
- "thread_root": The serialized thread root event (if include_roots was True), | ||
with bundled aggregations including the latest_event in unsigned.m.relations.m.thread. | ||
- "prev_batch": A pagination token for fetching older events in the thread. | ||
- "prev_batch": A pagination token for fetching older thread updates (if available). | ||
""" | ||
out: JsonDict = {} | ||
|
||
if threads.updates: | ||
updates_dict: JsonDict = {} | ||
for room_id, thread_updates in threads.updates.items(): | ||
room_updates: JsonDict = {} | ||
for thread_root_id, update in thread_updates.items(): | ||
# Serialize the update | ||
update_dict: JsonDict = {} | ||
|
||
# Serialize the thread_root event if present | ||
if update.thread_root is not None: | ||
# Create a mapping of event_id to bundled_aggregations | ||
bundle_aggs_map = ( | ||
{thread_root_id: update.bundled_aggregations} | ||
if update.bundled_aggregations | ||
else None | ||
) | ||
serialized_events = await event_serializer.serialize_events( | ||
[update.thread_root], | ||
time_now, | ||
bundle_aggregations=bundle_aggs_map, | ||
) | ||
if serialized_events: | ||
update_dict["thread_root"] = serialized_events[0] | ||
|
||
# Add prev_batch if present | ||
if update.prev_batch is not None: | ||
update_dict["prev_batch"] = str(update.prev_batch) | ||
|
||
room_updates[thread_root_id] = update_dict | ||
|
||
updates_dict[room_id] = room_updates | ||
|
||
out["updates"] = updates_dict | ||
|
||
if threads.prev_batch: | ||
out["prev_batch"] = str(threads.prev_batch) | ||
|
||
return out | ||
|
||
|
||
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None: | ||
SyncRestServlet(hs).register(http_server) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also comments on the MSC that may change how we approach things here -> matrix-org/matrix-spec-proposals#4360 (review)