From d560b860bf3cdf4877f53bfb05f1a3104898fe47 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Tue, 23 Nov 2021 22:24:49 +0000 Subject: [PATCH] Login in a more restorable way --- src/main.rs | 25 +++++++++++++++++-------- src/state.rs | 1 + 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4540911..356051c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use matrix_sdk::ruma::events::SyncMessageEvent; use matrix_sdk::ruma::UserId; use matrix_sdk::{Client, Session}; use std::convert::TryFrom; +use std::fs::File; use std::sync::Arc; use tokio::io::AsyncReadExt; use tokio::sync::RwLock; @@ -79,14 +80,22 @@ async fn main() -> anyhow::Result<()> { }; eprintln!("Logging in!"); - client - .login( - mxid.localpart(), - &config.matrix_password, - Some("mxmonzo"), - Some("rei's MxMonzo"), - ) - .await?; + + if config.matrix_persist.exists() { + let session: Session = serde_json::from_reader(File::open(config.matrix_persist)?)?; + client.restore_login(session).await?; + } else { + let session: Session = client + .login( + mxid.localpart(), + &config.matrix_password, + None, + Some("rei's MxMonzo"), + ) + .await? + .into(); + serde_json::to_writer(File::create(config.matrix_persist)?, &session)?; + } eprintln!("Syncing once!"); client.sync_once(SyncSettings::new()).await?; diff --git a/src/state.rs b/src/state.rs index dd74d17..fa4b2a1 100644 --- a/src/state.rs +++ b/src/state.rs @@ -19,6 +19,7 @@ pub struct Config { pub matrix_room: String, pub matrix_store: PathBuf, pub matrix_password: String, + pub matrix_persist: PathBuf, pub monzo_persist: PathBuf, pub monzo_client_id: String, pub monzo_client_secret: String,