base: Add the power level event to the room member.

master
Damir Jelić 2020-12-06 18:11:32 +01:00
parent 886a1c7a77
commit b36d907fac
3 changed files with 69 additions and 22 deletions

View File

@ -66,7 +66,7 @@ compile_error!("only one of 'native-tls' or 'rustls-tls' features can be enabled
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
#[cfg_attr(feature = "docs", doc(cfg(encryption)))] #[cfg_attr(feature = "docs", doc(cfg(encryption)))]
pub use matrix_sdk_base::crypto::LocalTrust; pub use matrix_sdk_base::crypto::LocalTrust;
pub use matrix_sdk_base::{Error as BaseError, Room, Session}; pub use matrix_sdk_base::{Error as BaseError, Room, RoomMember, Session};
pub use matrix_sdk_common::*; pub use matrix_sdk_common::*;
pub use reqwest; pub use reqwest;

View File

@ -48,7 +48,7 @@ pub mod responses;
mod session; mod session;
mod store; mod store;
pub use store::{Room, Store}; pub use store::{Room, RoomMember, Store};
pub use client::{BaseClient, BaseClientConfig, RoomState, RoomStateType}; pub use client::{BaseClient, BaseClientConfig, RoomState, RoomStateType};

View File

@ -13,8 +13,11 @@ use matrix_sdk_common::{
api::r0::sync::sync_events::RoomSummary as RumaSummary, api::r0::sync::sync_events::RoomSummary as RumaSummary,
events::{ events::{
presence::PresenceEvent, presence::PresenceEvent,
room::{encryption::EncryptionEventContent, member::MemberEventContent}, room::{
AnySyncStateEvent, EventContent, SyncStateEvent, encryption::EncryptionEventContent, member::MemberEventContent,
power_levels::PowerLevelsEventContent,
},
AnySyncStateEvent, EventContent, EventType, SyncStateEvent,
}, },
identifiers::{RoomAliasId, RoomId, UserId}, identifiers::{RoomAliasId, RoomId, UserId},
Raw, Raw,
@ -174,13 +177,27 @@ impl Room {
let x = move |u| async move { let x = move |u| async move {
let presence = self.store.get_presence_event(&u).await; let presence = self.store.get_presence_event(&u).await;
let power = self
.store
.get_state_event(self.room_id(), EventType::RoomPowerLevels, "")
.await
.map(|e| {
if let AnySyncStateEvent::RoomPowerLevels(e) = e {
Some(e)
} else {
None
}
})
.flatten();
self.store self.store
.get_member_event(self.room_id(), &u) .get_member_event(self.room_id(), &u)
.await .await
.map(|m| RoomMember { .map(|m| RoomMember {
user_id: UserId::try_from(m.state_key.as_str()).unwrap().into(),
event: m.into(), event: m.into(),
presence: presence.into(), presence: presence.into(),
power_levles: power.into(),
}) })
}; };
@ -221,7 +238,7 @@ impl Room {
// //
let own_user_id = self.own_user_id.clone(); let own_user_id = self.own_user_id.clone();
let is_own_member = |m: &RoomMember| &m.user_id() == &*own_user_id; let is_own_member = |m: &RoomMember| m.user_id() == &*own_user_id;
if !inner.summary.heroes.is_empty() { if !inner.summary.heroes.is_empty() {
let mut names = stream::iter(inner.summary.heroes.iter()) let mut names = stream::iter(inner.summary.heroes.iter())
@ -232,7 +249,7 @@ impl Room {
}) })
.map(|mem| { .map(|mem| {
mem.display_name() mem.display_name()
.clone() .map(|d| d.to_string())
.unwrap_or_else(|| mem.user_id().localpart().to_string()) .unwrap_or_else(|| mem.user_id().localpart().to_string())
}) })
.collect::<Vec<String>>() .collect::<Vec<String>>()
@ -245,7 +262,7 @@ impl Room {
.take(3) .take(3)
.map(|mem| { .map(|mem| {
mem.display_name() mem.display_name()
.clone() .map(|d| d.to_string())
.unwrap_or_else(|| mem.user_id().localpart().to_string()) .unwrap_or_else(|| mem.user_id().localpart().to_string())
}) })
.collect::<Vec<String>>() .collect::<Vec<String>>()
@ -259,7 +276,7 @@ impl Room {
.take(3) .take(3)
.map(|mem| { .map(|mem| {
mem.display_name() mem.display_name()
.clone() .map(|d| d.to_string())
.unwrap_or_else(|| mem.user_id().localpart().to_string()) .unwrap_or_else(|| mem.user_id().localpart().to_string())
}) })
.collect::<Vec<String>>() .collect::<Vec<String>>()
@ -298,13 +315,27 @@ impl Room {
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 power = self
.store
.get_state_event(self.room_id(), EventType::RoomPowerLevels, "")
.await
.map(|e| {
if let AnySyncStateEvent::RoomPowerLevels(e) = e {
Some(e)
} else {
None
}
})
.flatten();
self.store self.store
.get_member_event(&self.room_id, user_id) .get_member_event(&self.room_id, user_id)
.await .await
.map(|e| RoomMember { .map(|e| RoomMember {
user_id: UserId::try_from(e.state_key.as_str()).unwrap().into(),
event: e.into(), event: e.into(),
presence: presence.into(), presence: presence.into(),
power_levles: power.into(),
}) })
} }
@ -323,29 +354,33 @@ impl Room {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct RoomMember { pub struct RoomMember {
user_id: Arc<UserId>,
event: Arc<SyncStateEvent<MemberEventContent>>, event: Arc<SyncStateEvent<MemberEventContent>>,
presence: Arc<Option<PresenceEvent>>, presence: Arc<Option<PresenceEvent>>,
power_levles: Arc<Option<SyncStateEvent<PowerLevelsEventContent>>>,
} }
impl RoomMember { impl RoomMember {
pub fn user_id(&self) -> UserId { pub fn user_id(&self) -> &UserId {
UserId::try_from(self.event.state_key.clone()).unwrap() &self.user_id
} }
pub fn display_name(&self) -> &Option<String> { pub fn display_name(&self) -> Option<&str> {
&self.event.content.displayname self.event.content.displayname.as_deref()
} }
pub fn disambiguated_name(&self) -> String { pub fn power_level(&self) -> i64 {
self.event.state_key.clone() self.power_levles
} .as_ref()
.as_ref()
pub fn name(&self) -> String { .map(|e| {
self.event.state_key.clone() e.content
} .users
.get(&self.user_id())
pub fn unique_name(&self) -> String { .map(|p| (*p).into())
self.event.state_key.clone() .unwrap_or(e.content.users_default.into())
})
.unwrap_or(0)
} }
} }
@ -593,6 +628,18 @@ impl Store {
.map(|e| serde_json::from_slice(&e).unwrap()) .map(|e| serde_json::from_slice(&e).unwrap())
} }
pub async fn get_state_event(
&self,
room_id: &RoomId,
event_type: EventType,
state_key: &str,
) -> Option<AnySyncStateEvent> {
self.room_state
.get(format!("{}{}{}", room_id.as_str(), event_type, state_key).as_bytes())
.unwrap()
.map(|e| serde_json::from_slice(&e).unwrap())
}
pub async fn get_member_event( pub async fn get_member_event(
&self, &self,
room_id: &RoomId, room_id: &RoomId,