refactor(matrix): keep trying to log in
This commit is contained in:
parent
709b4a1d0d
commit
940b4eb49e
@ -1,4 +1,4 @@
|
|||||||
use std::{net::SocketAddr, process::exit};
|
use std::{net::SocketAddr, process::exit, time::Duration};
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use azalea::{
|
use azalea::{
|
||||||
@ -10,7 +10,7 @@ use hyper_util::rt::TokioIo;
|
|||||||
use log::{debug, error, info, trace};
|
use log::{debug, error, info, trace};
|
||||||
use mlua::{Error, Function, IntoLuaMulti, Table};
|
use mlua::{Error, Function, IntoLuaMulti, Table};
|
||||||
use ncr::utils::trim_header;
|
use ncr::utils::trim_header;
|
||||||
use tokio::net::TcpListener;
|
use tokio::{net::TcpListener, time::sleep};
|
||||||
|
|
||||||
#[cfg(feature = "matrix")]
|
#[cfg(feature = "matrix")]
|
||||||
use crate::matrix;
|
use crate::matrix;
|
||||||
@ -284,8 +284,12 @@ fn matrix_init(client: &Client, state: State) {
|
|||||||
if let Ok(options) = globals.get::<Table>("MatrixOptions") {
|
if let Ok(options) = globals.get::<Table>("MatrixOptions") {
|
||||||
let name = client.username();
|
let name = client.username();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Err(error) = matrix::login(state, options, globals, name).await {
|
loop {
|
||||||
error!("failed to log into matrix account: {error:?}");
|
let name = name.clone();
|
||||||
|
if let Err(error) = matrix::login(&state, &options, &globals, name).await {
|
||||||
|
error!("failed to log into matrix: {error:?}");
|
||||||
|
}
|
||||||
|
sleep(Duration::from_secs(10)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -99,10 +99,7 @@ pub async fn on_stripped_state_member(
|
|||||||
{
|
{
|
||||||
debug!("joining room {}", room.room_id());
|
debug!("joining room {}", room.room_id());
|
||||||
while let Err(error) = room.join().await {
|
while let Err(error) = room.join().await {
|
||||||
error!(
|
error!("failed to join room {}: {error:?}", room.room_id());
|
||||||
"failed to join room {}: {error:?}, retrying...",
|
|
||||||
room.room_id()
|
|
||||||
);
|
|
||||||
sleep(Duration::from_secs(10)).await;
|
sleep(Duration::from_secs(10)).await;
|
||||||
}
|
}
|
||||||
debug!("successfully joined room {}", room.room_id());
|
debug!("successfully joined room {}", room.room_id());
|
||||||
|
@ -56,7 +56,7 @@ async fn persist_sync_token(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn login(state: State, options: Table, globals: Table, name: String) -> Result<()> {
|
pub async fn login(state: &State, options: &Table, globals: &Table, name: String) -> Result<()> {
|
||||||
let (homeserver_url, username, password) = (
|
let (homeserver_url, username, password) = (
|
||||||
options.get::<String>("homeserver_url")?,
|
options.get::<String>("homeserver_url")?,
|
||||||
options.get::<String>("username")?,
|
options.get::<String>("username")?,
|
||||||
@ -106,7 +106,10 @@ pub async fn login(state: State, options: Table, globals: Table, name: String) -
|
|||||||
fs::write(&session_file, serde_json::to_string(&new_session)?).await?;
|
fs::write(&session_file, serde_json::to_string(&new_session)?).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.add_event_handler_context(Context { state, name });
|
client.add_event_handler_context(Context {
|
||||||
|
state: state.to_owned(),
|
||||||
|
name,
|
||||||
|
});
|
||||||
client.add_event_handler(on_stripped_state_member);
|
client.add_event_handler(on_stripped_state_member);
|
||||||
loop {
|
loop {
|
||||||
match client.sync_once(sync_settings.clone()).await {
|
match client.sync_once(sync_settings.clone()).await {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user