diff --git a/lib/src/room.dart b/lib/src/room.dart index 9e5ee931..63792d4b 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1187,22 +1187,18 @@ class Room { /// Set the power level of the user with the [userID] to the value [power]. /// 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 { - final powerMap = Map.from( - getState(EventTypes.RoomPowerLevels)?.content ?? {}, - ); - - final usersPowerMap = powerMap['users'] is Map - ? powerMap['users'] as Map - : (powerMap['users'] = {}); + Future setPower(String userId, int power) async { + final powerLevelMapCopy = + getState(EventTypes.RoomPowerLevels)?.content.copy() ?? {}; - usersPowerMap[userID] = power; + powerLevelMapCopy['users'] ??= {}; + powerLevelMapCopy.tryGetMap('users')?[userId] = power; return await client.setRoomStateWithKey( id, EventTypes.RoomPowerLevels, '', - powerMap, + powerLevelMapCopy, ); } diff --git a/test/room_test.dart b/test/room_test.dart index 3e08e2cc..2826f965 100644 --- a/test/room_test.dart +++ b/test/room_test.dart @@ -786,6 +786,20 @@ void main() { await room.invite('Testname'); }); + test('setPower', () async { + final powerLevelMap = + room.getState(EventTypes.RoomPowerLevels, '')!.content.copy(); + + // Request to fake api does not update anything: + await room.setPower('@bob:fakeServer.notExisting', 100); + + // Original power level map has not changed: + expect( + powerLevelMap, + room.getState(EventTypes.RoomPowerLevels, '')!.content.copy(), + ); + }); + test('getParticipants', () async { var userList = room.getParticipants(); expect(userList.length, 4);