Merge pull request #1929 from famedly/krille/fix-set-power-level
fix: Change power level without changing memory
This commit is contained in:
commit
625b032e96
|
|
@ -1189,22 +1189,31 @@ class Room {
|
||||||
/// Set the power level of the user with the [userID] to the value [power].
|
/// 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
|
/// 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.
|
/// power level event, there might something broken and this returns null.
|
||||||
Future<String> setPower(String userID, int power) async {
|
/// Please note, that you need to await the power level state from sync before
|
||||||
final powerMap = Map<String, Object?>.from(
|
/// the changes are actually applied. Especially if you want to set multiple
|
||||||
getState(EventTypes.RoomPowerLevels)?.content ?? {},
|
/// 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?>
|
var users = powerLevelMapCopy['users'];
|
||||||
? powerMap['users'] as Map<String, Object?>
|
|
||||||
: (powerMap['users'] = <String, Object?>{});
|
|
||||||
|
|
||||||
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(
|
return await client.setRoomStateWithKey(
|
||||||
id,
|
id,
|
||||||
EventTypes.RoomPowerLevels,
|
EventTypes.RoomPowerLevels,
|
||||||
'',
|
'',
|
||||||
powerMap,
|
powerLevelMapCopy,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -787,6 +787,20 @@ void main() {
|
||||||
await room.invite('Testname');
|
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 {
|
test('getParticipants', () async {
|
||||||
var userList = room.getParticipants();
|
var userList = room.getParticipants();
|
||||||
expect(userList.length, 4);
|
expect(userList.length, 4);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue