From 23e1830da270a0464521c8345dca81c5bd6902c3 Mon Sep 17 00:00:00 2001 From: Dave Date: Wed, 25 Feb 2026 13:46:20 +0000 Subject: [PATCH] Adding some Matrix bot debug code --- server/src/matrix/bot.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) 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; }