base: Add the normalized_power_level method back to the member.

master
Damir Jelić 2020-12-24 17:14:46 +01:00
parent a29d2e39c4
commit 2384069641
2 changed files with 29 additions and 10 deletions

View File

@ -31,6 +31,7 @@ pub struct RoomMember {
pub(crate) profile: Arc<Option<MemberEventContent>>, pub(crate) profile: Arc<Option<MemberEventContent>>,
pub(crate) presence: Arc<Option<PresenceEvent>>, pub(crate) presence: Arc<Option<PresenceEvent>>,
pub(crate) power_levles: Arc<Option<SyncStateEvent<PowerLevelsEventContent>>>, pub(crate) power_levles: Arc<Option<SyncStateEvent<PowerLevelsEventContent>>>,
pub(crate) max_power_level: i64,
} }
impl RoomMember { impl RoomMember {
@ -46,6 +47,14 @@ impl RoomMember {
} }
} }
pub fn normalized_power_level(&self) -> i64 {
if self.max_power_level > 0 {
(self.power_level() * 100) / self.max_power_level
} else {
self.power_level()
}
}
pub fn power_level(&self) -> i64 { pub fn power_level(&self) -> i64 {
self.power_levles self.power_levles
.as_ref() .as_ref()

View File

@ -14,7 +14,7 @@
use std::{ use std::{
convert::TryFrom, convert::TryFrom,
sync::{Arc, Mutex as SyncMutex}, sync::{Arc, RwLock as SyncRwLock},
}; };
use futures::{ use futures::{
@ -37,7 +37,7 @@ use super::{BaseRoomInfo, RoomMember};
pub struct Room { pub struct Room {
room_id: Arc<RoomId>, room_id: Arc<RoomId>,
own_user_id: Arc<UserId>, own_user_id: Arc<UserId>,
inner: Arc<SyncMutex<RoomInfo>>, inner: Arc<SyncRwLock<RoomInfo>>,
store: SledStore, store: SledStore,
} }
@ -72,7 +72,7 @@ impl Room {
own_user_id: Arc::new(own_user_id.clone()), own_user_id: Arc::new(own_user_id.clone()),
room_id: room_id.clone(), room_id: room_id.clone(),
store, store,
inner: Arc::new(SyncMutex::new(RoomInfo { inner: Arc::new(SyncRwLock::new(RoomInfo {
room_id, room_id,
room_type, room_type,
notification_counts: Default::default(), notification_counts: Default::default(),
@ -85,17 +85,23 @@ impl Room {
} }
pub fn are_members_synced(&self) -> bool { pub fn are_members_synced(&self) -> bool {
self.inner.lock().unwrap().members_synced self.inner.read().unwrap().members_synced
} }
pub fn room_type(&self) -> RoomType { pub fn room_type(&self) -> RoomType {
self.inner.lock().unwrap().room_type self.inner.read().unwrap().room_type
}
fn max_power_level(&self) -> i64 {
self.inner.read().unwrap().base_info.max_power_level
} }
pub async fn get_active_members(&self) -> impl Stream<Item = RoomMember> + '_ { pub async fn get_active_members(&self) -> impl Stream<Item = RoomMember> + '_ {
let joined = self.store.get_joined_user_ids(self.room_id()).await; let joined = self.store.get_joined_user_ids(self.room_id()).await;
let invited = self.store.get_invited_user_ids(self.room_id()).await; let invited = self.store.get_invited_user_ids(self.room_id()).await;
let max_power_level = self.max_power_level();
let into_member = move |u| async move { let into_member = move |u| async move {
let presence = self.store.get_presence_event(&u).await; let presence = self.store.get_presence_event(&u).await;
let profile = self.store.get_profile(self.room_id(), &u).await; let profile = self.store.get_profile(self.room_id(), &u).await;
@ -120,6 +126,7 @@ impl Room {
profile: profile.into(), profile: profile.into(),
presence: presence.into(), presence: presence.into(),
power_levles: power.into(), power_levles: power.into(),
max_power_level,
}) })
}; };
@ -134,7 +141,7 @@ impl Room {
/// [spec]: /// [spec]:
/// <https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room> /// <https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room>
pub async fn calculate_name(&self) -> String { pub async fn calculate_name(&self) -> String {
let inner = self.inner.lock().unwrap(); let inner = self.inner.read().unwrap();
if let Some(name) = &inner.base_info.name { if let Some(name) = &inner.base_info.name {
let name = name.trim(); let name = name.trim();
@ -218,7 +225,7 @@ impl Room {
} }
pub(crate) fn clone_info(&self) -> RoomInfo { pub(crate) fn clone_info(&self) -> RoomInfo {
(*self.inner.lock().unwrap()).clone() (*self.inner.read().unwrap()).clone()
} }
pub async fn joined_user_ids(&self) -> impl Stream<Item = UserId> { pub async fn joined_user_ids(&self) -> impl Stream<Item = UserId> {
@ -226,17 +233,19 @@ impl Room {
} }
pub fn is_encrypted(&self) -> bool { pub fn is_encrypted(&self) -> bool {
self.inner.lock().unwrap().is_encrypted() self.inner.read().unwrap().is_encrypted()
} }
pub fn update_summary(&self, summary: RoomInfo) { pub fn update_summary(&self, summary: RoomInfo) {
let mut inner = self.inner.lock().unwrap(); let mut inner = self.inner.write().unwrap();
*inner = summary; *inner = summary;
} }
pub async fn get_member(&self, user_id: &UserId) -> Option<RoomMember> { pub async fn get_member(&self, user_id: &UserId) -> Option<RoomMember> {
let presence = self.store.get_presence_event(user_id).await; let presence = self.store.get_presence_event(user_id).await;
let profile = self.store.get_profile(self.room_id(), user_id).await; let profile = self.store.get_profile(self.room_id(), user_id).await;
let max_power_level = self.max_power_level();
let power = self let power = self
.store .store
.get_state_event(self.room_id(), EventType::RoomPowerLevels, "") .get_state_event(self.room_id(), EventType::RoomPowerLevels, "")
@ -258,6 +267,7 @@ impl Room {
profile: profile.into(), profile: profile.into(),
presence: presence.into(), presence: presence.into(),
power_levles: power.into(), power_levles: power.into(),
max_power_level,
}) })
} }
@ -266,7 +276,7 @@ impl Room {
} }
pub fn last_prev_batch(&self) -> Option<String> { pub fn last_prev_batch(&self) -> Option<String> {
self.inner.lock().unwrap().last_prev_batch.clone() self.inner.read().unwrap().last_prev_batch.clone()
} }
pub async fn display_name(&self) -> String { pub async fn display_name(&self) -> String {