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::ruma::UserId;
use matrix_sdk::{Client, Session}; use matrix_sdk::{Client, Session};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fs::File;
use std::sync::Arc; use std::sync::Arc;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use tokio::sync::RwLock; use tokio::sync::RwLock;
@ -79,14 +80,22 @@ async fn main() -> anyhow::Result<()> {
}; };
eprintln!("Logging in!"); eprintln!("Logging in!");
client
.login( if config.matrix_persist.exists() {
mxid.localpart(), let session: Session = serde_json::from_reader(File::open(config.matrix_persist)?)?;
&config.matrix_password, client.restore_login(session).await?;
Some("mxmonzo"), } else {
Some("rei's MxMonzo"), let session: Session = client
) .login(
.await?; 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!"); eprintln!("Syncing once!");
client.sync_once(SyncSettings::new()).await?; client.sync_once(SyncSettings::new()).await?;

View File

@ -19,6 +19,7 @@ pub struct Config {
pub matrix_room: String, pub matrix_room: String,
pub matrix_store: PathBuf, pub matrix_store: PathBuf,
pub matrix_password: String, pub matrix_password: String,
pub matrix_persist: PathBuf,
pub monzo_persist: PathBuf, pub monzo_persist: PathBuf,
pub monzo_client_id: String, pub monzo_client_id: String,
pub monzo_client_secret: String, pub monzo_client_secret: String,