Skip to content

Commit 00ab99a

Browse files
committed
feat: implement and observe MSC 4278 config value
1 parent de66047 commit 00ab99a

File tree

7 files changed

+458
-33
lines changed

7 files changed

+458
-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: 82 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,63 @@ 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+
#[default]
1115+
On,
1116+
Private,
1117+
Off,
1118+
}
1119+
1120+
impl From<RumaMediaPreviews> for MediaPreviews {
1121+
fn from(value: RumaMediaPreviews) -> Self {
1122+
match value {
1123+
RumaMediaPreviews::On => Self::On,
1124+
RumaMediaPreviews::Private => Self::Private,
1125+
RumaMediaPreviews::Off => Self::Off,
1126+
_ => Default::default(),
1127+
}
1128+
}
1129+
}
1130+
1131+
impl From<MediaPreviews> for RumaMediaPreviews {
1132+
fn from(value: MediaPreviews) -> Self {
1133+
match value {
1134+
MediaPreviews::On => Self::On,
1135+
MediaPreviews::Private => Self::Private,
1136+
MediaPreviews::Off => Self::Off,
1137+
}
1138+
}
1139+
}
1140+
1141+
/// The policy that decides if avarars should be shown in invite requests.
1142+
#[derive(Clone, uniffi::Enum, Default)]
1143+
pub enum InviteAvatars {
1144+
#[default]
1145+
On,
1146+
Off,
1147+
}
1148+
1149+
impl From<RumaInviteAvatars> for InviteAvatars {
1150+
fn from(value: RumaInviteAvatars) -> Self {
1151+
match value {
1152+
RumaInviteAvatars::On => Self::On,
1153+
RumaInviteAvatars::Off => Self::Off,
1154+
_ => Default::default(),
1155+
}
1156+
}
1157+
}
1158+
1159+
impl From<InviteAvatars> for RumaInviteAvatars {
1160+
fn from(value: InviteAvatars) -> Self {
1161+
match value {
1162+
InviteAvatars::On => Self::On,
1163+
InviteAvatars::Off => Self::Off,
1164+
}
1165+
}
1166+
}
1167+
11071168
/// Details about an ignored user.
11081169
///
11091170
/// This is currently empty.
@@ -1351,6 +1412,27 @@ impl From<RumaSecretStorageV1AesHmacSha2Properties> for SecretStorageV1AesHmacSh
13511412
}
13521413
}
13531414

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

0 commit comments

Comments
 (0)