matrix: Rework room send error handling

* Use the tracing crate instead of println macros
 * Do not panic on failure to send messages
 * Move the send code to a separate function in preparation for adding
   a proper retry implementation
This commit is contained in:
2025-04-30 15:09:55 +00:00
parent cacbf4af45
commit 5c009476d6
3 changed files with 37 additions and 15 deletions

2
Cargo.lock generated
View File

@ -1295,7 +1295,7 @@ checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]]
name = "matrix-feedbot"
version = "0.1.0"
version = "0.1.2"
dependencies = [
"anyhow",
"chrono",

View File

@ -1,6 +1,6 @@
[package]
name = "matrix-feedbot"
version = "0.1.1"
version = "0.1.3"
edition = "2021"
license = "GPL-3.0-or-later"
authors = ["mirsal <mirsal@mirsal.fr>"]

View File

@ -33,6 +33,7 @@ use tokio::{
};
use tracing::{error, info};
use std::error::Error;
async fn on_stripped_state_member(
room_member: StrippedRoomMemberEvent,
@ -44,27 +45,46 @@ async fn on_stripped_state_member(
}
tokio::spawn(async move {
println!("Autojoining room {}", room.room_id());
info!("Autojoining room {}", room.room_id());
let mut delay = 2;
while let Err(err) = room.join().await {
// retry autojoin due to synapse sending invites, before the
// invited user can join for more information see
// https://github.com/matrix-org/synapse/issues/4345
eprintln!("Failed to join room {} ({err:?}), retrying in {delay}s", room.room_id());
error!("Failed to join room {} ({err:?}), retrying in {delay}s", room.room_id());
sleep(Duration::from_secs(delay)).await;
delay *= 2;
if delay > 3600 {
eprintln!("Can't join room {} ({err:?})", room.room_id());
error!("Can't join room {} ({err:?})", room.room_id());
break;
}
}
println!("Successfully joined room {}", room.room_id());
info!("Successfully joined room {}", room.room_id());
});
}
async fn send_to_room(
msg: &String,
room: Room,
) -> Result<(), Box<dyn Error>> {
info!("Joining room {}", room.room_id());
match room.client().join_room_by_id(room.room_id()).await {
Ok(_) => {
info!("Posting to room {}", room.room_id());
room.send(RoomMessageEventContent::text_html(msg.clone(), msg)).await?;
return Ok(());
},
Err(e) => {
error!("Failed to join room {e:?}");
return Err(e.into());
}
};
}
pub async fn login_and_sync<T: Clone>(
homeserver_url: String,
username: &str,
@ -78,10 +98,10 @@ pub async fn login_and_sync<T: Clone>(
client
.matrix_auth()
.login_username(username, password)
.initial_device_display_name("bender v0.1.0")
.initial_device_display_name("bender v0.1.3")
.await?;
println!("logged in as {username}");
info!("logged in as {username}");
client.add_event_handler(on_stripped_state_member);
@ -110,14 +130,16 @@ pub async fn login_and_sync<T: Clone>(
let msg = &msg.clone();
for r in rooms {
info!("Joining room {}", r);
let room = client.join_room_by_id(<&RoomId>::try_from(r.as_str())
.expect("invalid room ID")
).await.expect(format!("Failed to join room {}", r).as_str());
match client.get_room(<&RoomId>::try_from(r.as_str())
.expect(format!("Invalid Room ID: {}", r).as_str())) {
info!("Posting to room {}", r);
room.send(RoomMessageEventContent::text_html(msg.clone(), msg)).await
.expect("Failed to send matrix event");
Some(room) => match send_to_room(msg, room).await {
Ok(_) => info!("Done sending to room {}", r),
Err(e) => error!("Cannot send to room {}: {e:?}", r),
},
None => error!("Room {} not found in matrix state store", r)
};
}
},
Err(e) => {