diff --git a/crates/matrix-sdk/src/account.rs b/crates/matrix-sdk/src/account.rs index 50155f86f..0f98067f6 100644 --- a/crates/matrix-sdk/src/account.rs +++ b/crates/matrix-sdk/src/account.rs @@ -35,14 +35,14 @@ use ruma::{ }, assign, events::{ - room::MediaSource, AnyGlobalAccountDataEvent, AnyGlobalAccountDataEventContent, - GlobalAccountDataEvent, GlobalAccountDataEventContent, GlobalAccountDataEventType, - StaticEventContent, + room::MediaSource, AnyGlobalAccountDataEventContent, GlobalAccountDataEventContent, + GlobalAccountDataEventType, StaticEventContent, }, serde::Raw, thirdparty::Medium, ClientSecret, MxcUri, OwnedMxcUri, SessionId, UInt, }; +use serde::Deserialize; use crate::{config::RequestConfig, Client, Error, HttpError, Result}; @@ -633,7 +633,7 @@ impl Account { Ok(self.client.send(request, None).await?) } - /// Get an account data event of statically-known type. + /// Get the content of an account data event of statically-known type. /// /// # Example /// @@ -644,29 +644,29 @@ impl Account { /// # let account = client.account(); /// use matrix_sdk::ruma::events::ignored_user_list::IgnoredUserListEventContent; /// - /// let maybe_evt = account.account_data::().await?; - /// if let Some(raw_evt) = maybe_evt { - /// let evt = raw_evt.deserialize()?; + /// let maybe_content = account.account_data::().await?; + /// if let Some(raw_content) = maybe_content { + /// let content = raw_content.deserialize()?; /// println!("Ignored users:"); - /// for user_id in evt.content.ignored_users { + /// for user_id in content.ignored_users { /// println!("- {user_id}"); /// } /// } /// # anyhow::Ok(()) }; /// ``` - pub async fn account_data(&self) -> Result>>> + pub async fn account_data(&self) -> Result>> where C: GlobalAccountDataEventContent + StaticEventContent, { - Ok(self.client.store().get_account_data_event_static().await?) + get_raw_content(self.client.store().get_account_data_event_static::().await?) } - /// Get the account data event of the given type. + /// Get the content of an account data event of a given type. pub async fn account_data_raw( &self, event_type: GlobalAccountDataEventType, - ) -> Result>> { - Ok(self.client.store().get_account_data_event(event_type).await?) + ) -> Result>> { + get_raw_content(self.client.store().get_account_data_event(event_type).await?) } /// Set the given account data event. @@ -721,3 +721,16 @@ impl Account { Ok(self.client.send(request, None).await?) } } + +fn get_raw_content(raw: Option>) -> Result>> { + #[derive(Deserialize)] + #[serde(bound = "C: Sized")] // Replace default Deserialize bound + struct GetRawContent { + content: Raw, + } + + Ok(raw + .map(|event| event.deserialize_as::>()) + .transpose()? + .map(|get_raw| get_raw.content)) +}