Register room members only when needed

This commit is contained in:
miruka 2020-05-17 15:29:23 -04:00
parent 05a547971d
commit ef9b880152
3 changed files with 42 additions and 33 deletions

View File

@ -1,5 +1,7 @@
# TODO # TODO
- shorten indicator numbers with hundreds
- fix python getting stuck when loading large room - fix python getting stuck when loading large room
- fix lag when clicking accounts in the AccountBar with a very long room list - fix lag when clicking accounts in the AccountBar with a very long room list

View File

@ -752,7 +752,7 @@ class MatrixClient(nio.AsyncClient):
if not room.members_synced: if not room.members_synced:
await super().joined_members(room_id) await super().joined_members(room_id)
await self.register_nio_room(self.all_rooms[room_id]) await self.register_nio_room(room, force_register_members=True)
async def load_past_events(self, room_id: str) -> bool: async def load_past_events(self, room_id: str) -> bool:
@ -1188,7 +1188,10 @@ class MatrixClient(nio.AsyncClient):
async def register_nio_room( async def register_nio_room(
self, room: nio.MatrixRoom, left: bool = False, self,
room: nio.MatrixRoom,
left: bool = False,
force_register_members: bool = False,
) -> None: ) -> None:
"""Register a `nio.MatrixRoom` as a `Room` object in our model.""" """Register a `nio.MatrixRoom` as a `Room` object in our model."""
@ -1200,15 +1203,17 @@ class MatrixClient(nio.AsyncClient):
try: try:
registered = self.models[self.user_id, "rooms"][room.room_id] registered = self.models[self.user_id, "rooms"][room.room_id]
last_event_date = registered.last_event_date
typing_members = registered.typing_members
mentions = registered.mentions
unreads = registered.unreads
except KeyError: except KeyError:
registered = None
last_event_date = datetime.fromtimestamp(0) last_event_date = datetime.fromtimestamp(0)
typing_members = [] typing_members = []
mentions = 0 mentions = 0
unreads = 0 unreads = 0
else:
last_event_date = registered.last_event_date
typing_members = registered.typing_members
mentions = registered.mentions
unreads = registered.unreads
room_item = Room( room_item = Room(
id = room.room_id, id = room.room_id,
@ -1250,20 +1255,15 @@ class MatrixClient(nio.AsyncClient):
self.models[self.user_id, "rooms"][room.room_id] = room_item self.models[self.user_id, "rooms"][room.room_id] = room_item
# List members that left the room, then remove them from our model if not registered or force_register_members:
left_the_room = [ for user_id in room.users:
user_id await self.add_member(room, user_id)
for user_id in self.models[self.user_id, room.room_id, "members"]
if user_id not in room.users
]
for user_id in left_the_room:
del self.models[self.user_id, room.room_id, "members"][user_id]
HTML.rooms_user_id_names[room.room_id].pop(user_id, None)
# Add the room members to the added room async def add_member(self, room: nio.MatrixRoom, user_id: str) -> None:
new_dict = { member = room.users[user_id]
user_id: Member(
self.models[self.user_id, room.room_id, "members"][user_id] = Member(
id = user_id, id = user_id,
display_name = room.user_name(user_id) # disambiguated display_name = room.user_name(user_id) # disambiguated
if member.display_name else "", if member.display_name else "",
@ -1271,16 +1271,18 @@ class MatrixClient(nio.AsyncClient):
typing = user_id in room.typing_users, typing = user_id in room.typing_users,
power_level = member.power_level, power_level = member.power_level,
invited = member.invited, invited = member.invited,
) for user_id, member in room.users.items() )
}
self.models[self.user_id, room.room_id, "members"].update(new_dict)
for user_id, member in room.users.items():
if member.display_name: if member.display_name:
HTML.rooms_user_id_names[room.room_id][user_id] = \ HTML.rooms_user_id_names[room.room_id][user_id] = \
member.display_name member.display_name
async def remove_member(self, room: nio.MatrixRoom, user_id: str) -> None:
self.models[self.user_id, room.room_id, "members"].pop(user_id, None)
HTML.rooms_user_id_names[room.room_id].pop(user_id, None)
async def get_member_name_avatar( async def get_member_name_avatar(
self, room_id: str, user_id: str, self, room_id: str, user_id: str,
) -> Tuple[str, str]: ) -> Tuple[str, str]:

View File

@ -353,6 +353,11 @@ class NioCallbacks:
async def onRoomMemberEvent(self, room, ev) -> None: async def onRoomMemberEvent(self, room, ev) -> None:
if ev.membership in ("invite", "join") and ev.state_key in room.users:
await self.client.add_member(room, user_id=ev.state_key)
elif ev.membership in ("left", "ban"):
await self.client.remove_member(room, user_id=ev.state_key)
type_and_content = await self.process_room_member_event(room, ev) type_and_content = await self.process_room_member_event(room, ev)
if type_and_content is not None: if type_and_content is not None: