use attrs class

anoa/public_rooms_module_api
Mathieu Velten 2023-05-22 17:26:50 +02:00
parent 3347725cc1
commit b64aa1a3bb
5 changed files with 25 additions and 25 deletions

View File

@ -34,6 +34,7 @@ from synapse.api.errors import (
SynapseError, SynapseError,
) )
from synapse.types import JsonDict, ThirdPartyInstanceID from synapse.types import JsonDict, ThirdPartyInstanceID
from synapse.util import filter_none
from synapse.util.caches.descriptors import _CacheContext, cached from synapse.util.caches.descriptors import _CacheContext, cached
from synapse.util.caches.response_cache import ResponseCache from synapse.util.caches.response_cache import ResponseCache
@ -195,11 +196,10 @@ class RoomListHandler:
for i in range(len(results)): for i in range(len(results)):
r = results[i] r = results[i]
if ( if (
forwards forwards and new_room.num_joined_members >= r.num_joined_members
and new_room["num_joined_members"] >= r["num_joined_members"]
) or ( ) or (
not forwards 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) results.insert(i, new_room)
inserted = True inserted = True
@ -232,33 +232,33 @@ class RoomListHandler:
# If there was a token given then we assume that there # If there was a token given then we assume that there
# must be previous results. # must be previous results.
response["prev_batch"] = RoomListNextBatch( response["prev_batch"] = RoomListNextBatch(
last_joined_members=initial_entry["num_joined_members"], last_joined_members=initial_entry.num_joined_members,
last_room_id=initial_entry["room_id"], last_room_id=initial_entry.room_id,
direction_is_forward=False, direction_is_forward=False,
).to_token() ).to_token()
if more_to_come: if more_to_come:
response["next_batch"] = RoomListNextBatch( response["next_batch"] = RoomListNextBatch(
last_joined_members=final_entry["num_joined_members"], last_joined_members=final_entry.num_joined_members,
last_room_id=final_entry["room_id"], last_room_id=final_entry.room_id,
direction_is_forward=True, direction_is_forward=True,
).to_token() ).to_token()
else: else:
if batch_token is not None: if batch_token is not None:
response["next_batch"] = RoomListNextBatch( response["next_batch"] = RoomListNextBatch(
last_joined_members=final_entry["num_joined_members"], last_joined_members=final_entry.num_joined_members,
last_room_id=final_entry["room_id"], last_room_id=final_entry.room_id,
direction_is_forward=True, direction_is_forward=True,
).to_token() ).to_token()
if more_to_come: if more_to_come:
response["prev_batch"] = RoomListNextBatch( response["prev_batch"] = RoomListNextBatch(
last_joined_members=initial_entry["num_joined_members"], last_joined_members=initial_entry.num_joined_members,
last_room_id=initial_entry["room_id"], last_room_id=initial_entry.room_id,
direction_is_forward=False, direction_is_forward=False,
).to_token() ).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( response["total_room_count_estimate"] = await self.store.count_public_rooms(
network_tuple, network_tuple,

View File

@ -541,11 +541,6 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
room_type=room["room_type"], 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 entry
return [build_room_entry(r) for r in ret_val] return [build_room_entry(r) for r in ret_val]

View File

@ -961,17 +961,18 @@ class UserInfo:
is_shadow_banned: bool is_shadow_banned: bool
class PublicRoom(TypedDict, total=False): @attr.s(auto_attribs=True, frozen=True, slots=True)
class PublicRoom:
room_id: str room_id: str
name: Optional[str]
topic: Optional[str]
num_joined_members: int num_joined_members: int
canonical_alias: Optional[str]
avatar_url: Optional[str]
world_readable: bool world_readable: bool
guest_can_join: bool guest_can_join: bool
join_rule: Optional[str] name: Optional[str] = None
room_type: Optional[str] 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): class UserProfile(TypedDict):

View File

@ -201,3 +201,7 @@ class ExceptionBundle(Exception):
parts.append(str(e)) parts.append(str(e))
super().__init__("\n - ".join(parts)) super().__init__("\n - ".join(parts))
self.exceptions = exceptions self.exceptions = exceptions
def filter_none(attr: attr.Attribute, value: Any) -> bool:
return value is not None

View File

@ -75,7 +75,7 @@ class FetchPublicRoomsTestCase(HomeserverTestCase):
return rooms[:limit] return rooms[:limit]
(last_joined_members, last_room_id) = bounds (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, room1]
return [room3_2, room3, room1] return [room3_2, room3, room1]