Skip to content

Commit 6125580

Browse files
zecakehpoljar
authored andcommitted
feat: Add Account::fetch_account_data_static
It uses the statically-known type from the `StaticEventContent` implementation to call `fetch_account_data()`. This is the equivalent of `Account::account_data()` but for fetching from the server. It avoids the need to cast to the proper type after. Signed-off-by: Kévin Commaille <[email protected]>
1 parent 8b3e295 commit 6125580

File tree

6 files changed

+33
-43
lines changed

6 files changed

+33
-43
lines changed

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ use ruma::{
7575
},
7676
tag::TagEventContent,
7777
GlobalAccountDataEvent as RumaGlobalAccountDataEvent,
78-
GlobalAccountDataEventType as RumaGlobalAccountDataEventType,
7978
RoomAccountDataEvent as RumaRoomAccountDataEvent,
8079
},
8180
push::{HttpPusherData as RumaHttpPusherData, PushFormat as RumaPushFormat},
@@ -1253,13 +1252,10 @@ impl Client {
12531252
// Ignored users
12541253

12551254
pub async fn ignored_users(&self) -> Result<Vec<String>, ClientError> {
1256-
if let Some(raw_content) = self
1257-
.inner
1258-
.account()
1259-
.fetch_account_data(RumaGlobalAccountDataEventType::IgnoredUserList)
1260-
.await?
1255+
if let Some(raw_content) =
1256+
self.inner.account().fetch_account_data_static::<IgnoredUserListEventContent>().await?
12611257
{
1262-
let content = raw_content.deserialize_as::<IgnoredUserListEventContent>()?;
1258+
let content = raw_content.deserialize()?;
12631259
let user_ids: Vec<String> =
12641260
content.ignored_users.keys().map(|id| id.to_string()).collect();
12651261

crates/matrix-sdk/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.
88

99
### Features
1010

11+
- Add `Account::fetch_account_data_static` to fetch account data from the server
12+
with a statically-known type, with a signature similar to
13+
`Account::account_data`.
14+
([#5424](https://github.com/matrix-org/matrix-rust-sdk/pull/5424))
1115
- Add support to accept historic room key bundles that arrive out of order, i.e.
1216
the bundle arrives after the invite has already been accepted.
1317
([#5322](https://github.com/matrix-org/matrix-rust-sdk/pull/5322))

crates/matrix-sdk/src/account.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,8 @@ impl Account {
698698
Ok(self.client.send(request).await?)
699699
}
700700

701-
/// Get the content of an account data event of statically-known type.
701+
/// Get the content of an account data event of statically-known type, from
702+
/// storage.
702703
///
703704
/// # Examples
704705
///
@@ -726,7 +727,7 @@ impl Account {
726727
get_raw_content(self.client.state_store().get_account_data_event_static::<C>().await?)
727728
}
728729

729-
/// Get the content of an account data event of a given type.
730+
/// Get the content of an account data event of a given type, from storage.
730731
pub async fn account_data_raw(
731732
&self,
732733
event_type: GlobalAccountDataEventType,
@@ -781,6 +782,14 @@ impl Account {
781782
}
782783
}
783784

785+
/// Fetch an account data event of statically-known type from the server.
786+
pub async fn fetch_account_data_static<C>(&self) -> Result<Option<Raw<C>>>
787+
where
788+
C: GlobalAccountDataEventContent + StaticEventContent,
789+
{
790+
Ok(self.fetch_account_data(C::TYPE.into()).await?.map(Raw::cast))
791+
}
792+
784793
/// Set the given account data event.
785794
///
786795
/// # Examples
@@ -862,12 +871,12 @@ impl Account {
862871

863872
// We are fetching the content from the server because we currently can't rely
864873
// on `/sync` giving us the correct data in a timely manner.
865-
let raw_content = self.fetch_account_data(GlobalAccountDataEventType::Direct).await?;
874+
let raw_content = self.fetch_account_data_static::<DirectEventContent>().await?;
866875

867876
let mut content = if let Some(raw_content) = raw_content {
868877
// Log the error and pass it upwards if we fail to deserialize the m.direct
869878
// event.
870-
raw_content.deserialize_as::<DirectEventContent>().map_err(|err| {
879+
raw_content.deserialize().map_err(|err| {
871880
error!("unable to deserialize m.direct event content; aborting request to mark {room_id} as dm: {err}");
872881
err
873882
})?
@@ -1081,21 +1090,22 @@ impl Account {
10811090
pub async fn fetch_media_preview_config_event_content(
10821091
&self,
10831092
) -> Result<Option<MediaPreviewConfigEventContent>> {
1084-
// First we check if there is avalue in the stable event
1093+
// First we check if there is a value in the stable event
10851094
let media_preview_config =
1086-
self.fetch_account_data(GlobalAccountDataEventType::MediaPreviewConfig).await?;
1095+
self.fetch_account_data_static::<MediaPreviewConfigEventContent>().await?;
10871096

10881097
let media_preview_config = if let Some(media_preview_config) = media_preview_config {
10891098
Some(media_preview_config)
10901099
} else {
10911100
// If there is no value in the stable event, we check the unstable
1092-
self.fetch_account_data(GlobalAccountDataEventType::UnstableMediaPreviewConfig).await?
1101+
self.fetch_account_data_static::<UnstableMediaPreviewConfigEventContent>()
1102+
.await?
1103+
.map(Raw::cast)
10931104
};
10941105

10951106
// We deserialize the content of the event, if is not found we return the
10961107
// default
1097-
let media_preview_config = media_preview_config
1098-
.and_then(|value| value.deserialize_as::<MediaPreviewConfigEventContent>().ok());
1108+
let media_preview_config = media_preview_config.and_then(|value| value.deserialize().ok());
10991109

11001110
Ok(media_preview_config)
11011111
}

crates/matrix-sdk/src/encryption/recovery/mod.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl Recovery {
408408
/// # let client = Client::new(homeserver).await?;
409409
/// # let user_id = unimplemented!();
410410
/// let encryption = client.encryption();
411-
///
411+
///
412412
/// if let Some(handle) = encryption.recovery().reset_identity().await? {
413413
/// match handle.auth_type() {
414414
/// CrossSigningResetAuthType::Uiaa(uiaa) => {
@@ -592,14 +592,9 @@ impl Recovery {
592592
Ok(self
593593
.client
594594
.account()
595-
.fetch_account_data(BackupDisabledContent::event_type())
595+
.fetch_account_data_static::<BackupDisabledContent>()
596596
.await?
597-
.map(|event| {
598-
event
599-
.deserialize_as::<BackupDisabledContent>()
600-
.map(|event| event.disabled)
601-
.unwrap_or(false)
602-
})
597+
.map(|event| event.deserialize().map(|event| event.disabled).unwrap_or(false))
603598
.unwrap_or(false))
604599
}
605600

crates/matrix-sdk/src/encryption/recovery/types.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
// limitations under the License.
1414

1515
use matrix_sdk_base::crypto::store::types::RoomKeyCounts;
16-
use ruma::{
17-
events::{EventContent, GlobalAccountDataEventType},
18-
exports::ruma_macros::EventContent,
19-
};
16+
use ruma::exports::ruma_macros::EventContent;
2017
use serde::{Deserialize, Serialize};
2118
use thiserror::Error;
2219
use zeroize::{Zeroize, ZeroizeOnDrop};
@@ -115,12 +112,3 @@ pub(super) struct SecretStorageDisabledContent {}
115112
pub(super) struct BackupDisabledContent {
116113
pub disabled: bool,
117114
}
118-
119-
impl BackupDisabledContent {
120-
/// Get the event type of the [`BackupDisabledContent`] global account data
121-
/// event.
122-
pub(super) fn event_type() -> GlobalAccountDataEventType {
123-
// This is dumb, there's got to be a better way to get to the event type?
124-
Self { disabled: false }.event_type()
125-
}
126-
}

crates/matrix-sdk/src/encryption/secret_storage/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,9 @@ impl SecretStorage {
283283
pub async fn fetch_default_key_id(
284284
&self,
285285
) -> crate::Result<Option<Raw<SecretStorageDefaultKeyEventContent>>> {
286-
let maybe_default_key_id = self
287-
.client
286+
self.client
288287
.account()
289-
.fetch_account_data(GlobalAccountDataEventType::SecretStorageDefaultKey)
290-
.await?;
291-
292-
Ok(maybe_default_key_id.map(|event| event.cast()))
288+
.fetch_account_data_static::<SecretStorageDefaultKeyEventContent>()
289+
.await
293290
}
294291
}

0 commit comments

Comments
 (0)