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,
)
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,

View File

@ -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]

View File

@ -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):

View File

@ -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

View File

@ -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]