From e415cb87df6775a4d47337867260b682e80a6fd2 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 3 Aug 2021 15:28:34 -0400 Subject: [PATCH 1/3] Use the updated key for the allowed rooms. --- synapse/handlers/space_summary.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/synapse/handlers/space_summary.py b/synapse/handlers/space_summary.py index 3eb232c83ec5..cb4015c8de04 100644 --- a/synapse/handlers/space_summary.py +++ b/synapse/handlers/space_summary.py @@ -179,7 +179,7 @@ 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 @@ -236,9 +236,10 @@ async def get_space_summary( ) processed_events.add(ev_key) - # Before returning to the client, remove the allowed_spaces key for any - # rooms. + # Before returning to the client, remove the allowed_room_ids and + # allowed_spaces key for any rooms. for room in rooms_result: + room.pop("allowed_room_ids", None) room.pop("allowed_spaces", None) return {"rooms": rooms_result, "events": events_result} @@ -585,6 +586,8 @@ 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_room_ids": allowed_rooms, + # TODO Remove this key once the API is stable. "allowed_spaces": allowed_rooms, } From 6057919ebefef459a969e059052761db3d74448b Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 4 Aug 2021 08:31:29 -0400 Subject: [PATCH 2/3] Only include the allowed room IDs over federation. --- synapse/handlers/space_summary.py | 60 +++++++++++++++++++------------ 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/synapse/handlers/space_summary.py b/synapse/handlers/space_summary.py index cb4015c8de04..2517f278b65e 100644 --- a/synapse/handlers/space_summary.py +++ b/synapse/handlers/space_summary.py @@ -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_room_ids") or room.get("allowed_spaces") + allowed_rooms = room.get("allowed_room_ids") or room.get( + "allowed_spaces" + ) if ( not include_room and allowed_rooms @@ -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) @@ -236,12 +243,6 @@ async def get_space_summary( ) processed_events.add(ev_key) - # Before returning to the client, remove the allowed_room_ids and - # allowed_spaces key for any rooms. - for room in rooms_result: - room.pop("allowed_room_ids", None) - room.pop("allowed_spaces", None) - return {"rooms": rooms_result, "events": events_result} async def federation_space_summary( @@ -338,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: @@ -549,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 @@ -563,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"], @@ -586,11 +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_room_ids": allowed_rooms, - # TODO Remove this key once the API is stable. - "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} From d2f32a786cffa6bd072d936e0cf75aa721f6841b Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 4 Aug 2021 09:37:38 -0400 Subject: [PATCH 3/3] Newsfragment --- changelog.d/10530.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/10530.misc diff --git a/changelog.d/10530.misc b/changelog.d/10530.misc new file mode 100644 index 000000000000..3cf22f9daf9a --- /dev/null +++ b/changelog.d/10530.misc @@ -0,0 +1 @@ +Prepare for the new spaces summary endpoint (updates to [MSC2946](https://github.com/matrix-org/matrix-doc/pull/2946)).