Revert "ffi: Spawn tokio tasks for the remaining async fns"

This reverts commit eb4dab138e.
This commit is contained in:
Jonas Platte
2023-08-02 11:58:33 +02:00
committed by Jonas Platte
parent 34be01ecf2
commit b39d06353a
4 changed files with 185 additions and 334 deletions

View File

@@ -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>,
&self,
room_id: String,
is_encrypted: bool,
active_members_count: u64,
) -> Result<RoomNotificationSettings, NotificationSettingsError> {
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>,
&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>,
&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>,
&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<Self>) -> 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<Self>,
) -> Result<bool, NotificationSettingsError> {
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<bool, NotificationSettingsError> {
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>,
&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<Self>,
) -> Result<bool, NotificationSettingsError> {
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<bool, NotificationSettingsError> {
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>,
&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<Self>) -> Result<bool, NotificationSettingsError> {
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<bool, NotificationSettingsError> {
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<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::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>,
&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(())
}
}

View File

@@ -227,17 +227,18 @@ impl Room {
}
pub async fn add_timeline_listener(
self: Arc<Self>,
&self,
listener: Box<dyn TimelineListener>,
) -> 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<Self>, user_id: String) -> Result<bool, ClientError> {
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<bool, ClientError> {
let user_id = UserId::parse(&user_id)?;
Ok(self.inner.can_user_redact(&user_id).await?)
}
pub async fn can_user_ban(self: Arc<Self>, user_id: String) -> Result<bool, ClientError> {
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<bool, ClientError> {
let user_id = UserId::parse(&user_id)?;
Ok(self.inner.can_user_ban(&user_id).await?)
}
pub async fn can_user_invite(self: Arc<Self>, user_id: String) -> Result<bool, ClientError> {
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<bool, ClientError> {
let user_id = UserId::parse(&user_id)?;
Ok(self.inner.can_user_invite(&user_id).await?)
}
pub async fn can_user_kick(self: Arc<Self>, user_id: String) -> Result<bool, ClientError> {
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<bool, ClientError> {
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>,
&self,
user_id: String,
state_event: StateEventType,
) -> Result<bool, ClientError> {
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>,
&self,
user_id: String,
message: MessageLikeEventType,
) -> Result<bool, ClientError> {
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>,
&self,
user_id: String,
) -> Result<bool, ClientError> {
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<JoinHandle<Result<(), RoomError>>>,
join_hdl: Arc<Mutex<JoinHandle<Result<(), RoomError>>>>,
abort_hdl: AbortHandle,
}
impl SendAttachmentJoinHandle {
fn new(join_hdl: JoinHandle<Result<(), RoomError>>) -> Arc<Self> {
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<Self>) -> 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) {

View File

@@ -93,36 +93,21 @@ impl RoomListService {
}
async fn all_rooms(self: Arc<Self>) -> Result<Arc<RoomList>, 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<Self>) -> Result<Arc<RoomList>, 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<Self>, 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)
}
}

View File

@@ -61,94 +61,69 @@ impl SessionVerificationController {
*self.delegate.write().unwrap() = delegate;
}
pub async fn request_verification(self: Arc<Self>) -> 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<Self>) -> 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<Self>) -> 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<Self>) -> 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<Self>) -> 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(())
}
}