Login in a more restorable way

This commit is contained in:
Olivier 'reivilibre' 2021-11-23 22:24:49 +00:00
parent 808d45b893
commit d560b860bf
2 changed files with 18 additions and 8 deletions

View File

@ -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
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,
Some("mxmonzo"),
None,
Some("rei's MxMonzo"),
)
.await?;
.await?
.into();
serde_json::to_writer(File::create(config.matrix_persist)?, &session)?;
}
eprintln!("Syncing once!");
client.sync_once(SyncSettings::new()).await?;

View File

@ -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,