From b98773a0a92558b067e7ea2e9404363d3f8ea61a Mon Sep 17 00:00:00 2001 From: Val Lorentz Date: Tue, 31 Oct 2023 19:52:40 +0100 Subject: [PATCH] Add state store --- src/app.rs | 26 ++++++++++++++++++++++++++ src/config.rs | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/app.rs b/src/app.rs index d2772da..c0ca7fb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -54,11 +54,36 @@ impl App { let fps = FpsCounter::default(); let config = Config::new()?; let mode = Mode::Home; + let datadir = config.config._data_dir.join("default"); let future_clients = config.accounts.clone().map(|conf| { + let datadir = datadir.clone(); tokio::spawn(async move { let server_name = conf.user_id.server_name(); + let user_dir = datadir.join(conf.user_id.to_string()); + // If there is at least one device state for that user, use it. Otherwise, + // generate a random device id + let device_dir = std::fs::read_dir(&user_dir) + .unwrap_or_else(|e| panic!("Could not read user dir: {}", e)) + .next() + .map(|dir_entry| { + dir_entry + .unwrap_or_else(|e| panic!("Could not read entry in {}: {}", user_dir.display(), e)) + .path() + }) + .unwrap_or_else(|| user_dir.join(matrix_sdk::ruma::DeviceId::new().as_str())); + // Extract device id from the path + let device_id = device_dir + .as_path() + .file_name() + .expect("empty path to state store") + .to_str() + .expect("device id is not valid UTF8"); let client = matrix_sdk::Client::builder() .server_name(server_name) + .sqlite_store( + &device_dir, + conf.data_passphrase.as_ref().map(|s| s.as_str()), + ) .build() .await .with_context(|| format!("Could not initialize client for {}", server_name))?; @@ -66,6 +91,7 @@ impl App { .matrix_auth() .login_username(&conf.user_id, &conf.password) .initial_device_display_name(&conf.device_name) + .device_id(device_id) .send() .await .with_context(|| format!("Could not login as {}", conf.user_id))?; diff --git a/src/config.rs b/src/config.rs index 37cf205..c4a106f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,6 +27,8 @@ pub struct AccountConfig { pub password: String, #[serde(default = "default_device_name")] pub device_name: String, + /// Passphrase for the state store + pub data_passphrase: Option, } fn default_device_name() -> String {