diff --git a/lib/src/Room.dart b/lib/src/Room.dart index a097390..51ca60f 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -232,6 +232,20 @@ class Room { return res; } + /// Call the Matrix API to unban a banned user from this room. + Future setPower(String userID, int power) async { + Map 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 invite(String userID) async { dynamic res = await client.connection.jsonRequest( diff --git a/lib/src/Store.dart b/lib/src/Store.dart index 9015311..561939c 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -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 getPowerLevel(String roomID) async { + List> 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> getPowerLevels(String roomID) async { + List> res = await db.rawQuery( + "SELECT matrix_id, power_level FROM Users WHERE chat_id=?", + [roomID, client.userID]); + Map 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, ' + diff --git a/lib/src/User.dart b/lib/src/User.dart index 3c5fe5b..ebcca2c 100644 --- a/lib/src/User.dart +++ b/lib/src/User.dart @@ -87,6 +87,18 @@ class User { room: room); } + /// Checks if the client's user has the permission to kick this user. + Future 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 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 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 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 startDirectChat() async { // Try to find an existing direct chat