Fix old power events sent when setting user level

This commit is contained in:
miruka 2020-07-13 21:13:20 -04:00
parent 0a277f23b1
commit bd50cd46fa
3 changed files with 16 additions and 6 deletions

View File

@ -1,6 +1,5 @@
# TODO
- don't trust PowerLevelsEvent, may be old
- fix button layout
- improve power levels event text

View File

@ -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: <m.room.power_levels event content dict>}
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)

View File

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