Skip to content

feat(bindings): added APIs to get the media preview config from the store #5062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,13 +1401,27 @@ impl Client {
}

/// Get the media previews timeline display policy
/// currently stored in the cache.
pub async fn get_media_preview_display_policy(&self) -> Result<MediaPreviews, ClientError> {
let configuration = self.inner.account().get_media_preview_config_event_content().await?;
Ok(configuration.media_previews.into())
}

/// Set the invite request avatars display policy
pub async fn set_invite_avatars_display_policy(
&self,
policy: InviteAvatars,
) -> Result<(), ClientError> {
self.inner.account().set_invite_avatars_display_policy(policy.into()).await?;
Ok(())
}

/// Get the invite request avatars display policy
/// currently stored in the cache.
pub async fn get_invite_avatars_display_policy(&self) -> Result<InviteAvatars, ClientError> {
let configuration = self.inner.account().get_media_preview_config_event_content().await?;
Ok(configuration.invite_avatars.into())
}
}

#[matrix_sdk_ffi_macros::export(callback_interface)]
Expand Down
34 changes: 31 additions & 3 deletions crates/matrix-sdk/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,11 @@ impl Account {
Ok((initial_value, result_stream))
}

async fn get_media_preview_config_event_content(
/// Fetch the media preview configuration event content from the server.
///
/// If no value was found in either the stable and unstable type, the
/// default configuration will be returned.
pub async fn fetch_media_preview_config_event_content(
&self,
) -> Result<MediaPreviewConfigEventContent> {
// First we check if there is avalue in the stable event
Expand All @@ -1087,12 +1091,36 @@ impl Account {
Ok(media_preview_config)
}

/// Get the media preview configuration event content stored in the cache.
///
/// This will return the default configuration if no value was found.
/// Will check first for the stable event and then for the unstable one.
pub async fn get_media_preview_config_event_content(
&self,
) -> Result<MediaPreviewConfigEventContent> {
let media_preview_config = self
.account_data::<MediaPreviewConfigEventContent>()
.await?
.and_then(|r| r.deserialize().ok());

let media_preview_config = if let Some(media_preview_config) = media_preview_config {
media_preview_config
} else {
self.account_data::<UnstableMediaPreviewConfigEventContent>()
.await?
.and_then(|r| r.deserialize().ok())
.unwrap_or_default()
.into()
};
Ok(media_preview_config)
}

/// Set the media previews display policy in the timeline.
///
/// This will always use the unstable event until we know which Matrix
/// version will support it.
pub async fn set_media_previews_display_policy(&self, policy: MediaPreviews) -> Result<()> {
let mut media_preview_config = self.get_media_preview_config_event_content().await?;
let mut media_preview_config = self.fetch_media_preview_config_event_content().await?;
media_preview_config.media_previews = policy;

// Updating the unstable account data
Expand All @@ -1107,7 +1135,7 @@ impl Account {
/// This will always use the unstable event until we know which matrix
/// version will support it.
pub async fn set_invite_avatars_display_policy(&self, policy: InviteAvatars) -> Result<()> {
let mut media_preview_config = self.get_media_preview_config_event_content().await?;
let mut media_preview_config = self.fetch_media_preview_config_event_content().await?;
media_preview_config.invite_avatars = policy;

// Updating the unstable account data
Expand Down
59 changes: 16 additions & 43 deletions crates/matrix-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3341,16 +3341,16 @@ pub(crate) mod tests {
let client = server.client_builder().build().await;

server
.mock_global_account_data()
.ok(
client.user_id().unwrap(),
ruma::events::GlobalAccountDataEventType::MediaPreviewConfig,
json!({
.mock_sync()
.ok_and_run(&client, |builder| {
builder.add_global_account_data_event(GlobalAccountDataTestEvent::Custom(json!({
"content": {
"media_previews": "private",
"invite_avatars": "off"
}),
)
.mount()
},
"type": "m.media_preview_config"
})));
})
.await;

let (initial_value, stream) =
Expand Down Expand Up @@ -3391,25 +3391,16 @@ pub(crate) mod tests {
let client = server.client_builder().build().await;

server
.mock_global_account_data()
.not_found(
client.user_id().unwrap(),
ruma::events::GlobalAccountDataEventType::MediaPreviewConfig,
)
.mount()
.await;

server
.mock_global_account_data()
.ok(
client.user_id().unwrap(),
ruma::events::GlobalAccountDataEventType::UnstableMediaPreviewConfig,
json!({
.mock_sync()
.ok_and_run(&client, |builder| {
builder.add_global_account_data_event(GlobalAccountDataTestEvent::Custom(json!({
"content": {
"media_previews": "private",
"invite_avatars": "off"
}),
)
.mount()
},
"type": "io.element.msc4278.media_preview_config"
})));
})
.await;

let (initial_value, stream) =
Expand Down Expand Up @@ -3449,24 +3440,6 @@ pub(crate) mod tests {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

server
.mock_global_account_data()
.not_found(
client.user_id().unwrap(),
ruma::events::GlobalAccountDataEventType::MediaPreviewConfig,
)
.mount()
.await;

server
.mock_global_account_data()
.not_found(
client.user_id().unwrap(),
ruma::events::GlobalAccountDataEventType::UnstableMediaPreviewConfig,
)
.mount()
.await;

let (initial_value, _) = client.account().observe_media_preview_config().await.unwrap();

assert_eq!(initial_value.invite_avatars, InviteAvatars::On);
Expand Down
3 changes: 2 additions & 1 deletion crates/matrix-sdk/src/test_utils/mocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ impl MatrixMockServer {
/// tokio_test::block_on(async {
/// use matrix_sdk::test_utils::mocks::MatrixMockServer;
/// use serde_json::json;
/// use ruma::events::media_preview_config::MediaPreviews;
///
/// let mock_server = MatrixMockServer::new().await;
/// let client = mock_server.client_builder().build().await;
Expand All @@ -1042,7 +1043,7 @@ impl MatrixMockServer {
/// .mount()
/// .await;
///
/// let (_, _) = client.account().observe_media_preview_config().await.unwrap();
/// client.account().fetch_media_preview_config_event_content().await.unwrap();
///
/// # anyhow::Ok(()) });
/// ```
Expand Down
Loading