From 4732580c3dfb0aa7f9d2d80264ae6b42e9af0a4c Mon Sep 17 00:00:00 2001 From: krille-chan Date: Wed, 17 Apr 2024 09:46:27 +0200 Subject: [PATCH] fix: Make room.setPower more type safe and avoid change powerlevel in RAM before sending request to server This fixes the bug that the actual dart Map in the state has been manipulated because we have not worked with a copy of the map. Also this crashes if the powerlevelmap would had a wrong type in users. --- lib/src/room.dart | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/src/room.dart b/lib/src/room.dart index 82c45200..5a00401b 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1201,11 +1201,15 @@ class Room { /// Returns the event ID of the new state event. If there is no known /// power level event, there might something broken and this returns null. Future setPower(String userID, int power) async { - var powerMap = getState(EventTypes.RoomPowerLevels)?.content; - if (powerMap is! Map) { - powerMap = {}; - } - (powerMap['users'] ??= {})[userID] = power; + final powerMap = Map.from( + getState(EventTypes.RoomPowerLevels)?.content ?? {}, + ); + + final usersPowerMap = powerMap['users'] is Map + ? powerMap['users'] as Map + : (powerMap['users'] = {}); + + usersPowerMap[userID] = power; return await client.setRoomStateWithKey( id,