diff --git a/server/src/matrix/bot.rs b/server/src/matrix/bot.rs index 74813fe..682c81c 100644 --- a/server/src/matrix/bot.rs +++ b/server/src/matrix/bot.rs @@ -30,8 +30,10 @@ pub struct BotContext { /// listening for messages. Runs the full Matrix sync loop — call from a /// `tokio::spawn` task so it doesn't block the main thread. pub async fn run_bot(config: BotConfig, project_root: PathBuf) -> Result<(), String> { + let store_path = project_root.join(".story_kit").join("matrix_store"); let client = Client::builder() .homeserver_url(&config.homeserver) + .sqlite_store(&store_path, None) .build() .await .map_err(|e| format!("Failed to build Matrix client: {e}"))?; @@ -57,12 +59,18 @@ pub async fn run_bot(config: BotConfig, project_root: PathBuf) -> Result<(), Str .parse() .map_err(|_| format!("Invalid room ID '{}'", config.room_id))?; - client - .join_room_by_id(&target_room_id) - .await - .map_err(|e| format!("Failed to join room '{}': {e}", config.room_id))?; - - slog!("[matrix-bot] Joined room {target_room_id}"); + // Try to join the room with a timeout. Conduit sometimes hangs or + // returns errors on join if the bot is already a member. + match tokio::time::timeout( + std::time::Duration::from_secs(10), + client.join_room_by_id(&target_room_id), + ) + .await + { + Ok(Ok(_)) => slog!("[matrix-bot] Joined room {target_room_id}"), + Ok(Err(e)) => slog!("[matrix-bot] Join room error (may already be a member): {e}"), + Err(_) => slog!("[matrix-bot] Join room timed out (may already be a member)"), + } let ctx = BotContext { bot_user_id, @@ -92,8 +100,16 @@ async fn on_room_message( room: Room, Ctx(ctx): Ctx, ) { + slog!( + "[matrix-bot] Event received: room={} sender={} target={}", + room.room_id(), + ev.sender, + ctx.target_room_id + ); + // Only handle messages in the configured room if room.room_id() != &*ctx.target_room_id { + slog!("[matrix-bot] Ignoring message from wrong room"); return; }