From bd50cd46fadac113ad1fafcabd40532e43b5758b Mon Sep 17 00:00:00 2001 From: miruka Date: Mon, 13 Jul 2020 21:13:20 -0400 Subject: [PATCH] Fix old power events sent when setting user level --- TODO.md | 1 - src/backend/matrix_client.py | 8 ++++---- src/backend/nio_callbacks.py | 13 ++++++++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/TODO.md b/TODO.md index 039c9522..45327db1 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,5 @@ # TODO -- don't trust PowerLevelsEvent, may be old - fix button layout - improve power levels event text diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 93d25a99..a4d7644e 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -191,8 +191,8 @@ class MatrixClient(nio.AsyncClient): self.loaded_once_rooms: Set[str] = set() # {room_id} self.cleared_events_rooms: Set[str] = set() # {room_id} - # {room_id: } - self.power_levels_content: Dict[str, Dict[str, Any]] = {} + # {room_id: event} + self.power_level_events: Dict[str, nio.PowerLevelsEvent] = {} self.nio_callbacks = NioCallbacks(self) @@ -1128,10 +1128,10 @@ class MatrixClient(nio.AsyncClient): ) -> None: """Set a room member's power level.""" - while room_id not in self.power_levels_content: + while room_id not in self.power_level_events: await asyncio.sleep(0.2) - content = deepcopy(self.power_levels_content[room_id]) + content = deepcopy(self.power_level_events[room_id].source["content"]) content.setdefault("users", {})[user_id] = level await self.room_put_state(room_id, "m.room.power_levels", content) diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index cd5c4662..92fd3e0b 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -83,6 +83,14 @@ class NioCallbacks: if room_id not in self.client.past_tokens: self.client.past_tokens[room_id] = info.timeline.prev_batch + for ev in info.state: + if isinstance(ev, nio.PowerLevelsEvent): + stored = self.client.power_level_events.get(room_id) + time = ev.server_timestamp + + if not stored or time > stored.server_timestamp: + self.client.power_level_events[room_id] = ev + # TODO: way of knowing if a nio.MatrixRoom is left for room_id, info in resp.rooms.leave.items(): # TODO: handle in nio, these are rooms that were left before @@ -313,7 +321,10 @@ class NioCallbacks: async def onPowerLevelsEvent( self, room: nio.MatrixRoom, ev: nio.PowerLevelsEvent, ) -> None: - self.client.power_levels_content[room.room_id] = ev.source["content"] + stored = self.client.power_level_events.get(room.room_id) + + if not stored or ev.server_timestamp > stored.server_timestamp: + self.client.power_level_events[room.room_id] = ev try: previous_levels = ev.source["unsigned"]["prev_content"]["users"]