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

Support MSC3266 room summaries over federation #11507

Merged
merged 19 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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/11507.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support [MSC3266](https://github.com/matrix-org/matrix-doc/pull/3266) room summaries over federation.
51 changes: 46 additions & 5 deletions synapse/handlers/room_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ async def _is_local_room_accessible(
return False

async def _is_remote_room_accessible(
self, requester: str, room_id: str, room: JsonDict
self, requester: Optional[str], room_id: str, room: JsonDict
) -> bool:
"""
Calculate whether the room received over federation should be shown to the requester.
Expand All @@ -645,7 +645,7 @@ async def _is_remote_room_accessible(
due to an invite, etc.

Args:
requester: The user requesting the summary.
requester: The user requesting the summary. If not passed only world readability is checked.
deepbluev7 marked this conversation as resolved.
Show resolved Hide resolved
room_id: The room ID returned over federation.
room: The summary of the room returned over federation.

Expand All @@ -659,6 +659,8 @@ async def _is_remote_room_accessible(
or room.get("world_readable") is True
):
return True
elif not requester:
return False

# Check if the user is a member of any of the allowed rooms from the response.
allowed_rooms = room.get("allowed_room_ids")
Expand Down Expand Up @@ -713,8 +715,12 @@ async def _build_room_entry(self, room_id: str, for_federation: bool) -> JsonDic
),
"guest_can_join": stats["guest_access"] == "can_join",
"room_type": create_event.content.get(EventContentFields.ROOM_TYPE),
"im.nheko.summary.version": stats["version"],
Copy link
Member

Choose a reason for hiding this comment

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

In some situations this could potentially fix the hack on L656-657 about assuming knock is valid. Would probably not be available often enough though.

We should guard these with the unstable config flag, probably?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we should wait for this to be stable first and available for a while and then we can use that to fix the hack, since we need the remote server to use it. After a few months it is probably safe to just assume a room is version 1, if no version is returned.

I think an unstable flag would probably make more sense for the whole feature instead of just some namespaced fields. I was hoping the MSC just FCPs and passes in the next few weeks, since it is quite small and then we can just stabilize it (so I don't need to figure out how to add unstable flags :D). But I'll defer that to your judgement. If you want a flag, I can add it. I am just lazy .-.

Copy link
Member

Choose a reason for hiding this comment

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

I think an unstable flag would probably make more sense for the whole feature instead of just some namespaced fields. I was hoping the MSC just FCPs and passes in the next few weeks, since it is quite small and then we can just stabilize it (so I don't need to figure out how to add unstable flags :D). But I'll defer that to your judgement. If you want a flag, I can add it. I am just lazy .-.

There already is one for the whole feature -- I'm suggesting to re-use it and guard these fields with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I think I figured it out. I cache the flag in the Handler, as it doesn't cache the hs yet and I think just caching the flag makes more sense? No idea :D

}

if stats["encryption"]:
entry["im.nheko.summary.encryption"] = stats["encryption"]
deepbluev7 marked this conversation as resolved.
Show resolved Hide resolved

# Federation requests need to provide additional information so the
# requested server is able to filter the response appropriately.
if for_federation:
Expand Down Expand Up @@ -812,15 +818,50 @@ async def get_room_summary(

room_summary["membership"] = membership or "leave"
else:
# TODO federation API, descoped from initial unstable implementation
# as MSC needs more maturing on that side.
raise SynapseError(400, "Federation is not currently supported.")
# Reuse the hierarchy query over federation
if remote_room_hosts is None:
raise SynapseError(400, "Missing via to query remote room")

(
room_entry,
children_room_entries,
inaccessible_children,
) = await self._summarize_remote_room_hierarchy(
_RoomQueueEntry(room_id, remote_room_hosts),
suggested_only=True,
)

if room_entry:
Copy link
Member

Choose a reason for hiding this comment

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

I'm finding the level of indentation here rather difficult to follow. It could be nicer to check if not room_entry and raise immediately, rather than at the bottom of the method, but won't block on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've inverted the logic now and combined the 2 if cases, which gets rid of 2 levels of indentation. If I don't combine them I would have to duplicate the error messages, because I want the error to be the same for inaccessible or not found.

room = room_entry.room
fed_room_id = room_entry.room_id
deepbluev7 marked this conversation as resolved.
Show resolved Hide resolved

if fed_room_id == room_id:
deepbluev7 marked this conversation as resolved.
Show resolved Hide resolved
# The results over federation might include rooms that the we,
# as the requesting server, are allowed to see, but the requesting
# user is not permitted to see.
deepbluev7 marked this conversation as resolved.
Show resolved Hide resolved
#
# Filter the returned results to only what is accessible to the user.
if await self._is_remote_room_accessible(
requester, fed_room_id, 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)
deepbluev7 marked this conversation as resolved.
Show resolved Hide resolved

# A remote room can't be in the joined state
room["membership"] = "leave"
deepbluev7 marked this conversation as resolved.
Show resolved Hide resolved

return room

raise NotFoundError("Room not found or is not accessible")

return room_summary


@attr.s(frozen=True, slots=True, auto_attribs=True)
class _RoomQueueEntry:

deepbluev7 marked this conversation as resolved.
Show resolved Hide resolved
# The room ID of this entry.
room_id: str
# The server to query if the room is not known locally.
Expand Down