Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bindings/matrix-sdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ oauth2.workspace = true
[target.'cfg(target_family = "wasm")'.dependencies]
console_error_panic_hook = "0.1.7"
tokio = { workspace = true, features = ["sync", "macros"] }
uniffi.workspace = true
uniffi = { workspace = true, features = ["wasm-unstable-single-threaded"] }
futures-executor.workspace = true

[target.'cfg(not(target_family = "wasm"))'.dependencies]
async-compat.workspace = true
Expand Down
127 changes: 84 additions & 43 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,6 @@ impl Client {
}
}

#[cfg(not(target_family = "wasm"))]
#[matrix_sdk_ffi_macros::export]
impl Client {
/// Retrieves a media file from the media source
Expand All @@ -942,22 +941,60 @@ impl Client {
use_cache: bool,
temp_dir: Option<String>,
) -> Result<Arc<MediaFileHandle>, ClientError> {
let source = (*media_source).clone();
let mime_type: mime::Mime = mime_type.parse()?;
#[cfg(not(target_family = "wasm"))]
{
let source = (*media_source).clone();
let mime_type: mime::Mime = mime_type.parse()?;

let handle = self
.inner
.media()
.get_media_file(
&MediaRequestParameters { source: source.media_source, format: MediaFormat::File },
filename,
&mime_type,
use_cache,
temp_dir,
)
.await?;
let handle = self
.inner
.media()
.get_media_file(
&MediaRequestParameters {
source: source.media_source,
format: MediaFormat::File,
},
filename,
&mime_type,
use_cache,
temp_dir,
)
.await?;

Ok(Arc::new(MediaFileHandle::new(handle)))
}

/// MediaFileHandle uses SdkMediaFileHandle which requires an
/// intermediate TempFile which is not available on wasm
/// platforms due to lack of an accessible file system.
#[cfg(target_family = "wasm")]
Err(ClientError::Generic {
msg: "get_media_file is not supported on wasm platforms".to_owned(),
details: None,
})
}

pub async fn set_display_name(&self, name: String) -> Result<(), ClientError> {
#[cfg(not(target_family = "wasm"))]
{
self.inner
.account()
.set_display_name(Some(name.as_str()))
.await
.context("Unable to set display name")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't we want to just get rid of the context() call here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context call still works for non wasm platforms.

So what it now does is use map_err on wasm and context everywhere else. That way it should be (almost) the same for both cases in behaviour. Of course map_err isn't identical to context (especially due to just using the to_string() message on wasm) but it still generally ensures the information is the same on both.

But I can also just remove it in general I guess. I just thought it might make sense to preserve the context on existing bindings basically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also its not disbled on wasm anymore. It does actually execute the action on wasm now. It just does different error handling.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry I misread this part. Thanks for pointing this out.

}

#[cfg(target_family = "wasm")]
{
self.inner.account().set_display_name(Some(name.as_str())).await.map_err(|e| {
ClientError::Generic {
msg: "Unable to set display name".to_owned(),
details: Some(e.to_string()),
}
})?;
}

Ok(Arc::new(MediaFileHandle::new(handle)))
Ok(())
}
}

Expand Down Expand Up @@ -1093,15 +1130,6 @@ impl Client {
Ok(display_name)
}

pub async fn set_display_name(&self, name: String) -> Result<(), ClientError> {
self.inner
.account()
.set_display_name(Some(name.as_str()))
.await
.context("Unable to set display name")?;
Ok(())
}

