1515# limitations under the License.
1616
1717import logging
18- from typing import Any , Dict
18+ from typing import Any , Dict , Optional
1919
2020from six .moves import urllib
2121
2222from twisted .internet import defer
2323
2424from synapse .api .constants import Membership
25+ from synapse .api .errors import Codes , HttpResponseException , SynapseError
2526from synapse .api .urls import (
2627 FEDERATION_UNSTABLE_PREFIX ,
2728 FEDERATION_V1_PREFIX ,
@@ -326,18 +327,25 @@ def send_invite_v2(self, destination, room_id, event_id, content):
326327 @log_function
327328 def get_public_rooms (
328329 self ,
329- remote_server ,
330- limit ,
331- since_token ,
332- search_filter = None ,
333- include_all_networks = False ,
334- third_party_instance_id = None ,
330+ remote_server : str ,
331+ limit : Optional [ int ] = None ,
332+ since_token : Optional [ str ] = None ,
333+ search_filter : Optional [ Dict ] = None ,
334+ include_all_networks : bool = False ,
335+ third_party_instance_id : Optional [ str ] = None ,
335336 ):
337+ """Get the list of public rooms from a remote homeserver
338+
339+ See synapse.federation.federation_client.FederationClient.get_public_rooms for
340+ more information.
341+ """
336342 if search_filter :
337343 # this uses MSC2197 (Search Filtering over Federation)
338344 path = _create_v1_path ("/publicRooms" )
339345
340- data = {"include_all_networks" : "true" if include_all_networks else "false" }
346+ data = {
347+ "include_all_networks" : "true" if include_all_networks else "false"
348+ } # type: Dict[str, Any]
341349 if third_party_instance_id :
342350 data ["third_party_instance_id" ] = third_party_instance_id
343351 if limit :
@@ -347,9 +355,19 @@ def get_public_rooms(
347355
348356 data ["filter" ] = search_filter
349357
350- response = yield self .client .post_json (
351- destination = remote_server , path = path , data = data , ignore_backoff = True
352- )
358+ try :
359+ response = yield self .client .post_json (
360+ destination = remote_server , path = path , data = data , ignore_backoff = True
361+ )
362+ except HttpResponseException as e :
363+ if e .code == 403 :
364+ raise SynapseError (
365+ 403 ,
366+ "You are not allowed to view the public rooms list of %s"
367+ % (remote_server ,),
368+ errcode = Codes .FORBIDDEN ,
369+ )
370+ raise
353371 else :
354372 path = _create_v1_path ("/publicRooms" )
355373
@@ -363,9 +381,19 @@ def get_public_rooms(
363381 if since_token :
364382 args ["since" ] = [since_token ]
365383
366- response = yield self .client .get_json (
367- destination = remote_server , path = path , args = args , ignore_backoff = True
368- )
384+ try :
385+ response = yield self .client .get_json (
386+ destination = remote_server , path = path , args = args , ignore_backoff = True
387+ )
388+ except HttpResponseException as e :
389+ if e .code == 403 :
390+ raise SynapseError (
391+ 403 ,
392+ "You are not allowed to view the public rooms list of %s"
393+ % (remote_server ,),
394+ errcode = Codes .FORBIDDEN ,
395+ )
396+ raise
369397
370398 return response
371399
0 commit comments