diff --git a/crates/matrix-sdk-ui/src/timeline/tests/mod.rs b/crates/matrix-sdk-ui/src/timeline/tests/mod.rs index 13f974f162..1f727ede54 100644 --- a/crates/matrix-sdk-ui/src/timeline/tests/mod.rs +++ b/crates/matrix-sdk-ui/src/timeline/tests/mod.rs @@ -16,7 +16,6 @@ use std::{ collections::{BTreeMap, HashMap}, - future::ready, ops::Sub, sync::Arc, time::{Duration, SystemTime}, @@ -25,7 +24,6 @@ use std::{ use eyeball::{SharedObservable, Subscriber}; use eyeball_im::VectorDiff; use futures_core::Stream; -use futures_util::FutureExt as _; use indexmap::IndexMap; use matrix_sdk::{ config::RequestConfig, @@ -380,8 +378,8 @@ impl RoomDataProvider for TestRoomDataProvider { RoomVersionId::V10 } - fn crypto_context_info(&self) -> BoxFuture<'_, CryptoContextInfo> { - ready(CryptoContextInfo { + async fn crypto_context_info(&self) -> CryptoContextInfo { + CryptoContextInfo { device_creation_ts: MilliSecondsSinceUnixEpoch::from_system_time( SystemTime::now().sub(Duration::from_secs(60 * 3)), ) @@ -389,47 +387,39 @@ impl RoomDataProvider for TestRoomDataProvider { is_backup_configured: false, this_device_is_verified: true, backup_exists_on_server: true, - }) - .boxed() + } } - fn profile_from_user_id<'a>(&'a self, _user_id: &'a UserId) -> BoxFuture<'a, Option> { - ready(None).boxed() + async fn profile_from_user_id<'a>(&'a self, _user_id: &'a UserId) -> Option { + None } fn profile_from_latest_event(&self, _latest_event: &LatestEvent) -> Option { None } - fn load_user_receipt( + async fn load_user_receipt( &self, receipt_type: ReceiptType, thread: ReceiptThread, user_id: &UserId, - ) -> BoxFuture<'_, Option<(OwnedEventId, Receipt)>> { - ready( - self.initial_user_receipts - .get(&receipt_type) - .and_then(|thread_map| thread_map.get(&thread)) - .and_then(|user_map| user_map.get(user_id)) - .cloned(), - ) - .boxed() - } - - fn load_event_receipts( - &self, - event_id: &EventId, - ) -> BoxFuture<'_, IndexMap> { - ready(if event_id == event_id!("$event_with_bob_receipt") { + ) -> Option<(OwnedEventId, Receipt)> { + self.initial_user_receipts + .get(&receipt_type) + .and_then(|thread_map| thread_map.get(&thread)) + .and_then(|user_map| user_map.get(user_id)) + .cloned() + } + + async fn load_event_receipts(&self, event_id: &EventId) -> IndexMap { + if event_id == event_id!("$event_with_bob_receipt") { [(BOB.to_owned(), Receipt::new(MilliSecondsSinceUnixEpoch(uint!(10))))].into() } else { IndexMap::new() - }) - .boxed() + } } - fn push_rules_and_context(&self) -> BoxFuture<'_, Option<(Ruleset, PushConditionRoomCtx)>> { + async fn push_rules_and_context(&self) -> Option<(Ruleset, PushConditionRoomCtx)> { let push_rules = Ruleset::server_default(&ALICE); let power_levels = PushConditionPowerLevelsCtx { users: BTreeMap::new(), @@ -444,32 +434,26 @@ impl RoomDataProvider for TestRoomDataProvider { power_levels: Some(power_levels), }; - ready(Some((push_rules, push_context))).boxed() + Some((push_rules, push_context)) } - fn load_fully_read_marker(&self) -> BoxFuture<'_, Option> { - ready(self.fully_read_marker.clone()).boxed() + async fn load_fully_read_marker(&self) -> Option { + self.fully_read_marker.clone() } - fn send(&self, content: AnyMessageLikeEventContent) -> BoxFuture<'_, Result<(), super::Error>> { - async move { - self.sent_events.write().await.push(content); - Ok(()) - } - .boxed() + async fn send(&self, content: AnyMessageLikeEventContent) -> Result<(), super::Error> { + self.sent_events.write().await.push(content); + Ok(()) } - fn redact<'a>( + async fn redact<'a>( &'a self, event_id: &'a EventId, _reason: Option<&'a str>, _transaction_id: Option, - ) -> BoxFuture<'a, Result<(), super::Error>> { - async move { - self.redacted.write().await.push(event_id.to_owned()); - Ok(()) - } - .boxed() + ) -> Result<(), super::Error> { + self.redacted.write().await.push(event_id.to_owned()); + Ok(()) } fn room_info(&self) -> Subscriber { diff --git a/crates/matrix-sdk-ui/src/timeline/traits.rs b/crates/matrix-sdk-ui/src/timeline/traits.rs index b865ff8e33..55cae54db5 100644 --- a/crates/matrix-sdk-ui/src/timeline/traits.rs +++ b/crates/matrix-sdk-ui/src/timeline/traits.rs @@ -15,14 +15,12 @@ use std::future::Future; use eyeball::Subscriber; -use futures_util::FutureExt as _; use indexmap::IndexMap; #[cfg(test)] use matrix_sdk::crypto::{DecryptionSettings, RoomEventDecryptionResult, TrustRequirement}; use matrix_sdk::{ crypto::types::events::CryptoContextInfo, deserialized_responses::TimelineEvent, - event_cache::paginator::PaginableRoom, AsyncTraitDeps, BoxFuture, Result, Room, - SendOutsideWasm, + event_cache::paginator::PaginableRoom, AsyncTraitDeps, Result, Room, SendOutsideWasm, }; use matrix_sdk_base::{latest_event::LatestEvent, RoomInfo}; use ruma::{ @@ -78,9 +76,13 @@ pub(super) trait RoomDataProvider: fn own_user_id(&self) -> &UserId; fn room_version(&self) -> RoomVersionId; - fn crypto_context_info(&self) -> BoxFuture<'_, CryptoContextInfo>; + fn crypto_context_info(&self) + -> impl Future + SendOutsideWasm + '_; - fn profile_from_user_id<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, Option>; + fn profile_from_user_id<'a>( + &'a self, + user_id: &'a UserId, + ) -> impl Future> + SendOutsideWasm + 'a; fn profile_from_latest_event(&self, latest_event: &LatestEvent) -> Option; /// Loads a user receipt from the storage backend. @@ -89,21 +91,26 @@ pub(super) trait RoomDataProvider: receipt_type: ReceiptType, thread: ReceiptThread, user_id: &'a UserId, - ) -> BoxFuture<'a, Option<(OwnedEventId, Receipt)>>; + ) -> impl Future> + SendOutsideWasm + 'a; /// Loads read receipts for an event from the storage backend. fn load_event_receipts<'a>( &'a self, event_id: &'a EventId, - ) -> BoxFuture<'a, IndexMap>; + ) -> impl Future> + SendOutsideWasm + 'a; /// Load the current fully-read event id, from storage. - fn load_fully_read_marker(&self) -> BoxFuture<'_, Option>; + fn load_fully_read_marker(&self) -> impl Future> + '_; - fn push_rules_and_context(&self) -> BoxFuture<'_, Option<(Ruleset, PushConditionRoomCtx)>>; + fn push_rules_and_context( + &self, + ) -> impl Future> + SendOutsideWasm + '_; /// Send an event to that room. - fn send(&self, content: AnyMessageLikeEventContent) -> BoxFuture<'_, Result<(), super::Error>>; + fn send( + &self, + content: AnyMessageLikeEventContent, + ) -> impl Future> + SendOutsideWasm + '_; /// Redact an event from that room. fn redact<'a>( @@ -111,7 +118,7 @@ pub(super) trait RoomDataProvider: event_id: &'a EventId, reason: Option<&'a str>, transaction_id: Option, - ) -> BoxFuture<'a, Result<(), super::Error>>; + ) -> impl Future> + SendOutsideWasm + 'a; fn room_info(&self) -> Subscriber; } @@ -125,27 +132,24 @@ impl RoomDataProvider for Room { (**self).clone_info().room_version_or_default() } - fn crypto_context_info(&self) -> BoxFuture<'_, CryptoContextInfo> { - async move { self.crypto_context_info().await }.boxed() + async fn crypto_context_info(&self) -> CryptoContextInfo { + self.crypto_context_info().await } - fn profile_from_user_id<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, Option> { - async move { - match self.get_member_no_sync(user_id).await { - Ok(Some(member)) => Some(Profile { - display_name: member.display_name().map(ToOwned::to_owned), - display_name_ambiguous: member.name_ambiguous(), - avatar_url: member.avatar_url().map(ToOwned::to_owned), - }), - Ok(None) if self.are_members_synced() => Some(Profile::default()), - Ok(None) => None, - Err(e) => { - error!(%user_id, "Failed to fetch room member information: {e}"); - None - } + async fn profile_from_user_id<'a>(&'a self, user_id: &'a UserId) -> Option { + match self.get_member_no_sync(user_id).await { + Ok(Some(member)) => Some(Profile { + display_name: member.display_name().map(ToOwned::to_owned), + display_name_ambiguous: member.name_ambiguous(), + avatar_url: member.avatar_url().map(ToOwned::to_owned), + }), + Ok(None) if self.are_members_synced() => Some(Profile::default()), + Ok(None) => None, + Err(e) => { + error!(%user_id, "Failed to fetch room member information: {e}"); + None } } - .boxed() } fn profile_from_latest_event(&self, latest_event: &LatestEvent) -> Option { @@ -160,128 +164,110 @@ impl RoomDataProvider for Room { }) } - fn load_user_receipt<'a>( + async fn load_user_receipt<'a>( &'a self, receipt_type: ReceiptType, thread: ReceiptThread, user_id: &'a UserId, - ) -> BoxFuture<'a, Option<(OwnedEventId, Receipt)>> { - async move { - match self.load_user_receipt(receipt_type.clone(), thread.clone(), user_id).await { - Ok(receipt) => receipt, - Err(e) => { - error!( - ?receipt_type, - ?thread, - ?user_id, - "Failed to get read receipt for user: {e}" - ); - None - } + ) -> Option<(OwnedEventId, Receipt)> { + match self.load_user_receipt(receipt_type.clone(), thread.clone(), user_id).await { + Ok(receipt) => receipt, + Err(e) => { + error!( + ?receipt_type, + ?thread, + ?user_id, + "Failed to get read receipt for user: {e}" + ); + None } } - .boxed() } - fn load_event_receipts<'a>( + async fn load_event_receipts<'a>( &'a self, event_id: &'a EventId, - ) -> BoxFuture<'a, IndexMap> { - async move { - let mut unthreaded_receipts = match self - .load_event_receipts(ReceiptType::Read, ReceiptThread::Unthreaded, event_id) - .await - { - Ok(receipts) => receipts.into_iter().collect(), - Err(e) => { - error!(?event_id, "Failed to get unthreaded read receipts for event: {e}"); - IndexMap::new() - } - }; + ) -> IndexMap { + let mut unthreaded_receipts = match self + .load_event_receipts(ReceiptType::Read, ReceiptThread::Unthreaded, event_id) + .await + { + Ok(receipts) => receipts.into_iter().collect(), + Err(e) => { + error!(?event_id, "Failed to get unthreaded read receipts for event: {e}"); + IndexMap::new() + } + }; - let main_thread_receipts = match self - .load_event_receipts(ReceiptType::Read, ReceiptThread::Main, event_id) - .await - { - Ok(receipts) => receipts, - Err(e) => { - error!(?event_id, "Failed to get main thread read receipts for event: {e}"); - Vec::new() - } - }; + let main_thread_receipts = match self + .load_event_receipts(ReceiptType::Read, ReceiptThread::Main, event_id) + .await + { + Ok(receipts) => receipts, + Err(e) => { + error!(?event_id, "Failed to get main thread read receipts for event: {e}"); + Vec::new() + } + }; - unthreaded_receipts.extend(main_thread_receipts); - unthreaded_receipts - } - .boxed() + unthreaded_receipts.extend(main_thread_receipts); + unthreaded_receipts } - fn push_rules_and_context(&self) -> BoxFuture<'_, Option<(Ruleset, PushConditionRoomCtx)>> { - async { - match self.push_context().await { - Ok(Some(push_context)) => match self.client().account().push_rules().await { - Ok(push_rules) => Some((push_rules, push_context)), - Err(e) => { - error!("Could not get push rules: {e}"); - None - } - }, - Ok(None) => { - debug!("Could not aggregate push context"); - None - } + async fn push_rules_and_context(&self) -> Option<(Ruleset, PushConditionRoomCtx)> { + match self.push_context().await { + Ok(Some(push_context)) => match self.client().account().push_rules().await { + Ok(push_rules) => Some((push_rules, push_context)), Err(e) => { - error!("Could not get push context: {e}"); + error!("Could not get push rules: {e}"); None } + }, + Ok(None) => { + debug!("Could not aggregate push context"); + None + } + Err(e) => { + error!("Could not get push context: {e}"); + None } } - .boxed() } - fn load_fully_read_marker(&self) -> BoxFuture<'_, Option> { - async { - match self.account_data_static::().await { - Ok(Some(fully_read)) => match fully_read.deserialize() { - Ok(fully_read) => Some(fully_read.content.event_id), - Err(e) => { - error!("Failed to deserialize fully-read account data: {e}"); - None - } - }, + async fn load_fully_read_marker(&self) -> Option { + match self.account_data_static::().await { + Ok(Some(fully_read)) => match fully_read.deserialize() { + Ok(fully_read) => Some(fully_read.content.event_id), Err(e) => { - error!("Failed to get fully-read account data from the store: {e}"); + error!("Failed to deserialize fully-read account data: {e}"); None } - _ => None, + }, + Err(e) => { + error!("Failed to get fully-read account data from the store: {e}"); + None } + _ => None, } - .boxed() } - fn send(&self, content: AnyMessageLikeEventContent) -> BoxFuture<'_, Result<(), super::Error>> { - async move { - let _ = self.send_queue().send(content).await?; - Ok(()) - } - .boxed() + async fn send(&self, content: AnyMessageLikeEventContent) -> Result<(), super::Error> { + let _ = self.send_queue().send(content).await?; + Ok(()) } - fn redact<'a>( + async fn redact<'a>( &'a self, event_id: &'a EventId, reason: Option<&'a str>, transaction_id: Option, - ) -> BoxFuture<'a, Result<(), super::Error>> { - async move { - let _ = self - .redact(event_id, reason, transaction_id) - .await - .map_err(RedactError::HttpError) - .map_err(super::Error::RedactError)?; - Ok(()) - } - .boxed() + ) -> Result<(), super::Error> { + let _ = self + .redact(event_id, reason, transaction_id) + .await + .map_err(RedactError::HttpError) + .map_err(super::Error::RedactError)?; + Ok(()) } fn room_info(&self) -> Subscriber {