pub async fn upload_avatar(&self, mime_type: String, data: Vec<u8>) -> Result<(), ClientError> {
let mime: Mime = mime_type.parse()?;
self.inner.account().upload_avatar(&mime, data).await?;
Expand Down Expand Up @@ -2453,25 +2481,25 @@ fn gen_transaction_id() -> String {

/// A file handle that takes ownership of a media file on disk. When the handle
/// is dropped, the file will be removed from the disk.
#[cfg(not(target_family = "wasm"))]
#[derive(uniffi::Object)]
pub struct MediaFileHandle {
#[cfg(not(target_family = "wasm"))]
inner: std::sync::RwLock<Option<SdkMediaFileHandle>>,
}

#[cfg(not(target_family = "wasm"))]
impl MediaFileHandle {
#[cfg(not(target_family = "wasm"))]
fn new(handle: SdkMediaFileHandle) -> Self {
Self { inner: std::sync::RwLock::new(Some(handle)) }
}
}

#[cfg(not(target_family = "wasm"))]
#[matrix_sdk_ffi_macros::export]
impl MediaFileHandle {
/// Get the media file's path.
pub fn path(&self) -> Result<String, ClientError> {
Ok(self
#[cfg(not(target_family = "wasm"))]
return Ok(self
.inner
.read()
.unwrap()
Expand All @@ -2480,24 +2508,37 @@ impl MediaFileHandle {
.path()
.to_str()
.unwrap()
.to_owned())
.to_owned());
#[cfg(target_family = "wasm")]
Err(ClientError::Generic {
msg: "MediaFileHandle.path() is not supported on WASM targets".to_string(),
details: None,
})
}

pub fn persist(&self, path: String) -> Result<bool, ClientError> {
let mut guard = self.inner.write().unwrap();
Ok(
match guard
.take()
.context("MediaFileHandle was already persisted")?
.persist(path.as_ref())
{
Ok(_) => true,
Err(e) => {
*guard = Some(e.file);
false
}
},
)
#[cfg(not(target_family = "wasm"))]
{
let mut guard = self.inner.write().unwrap();
Ok(
match guard
.take()
.context("MediaFileHandle was already persisted")?
.persist(path.as_ref())
{
Ok(_) => true,
Err(e) => {
*guard = Some(e.file);
false
}
},
)
}
#[cfg(target_family = "wasm")]
Err(ClientError::Generic {
msg: "MediaFileHandle.persist() is not supported on WASM targets".to_string(),
details: None,
})
}
}

Expand Down
32 changes: 26 additions & 6 deletions bindings/matrix-sdk-ffi/src/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,20 @@ impl ClientBuilder {
system_is_memory_constrained: false,
username: None,
homeserver_cfg: None,
#[cfg(not(target_family = "wasm"))]
user_agent: None,
sliding_sync_version_builder: SlidingSyncVersionBuilder::None,
#[cfg(not(target_family = "wasm"))]
proxy: None,
#[cfg(not(target_family = "wasm"))]
disable_ssl_verification: false,
disable_automatic_token_refresh: false,
cross_process_store_locks_holder_name: None,
enable_oidc_refresh_lock: false,
session_delegate: None,
#[cfg(not(target_family = "wasm"))]
additional_root_certificates: Default::default(),
#[cfg(not(target_family = "wasm"))]
disable_built_in_root_certificates: false,
encryption_settings: EncryptionSettings {
auto_enable_cross_signing: false,
Expand Down Expand Up @@ -559,18 +564,23 @@ impl ClientBuilder {
}
}

#[cfg(not(target_family = "wasm"))]
#[matrix_sdk_ffi_macros::export]
impl ClientBuilder {
pub fn proxy(self: Arc<Self>, url: String) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.proxy = Some(url);
#[cfg(not(target_family = "wasm"))]
{
builder.proxy = Some(url);
}
Arc::new(builder)
}

pub fn disable_ssl_verification(self: Arc<Self>) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.disable_ssl_verification = true;
#[cfg(not(target_family = "wasm"))]
{
builder.disable_ssl_verification = true;
}
Arc::new(builder)
}

Expand All @@ -579,7 +589,11 @@ impl ClientBuilder {
certificates: Vec<CertificateBytes>,
) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.additional_root_certificates = certificates;

#[cfg(not(target_family = "wasm"))]
{
builder.additional_root_certificates = certificates;
}

Arc::new(builder)
}
Expand All @@ -589,13 +603,19 @@ impl ClientBuilder {
/// [`add_root_certificates`][ClientBuilder::add_root_certificates].
pub fn disable_built_in_root_certificates(self: Arc<Self>) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.disable_built_in_root_certificates = true;
#[cfg(not(target_family = "wasm"))]
{
builder.disable_built_in_root_certificates = true;
}
Arc::new(builder)
}

pub fn user_agent(self: Arc<Self>, user_agent: String) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.user_agent = Some(user_agent);
#[cfg(not(target_family = "wasm"))]
{
builder.user_agent = Some(user_agent);
}
Arc::new(builder)
}
}
Expand Down
Loading