Merge branch 'room-fix-power-levels' into 'master'

[Room] Fix power level setters and getters

See merge request famedly/famedlysdk!133
This commit is contained in:
Christian Pauly 2019-12-05 09:10:02 +00:00
commit b9e0b68072

View file

@ -468,13 +468,15 @@ 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].
Future<dynamic> setPower(String userID, int power) async { Future<dynamic> setPower(String userID, int power) async {
if (states["m.room.power_levels"] == null) return null; if (states["m.room.power_levels"] == null) return null;
Map powerMap = states["m.room.power_levels"].content["users"]; Map<String, dynamic> powerMap = {}
powerMap[userID] = power; ..addAll(states["m.room.power_levels"].content);
if (powerMap["users"] == null) powerMap["users"] = {};
powerMap["users"][userID] = power;
dynamic res = await client.connection.jsonRequest( dynamic res = await client.connection.jsonRequest(
type: HTTPType.PUT, type: HTTPType.PUT,
action: "/client/r0/rooms/$id/state/m.room.power_levels", action: "/client/r0/rooms/$id/state/m.room.power_levels",
data: {"users": powerMap}); data: powerMap);
if (res is ErrorResponse) client.connection.onError.add(res); if (res is ErrorResponse) client.connection.onError.add(res);
return res; return res;
} }
@ -843,9 +845,9 @@ class Room {
bool get canChangePowerLevel => canSendEvent("m.room.power_levels"); bool get canChangePowerLevel => canSendEvent("m.room.power_levels");
bool canSendEvent(String eventType) { bool canSendEvent(String eventType) {
if (getState("m.room.power_levels") == null || if (getState("m.room.power_levels") == null) return false;
getState("m.room.power_levels").content["events"] == null) return false; if (getState("m.room.power_levels").content["events"] == null ||
if (getState("m.room.power_levels").content["events"][eventType] == null) getState("m.room.power_levels").content["events"][eventType] == null)
return eventType == "m.room.message" return eventType == "m.room.message"
? canSendDefaultMessages ? canSendDefaultMessages
: canSendDefaultStates; : canSendDefaultStates;