story-kit: merge 288_bug_ambient_mode_state_lost_on_server_restart

This commit is contained in:
Dave
2026-03-18 14:58:06 +00:00
parent 7c023c6beb
commit 8fcfadcb04
3 changed files with 158 additions and 4 deletions

View File

@@ -30,7 +30,7 @@ use matrix_sdk::encryption::verification::{
};
use matrix_sdk::ruma::events::key::verification::request::ToDeviceKeyVerificationRequestEvent;
use super::config::BotConfig;
use super::config::{BotConfig, save_ambient_rooms};
// ---------------------------------------------------------------------------
// Conversation history types
@@ -333,6 +333,21 @@ pub async fn run_bot(
persisted.len()
);
// Restore persisted ambient rooms from config, ignoring any that are not
// in the configured target_room_ids to avoid stale entries.
let persisted_ambient: HashSet<OwnedRoomId> = config
.ambient_rooms
.iter()
.filter_map(|s| s.parse::<OwnedRoomId>().ok())
.collect();
if !persisted_ambient.is_empty() {
slog!(
"[matrix-bot] Restored ambient mode for {} room(s): {:?}",
persisted_ambient.len(),
persisted_ambient
);
}
let bot_name = config
.display_name
.clone()
@@ -351,7 +366,7 @@ pub async fn run_bot(
pending_perm_replies: Arc::new(TokioMutex::new(HashMap::new())),
permission_timeout_secs: config.permission_timeout_secs,
bot_name,
ambient_rooms: Arc::new(TokioMutex::new(HashSet::new())),
ambient_rooms: Arc::new(TokioMutex::new(persisted_ambient)),
};
slog!("[matrix-bot] Cryptographic identity verification is always ON — commands from unencrypted rooms or unverified devices are rejected");
@@ -798,14 +813,18 @@ async fn on_room_message(
.then(|| parse_ambient_command(&body, &ctx.bot_user_id, &ctx.bot_name))
.flatten();
if let Some(enable) = ambient_cmd {
{
let ambient_room_ids: Vec<String> = {
let mut ambient = ctx.ambient_rooms.lock().await;
if enable {
ambient.insert(incoming_room_id.clone());
} else {
ambient.remove(&incoming_room_id);
}
} // lock released before the async send below
ambient.iter().map(|r| r.to_string()).collect()
}; // lock released before the async send below
// Persist updated ambient rooms to bot.toml so the state survives restarts.
save_ambient_rooms(&ctx.project_root, &ambient_room_ids);
let confirmation = if enable {
"Ambient mode on. I'll respond to all messages in this room."