Skip to content

Commit 154f29e

Browse files
authored
feat(sdk): implement and observe MSC4278 config value
This patch - Updates Ruma to use the improved MediaPreviewConfig event type that also supports a `Default` for the content type - Implemented a way to observe the stable and unstable values of the event and return the used one accordingly, if no one is present the default will be used - Set the value (will only use unstable type for now)
1 parent 7f07731 commit 154f29e

File tree

7 files changed

+490
-33
lines changed

7 files changed

+490
-33
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ reqwest = { version = "0.12.12", default-features = false }
6161
rmp-serde = "1.3.0"
6262
# Be careful to use commits from the https://github.com/ruma/ruma/tree/ruma-0.12
6363
# branch until a proper release with breaking changes happens.
64-
ruma = { git = "https://github.com/ruma/ruma", rev = "689d9613a985edc089b5b729e6d9362f09b5df4f", features = [
64+
ruma = { git = "https://github.com/ruma/ruma", rev = "a8fd1b0322649bf59e2a5cfc73ab4fe46b21edd7", features = [
6565
"client-api-c",
6666
"compat-upload-signatures",
6767
"compat-user-id",
@@ -77,7 +77,7 @@ ruma = { git = "https://github.com/ruma/ruma", rev = "689d9613a985edc089b5b729e6
7777
"unstable-msc4171",
7878
"unstable-msc4278",
7979
] }
80-
ruma-common = { git = "https://github.com/ruma/ruma", rev = "689d9613a985edc089b5b729e6d9362f09b5df4f" }
80+
ruma-common = { git = "https://github.com/ruma/ruma", rev = "a8fd1b0322649bf59e2a5cfc73ab4fe46b21edd7" }
8181
serde = "1.0.217"
8282
serde_html_form = "0.2.7"
8383
serde_json = "1.0.138"

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66

77
use anyhow::{anyhow, Context as _};
88
use async_compat::get_runtime_handle;
9-
use futures_util::StreamExt;
9+
use futures_util::{pin_mut, StreamExt};
1010
use matrix_sdk::{
1111
authentication::oauth::{
1212
AccountManagementActionFull, ClientId, OAuthAuthorizationData, OAuthSession,
@@ -89,8 +89,8 @@ use crate::{
8989
room_directory_search::RoomDirectorySearch,
9090
room_preview::RoomPreview,
9191
ruma::{
92-
AccountDataEvent, AccountDataEventType, AuthData, MediaSource, RoomAccountDataEvent,
93-
RoomAccountDataEventType,
92+
AccountDataEvent, AccountDataEventType, AuthData, InviteAvatars, MediaPreviewConfig,
93+
MediaPreviews, MediaSource, RoomAccountDataEvent, RoomAccountDataEventType,
9494
},
9595
sync_service::{SyncService, SyncServiceBuilder},
9696
task_handle::TaskHandle,
@@ -1373,6 +1373,46 @@ impl Client {
13731373
pub async fn is_report_room_api_supported(&self) -> Result<bool, ClientError> {
13741374
Ok(self.inner.server_versions().await?.contains(&ruma::api::MatrixVersion::V1_13))
13751375
}
1376+
1377+
/// Subscribe to changes in the media preview configuration.
1378+
pub async fn subscribe_to_media_preview_config(
1379+
&self,
1380+
listener: Box<dyn MediaPreviewConfigListener>,
1381+
) -> Result<Arc<TaskHandle>, ClientError> {
1382+
let (initial_value, stream) = self.inner.account().observe_media_preview_config().await?;
1383+
Ok(Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move {
1384+
// Send the initial value to the listener.
1385+
listener.on_change(initial_value.into());
1386+
// Listen for changes and notify the listener.
1387+
pin_mut!(stream);
1388+
while let Some(media_preview_config) = stream.next().await {
1389+
listener.on_change(media_preview_config.into());
1390+
}
1391+
}))))
1392+
}
1393+
1394+
/// Set the media previews timeline display policy
1395+
pub async fn set_media_preview_display_policy(
1396+
&self,
1397+
policy: MediaPreviews,
1398+
) -> Result<(), ClientError> {
1399+
self.inner.account().set_media_previews_display_policy(policy.into()).await?;
1400+
Ok(())
1401+
}
1402+
1403+
/// Get the media previews timeline display policy
1404+
pub async fn set_invite_avatars_display_policy(
1405+
&self,
1406+
policy: InviteAvatars,
1407+
) -> Result<(), ClientError> {
1408+
self.inner.account().set_invite_avatars_display_policy(policy.into()).await?;
1409+
Ok(())
1410+
}
1411+
}
1412+
1413+
#[matrix_sdk_ffi_macros::export(callback_interface)]
1414+
pub trait MediaPreviewConfigListener: Sync + Send {
1415+
fn on_change(&self, media_preview_config: MediaPreviewConfig);
13761416
}
13771417

13781418
#[matrix_sdk_ffi_macros::export(callback_interface)]

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ use ruma::{
3030
ignored_user_list::{IgnoredUser as RumaIgnoredUser, IgnoredUserListEventContent},
3131
location::AssetType as RumaAssetType,
3232
marked_unread::{MarkedUnreadEventContent, UnstableMarkedUnreadEventContent},
33+
media_preview_config::{
34+
InviteAvatars as RumaInviteAvatars, MediaPreviewConfigEventContent,
35+
MediaPreviews as RumaMediaPreviews,
36+
},
3337
poll::start::PollKind as RumaPollKind,
3438
push_rules::PushRulesEventContent,
3539
room::{
@@ -1104,6 +1108,68 @@ pub enum AccountDataEvent {
11041108
},
11051109
}
11061110

1111+
/// The policy that decides if media previews should be shown in the timeline.
1112+
#[derive(Clone, uniffi::Enum, Default)]
1113+
pub enum MediaPreviews {
1114+
/// Always show media previews in the timeline.
1115+
#[default]
1116+
On,
1117+
/// Show media previews in the timeline only if the room is private.
1118+
Private,
1119+
/// Never show media previews in the timeline.
1120+
Off,
1121+
}
1122+
1123+
impl From<RumaMediaPreviews> for MediaPreviews {
1124+
fn from(value: RumaMediaPreviews) -> Self {
1125+
match value {
1126+
RumaMediaPreviews::On => Self::On,
1127+
RumaMediaPreviews::Private => Self::Private,
1128+
RumaMediaPreviews::Off => Self::Off,
1129+
_ => Default::default(),
1130+
}
1131+
}
1132+
}
1133+
1134+
impl From<MediaPreviews> for RumaMediaPreviews {
1135+
fn from(value: MediaPreviews) -> Self {
1136+
match value {
1137+
MediaPreviews::On => Self::On,
1138+
MediaPreviews::Private => Self::Private,
1139+
MediaPreviews::Off => Self::Off,
1140+
}
1141+
}
1142+
}
1143+
1144+
/// The policy that decides if avatars should be shown in invite requests.
1145+
#[derive(Clone, uniffi::Enum, Default)]
1146+
pub enum InviteAvatars {
1147+
/// Always show avatars in invite requests.
1148+
#[default]
1149+
On,
1150+
/// Never show avatars in invite requests.
1151+
Off,
1152+
}
1153+
1154+
impl From<RumaInviteAvatars> for InviteAvatars {
1155+
fn from(value: RumaInviteAvatars) -> Self {
1156+
match value {
1157+
RumaInviteAvatars::On => Self::On,
1158+
RumaInviteAvatars::Off => Self::Off,
1159+
_ => Default::default(),
1160+
}
1161+
}
1162+
}
1163+
1164+
impl From<InviteAvatars> for RumaInviteAvatars {
1165+
fn from(value: InviteAvatars) -> Self {
1166+
match value {
1167+
InviteAvatars::On => Self::On,
1168+
InviteAvatars::Off => Self::Off,
1169+
}
1170+
}
1171+
}
1172+
11071173
/// Details about an ignored user.
11081174
///
11091175
/// This is currently empty.
@@ -1351,6 +1417,28 @@ impl From<RumaSecretStorageV1AesHmacSha2Properties> for SecretStorageV1AesHmacSh
13511417
}
13521418
}
13531419

1420+
/// The content of an `m.media_preview_config` event.
1421+
///
1422+
/// Is also the content of the unstable
1423+
/// `io.element.msc4278.media_preview_config`.
1424+
#[derive(Clone, uniffi::Record, Default)]
1425+
pub struct MediaPreviewConfig {
1426+
/// The media previews setting for the user.
1427+
pub media_previews: MediaPreviews,
1428+
1429+
/// The invite avatars setting for the user.
1430+
pub invite_avatars: InviteAvatars,
1431+
}
1432+
1433+
impl From<MediaPreviewConfigEventContent> for MediaPreviewConfig {
1434+
fn from(value: MediaPreviewConfigEventContent) -> Self {
1435+
Self {
1436+
media_previews: value.media_previews.into(),
1437+
invite_avatars: value.invite_avatars.into(),
1438+
}
1439+
}
1440+
}
1441+
13541442
/// A passphrase from which a key is to be derived.
13551443
#[derive(Clone, uniffi::Record)]
13561444
pub struct PassPhrase {

0 commit comments

Comments
 (0)