Skip to content

Commit

Permalink
feat: make it possible to configure the state store as an in-memory s…
Browse files Browse the repository at this point in the history
…tore in FFI
  • Loading branch information
bnjbvr committed Jul 10, 2023
1 parent 708a7d9 commit 2225e8a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
8 changes: 4 additions & 4 deletions bindings/matrix-sdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ path = "../../crates/matrix-sdk"
default-features = false
features = [
"anyhow",
"experimental-sliding-sync",
"e2e-encryption",
"experimental-sliding-sync",
"markdown",
"rustls-tls", # note: differ from block below
"socks",
"rustls-tls",
"sqlite",
]

Expand All @@ -76,10 +76,10 @@ path = "../../crates/matrix-sdk"
default-features = false
features = [
"anyhow",
"experimental-sliding-sync",
"e2e-encryption",
"experimental-sliding-sync",
"markdown",
"native-tls",
"native-tls", # note: differ from block above
"socks",
"sqlite",
]
25 changes: 23 additions & 2 deletions bindings/matrix-sdk-ffi/src/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::{fs, path::PathBuf, sync::Arc};

use anyhow::anyhow;
use matrix_sdk::{
config::StoreConfig,
ruma::{
api::{error::UnknownVersionError, MatrixVersion},
ServerName, UserId,
},
Client as MatrixClient, ClientBuilder as MatrixClientBuilder,
Client as MatrixClient, ClientBuilder as MatrixClientBuilder, MemoryStore, SqliteCryptoStore,
};
use sanitize_filename_reader_friendly::sanitize;
use url::Url;
Expand All @@ -28,6 +29,7 @@ pub struct ClientBuilder {
proxy: Option<String>,
disable_ssl_verification: bool,
inner: MatrixClientBuilder,
with_memory_state_store: bool,
}

#[uniffi::export]
Expand Down Expand Up @@ -97,6 +99,12 @@ impl ClientBuilder {
Arc::new(builder)
}

pub fn with_memory_state_store(self: Arc<Self>) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.with_memory_state_store = true;
Arc::new(builder)
}

pub fn build(self: Arc<Self>) -> Result<Arc<Client>, ClientError> {
Ok(self.build_inner()?)
}
Expand All @@ -112,7 +120,19 @@ impl ClientBuilder {
let data_path = PathBuf::from(base_path).join(sanitize(username));
fs::create_dir_all(&data_path)?;

inner_builder = inner_builder.sqlite_store(&data_path, builder.passphrase.as_deref());
if builder.with_memory_state_store {
let sqlite_crypto_store = RUNTIME.block_on(async move {
SqliteCryptoStore::open(&data_path, builder.passphrase.as_deref()).await
})?;
inner_builder = inner_builder.store_config(
StoreConfig::new()
.crypto_store(sqlite_crypto_store)
.state_store(MemoryStore::new()),
);
} else {
inner_builder =
inner_builder.sqlite_store(&data_path, builder.passphrase.as_deref());
}
}

// Determine server either from URL, server name or user ID.
Expand Down Expand Up @@ -190,6 +210,7 @@ impl Default for ClientBuilder {
proxy: None,
disable_ssl_verification: false,
inner: MatrixClient::builder(),
with_memory_state_store: false,
}
}
}
5 changes: 3 additions & 2 deletions crates/matrix-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use bytes;
pub use matrix_sdk_base::crypto;
pub use matrix_sdk_base::{
deserialized_responses,
store::{DynStateStore, StateStoreExt},
store::{DynStateStore, MemoryStore, StateStoreExt},
DisplayName, Room as BaseRoom, RoomInfo, RoomMember as BaseRoomMember, RoomMemberships,
RoomState, SessionMeta, StateChanges, StateStore, StoreError,
};
Expand Down Expand Up @@ -50,7 +50,6 @@ pub mod sliding_sync;

#[cfg(feature = "e2e-encryption")]
pub mod encryption;

pub use account::Account;
pub use authentication::{AuthApi, AuthSession};
pub use client::{Client, ClientBuildError, ClientBuilder, LoopCtrl, SendRequest, UnknownToken};
Expand All @@ -61,6 +60,8 @@ pub use error::{
RumaApiError,
};
pub use http_client::TransmissionProgress;
#[cfg(all(feature = "e2e-encryption", feature = "sqlite"))]
pub use matrix_sdk_sqlite::SqliteCryptoStore;
pub use media::Media;
pub use ruma::{IdParseError, OwnedServerName, ServerName};
#[cfg(feature = "experimental-sliding-sync")]
Expand Down

0 comments on commit 2225e8a

Please sign in to comment.