From 30e1f4cd6bc6f3a3dd62a819e20d2760ad601008 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Thu, 15 Jun 2023 21:57:43 +0200 Subject: [PATCH] feature (accounts): implemented session restoring --- .gitignore | 6 +++--- src/accounts/mod.rs | 12 ++++++++++++ src/main.rs | 8 +++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 74d7152..80b55f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ +target/ +data/ + # IDE stuff .idea - -# Rust stuff -target/ diff --git a/src/accounts/mod.rs b/src/accounts/mod.rs index 400cdbf..4de215a 100644 --- a/src/accounts/mod.rs +++ b/src/accounts/mod.rs @@ -14,6 +14,7 @@ pub struct Account { pub async fn account_add(homeserver:&str, username:&str, password:&str) -> anyhow::Result { let client = Client::builder() .homeserver_url(homeserver) + .sled_store("data", Some("supersecure"))? .build() .await?; @@ -38,10 +39,21 @@ pub async fn account_add(homeserver:&str, username:&str, password:&str) -> anyho let serialized_account = serde_json::to_string(&account)?; println!("serialized account: {}", serialized_account); + // TODO: save serialized account info in userdata dir Ok(account) } pub async fn account_login(account: Account) -> anyhow::Result<()> { + let client = Client::builder() + .homeserver_url(account.homeserver) + .sled_store("data", Some("supersecure"))? + .build() + .await?; + + client.restore_login(account.session.clone()).await?; + + println!("restored account: {} device ID: {}", &account.session.user_id, &account.session.device_id); + Ok(()) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 99bfd77..292341d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,19 @@ mod ui; mod accounts; +use matrix_sdk::ruma::exports::serde_json; use tokio::time::{sleep, Duration}; #[tokio::main] async fn main() -> anyhow::Result<()> { tracing_subscriber::fmt::init(); - accounts::account_add("https://nerdcult.net", "test", "abcd1234").await?; + // accounts::account_add("https://nerdcult.net", "test", "abcd1234").await?; + + let serialized_account = "{\"homeserver\":\"https://nerdcult.net\",\"session\":{\"access_token\":\"U2XH1vfu3dJTTPV7BIeZh2yGiMN49vBF\",\"user_id\":\"@test:nerdcult.net\",\"device_id\":\"UsdqFbcEC3\"},\"sync_token\":null}"; + let account: accounts::Account = serde_json::from_str(serialized_account)?; + + accounts::account_login(account).await?; Ok(()) }