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

@@ -53,6 +53,11 @@ pub struct BotConfig {
/// If unset, the bot falls back to "Assistant".
#[serde(default)]
pub display_name: Option<String>,
/// Room IDs where ambient mode is active (bot responds to all messages).
/// Updated at runtime when the user toggles ambient mode — do not edit
/// manually while the bot is running.
#[serde(default)]
pub ambient_rooms: Vec<String>,
}
impl BotConfig {
@@ -97,6 +102,46 @@ impl BotConfig {
}
}
/// Persist the current set of ambient room IDs back to `bot.toml`.
///
/// Reads the existing file as a TOML document, updates the `ambient_rooms`
/// array, and writes the result back. Errors are logged but not propagated
/// so a persistence failure never interrupts the bot's message handling.
pub fn save_ambient_rooms(project_root: &Path, room_ids: &[String]) {
let path = project_root.join(".story_kit").join("bot.toml");
let content = match std::fs::read_to_string(&path) {
Ok(c) => c,
Err(e) => {
eprintln!("[matrix-bot] save_ambient_rooms: failed to read bot.toml: {e}");
return;
}
};
let mut doc: toml::Value = match toml::from_str(&content) {
Ok(v) => v,
Err(e) => {
eprintln!("[matrix-bot] save_ambient_rooms: failed to parse bot.toml: {e}");
return;
}
};
if let toml::Value::Table(ref mut t) = doc {
let arr = toml::Value::Array(
room_ids
.iter()
.map(|s| toml::Value::String(s.clone()))
.collect(),
);
t.insert("ambient_rooms".to_string(), arr);
}
match toml::to_string_pretty(&doc) {
Ok(new_content) => {
if let Err(e) = std::fs::write(&path, new_content) {
eprintln!("[matrix-bot] save_ambient_rooms: failed to write bot.toml: {e}");
}
}
Err(e) => eprintln!("[matrix-bot] save_ambient_rooms: failed to serialise bot.toml: {e}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -378,4 +423,90 @@ require_verified_devices = true
"bot.toml with legacy require_verified_devices key must still load"
);
}
#[test]
fn load_reads_ambient_rooms() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"
homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
ambient_rooms = ["!abc:example.com"]
"#,
)
.unwrap();
let config = BotConfig::load(tmp.path()).unwrap();
assert_eq!(config.ambient_rooms, vec!["!abc:example.com"]);
}
#[test]
fn load_ambient_rooms_defaults_to_empty_when_absent() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"
homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
"#,
)
.unwrap();
let config = BotConfig::load(tmp.path()).unwrap();
assert!(config.ambient_rooms.is_empty());
}
#[test]
fn save_ambient_rooms_persists_to_bot_toml() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
"#,
)
.unwrap();
save_ambient_rooms(tmp.path(), &["!abc:example.com".to_string()]);
let config = BotConfig::load(tmp.path()).unwrap();
assert_eq!(config.ambient_rooms, vec!["!abc:example.com"]);
}
#[test]
fn save_ambient_rooms_clears_when_empty() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
ambient_rooms = ["!abc:example.com"]
"#,
)
.unwrap();
save_ambient_rooms(tmp.path(), &[]);
let config = BotConfig::load(tmp.path()).unwrap();
assert!(config.ambient_rooms.is_empty());
}
}