From 6fadb560fcdc92466b0b5cac02551cb41ca9f148 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Wed, 14 Aug 2019 13:30:36 +0100 Subject: [PATCH 1/5] Support MSC2197 outbound with unstable prefix Signed-off-by: Olivier Wilkinson (reivilibre) --- synapse/federation/transport/client.py | 46 +++++++++++++++++--------- synapse/handlers/room_list.py | 29 +++++++++++++++- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/synapse/federation/transport/client.py b/synapse/federation/transport/client.py index 0cea0d2a1..2e99f77eb 100644 --- a/synapse/federation/transport/client.py +++ b/synapse/federation/transport/client.py @@ -327,21 +327,37 @@ class TransportLayerClient(object): include_all_networks=False, third_party_instance_id=None, ): - path = _create_v1_path("/publicRooms") - - args = {"include_all_networks": "true" if include_all_networks else "false"} - if third_party_instance_id: - args["third_party_instance_id"] = (third_party_instance_id,) - if limit: - args["limit"] = [str(limit)] - if since_token: - args["since"] = [since_token] - - # TODO(erikj): Actually send the search_filter across federation. - - response = yield self.client.get_json( - destination=remote_server, path=path, args=args, ignore_backoff=True - ) + if search_filter: + # TODO(MSC2197): Move to V1 prefix + path = _create_path(FEDERATION_UNSTABLE_PREFIX, "/publicRooms") + + data = {"include_all_networks": "true" if include_all_networks else "false"} + if third_party_instance_id: + data["third_party_instance_id"] = third_party_instance_id + if limit: + data["limit"] = str(limit) + if since_token: + data["since"] = since_token + + data["filter"] = search_filter + + response = yield self.client.post_json( + destination=remote_server, path=path, data=data, ignore_backoff=True + ) + else: + path = _create_v1_path("/publicRooms") + + args = {"include_all_networks": "true" if include_all_networks else "false"} + if third_party_instance_id: + args["third_party_instance_id"] = (third_party_instance_id,) + if limit: + args["limit"] = [str(limit)] + if since_token: + args["since"] = [since_token] + + response = yield self.client.get_json( + destination=remote_server, path=path, args=args, ignore_backoff=True + ) return response diff --git a/synapse/handlers/room_list.py b/synapse/handlers/room_list.py index e9094ad02..a7e55f00e 100644 --- a/synapse/handlers/room_list.py +++ b/synapse/handlers/room_list.py @@ -25,6 +25,7 @@ from unpaddedbase64 import decode_base64, encode_base64 from twisted.internet import defer from synapse.api.constants import EventTypes, JoinRules +from synapse.api.errors import Codes, HttpResponseException from synapse.types import ThirdPartyInstanceID from synapse.util.async_helpers import concurrently_execute from synapse.util.caches.descriptors import cachedInlineCallbacks @@ -485,7 +486,33 @@ class RoomListHandler(BaseHandler): return {"chunk": [], "total_room_count_estimate": 0} if search_filter: - # We currently don't support searching across federation, so we have + # Searching across federation is defined in MSC2197. + # However, the remote homeserver may or may not actually support it. + # So we first try an MSC2197 remote-filtered search, then fall back + # to a locally-filtered search if we must. + + try: + res = yield self._get_remote_list_cached( + server_name, + limit=limit, + since_token=since_token, + include_all_networks=include_all_networks, + third_party_instance_id=third_party_instance_id, + search_filter=search_filter, + ) + return res + except HttpResponseException as hre: + syn_err = hre.to_synapse_error() + if hre.code in (404, 405) or syn_err.errcode in ( + Codes.UNRECOGNIZED, + Codes.NOT_FOUND, + ): + logger.debug("Falling back to locally-filtered /publicRooms") + else: + raise # Not an error that should trigger a fallback. + + # if we reach this point, then we fall back to the situation where + # we currently don't support searching across federation, so we have # to do it manually without pagination limit = None since_token = None From 2253b083d9b3cc0aba89aba98298214cf960acd7 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 15 Aug 2019 11:06:21 +0100 Subject: [PATCH 2/5] Add support for inbound MSC2197 requests on unstable Federation API Signed-off-by: Olivier Wilkinson (reivilibre) --- synapse/federation/transport/server.py | 60 +++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/synapse/federation/transport/server.py b/synapse/federation/transport/server.py index ea4e1b6d0..e17555c4c 100644 --- a/synapse/federation/transport/server.py +++ b/synapse/federation/transport/server.py @@ -751,6 +751,64 @@ class PublicRoomList(BaseFederationServlet): return 200, data +class UnstablePublicRoomList(BaseFederationServlet): + """ + Fetch the public room list for this server. + + This API returns information in the same format as /publicRooms on the + client API, but will only ever include local public rooms and hence is + intended for consumption by other home servers. + + This is the unstable-prefixed version which adds support for MSC2197, which + is still undergoing review. + """ + + PATH = "/publicRooms" + PREFIX = FEDERATION_UNSTABLE_PREFIX + + def __init__(self, handler, authenticator, ratelimiter, server_name, allow_access): + super(UnstablePublicRoomList, self).__init__( + handler, authenticator, ratelimiter, server_name + ) + self.allow_access = allow_access + + # TODO(MSC2197): Move away from Unstable prefix and back to normal prefix + async def on_POST(self, origin, content, query): + if not self.allow_access: + raise FederationDeniedError(origin) + + limit = int(content.get("limit", 100)) + since_token = content.get("since", None) + search_filter = content.get("filter", None) + + include_all_networks = content.get("include_all_networks", False) + third_party_instance_id = content.get("third_party_instance_id", None) + + if include_all_networks: + network_tuple = None + if third_party_instance_id is not None: + raise SynapseError( + 400, "Can't use include_all_networks with an explicit network" + ) + elif third_party_instance_id is None: + network_tuple = ThirdPartyInstanceID(None, None) + else: + network_tuple = ThirdPartyInstanceID.from_string(third_party_instance_id) + + if search_filter is None: + logger.warning("Nonefilter") + + data = await self.handler.get_local_public_room_list( + limit=limit, + since_token=since_token, + search_filter=search_filter, + network_tuple=network_tuple, + from_federation=True, + ) + + return 200, data + + class FederationVersionServlet(BaseFederationServlet): PATH = "/version" @@ -1315,7 +1373,7 @@ FEDERATION_SERVLET_CLASSES = ( OPENID_SERVLET_CLASSES = (OpenIdUserInfo,) -ROOM_LIST_CLASSES = (PublicRoomList,) +ROOM_LIST_CLASSES = (PublicRoomList, UnstablePublicRoomList) GROUP_SERVER_SERVLET_CLASSES = ( FederationGroupsProfileServlet, From a3df04a899f9feccf145e167e7ddc5228fa927e2 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 15 Aug 2019 11:08:54 +0100 Subject: [PATCH 3/5] Newsfile Signed-off-by: Olivier Wilkinson (reivilibre) --- changelog.d/5859.feature | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelog.d/5859.feature diff --git a/changelog.d/5859.feature b/changelog.d/5859.feature new file mode 100644 index 000000000..c897c6603 --- /dev/null +++ b/changelog.d/5859.feature @@ -0,0 +1,2 @@ +Add unstable support for MSC2197 (filtered search requests over federation), in +order to allow upcoming room directory query performance improvements. From bb29bc29374d10d151ebff13c4e95e07c0ef3a29 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Tue, 20 Aug 2019 08:49:31 +0100 Subject: [PATCH 4/5] Use MSC2197 on stable prefix as it has almost finished FCP Signed-off-by: Olivier Wilkinson (reivilibre) --- synapse/federation/transport/client.py | 4 ++-- synapse/federation/transport/server.py | 26 ++------------------------ 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/synapse/federation/transport/client.py b/synapse/federation/transport/client.py index 2e99f77eb..482a101c0 100644 --- a/synapse/federation/transport/client.py +++ b/synapse/federation/transport/client.py @@ -328,8 +328,8 @@ class TransportLayerClient(object): third_party_instance_id=None, ): if search_filter: - # TODO(MSC2197): Move to V1 prefix - path = _create_path(FEDERATION_UNSTABLE_PREFIX, "/publicRooms") + # this uses MSC2197 (Search Filtering over Federation) + path = _create_v1_path("/publicRooms") data = {"include_all_networks": "true" if include_all_networks else "false"} if third_party_instance_id: diff --git a/synapse/federation/transport/server.py b/synapse/federation/transport/server.py index e17555c4c..027b33f67 100644 --- a/synapse/federation/transport/server.py +++ b/synapse/federation/transport/server.py @@ -750,30 +750,8 @@ class PublicRoomList(BaseFederationServlet): ) return 200, data - -class UnstablePublicRoomList(BaseFederationServlet): - """ - Fetch the public room list for this server. - - This API returns information in the same format as /publicRooms on the - client API, but will only ever include local public rooms and hence is - intended for consumption by other home servers. - - This is the unstable-prefixed version which adds support for MSC2197, which - is still undergoing review. - """ - - PATH = "/publicRooms" - PREFIX = FEDERATION_UNSTABLE_PREFIX - - def __init__(self, handler, authenticator, ratelimiter, server_name, allow_access): - super(UnstablePublicRoomList, self).__init__( - handler, authenticator, ratelimiter, server_name - ) - self.allow_access = allow_access - - # TODO(MSC2197): Move away from Unstable prefix and back to normal prefix async def on_POST(self, origin, content, query): + # This implements MSC2197 (Search Filtering over Federation) if not self.allow_access: raise FederationDeniedError(origin) @@ -1373,7 +1351,7 @@ FEDERATION_SERVLET_CLASSES = ( OPENID_SERVLET_CLASSES = (OpenIdUserInfo,) -ROOM_LIST_CLASSES = (PublicRoomList, UnstablePublicRoomList) +ROOM_LIST_CLASSES = (PublicRoomList,) GROUP_SERVER_SERVLET_CLASSES = ( FederationGroupsProfileServlet, From 502728777c00e3242f7436c18b8d918b6613d377 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Tue, 20 Aug 2019 08:49:53 +0100 Subject: [PATCH 5/5] Newsfile on one line Signed-off-by: Olivier Wilkinson (reivilibre) --- changelog.d/5859.feature | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/changelog.d/5859.feature b/changelog.d/5859.feature index c897c6603..52df7fc81 100644 --- a/changelog.d/5859.feature +++ b/changelog.d/5859.feature @@ -1,2 +1 @@ -Add unstable support for MSC2197 (filtered search requests over federation), in -order to allow upcoming room directory query performance improvements. +Add unstable support for MSC2197 (filtered search requests over federation), in order to allow upcoming room directory query performance improvements.