From b64aa1a3bb381ce888a6b02da0444f66ca0ee305 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Mon, 22 May 2023 17:26:50 +0200 Subject: [PATCH] use attrs class --- synapse/handlers/room_list.py | 24 ++++++++++----------- synapse/storage/databases/main/room.py | 5 ----- synapse/types/__init__.py | 15 +++++++------ synapse/util/__init__.py | 4 ++++ tests/module_api/test_fetch_public_rooms.py | 2 +- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/synapse/handlers/room_list.py b/synapse/handlers/room_list.py index 7e952f711a..f759c2888b 100644 --- a/synapse/handlers/room_list.py +++ b/synapse/handlers/room_list.py @@ -34,6 +34,7 @@ from synapse.api.errors import ( SynapseError, ) from synapse.types import JsonDict, ThirdPartyInstanceID +from synapse.util import filter_none from synapse.util.caches.descriptors import _CacheContext, cached from synapse.util.caches.response_cache import ResponseCache @@ -195,11 +196,10 @@ class RoomListHandler: for i in range(len(results)): r = results[i] if ( - forwards - and new_room["num_joined_members"] >= r["num_joined_members"] + forwards and new_room.num_joined_members >= r.num_joined_members ) or ( not forwards - and new_room["num_joined_members"] <= r["num_joined_members"] + and new_room.num_joined_members <= r.num_joined_members ): results.insert(i, new_room) inserted = True @@ -232,33 +232,33 @@ class RoomListHandler: # If there was a token given then we assume that there # must be previous results. response["prev_batch"] = RoomListNextBatch( - last_joined_members=initial_entry["num_joined_members"], - last_room_id=initial_entry["room_id"], + last_joined_members=initial_entry.num_joined_members, + last_room_id=initial_entry.room_id, direction_is_forward=False, ).to_token() if more_to_come: response["next_batch"] = RoomListNextBatch( - last_joined_members=final_entry["num_joined_members"], - last_room_id=final_entry["room_id"], + last_joined_members=final_entry.num_joined_members, + last_room_id=final_entry.room_id, direction_is_forward=True, ).to_token() else: if batch_token is not None: response["next_batch"] = RoomListNextBatch( - last_joined_members=final_entry["num_joined_members"], - last_room_id=final_entry["room_id"], + last_joined_members=final_entry.num_joined_members, + last_room_id=final_entry.room_id, direction_is_forward=True, ).to_token() if more_to_come: response["prev_batch"] = RoomListNextBatch( - last_joined_members=initial_entry["num_joined_members"], - last_room_id=initial_entry["room_id"], + last_joined_members=initial_entry.num_joined_members, + last_room_id=initial_entry.room_id, direction_is_forward=False, ).to_token() - response["chunk"] = results + response["chunk"] = [attr.asdict(r, filter=filter_none) for r in results] response["total_room_count_estimate"] = await self.store.count_public_rooms( network_tuple, diff --git a/synapse/storage/databases/main/room.py b/synapse/storage/databases/main/room.py index 7ecb8fb3c3..745e833b4d 100644 --- a/synapse/storage/databases/main/room.py +++ b/synapse/storage/databases/main/room.py @@ -541,11 +541,6 @@ class RoomWorkerStore(CacheInvalidationWorkerStore): room_type=room["room_type"], ) - # Filter out Nones – rather omit the field altogether - for key in list(entry): - if entry[key] is None: # type: ignore[literal-required] - del entry[key] # type: ignore[misc] - return entry return [build_room_entry(r) for r in ret_val] diff --git a/synapse/types/__init__.py b/synapse/types/__init__.py index 7f55cf74ca..a4afe6e8c2 100644 --- a/synapse/types/__init__.py +++ b/synapse/types/__init__.py @@ -961,17 +961,18 @@ class UserInfo: is_shadow_banned: bool -class PublicRoom(TypedDict, total=False): +@attr.s(auto_attribs=True, frozen=True, slots=True) +class PublicRoom: room_id: str - name: Optional[str] - topic: Optional[str] num_joined_members: int - canonical_alias: Optional[str] - avatar_url: Optional[str] world_readable: bool guest_can_join: bool - join_rule: Optional[str] - room_type: Optional[str] + name: Optional[str] = None + topic: Optional[str] = None + canonical_alias: Optional[str] = None + avatar_url: Optional[str] = None + join_rule: Optional[str] = None + room_type: Optional[str] = None class UserProfile(TypedDict): diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py index 9ddd26ccaa..6f40914639 100644 --- a/synapse/util/__init__.py +++ b/synapse/util/__init__.py @@ -201,3 +201,7 @@ class ExceptionBundle(Exception): parts.append(str(e)) super().__init__("\n - ".join(parts)) self.exceptions = exceptions + + +def filter_none(attr: attr.Attribute, value: Any) -> bool: + return value is not None diff --git a/tests/module_api/test_fetch_public_rooms.py b/tests/module_api/test_fetch_public_rooms.py index b0b448c41c..dacc05ac5f 100644 --- a/tests/module_api/test_fetch_public_rooms.py +++ b/tests/module_api/test_fetch_public_rooms.py @@ -75,7 +75,7 @@ class FetchPublicRoomsTestCase(HomeserverTestCase): return rooms[:limit] (last_joined_members, last_room_id) = bounds - if last_joined_members < 3 or last_room_id == room3_2["room_id"]: + if last_joined_members < 3 or last_room_id == room3_2.room_id: return [room3, room1] return [room3_2, room3, room1]