events: Add RoomPowerLevels::{for_user, max}

This commit is contained in:
Jonas Platte 2022-04-13 15:40:57 +02:00
parent 2f32e9863e
commit d0faeb52f3
No known key found for this signature in database
GPG Key ID: BBA95679259D342F

View File

@ -2,7 +2,7 @@
//! //!
//! [`m.room.power_levels`]: https://spec.matrix.org/v1.2/client-server-api/#mroompower_levels //! [`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 js_int::{int, Int};
use ruma_macros::EventContent; use ruma_macros::EventContent;
@ -238,6 +238,18 @@ pub struct RoomPowerLevels {
pub notifications: NotificationPowerLevels, 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 { impl From<RoomPowerLevelsEventContent> for RoomPowerLevels {
fn from(c: RoomPowerLevelsEventContent) -> Self { fn from(c: RoomPowerLevelsEventContent) -> Self {
Self { Self {