events: Add RoomPowerLevels::{for_user, max}
//!
//! [`m.room.power_levels`]: https://spec.matrix.org/v1.2/client-server-api/#mroompower_levels
-use std::collections::BTreeMap;
+use std::{cmp::max, collections::BTreeMap};
use js_int::{int, Int};
use ruma_macros::EventContent;
@@ -238,6 +238,18 @@ pub struct RoomPowerLevels {
pub notifications: NotificationPowerLevels,
}
+impl RoomPowerLevels {
+ /// Get the power level of a specific user.
+ pub fn for_user(&self, user_id: &UserId) -> Int {
+ self.users.get(user_id).map_or(self.users_default, |pl| *pl)
+ }
+
+ /// Get the maximum power level of any user.
+ pub fn max(&self) -> Int {
+ self.users.values().fold(self.users_default, |max_pl, user_pl| max(max_pl, *user_pl))
+ }
+}
+
impl From<RoomPowerLevelsEventContent> for RoomPowerLevels {
fn from(c: RoomPowerLevelsEventContent) -> Self {
Self {
