diff --git a/src/main.rs b/src/main.rs index 7eab6db..8a9bbf2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,18 @@ use matrix_sdk::{ room::Room, ruma::{ - events::room::message::{ - MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEvent, + events::room::{ + message::{ + MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEvent, + }, + member::StrippedRoomMemberEvent }, OwnedRoomId, }, + Client, Error, LoopCtrl, }; +use tokio::time::{sleep, Duration}; use std::path::PathBuf; use once_cell::sync::Lazy; @@ -24,6 +29,30 @@ static ROOMS_CONFIG_FILE: Lazy = Lazy::new(|| DATA_DIR.join("rooms_conf static CONFIG: Lazy = Lazy::new(|| config::Config::load()); +// Autojoin rooms the bot is invited to +async fn on_stripped_state_member(room_member: StrippedRoomMemberEvent, client: Client, room: Room) { + if room_member.state_key != client.user_id().unwrap() { + return; + } + + if let Room::Invited(room) = room { + tokio::spawn(async move { + let mut delay = 2; + + while let Err(err) = room.accept_invitation().await { + eprintln!("Warn: {} ({err:?}), retrying in {delay}s", room.room_id()); + + sleep(Duration::from_secs(delay)).await; + delay *= 2; + + if delay > 3600 { + eprintln!("Warn: {} ({err:?})", room.room_id()); + break; + } + } + }); + } +} async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) -> anyhow::Result<()> { if let Room::Joined(room) = room { @@ -187,6 +216,7 @@ async fn main() -> anyhow::Result<()> { }; client.add_event_handler(on_room_message); + client.add_event_handler(on_stripped_state_member); client .sync_with_result_callback(sync_settings, |sync_result| async move {