Skip to content

Commit 602eef4

Browse files
committed
change: separated initial_value function from the observing one
1 parent f519b94 commit 602eef4

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

crates/matrix-sdk/src/account.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use futures_core::Stream;
1818
use futures_util::{stream, StreamExt};
1919
use matrix_sdk_base::{
20+
event_cache::store::media,
2021
media::{MediaFormat, MediaRequestParameters},
2122
store::StateStoreExt,
2223
StateStoreDataKey, StateStoreDataValue,
@@ -39,7 +40,7 @@ use ruma::{
3940
events::{
4041
ignored_user_list::{IgnoredUser, IgnoredUserListEventContent},
4142
media_preview_config::{
42-
InviteAvatars, MediaPreviewConfigEventContent, MediaPreviews,
43+
self, InviteAvatars, MediaPreviewConfigEventContent, MediaPreviews,
4344
UnstableMediaPreviewConfigEventContent,
4445
},
4546
push_rules::PushRulesEventContent,
@@ -1027,22 +1028,7 @@ impl Account {
10271028
(MediaPreviewConfigEventContent, impl Stream<Item = MediaPreviewConfigEventContent>),
10281029
Error,
10291030
> {
1030-
// First we check if there is avalue in the stable event
1031-
let initial_value =
1032-
self.fetch_account_data(GlobalAccountDataEventType::MediaPreviewConfig).await?;
1033-
1034-
let initial_value = if let Some(initial_value) = initial_value {
1035-
Some(initial_value)
1036-
} else {
1037-
// If there is no value in the stable event, we check the unstable
1038-
self.fetch_account_data(GlobalAccountDataEventType::UnstableMediaPreviewConfig).await?
1039-
};
1040-
1041-
// We deserialize the content of the event, if is not found we return the
1042-
// default
1043-
let initial_value = initial_value
1044-
.and_then(|value| value.deserialize_as::<MediaPreviewConfigEventContent>().ok())
1045-
.unwrap_or_default();
1031+
let initial_value = self.get_media_preview_config_event_content().await?;
10461032

10471033
// We need to create two observers, one for the stable event and one for the
10481034
// unstable and combine them into a single stream.
@@ -1074,12 +1060,35 @@ impl Account {
10741060
Ok((initial_value, result_stream))
10751061
}
10761062

1063+
async fn get_media_preview_config_event_content(
1064+
&self,
1065+
) -> Result<MediaPreviewConfigEventContent> {
1066+
// First we check if there is avalue in the stable event
1067+
let media_preview_config =
1068+
self.fetch_account_data(GlobalAccountDataEventType::MediaPreviewConfig).await?;
1069+
1070+
let media_preview_config = if let Some(media_preview_config) = media_preview_config {
1071+
Some(media_preview_config)
1072+
} else {
1073+
// If there is no value in the stable event, we check the unstable
1074+
self.fetch_account_data(GlobalAccountDataEventType::UnstableMediaPreviewConfig).await?
1075+
};
1076+
1077+
// We deserialize the content of the event, if is not found we return the
1078+
// default
1079+
let media_preview_config = media_preview_config
1080+
.and_then(|value| value.deserialize_as::<MediaPreviewConfigEventContent>().ok())
1081+
.unwrap_or_default();
1082+
1083+
Ok(media_preview_config)
1084+
}
1085+
10771086
/// Set the media previews display policy in the timeline.
10781087
///
10791088
/// This will always use the unstable event until we know which Matrix
10801089
/// version will support it.
10811090
pub async fn set_media_previews_display_policy(&self, policy: MediaPreviews) -> Result<()> {
1082-
let (mut media_preview_config, _) = self.observe_media_preview_config().await?;
1091+
let mut media_preview_config = self.get_media_preview_config_event_content().await?;
10831092
media_preview_config.media_previews = policy;
10841093

10851094
// Updating the unstable account data
@@ -1094,7 +1103,7 @@ impl Account {
10941103
/// This will always use the unstable event until we know which matrix
10951104
/// version will support it.
10961105
pub async fn set_invite_avatars_display_policy(&self, policy: InviteAvatars) -> Result<()> {
1097-
let (mut media_preview_config, _) = self.observe_media_preview_config().await?;
1106+
let mut media_preview_config = self.get_media_preview_config_event_content().await?;
10981107
media_preview_config.invite_avatars = policy;
10991108

11001109
// Updating the unstable account data

0 commit comments

Comments
 (0)