Merge pull request #1929 from famedly/krille/fix-set-power-level

fix: Change power level without changing memory
This commit is contained in:
Krille-chan 2024-10-08 15:13:33 +02:00 committed by GitHub
commit 625b032e96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 9 deletions

View File

@ -1189,22 +1189,31 @@ 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<String> setPower(String userID, int power) async {
final powerMap = Map<String, Object?>.from(
getState(EventTypes.RoomPowerLevels)?.content ?? {},
);
/// Please note, that you need to await the power level state from sync before
/// the changes are actually applied. Especially if you want to set multiple
/// power levels at once, you need to await each change in the sync, to not
/// override those.
Future<String> setPower(String userId, int power) async {
final powerLevelMapCopy =
getState(EventTypes.RoomPowerLevels)?.content.copy() ?? {};
final usersPowerMap = powerMap['users'] is Map<String, Object?>
? powerMap['users'] as Map<String, Object?>
: (powerMap['users'] = <String, Object?>{});
var users = powerLevelMapCopy['users'];
usersPowerMap[userID] = power;
if (users is! Map<String, Object?>) {
if (users != null) {
Logs().v(
'Repairing Power Level "users" has the wrong type "${powerLevelMapCopy['users'].runtimeType}"');
}
users = powerLevelMapCopy['users'] = <String, Object?>{};
}
users[userId] = power;
return await client.setRoomStateWithKey(
id,
EventTypes.RoomPowerLevels,
'',
powerMap,
powerLevelMapCopy,
);
}

View File

@ -787,6 +787,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);