Add a loop to Matrix login
This commit is contained in:
parent
2e229b70d9
commit
70108247d4
@ -126,6 +126,7 @@ async fn main() {
|
|||||||
|
|
||||||
let matrix_configuration = bot_configuration.matrix.to_owned();
|
let matrix_configuration = bot_configuration.matrix.to_owned();
|
||||||
if matrix_configuration.enabled {
|
if matrix_configuration.enabled {
|
||||||
|
log_message(Matrix, &"Matrix is enabled! Logging in...".to_string());
|
||||||
tokio::spawn(login_and_sync(matrix_configuration, state.clone()));
|
tokio::spawn(login_and_sync(matrix_configuration, state.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
125
src/matrix.rs
125
src/matrix.rs
@ -16,68 +16,69 @@ struct MatrixState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn login_and_sync(matrix_configuration: MatrixConfiguration, bot_state: Arc<State>) {
|
pub async fn login_and_sync(matrix_configuration: MatrixConfiguration, bot_state: Arc<State>) {
|
||||||
log_message(Matrix, &"Matrix is enabled! Logging in...".to_string());
|
loop {
|
||||||
let client_builder =
|
let client_builder =
|
||||||
matrix_sdk::Client::builder().homeserver_url(&matrix_configuration.homeserver_url);
|
matrix_sdk::Client::builder().homeserver_url(&matrix_configuration.homeserver_url);
|
||||||
let client = match client_builder.build().await {
|
let client = match client_builder.build().await {
|
||||||
Ok(client) => client,
|
Ok(client) => client,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
log_message(MatrixError, &format!("Unable to build client: {}", error));
|
log_message(MatrixError, &format!("Unable to build client: {}", error));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match client
|
match client
|
||||||
.login_username(
|
.login_username(
|
||||||
&matrix_configuration.username,
|
&matrix_configuration.username,
|
||||||
&matrix_configuration.password,
|
&matrix_configuration.password,
|
||||||
)
|
)
|
||||||
.device_id("ERRORNOWATCHER")
|
.device_id("ERRORNOWATCHER")
|
||||||
.initial_device_display_name("ErrorNoWatcher")
|
.initial_device_display_name("ErrorNoWatcher")
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
log_message(MatrixError, &format!("Unable to login: {}", error));
|
log_message(MatrixError, &format!("Unable to login: {}", error));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let response = match client.sync_once(SyncSettings::default()).await {
|
let response = match client.sync_once(SyncSettings::default()).await {
|
||||||
Ok(response) => response,
|
Ok(response) => response,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
log_message(MatrixError, &format!("Unable to synchronize: {}", error));
|
log_message(MatrixError, &format!("Unable to synchronize: {}", error));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let display_name = match client.account().get_display_name().await {
|
let display_name = match client.account().get_display_name().await {
|
||||||
Ok(display_name) => display_name.unwrap_or(match client.user_id() {
|
Ok(display_name) => display_name.unwrap_or(match client.user_id() {
|
||||||
Some(user_id) => user_id.to_string(),
|
Some(user_id) => user_id.to_string(),
|
||||||
None => matrix_configuration.username.to_owned(),
|
None => matrix_configuration.username.to_owned(),
|
||||||
}),
|
}),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
log_message(
|
log_message(
|
||||||
MatrixError,
|
MatrixError,
|
||||||
&format!("Unable to get display name: {}", error),
|
&format!("Unable to get display name: {}", error),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
log_message(
|
log_message(
|
||||||
Matrix,
|
Matrix,
|
||||||
&format!("Successfully logged in as {}!", display_name),
|
&format!("Successfully logged in as {}!", display_name),
|
||||||
);
|
);
|
||||||
let matrix_state = MatrixState {
|
let matrix_state = MatrixState {
|
||||||
bot_state,
|
bot_state: bot_state.clone(),
|
||||||
matrix_configuration: matrix_configuration.clone(),
|
matrix_configuration: matrix_configuration.clone(),
|
||||||
display_name,
|
display_name,
|
||||||
};
|
};
|
||||||
client.add_event_handler_context(matrix_state);
|
client.add_event_handler_context(matrix_state);
|
||||||
client.add_event_handler(room_message_handler);
|
client.add_event_handler(room_message_handler);
|
||||||
let settings = SyncSettings::default().token(response.next_batch);
|
let settings = SyncSettings::default().token(response.next_batch);
|
||||||
match client.sync(settings).await {
|
match client.sync(settings).await {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(error) => log_message(MatrixError, &format!("Unable to synchronize: {}", error)),
|
Err(error) => log_message(MatrixError, &format!("Unable to synchronize: {}", error)),
|
||||||
};
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn room_message_handler(
|
async fn room_message_handler(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user