Convert room member storage tuples to attrs. (#10629)

Instead of using namedtuples. This helps with asserting type hints
and code completion.
This commit is contained in:
Patrick Cloke 2021-08-18 09:22:07 -04:00 committed by GitHub
parent 6e613a10d0
commit bec01c0758
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 30 deletions

View file

@ -307,7 +307,9 @@ class RoomMemberWorkerStore(EventsWorkerStore):
)
@cached()
async def get_invited_rooms_for_local_user(self, user_id: str) -> RoomsForUser:
async def get_invited_rooms_for_local_user(
self, user_id: str
) -> List[RoomsForUser]:
"""Get all the rooms the *local* user is invited to.
Args:
@ -522,7 +524,9 @@ class RoomMemberWorkerStore(EventsWorkerStore):
_get_users_server_still_shares_room_with_txn,
)
async def get_rooms_for_user(self, user_id: str, on_invalidate=None):
async def get_rooms_for_user(
self, user_id: str, on_invalidate=None
) -> FrozenSet[str]:
"""Returns a set of room_ids the user is currently joined to.
If a remote user only returns rooms this server is currently

View file

@ -365,7 +365,7 @@ class UserDirectoryBackgroundUpdateStore(StateDeltasStore):
return False
async def update_profile_in_user_dir(
self, user_id: str, display_name: str, avatar_url: str
self, user_id: str, display_name: Optional[str], avatar_url: Optional[str]
) -> None:
"""
Update or add a user's profile in the user directory.

View file

@ -14,25 +14,40 @@
# limitations under the License.
import logging
from collections import namedtuple
from typing import List, Optional, Tuple
import attr
from synapse.types import PersistedEventPosition
logger = logging.getLogger(__name__)
RoomsForUser = namedtuple(
"RoomsForUser", ("room_id", "sender", "membership", "event_id", "stream_ordering")
)
GetRoomsForUserWithStreamOrdering = namedtuple(
"GetRoomsForUserWithStreamOrdering", ("room_id", "event_pos")
)
@attr.s(slots=True, frozen=True, weakref_slot=True, auto_attribs=True)
class RoomsForUser:
room_id: str
sender: str
membership: str
event_id: str
stream_ordering: int
# We store this using a namedtuple so that we save about 3x space over using a
# dict.
ProfileInfo = namedtuple("ProfileInfo", ("avatar_url", "display_name"))
@attr.s(slots=True, frozen=True, weakref_slot=True, auto_attribs=True)
class GetRoomsForUserWithStreamOrdering:
room_id: str
event_pos: PersistedEventPosition
# "members" points to a truncated list of (user_id, event_id) tuples for users of
# a given membership type, suitable for use in calculating heroes for a room.
# "count" points to the total numberr of users of a given membership type.
MemberSummary = namedtuple("MemberSummary", ("members", "count"))
@attr.s(slots=True, frozen=True, weakref_slot=True, auto_attribs=True)
class ProfileInfo:
avatar_url: Optional[str]
display_name: Optional[str]
@attr.s(slots=True, frozen=True, weakref_slot=True, auto_attribs=True)
class MemberSummary:
# A truncated list of (user_id, event_id) tuples for users of a given
# membership type, suitable for use in calculating heroes for a room.
members: List[Tuple[str, str]]
# The total number of users of a given membership type.
count: int