[Room] Add powerlevels getter

This commit is contained in:
Christian Pauly 2019-11-26 06:38:44 +00:00
parent 3407c2f605
commit 8051b360ac
4 changed files with 95 additions and 1 deletions

View file

@ -138,4 +138,7 @@ class Event extends RoomState {
final String eventID = await room.sendTextEvent(text, txid: txid);
return eventID;
}
/// Whether the client is allowed to redact this event.
bool get canRedact => senderId == room.client.userID || room.canRedact;
}

View file

@ -465,7 +465,7 @@ class Room {
/// Set the power level of the user with the [userID] to the value [power].
Future<dynamic> setPower(String userID, int power) async {
if (states["m.room.power_levels"] == null) return null;
Map<String, int> powerMap = states["m.room.power_levels"].content["users"];
Map powerMap = states["m.room.power_levels"].content["users"];
powerMap[userID] = power;
dynamic res = await client.connection.jsonRequest(
@ -819,4 +819,41 @@ class Room {
if (setAvatarResp is ErrorResponse) return setAvatarResp;
return setAvatarResp["event_id"];
}
bool _hasPermissionFor(String action) {
if (getState("m.room.power_levels") == null ||
getState("m.room.power_levels").content[action] == null) return false;
return ownPowerLevel >= getState("m.room.power_levels").content[action];
}
/// The level required to ban a user.
bool get canBan => _hasPermissionFor("ban");
/// The default level required to send message events. Can be overridden by the events key.
bool get canSendDefaultMessages => _hasPermissionFor("events_default");
/// The level required to invite a user.
bool get canInvite => _hasPermissionFor("invite");
/// The level required to kick a user.
bool get canKick => _hasPermissionFor("kick");
/// The level required to redact an event.
bool get canRedact => _hasPermissionFor("redact");
/// The default level required to send state events. Can be overridden by the events key.
bool get canSendDefaultStates => _hasPermissionFor("state_default");
bool get canChangePowerLevel => canSendEvent("m.room.power_levels");
bool canSendEvent(String eventType) {
if (getState("m.room.power_levels") == null ||
getState("m.room.power_levels").content["events"] == null) return false;
if (getState("m.room.power_levels").content["events"][eventType] == null)
return eventType == "m.room.message"
? canSendDefaultMessages
: canSendDefaultStates;
return ownPowerLevel >=
getState("m.room.power_levels").content["events"][eventType];
}
}

View file

@ -165,4 +165,15 @@ class User extends RoomState {
return newRoomID;
}
/// Whether the client is allowed to ban/unban this user.
bool get canBan => room.canBan && powerLevel < room.ownPowerLevel;
/// Whether the client is allowed to kick this user.
bool get canKick => room.canKick && powerLevel < room.ownPowerLevel;
/// Whether the client is allowed to change the power level of this user.
/// Please be aware that you can only set the power level to at least your own!
bool get canChangePowerLevel =>
room.canChangePowerLevel && powerLevel < room.ownPowerLevel;
}

View file

@ -214,8 +214,51 @@ void main() {
expect(room.ownPowerLevel, 100);
expect(room.getPowerLevelByUserId(matrix.userID), room.ownPowerLevel);
expect(room.getPowerLevelByUserId("@nouser:example.com"), 10);
expect(room.ownPowerLevel, 100);
expect(room.canBan, true);
expect(room.canInvite, true);
expect(room.canKick, true);
expect(room.canRedact, true);
expect(room.canSendDefaultMessages, true);
expect(room.canSendDefaultStates, true);
expect(room.canChangePowerLevel, true);
expect(room.canSendEvent("m.room.name"), true);
expect(room.canSendEvent("m.room.power_levels"), true);
expect(room.canSendEvent("m.room.member"), true);
expect(room.powerLevels,
room.states["m.room.power_levels"].content["users"]);
room.states["m.room.power_levels"] = RoomState(
senderId: "@test:example.com",
typeKey: "m.room.power_levels",
roomId: room.id,
room: room,
eventId: "123abc",
content: {
"ban": 50,
"events": {"m.room.name": 0, "m.room.power_levels": 100},
"events_default": 0,
"invite": 50,
"kick": 50,
"notifications": {"room": 20},
"redact": 50,
"state_default": 50,
"users": {},
"users_default": 0
},
stateKey: "");
expect(room.ownPowerLevel, 0);
expect(room.canBan, false);
expect(room.canInvite, false);
expect(room.canKick, false);
expect(room.canRedact, false);
expect(room.canSendDefaultMessages, true);
expect(room.canSendDefaultStates, false);
expect(room.canChangePowerLevel, false);
expect(room.canSendEvent("m.room.name"), true);
expect(room.canSendEvent("m.room.power_levels"), false);
expect(room.canSendEvent("m.room.member"), false);
expect(room.canSendEvent("m.room.message"), true);
final dynamic resp =
await room.setPower("@test:fakeServer.notExisting", 90);
expect(resp["event_id"], "42");