Add power level management

This commit is contained in:
Christian Pauly 2019-06-11 13:32:14 +02:00
parent 037953be34
commit 8be14fa58f
3 changed files with 53 additions and 0 deletions

View File

@ -232,6 +232,20 @@ class Room {
return res;
}
/// Call the Matrix API to unban a banned user from this room.
Future<dynamic> setPower(String userID, int power) async {
Map<String,int> powerMap = await client.store.getPowerLevels(id);
powerMap[userID] = power;
dynamic res = await client.connection.jsonRequest(
type: "PUT",
action:
"/client/r0/rooms/$id/state/m.room.power_levels/",
data: {"users": powerMap});
if (res is ErrorResponse) client.connection.onError.add(res);
return res;
}
/// Call the Matrix API to invite a user to this room.
Future<dynamic> invite(String userID) async {
dynamic res = await client.connection.jsonRequest(

View File

@ -531,6 +531,27 @@ class Store {
return res[0]["id"];
}
/// Returns the power level of the user for the given [roomID]. Returns null if
/// the room or the own user wasn't found.
Future<int> getPowerLevel(String roomID) async {
List<Map<String, dynamic>> res = await db.rawQuery(
"SELECT power_level FROM Users WHERE matrix_id=? AND chat_id=?",
[roomID, client.userID]);
if (res.length != 1) return null;
return res[0]["power_level"];
}
/// Returns the power levels from all users for the given [roomID].
Future<Map<String, int>> getPowerLevels(String roomID) async {
List<Map<String, dynamic>> res = await db.rawQuery(
"SELECT matrix_id, power_level FROM Users WHERE chat_id=?",
[roomID, client.userID]);
Map<String, int> powerMap = {};
for (int i = 0; i < res.length; i++)
powerMap[res[i]["matrix_id"]] = res[i]["power_level"];
return powerMap;
}
/// The database sheme for the Client class.
static final String ClientsScheme = 'CREATE TABLE IF NOT EXISTS Clients(' +
'client TEXT PRIMARY KEY, ' +

View File

@ -87,6 +87,18 @@ class User {
room: room);
}
/// Checks if the client's user has the permission to kick this user.
Future<bool> get canKick async {
final int ownPowerLevel = await room.client.store.getPowerLevel(room.id);
return ownPowerLevel > powerLevel && ownPowerLevel >= room.powerLevels["power_kick"];
}
/// Checks if the client's user has the permission to ban or unban this user.
Future<bool> get canBan async {
final int ownPowerLevel = await room.client.store.getPowerLevel(room.id);
return ownPowerLevel > powerLevel && ownPowerLevel >= room.powerLevels["power_ban"];
}
/// Call the Matrix API to kick this user from this room.
Future<dynamic> kick() async {
dynamic res = await room.kick(id);
@ -105,6 +117,12 @@ class User {
return res;
}
/// Call the Matrix API to change the power level of this user.
Future<dynamic> setPower(int power) async {
dynamic res = await room.setPower(id, power);
return res;
}
/// Returns an existing direct chat with this user or creates a new one.
Future<String> startDirectChat() async {
// Try to find an existing direct chat