From 7709a99e6ffe690abe9dd6999bf09f6f1ad7d32f Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Mon, 22 May 2023 18:32:32 +0200 Subject: [PATCH] Fix order --- synapse/handlers/room_list.py | 1 + .../callbacks/public_rooms_callbacks.py | 4 +- tests/module_api/test_fetch_public_rooms.py | 68 +++++++++++-------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/synapse/handlers/room_list.py b/synapse/handlers/room_list.py index f759c2888b..6c821669b0 100644 --- a/synapse/handlers/room_list.py +++ b/synapse/handlers/room_list.py @@ -189,6 +189,7 @@ class RoomListHandler: if batch_token else None, ) + module_public_rooms.reverse() # Insert the module's reported public rooms into the list for new_room in module_public_rooms: diff --git a/synapse/module_api/callbacks/public_rooms_callbacks.py b/synapse/module_api/callbacks/public_rooms_callbacks.py index 88f996b684..9934f70c27 100644 --- a/synapse/module_api/callbacks/public_rooms_callbacks.py +++ b/synapse/module_api/callbacks/public_rooms_callbacks.py @@ -13,7 +13,7 @@ # limitations under the License. import logging -from typing import Awaitable, Callable, Iterable, List, Optional, Tuple +from typing import Awaitable, Callable, List, Optional, Tuple from synapse.types import PublicRoom @@ -23,7 +23,7 @@ logger = logging.getLogger(__name__) # Types for callbacks to be registered via the module api FETCH_PUBLIC_ROOMS_CALLBACK = Callable[ [bool, Optional[int], Optional[Tuple[int, str]]], - Awaitable[Iterable[PublicRoom]], + Awaitable[List[PublicRoom]], ] diff --git a/tests/module_api/test_fetch_public_rooms.py b/tests/module_api/test_fetch_public_rooms.py index dacc05ac5f..bee351f29e 100644 --- a/tests/module_api/test_fetch_public_rooms.py +++ b/tests/module_api/test_fetch_public_rooms.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from http import HTTPStatus -from typing import Iterable, Optional, Tuple +from typing import List, Optional, Tuple from twisted.test.proto_helpers import MemoryReactor @@ -47,38 +47,46 @@ class FetchPublicRoomsTestCase(HomeserverTestCase): async def cb( forwards: bool, limit: Optional[int], bounds: Optional[Tuple[int, str]] - ) -> Iterable[PublicRoom]: - room1 = PublicRoom( - room_id="!test1:test", - num_joined_members=1, - world_readable=True, - guest_can_join=False, - ) - room3 = PublicRoom( - room_id="!test3:test", - num_joined_members=3, - world_readable=True, - guest_can_join=False, - ) - room3_2 = PublicRoom( - room_id="!test3_2:test", - num_joined_members=3, - world_readable=True, - guest_can_join=False, - ) - rooms = [room3_2, room3, room1] - if not forwards: - rooms.reverse() + ) -> List[PublicRoom]: + rooms_db = [ + PublicRoom( + room_id="!test1:test", + num_joined_members=1, + world_readable=True, + guest_can_join=False, + ), + PublicRoom( + room_id="!test3:test", + num_joined_members=3, + world_readable=True, + guest_can_join=False, + ), + PublicRoom( + room_id="!test3_2:test", + num_joined_members=3, + world_readable=True, + guest_can_join=False, + ), + ] + + result = [] + if limit is not None and bounds is not None: + (last_joined_members, last_room_id) = bounds + for r in rooms_db: + if r.num_joined_members <= last_joined_members: + if r.room_id == last_room_id: + break + result.append(r) + else: + result = rooms_db + + if forwards: + result.reverse() if limit is not None: - if bounds is None: - return rooms[:limit] + result = result[:limit] - (last_joined_members, last_room_id) = bounds - if last_joined_members < 3 or last_room_id == room3_2.room_id: - return [room3, room1] - - return [room3_2, room3, room1] + return result self._module_api.register_public_rooms_callbacks(fetch_public_rooms=cb)