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

Update the API response for spaces summary over federation #10530

Merged
merged 3 commits into from
Aug 6, 2021
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/10530.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prepare for the new spaces summary endpoint (updates to [MSC2946](https://github.com/matrix-org/matrix-doc/pull/2946)).
57 changes: 38 additions & 19 deletions synapse/handlers/space_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ async def get_space_summary(

# Check if the user is a member of any of the allowed spaces
# from the response.
allowed_rooms = room.get("allowed_spaces")
allowed_rooms = room.get("allowed_room_ids") or room.get(
"allowed_spaces"
)
if (
not include_room
and allowed_rooms
Expand All @@ -198,6 +200,11 @@ async def get_space_summary(

# The user can see the room, include it!
if include_room:
# Before returning to the client, remove the allowed_room_ids
# and allowed_spaces keys.
room.pop("allowed_room_ids", None)
room.pop("allowed_spaces", None)

rooms_result.append(room)
events.extend(room_entry.children)

Expand Down Expand Up @@ -236,11 +243,6 @@ async def get_space_summary(
)
processed_events.add(ev_key)

# Before returning to the client, remove the allowed_spaces key for any
# rooms.
for room in rooms_result:
room.pop("allowed_spaces", None)

return {"rooms": rooms_result, "events": events_result}

async def federation_space_summary(
Expand Down Expand Up @@ -337,7 +339,7 @@ async def _summarize_local_room(
if not await self._is_room_accessible(room_id, requester, origin):
return None

room_entry = await self._build_room_entry(room_id)
room_entry = await self._build_room_entry(room_id, for_federation=bool(origin))

# If the room is not a space, return just the room information.
if room_entry.get("room_type") != RoomTypes.SPACE:
Expand Down Expand Up @@ -548,8 +550,18 @@ async def _is_room_accessible(
)
return False

async def _build_room_entry(self, room_id: str) -> JsonDict:
"""Generate en entry suitable for the 'rooms' list in the summary response"""
async def _build_room_entry(self, room_id: str, for_federation: bool) -> JsonDict:
"""
Generate en entry suitable for the 'rooms' list in the summary response.

Args:
room_id: The room ID to summarize.
for_federation: True if this is a summary requested over federation
(which includes additional fields).

Returns:
The JSON dictionary for the room.
"""
stats = await self._store.get_room_with_stats(room_id)

# currently this should be impossible because we call
Expand All @@ -562,15 +574,6 @@ async def _build_room_entry(self, room_id: str) -> JsonDict:
current_state_ids[(EventTypes.Create, "")]
)

room_version = await self._store.get_room_version(room_id)
allowed_rooms = None
if await self._event_auth_handler.has_restricted_join_rules(
current_state_ids, room_version
):
allowed_rooms = await self._event_auth_handler.get_rooms_that_allow_join(
current_state_ids
)

entry = {
"room_id": stats["room_id"],
"name": stats["name"],
Expand All @@ -585,9 +588,25 @@ async def _build_room_entry(self, room_id: str) -> JsonDict:
"guest_can_join": stats["guest_access"] == "can_join",
"creation_ts": create_event.origin_server_ts,
"room_type": create_event.content.get(EventContentFields.ROOM_TYPE),
"allowed_spaces": allowed_rooms,
}

# Federation requests need to provide additional information so the
# requested server is able to filter the response appropriately.
if for_federation:
room_version = await self._store.get_room_version(room_id)
if await self._event_auth_handler.has_restricted_join_rules(
current_state_ids, room_version
):
allowed_rooms = (
await self._event_auth_handler.get_rooms_that_allow_join(
current_state_ids
)
)
if allowed_rooms:
entry["allowed_room_ids"] = allowed_rooms
# TODO Remove this key once the API is stable.
entry["allowed_spaces"] = allowed_rooms

# Filter out Nones – rather omit the field altogether
room_entry = {k: v for k, v in entry.items() if v is not None}

Expand Down