From b39d06353aa10783aeaf56589b1da3bbc1d7891b Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 2 Aug 2023 11:58:33 +0200 Subject: [PATCH] Revert "ffi: Spawn tokio tasks for the remaining async fns" This reverts commit eb4dab138e6ae791b2fc7a9b76bed1b5423c572f. --- .../src/notification_settings.rs | 271 +++++++----------- bindings/matrix-sdk-ffi/src/room.rs | 98 ++----- bindings/matrix-sdk-ffi/src/room_list.rs | 35 +-- .../src/session_verification.rs | 115 +++----- 4 files changed, 185 insertions(+), 334 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/notification_settings.rs b/bindings/matrix-sdk-ffi/src/notification_settings.rs index aa292f4cc..1d37f3cdc 100644 --- a/bindings/matrix-sdk-ffi/src/notification_settings.rs +++ b/bindings/matrix-sdk-ffi/src/notification_settings.rs @@ -140,53 +140,40 @@ impl NotificationSettings { /// * `active_members_count` - the room's active members count (joined + /// invited) pub async fn get_room_notification_settings( - self: Arc, + &self, room_id: String, is_encrypted: bool, active_members_count: u64, ) -> Result { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - let parsed_room_id = RoomId::parse(&room_id) - .map_err(|_e| NotificationSettingsError::InvalidRoomId(room_id))?; - // Get the current user defined mode for this room - if let Some(mode) = notification_settings - .get_user_defined_room_notification_mode(&parsed_room_id) - .await - { - return Ok(RoomNotificationSettings::new(mode.into(), false)); - } + let notification_settings = self.sdk_notification_settings.read().await; + let parsed_room_id = RoomId::parse(&room_id) + .map_err(|_e| NotificationSettingsError::InvalidRoomId(room_id))?; + // Get the current user defined mode for this room + if let Some(mode) = + notification_settings.get_user_defined_room_notification_mode(&parsed_room_id).await + { + return Ok(RoomNotificationSettings::new(mode.into(), false)); + } - // If the user has not defined a notification mode, return the default one for - // this room - let mode = notification_settings - .get_default_room_notification_mode(is_encrypted, active_members_count) - .await; - Ok(RoomNotificationSettings::new(mode.into(), true)) - }) - .await - .unwrap() + // If the user has not defined a notification mode, return the default one for + // this room + let mode = notification_settings + .get_default_room_notification_mode(is_encrypted, active_members_count) + .await; + Ok(RoomNotificationSettings::new(mode.into(), true)) } /// Sets the notification mode for a room. pub async fn set_room_notification_mode( - self: Arc, + &self, room_id: String, mode: RoomNotificationMode, ) -> Result<(), NotificationSettingsError> { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - let parsed_room_idom_id = RoomId::parse(&room_id) - .map_err(|_e| NotificationSettingsError::InvalidRoomId(room_id))?; - notification_settings - .set_room_notification_mode(&parsed_room_idom_id, mode.into()) - .await?; - Ok(()) - }) - .await - .unwrap() + let notification_settings = self.sdk_notification_settings.read().await; + let parsed_room_idom_id = RoomId::parse(&room_id) + .map_err(|_e| NotificationSettingsError::InvalidRoomId(room_id))?; + notification_settings.set_room_notification_mode(&parsed_room_idom_id, mode.into()).await?; + Ok(()) } /// Get the default room notification mode @@ -200,186 +187,126 @@ impl NotificationSettings { /// * `active_members_count` - the room's active members count (joined + /// invited) pub async fn get_default_room_notification_mode( - self: Arc, + &self, is_encrypted: bool, active_members_count: u64, ) -> RoomNotificationMode { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - let mode = notification_settings - .get_default_room_notification_mode(is_encrypted, active_members_count) - .await; - mode.into() - }) - .await - .unwrap() + let notification_settings = self.sdk_notification_settings.read().await; + let mode = notification_settings + .get_default_room_notification_mode(is_encrypted, active_members_count) + .await; + mode.into() } /// Restores the default notification mode for a room pub async fn restore_default_room_notification_mode( - self: Arc, + &self, room_id: String, ) -> Result<(), NotificationSettingsError> { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - let parsed_room_idom_id = RoomId::parse(&room_id) - .map_err(|_e| NotificationSettingsError::InvalidRoomId(room_id))?; - notification_settings.delete_user_defined_room_rules(&parsed_room_idom_id).await?; - Ok(()) - }) - .await - .unwrap() + let notification_settings = self.sdk_notification_settings.read().await; + let parsed_room_idom_id = RoomId::parse(&room_id) + .map_err(|_e| NotificationSettingsError::InvalidRoomId(room_id))?; + notification_settings.delete_user_defined_room_rules(&parsed_room_idom_id).await?; + Ok(()) } /// Get whether some enabled keyword rules exist. - pub async fn contains_keywords_rules(self: Arc) -> bool { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - notification_settings.contains_keyword_rules().await - }) - .await - .unwrap() + pub async fn contains_keywords_rules(&self) -> bool { + let notification_settings = self.sdk_notification_settings.read().await; + notification_settings.contains_keyword_rules().await } /// Get whether room mentions are enabled. - pub async fn is_room_mention_enabled( - self: Arc, - ) -> Result { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - let enabled = notification_settings - .is_push_rule_enabled( - RuleKind::Override, - PredefinedOverrideRuleId::IsRoomMention.as_str(), - ) - .await?; - Ok(enabled) - }) - .await - .unwrap() + pub async fn is_room_mention_enabled(&self) -> Result { + let notification_settings = self.sdk_notification_settings.read().await; + let enabled = notification_settings + .is_push_rule_enabled( + RuleKind::Override, + PredefinedOverrideRuleId::IsRoomMention.as_str(), + ) + .await?; + Ok(enabled) } /// Set whether room mentions are enabled. pub async fn set_room_mention_enabled( - self: Arc, + &self, enabled: bool, ) -> Result<(), NotificationSettingsError> { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - notification_settings - .set_push_rule_enabled( - RuleKind::Override, - PredefinedOverrideRuleId::IsRoomMention.as_str(), - enabled, - ) - .await?; - Ok(()) - }) - .await - .unwrap() + let notification_settings = self.sdk_notification_settings.read().await; + notification_settings + .set_push_rule_enabled( + RuleKind::Override, + PredefinedOverrideRuleId::IsRoomMention.as_str(), + enabled, + ) + .await?; + Ok(()) } /// Get whether user mentions are enabled. - pub async fn is_user_mention_enabled( - self: Arc, - ) -> Result { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - let enabled = notification_settings - .is_push_rule_enabled( - RuleKind::Override, - PredefinedOverrideRuleId::IsUserMention.as_str(), - ) - .await?; - Ok(enabled) - }) - .await - .unwrap() + pub async fn is_user_mention_enabled(&self) -> Result { + let notification_settings = self.sdk_notification_settings.read().await; + let enabled = notification_settings + .is_push_rule_enabled( + RuleKind::Override, + PredefinedOverrideRuleId::IsUserMention.as_str(), + ) + .await?; + Ok(enabled) } /// Set whether user mentions are enabled. pub async fn set_user_mention_enabled( - self: Arc, + &self, enabled: bool, ) -> Result<(), NotificationSettingsError> { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - notification_settings - .set_push_rule_enabled( - RuleKind::Override, - PredefinedOverrideRuleId::IsUserMention.as_str(), - enabled, - ) - .await?; - Ok(()) - }) - .await - .unwrap() + let notification_settings = self.sdk_notification_settings.read().await; + notification_settings + .set_push_rule_enabled( + RuleKind::Override, + PredefinedOverrideRuleId::IsUserMention.as_str(), + enabled, + ) + .await?; + Ok(()) } /// Get whether the `.m.rule.call` push rule is enabled - pub async fn is_call_enabled(self: Arc) -> Result { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - let enabled = notification_settings - .is_push_rule_enabled( - RuleKind::Underride, - PredefinedUnderrideRuleId::Call.as_str(), - ) - .await?; - Ok(enabled) - }) - .await - .unwrap() + pub async fn is_call_enabled(&self) -> Result { + let notification_settings = self.sdk_notification_settings.read().await; + let enabled = notification_settings + .is_push_rule_enabled(RuleKind::Underride, PredefinedUnderrideRuleId::Call.as_str()) + .await?; + Ok(enabled) } /// Set whether the `.m.rule.call` push rule is enabled - pub async fn set_call_enabled( - self: Arc, - enabled: bool, - ) -> Result<(), NotificationSettingsError> { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - notification_settings - .set_push_rule_enabled( - RuleKind::Underride, - PredefinedUnderrideRuleId::Call.as_str(), - enabled, - ) - .await?; - Ok(()) - }) - .await - .unwrap() + pub async fn set_call_enabled(&self, enabled: bool) -> Result<(), NotificationSettingsError> { + let notification_settings = self.sdk_notification_settings.read().await; + notification_settings + .set_push_rule_enabled( + RuleKind::Underride, + PredefinedUnderrideRuleId::Call.as_str(), + enabled, + ) + .await?; + Ok(()) } /// Unmute a room. pub async fn unmute_room( - self: Arc, + &self, room_id: String, is_encrypted: bool, members_count: u64, ) -> Result<(), NotificationSettingsError> { - RUNTIME - .spawn(async move { - let notification_settings = self.sdk_notification_settings.read().await; - let parsed_room_idom_id = RoomId::parse(&room_id) - .map_err(|_e| NotificationSettingsError::InvalidRoomId(room_id))?; - notification_settings - .unmute_room(&parsed_room_idom_id, is_encrypted, members_count) - .await?; - Ok(()) - }) - .await - .unwrap() + let notification_settings = self.sdk_notification_settings.read().await; + let parsed_room_idom_id = RoomId::parse(&room_id) + .map_err(|_e| NotificationSettingsError::InvalidRoomId(room_id))?; + notification_settings + .unmute_room(&parsed_room_idom_id, is_encrypted, members_count) + .await?; + Ok(()) } } diff --git a/bindings/matrix-sdk-ffi/src/room.rs b/bindings/matrix-sdk-ffi/src/room.rs index d1bd41805..5eed4dc24 100644 --- a/bindings/matrix-sdk-ffi/src/room.rs +++ b/bindings/matrix-sdk-ffi/src/room.rs @@ -227,17 +227,18 @@ impl Room { } pub async fn add_timeline_listener( - self: Arc, + &self, listener: Box, ) -> RoomTimelineListenerResult { + let timeline = self.timeline.clone(); + let room = self.inner.clone(); RUNTIME .spawn(async move { - let timeline = self - .timeline + let timeline = timeline .write() .await .get_or_insert_with(|| { - let timeline = RUNTIME.block_on(self.inner.timeline()); + let timeline = RUNTIME.block_on(room.timeline()); Arc::new(timeline) }) .clone(); @@ -806,85 +807,50 @@ impl Room { }) } - pub async fn can_user_redact(self: Arc, user_id: String) -> Result { - RUNTIME - .spawn(async move { - let user_id = UserId::parse(&user_id)?; - Ok(self.inner.can_user_redact(&user_id).await?) - }) - .await - .unwrap() + pub async fn can_user_redact(&self, user_id: String) -> Result { + let user_id = UserId::parse(&user_id)?; + Ok(self.inner.can_user_redact(&user_id).await?) } - pub async fn can_user_ban(self: Arc, user_id: String) -> Result { - RUNTIME - .spawn(async move { - let user_id = UserId::parse(&user_id)?; - Ok(self.inner.can_user_ban(&user_id).await?) - }) - .await - .unwrap() + pub async fn can_user_ban(&self, user_id: String) -> Result { + let user_id = UserId::parse(&user_id)?; + Ok(self.inner.can_user_ban(&user_id).await?) } - pub async fn can_user_invite(self: Arc, user_id: String) -> Result { - RUNTIME - .spawn(async move { - let user_id = UserId::parse(&user_id)?; - Ok(self.inner.can_user_invite(&user_id).await?) - }) - .await - .unwrap() + pub async fn can_user_invite(&self, user_id: String) -> Result { + let user_id = UserId::parse(&user_id)?; + Ok(self.inner.can_user_invite(&user_id).await?) } - pub async fn can_user_kick(self: Arc, user_id: String) -> Result { - RUNTIME - .spawn(async move { - let user_id = UserId::parse(&user_id)?; - Ok(self.inner.can_user_kick(&user_id).await?) - }) - .await - .unwrap() + pub async fn can_user_kick(&self, user_id: String) -> Result { + let user_id = UserId::parse(&user_id)?; + Ok(self.inner.can_user_kick(&user_id).await?) } pub async fn can_user_send_state( - self: Arc, + &self, user_id: String, state_event: StateEventType, ) -> Result { - RUNTIME - .spawn(async move { - let user_id = UserId::parse(&user_id)?; - Ok(self.inner.can_user_send_state(&user_id, state_event.into()).await?) - }) - .await - .unwrap() + let user_id = UserId::parse(&user_id)?; + Ok(self.inner.can_user_send_state(&user_id, state_event.into()).await?) } pub async fn can_user_send_message( - self: Arc, + &self, user_id: String, message: MessageLikeEventType, ) -> Result { - RUNTIME - .spawn(async move { - let user_id = UserId::parse(&user_id)?; - Ok(self.inner.can_user_send_message(&user_id, message.into()).await?) - }) - .await - .unwrap() + let user_id = UserId::parse(&user_id)?; + Ok(self.inner.can_user_send_message(&user_id, message.into()).await?) } pub async fn can_user_trigger_room_notification( - self: Arc, + &self, user_id: String, ) -> Result { - RUNTIME - .spawn(async move { - let user_id = UserId::parse(&user_id)?; - Ok(self.inner.can_user_trigger_room_notification(&user_id).await?) - }) - .await - .unwrap() + let user_id = UserId::parse(&user_id)?; + Ok(self.inner.can_user_trigger_room_notification(&user_id).await?) } pub fn own_user_id(&self) -> String { @@ -949,25 +915,23 @@ impl Room { #[derive(uniffi::Object)] pub struct SendAttachmentJoinHandle { - join_hdl: Mutex>>, + join_hdl: Arc>>>, abort_hdl: AbortHandle, } impl SendAttachmentJoinHandle { fn new(join_hdl: JoinHandle>) -> Arc { let abort_hdl = join_hdl.abort_handle(); - let join_hdl = Mutex::new(join_hdl); + let join_hdl = Arc::new(Mutex::new(join_hdl)); Arc::new(Self { join_hdl, abort_hdl }) } } #[uniffi::export(async_runtime = "tokio")] impl SendAttachmentJoinHandle { - pub async fn join(self: Arc) -> Result<(), RoomError> { - RUNTIME - .spawn(async move { (&mut *self.join_hdl.lock().await).await.unwrap() }) - .await - .unwrap() + pub async fn join(&self) -> Result<(), RoomError> { + let join_hdl = self.join_hdl.clone(); + RUNTIME.spawn(async move { (&mut *join_hdl.lock().await).await.unwrap() }).await.unwrap() } pub fn cancel(&self) { diff --git a/bindings/matrix-sdk-ffi/src/room_list.rs b/bindings/matrix-sdk-ffi/src/room_list.rs index d7696c269..6dc7057eb 100644 --- a/bindings/matrix-sdk-ffi/src/room_list.rs +++ b/bindings/matrix-sdk-ffi/src/room_list.rs @@ -93,36 +93,21 @@ impl RoomListService { } async fn all_rooms(self: Arc) -> Result, RoomListError> { - RUNTIME - .spawn(async move { - Ok(Arc::new(RoomList { - room_list_service: self.clone(), - inner: Arc::new(self.inner.all_rooms().await.map_err(RoomListError::from)?), - })) - }) - .await - .unwrap() + Ok(Arc::new(RoomList { + room_list_service: self.clone(), + inner: Arc::new(self.inner.all_rooms().await.map_err(RoomListError::from)?), + })) } async fn invites(self: Arc) -> Result, RoomListError> { - RUNTIME - .spawn(async move { - Ok(Arc::new(RoomList { - room_list_service: self.clone(), - inner: Arc::new(self.inner.invites().await.map_err(RoomListError::from)?), - })) - }) - .await - .unwrap() + Ok(Arc::new(RoomList { + room_list_service: self.clone(), + inner: Arc::new(self.inner.invites().await.map_err(RoomListError::from)?), + })) } - async fn apply_input(self: Arc, input: RoomListInput) -> Result<(), RoomListError> { - RUNTIME - .spawn(async move { - self.inner.apply_input(input.into()).await.map(|_| ()).map_err(Into::into) - }) - .await - .unwrap() + async fn apply_input(&self, input: RoomListInput) -> Result<(), RoomListError> { + self.inner.apply_input(input.into()).await.map(|_| ()).map_err(Into::into) } } diff --git a/bindings/matrix-sdk-ffi/src/session_verification.rs b/bindings/matrix-sdk-ffi/src/session_verification.rs index e16223ec0..1acefc2d6 100644 --- a/bindings/matrix-sdk-ffi/src/session_verification.rs +++ b/bindings/matrix-sdk-ffi/src/session_verification.rs @@ -61,94 +61,69 @@ impl SessionVerificationController { *self.delegate.write().unwrap() = delegate; } - pub async fn request_verification(self: Arc) -> Result<(), ClientError> { - RUNTIME - .spawn(async move { - let methods = vec![VerificationMethod::SasV1]; - let verification_request = self - .user_identity - .request_verification_with_methods(methods) - .await - .map_err(anyhow::Error::from)?; - *self.verification_request.write().unwrap() = Some(verification_request); - - Ok(()) - }) + pub async fn request_verification(&self) -> Result<(), ClientError> { + let methods = vec![VerificationMethod::SasV1]; + let verification_request = self + .user_identity + .request_verification_with_methods(methods) .await - .unwrap() + .map_err(anyhow::Error::from)?; + *self.verification_request.write().unwrap() = Some(verification_request); + + Ok(()) } - pub async fn start_sas_verification(self: Arc) -> Result<(), ClientError> { - RUNTIME - .spawn(async move { - let verification_request = self.verification_request.read().unwrap().clone(); + pub async fn start_sas_verification(&self) -> Result<(), ClientError> { + let verification_request = self.verification_request.read().unwrap().clone(); - if let Some(verification) = verification_request { - match verification.start_sas().await { - Ok(Some(verification)) => { - *self.sas_verification.write().unwrap() = Some(verification.clone()); + if let Some(verification) = verification_request { + match verification.start_sas().await { + Ok(Some(verification)) => { + *self.sas_verification.write().unwrap() = Some(verification.clone()); - if let Some(delegate) = &*self.delegate.read().unwrap() { - delegate.did_start_sas_verification() - } + if let Some(delegate) = &*self.delegate.read().unwrap() { + delegate.did_start_sas_verification() + } - let delegate = self.delegate.clone(); - RUNTIME.spawn(Self::listen_to_changes(delegate, verification)); - } - _ => { - if let Some(delegate) = &*self.delegate.read().unwrap() { - delegate.did_fail() - } - } + let delegate = self.delegate.clone(); + RUNTIME.spawn(Self::listen_to_changes(delegate, verification)); + } + _ => { + if let Some(delegate) = &*self.delegate.read().unwrap() { + delegate.did_fail() } } + } + } - Ok(()) - }) - .await - .unwrap() + Ok(()) } - pub async fn approve_verification(self: Arc) -> Result<(), ClientError> { - RUNTIME - .spawn(async move { - let sas_verification = self.sas_verification.read().unwrap().clone(); - if let Some(sas_verification) = sas_verification { - sas_verification.confirm().await?; - } + pub async fn approve_verification(&self) -> Result<(), ClientError> { + let sas_verification = self.sas_verification.read().unwrap().clone(); + if let Some(sas_verification) = sas_verification { + sas_verification.confirm().await?; + } - Ok(()) - }) - .await - .unwrap() + Ok(()) } - pub async fn decline_verification(self: Arc) -> Result<(), ClientError> { - RUNTIME - .spawn(async move { - let sas_verification = self.sas_verification.read().unwrap().clone(); - if let Some(sas_verification) = sas_verification { - sas_verification.mismatch().await?; - } + pub async fn decline_verification(&self) -> Result<(), ClientError> { + let sas_verification = self.sas_verification.read().unwrap().clone(); + if let Some(sas_verification) = sas_verification { + sas_verification.mismatch().await?; + } - Ok(()) - }) - .await - .unwrap() + Ok(()) } - pub async fn cancel_verification(self: Arc) -> Result<(), ClientError> { - RUNTIME - .spawn(async move { - let verification_request = self.verification_request.read().unwrap().clone(); - if let Some(verification) = verification_request { - verification.cancel().await?; - } + pub async fn cancel_verification(&self) -> Result<(), ClientError> { + let verification_request = self.verification_request.read().unwrap().clone(); + if let Some(verification) = verification_request { + verification.cancel().await?; + } - Ok(()) - }) - .await - .unwrap() + Ok(()) } }