Update the API response for spaces summary over federation. (#10530)

This adds 'allowed_room_ids' (in addition to 'allowed_spaces', for backwards
compatibility) to the federation response of the spaces summary.

A future PR will remove the 'allowed_spaces' flag.
This commit is contained in:
Patrick Cloke 2021-08-06 07:40:29 -04:00 committed by GitHub
parent 74d7336686
commit f4ade972ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 19 deletions

1
changelog.d/10530.misc Normal file
View File

@ -0,0 +1 @@
Prepare for the new spaces summary endpoint (updates to [MSC2946](https://github.com/matrix-org/matrix-doc/pull/2946)).

View File

@ -179,7 +179,9 @@ class SpaceSummaryHandler:
# Check if the user is a member of any of the allowed spaces # Check if the user is a member of any of the allowed spaces
# from the response. # from the response.
allowed_rooms = room.get("allowed_spaces") allowed_rooms = room.get("allowed_room_ids") or room.get(
"allowed_spaces"
)
if ( if (
not include_room not include_room
and allowed_rooms and allowed_rooms
@ -198,6 +200,11 @@ class SpaceSummaryHandler:
# The user can see the room, include it! # The user can see the room, include it!
if include_room: 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) rooms_result.append(room)
events.extend(room_entry.children) events.extend(room_entry.children)
@ -236,11 +243,6 @@ class SpaceSummaryHandler:
) )
processed_events.add(ev_key) 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} return {"rooms": rooms_result, "events": events_result}
async def federation_space_summary( async def federation_space_summary(
@ -337,7 +339,7 @@ class SpaceSummaryHandler:
if not await self._is_room_accessible(room_id, requester, origin): if not await self._is_room_accessible(room_id, requester, origin):
return None 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 the room is not a space, return just the room information.
if room_entry.get("room_type") != RoomTypes.SPACE: if room_entry.get("room_type") != RoomTypes.SPACE:
@ -548,8 +550,18 @@ class SpaceSummaryHandler:
) )
return False return False
async def _build_room_entry(self, room_id: str) -> JsonDict: 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""" """
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) stats = await self._store.get_room_with_stats(room_id)
# currently this should be impossible because we call # currently this should be impossible because we call
@ -562,15 +574,6 @@ class SpaceSummaryHandler:
current_state_ids[(EventTypes.Create, "")] 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 = { entry = {
"room_id": stats["room_id"], "room_id": stats["room_id"],
"name": stats["name"], "name": stats["name"],
@ -585,9 +588,25 @@ class SpaceSummaryHandler:
"guest_can_join": stats["guest_access"] == "can_join", "guest_can_join": stats["guest_access"] == "can_join",
"creation_ts": create_event.origin_server_ts, "creation_ts": create_event.origin_server_ts,
"room_type": create_event.content.get(EventContentFields.ROOM_TYPE), "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 # Filter out Nones rather omit the field altogether
room_entry = {k: v for k, v in entry.items() if v is not None} room_entry = {k: v for k, v in entry.items() if v is not None}