Skip to content

Commit 60a2d20

Browse files
committed
feat: MSC 4278 implementation
1 parent fc071ba commit 60a2d20

File tree

6 files changed

+188
-20
lines changed

6 files changed

+188
-20
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ reqwest = { version = "0.12.12", default-features = false }
6060
rmp-serde = "1.3.0"
6161
# Be careful to use commits from the https://github.com/ruma/ruma/tree/ruma-0.12
6262
# branch until a proper release with breaking changes happens.
63-
ruma = { version = "0.12.2", features = [
63+
ruma = { git = "https://github.com/ruma/ruma", rev = "689d9613a985edc089b5b729e6d9362f09b5df4f", features = [
6464
"client-api-c",
6565
"compat-upload-signatures",
6666
"compat-user-id",
@@ -74,8 +74,9 @@ ruma = { version = "0.12.2", features = [
7474
"unstable-msc4075",
7575
"unstable-msc4140",
7676
"unstable-msc4171",
77-
] }
78-
ruma-common = "0.15.2"
77+
"unstable-msc4278",
78+
] }
79+
ruma-common = { git = "https://github.com/ruma/ruma", rev = "689d9613a985edc089b5b729e6d9362f09b5df4f" }
7980
serde = "1.0.217"
8081
serde_html_form = "0.2.7"
8182
serde_json = "1.0.138"

bindings/matrix-sdk-ffi/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ matrix-sdk-ffi-macros = { workspace = true }
3333
matrix-sdk-ui = { workspace = true, features = ["uniffi"] }
3434
mime = "0.3.16"
3535
once_cell = { workspace = true }
36-
ruma = { workspace = true, features = ["html", "unstable-unspecified", "unstable-msc3488", "compat-unset-avatar", "unstable-msc3245-v1-compat"] }
36+
ruma = { workspace = true, features = ["html", "unstable-unspecified", "unstable-msc3488", "compat-unset-avatar", "unstable-msc3245-v1-compat", "unstable-msc4278"] }
3737
serde = { workspace = true }
3838
serde_json = { workspace = true }
3939
thiserror = { workspace = true }

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ use ruma::{
4747
events::{
4848
ignored_user_list::IgnoredUserListEventContent,
4949
key::verification::request::ToDeviceKeyVerificationRequestEvent,
50+
media_preview_config::{
51+
InviteAvatars as RumaInviteAvatars, MediaPreviews as RumaMediaPreviews,
52+
},
5053
room::{
5154
history_visibility::RoomHistoryVisibilityEventContent,
5255
join_rules::{
@@ -1207,6 +1210,91 @@ impl Client {
12071210
pub async fn is_report_room_api_supported(&self) -> Result<bool, ClientError> {
12081211
Ok(self.inner.server_versions().await?.contains(&ruma::api::MatrixVersion::V1_13))
12091212
}
1213+
1214+
/// Get the media previews display policy setting.
1215+
pub async fn get_media_previews(&self) -> Result<MediaPreviews, ClientError> {
1216+
let media_previews = self.inner.account().get_media_previews_setting().await?;
1217+
Ok(media_previews.into())
1218+
}
1219+
1220+
/// Set the media previews display policy setting.
1221+
pub async fn set_media_previews(
1222+
&self,
1223+
media_previews: MediaPreviews,
1224+
) -> Result<(), ClientError> {
1225+
self.inner.account().set_media_previews_setting(media_previews.into()).await?;
1226+
Ok(())
1227+
}
1228+
1229+
/// Get the invite avatars display policy setting.
1230+
pub async fn get_invite_avatars(&self) -> Result<InviteAvatars, ClientError> {
1231+
let invite_avatars = self.inner.account().get_invite_avatars_setting().await?;
1232+
Ok(invite_avatars.into())
1233+
}
1234+
1235+
/// Set the invite avatars display policy setting.
1236+
pub async fn set_invite_avatars(
1237+
&self,
1238+
invite_avatars: InviteAvatars,
1239+
) -> Result<(), ClientError> {
1240+
self.inner.account().set_invite_avatars_setting(invite_avatars.into()).await?;
1241+
Ok(())
1242+
}
1243+
}
1244+
1245+
#[derive(uniffi::Enum)]
1246+
pub enum MediaPreviews {
1247+
On,
1248+
Private,
1249+
Off,
1250+
}
1251+
1252+
impl From<RumaMediaPreviews> for MediaPreviews {
1253+
fn from(value: RumaMediaPreviews) -> Self {
1254+
match value {
1255+
RumaMediaPreviews::On => MediaPreviews::On,
1256+
RumaMediaPreviews::Private => MediaPreviews::Private,
1257+
RumaMediaPreviews::Off => MediaPreviews::Off,
1258+
// Use the default value of On for unhandled cases.
1259+
_ => MediaPreviews::On,
1260+
}
1261+
}
1262+
}
1263+
1264+
impl From<MediaPreviews> for RumaMediaPreviews {
1265+
fn from(value: MediaPreviews) -> Self {
1266+
match value {
1267+
MediaPreviews::On => RumaMediaPreviews::On,
1268+
MediaPreviews::Private => RumaMediaPreviews::Private,
1269+
MediaPreviews::Off => RumaMediaPreviews::Off,
1270+
}
1271+
}
1272+
}
1273+
1274+
#[derive(uniffi::Enum)]
1275+
pub enum InviteAvatars {
1276+
On,
1277+
Off,
1278+
}
1279+
1280+
impl From<RumaInviteAvatars> for InviteAvatars {
1281+
fn from(value: RumaInviteAvatars) -> Self {
1282+
match value {
1283+
RumaInviteAvatars::On => InviteAvatars::On,
1284+
RumaInviteAvatars::Off => InviteAvatars::Off,
1285+
// Use the default value of On for unhandled cases.
1286+
_ => InviteAvatars::On,
1287+
}
1288+
}
1289+
}
1290+
1291+
impl From<InviteAvatars> for RumaInviteAvatars {
1292+
fn from(value: InviteAvatars) -> Self {
1293+
match value {
1294+
InviteAvatars::On => RumaInviteAvatars::On,
1295+
InviteAvatars::Off => RumaInviteAvatars::Off,
1296+
}
1297+
}
12101298
}
12111299

12121300
#[matrix_sdk_ffi_macros::export(callback_interface)]

crates/matrix-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ ruma = { workspace = true, features = [
100100
"unstable-msc4230",
101101
"unstable-msc2967",
102102
"unstable-msc4108",
103+
"unstable-msc4278",
103104
] }
104105
serde = { workspace = true }
105106
serde_html_form = { workspace = true }

crates/matrix-sdk/src/account.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ use ruma::{
3636
assign,
3737
events::{
3838
ignored_user_list::{IgnoredUser, IgnoredUserListEventContent},
39+
media_preview_config::{
40+
InviteAvatars, MediaPreviewConfigEventContent, MediaPreviews,
41+
UnstableMediaPreviewConfigEventContent,
42+
},
3943
push_rules::PushRulesEventContent,
4044
room::MediaSource,
4145
AnyGlobalAccountDataEventContent, GlobalAccountDataEventContent,
@@ -916,6 +920,88 @@ impl Account {
916920
Ok(ignored_user_list)
917921
}
918922

923+
/// Set the media previews display policy for the account.
924+
///
925+
/// This is an unstable event, so we always default to setting the unstable
926+
/// version of the event for now.
927+
pub async fn set_media_previews_setting(&self, new_setting: MediaPreviews) -> Result<()> {
928+
let mut media_preview_config_to_update =
929+
self.get_unstable_media_preview_config().await?.unwrap_or_else(|| {
930+
UnstableMediaPreviewConfigEventContent::new(Default::default(), Default::default())
931+
});
932+
media_preview_config_to_update.media_previews = new_setting;
933+
934+
// Updating the account data
935+
self.set_account_data(media_preview_config_to_update).await?;
936+
// TODO: I think I should reset all the storage and perform a new local sync
937+
// here but I don't know how
938+
Ok(())
939+
}
940+
941+
/// Set the avatars in invite requests display policy for the account.
942+
///
943+
/// This is an unstable event, so we always default to setting the unstable
944+
/// version of the event for now.
945+
pub async fn set_invite_avatars_setting(&self, new_setting: InviteAvatars) -> Result<()> {
946+
let mut media_preview_config_to_update =
947+
self.get_unstable_media_preview_config().await?.unwrap_or_else(|| {
948+
UnstableMediaPreviewConfigEventContent::new(Default::default(), Default::default())
949+
});
950+
media_preview_config_to_update.invite_avatars = new_setting;
951+
// Updating the account data
952+
self.set_account_data(media_preview_config_to_update).await?;
953+
// TODO: I think I should reset all the storage and perform a new local sync
954+
// here but I don't know how
955+
Ok(())
956+
}
957+
958+
/// Get the current media previews display policy for the account.
959+
pub async fn get_media_previews_setting(&self) -> Result<MediaPreviews> {
960+
if let Some(media_preview_config) = self.get_media_preview_config().await? {
961+
Ok(media_preview_config.media_previews)
962+
} else if let Some(unstable_media_preview_config) =
963+
self.get_unstable_media_preview_config().await?
964+
{
965+
Ok(unstable_media_preview_config.media_previews)
966+
} else {
967+
Ok(Default::default())
968+
}
969+
}
970+
971+
/// Get the current avatars in invite requests display policy for the
972+
/// account.
973+
pub async fn get_invite_avatars_setting(&self) -> Result<InviteAvatars> {
974+
if let Some(media_preview_config) = self.get_media_preview_config().await? {
975+
Ok(media_preview_config.invite_avatars)
976+
} else if let Some(unstable_media_preview_config) =
977+
self.get_unstable_media_preview_config().await?
978+
{
979+
Ok(unstable_media_preview_config.invite_avatars)
980+
} else {
981+
Ok(Default::default())
982+
}
983+
}
984+
985+
async fn get_unstable_media_preview_config(
986+
&self,
987+
) -> Result<Option<UnstableMediaPreviewConfigEventContent>> {
988+
let unstable_media_preview_config = self
989+
.account_data::<UnstableMediaPreviewConfigEventContent>()
990+
.await?
991+
.map(|c| c.deserialize())
992+
.transpose()?;
993+
Ok(unstable_media_preview_config)
994+
}
995+
996+
async fn get_media_preview_config(&self) -> Result<Option<MediaPreviewConfigEventContent>> {
997+
let media_preview_config = self
998+
.account_data::<MediaPreviewConfigEventContent>()
999+
.await?
1000+
.map(|c| c.deserialize())
1001+
.transpose()?;
1002+
Ok(media_preview_config)
1003+
}
1004+
9191005
/// Get the current push rules from storage.
9201006
///
9211007
/// If no push rules event was found, or it fails to deserialize, a ruleset

0 commit comments

Comments
 (0)