Making Matrix config fields optional to meet Twilio login needs
This commit is contained in:
@@ -13,11 +13,17 @@ fn default_permission_timeout_secs() -> u64 {
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct BotConfig {
|
||||
/// Matrix homeserver URL, e.g. `https://matrix.example.com`
|
||||
pub homeserver: String,
|
||||
/// Only required when `transport = "matrix"` (the default).
|
||||
#[serde(default)]
|
||||
pub homeserver: Option<String>,
|
||||
/// Bot user ID, e.g. `@storykit:example.com`
|
||||
pub username: String,
|
||||
/// Only required when `transport = "matrix"`.
|
||||
#[serde(default)]
|
||||
pub username: Option<String>,
|
||||
/// Bot password
|
||||
pub password: String,
|
||||
/// Only required when `transport = "matrix"`.
|
||||
#[serde(default)]
|
||||
pub password: Option<String>,
|
||||
/// Matrix room IDs to join, e.g. `["!roomid:example.com"]`.
|
||||
/// Use an array for multiple rooms; a single string is accepted via the
|
||||
/// deprecated `room_id` key for backwards compatibility.
|
||||
@@ -227,12 +233,33 @@ impl BotConfig {
|
||||
);
|
||||
return None;
|
||||
}
|
||||
} else if config.room_ids.is_empty() {
|
||||
eprintln!(
|
||||
"[matrix-bot] bot.toml has no room_ids configured — \
|
||||
add `room_ids = [\"!roomid:example.com\"]` to bot.toml"
|
||||
);
|
||||
return None;
|
||||
} else {
|
||||
// Default transport is Matrix — validate Matrix-specific fields.
|
||||
if config.homeserver.as_ref().is_none_or(|s| s.is_empty()) {
|
||||
eprintln!(
|
||||
"[bot] bot.toml: transport=\"matrix\" requires homeserver"
|
||||
);
|
||||
return None;
|
||||
}
|
||||
if config.username.as_ref().is_none_or(|s| s.is_empty()) {
|
||||
eprintln!(
|
||||
"[bot] bot.toml: transport=\"matrix\" requires username"
|
||||
);
|
||||
return None;
|
||||
}
|
||||
if config.password.as_ref().is_none_or(|s| s.is_empty()) {
|
||||
eprintln!(
|
||||
"[bot] bot.toml: transport=\"matrix\" requires password"
|
||||
);
|
||||
return None;
|
||||
}
|
||||
if config.room_ids.is_empty() {
|
||||
eprintln!(
|
||||
"[matrix-bot] bot.toml has no room_ids configured — \
|
||||
add `room_ids = [\"!roomid:example.com\"]` to bot.toml"
|
||||
);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(config)
|
||||
}
|
||||
@@ -335,8 +362,8 @@ enabled = true
|
||||
let result = BotConfig::load(tmp.path());
|
||||
assert!(result.is_some());
|
||||
let config = result.unwrap();
|
||||
assert_eq!(config.homeserver, "https://matrix.example.com");
|
||||
assert_eq!(config.username, "@bot:example.com");
|
||||
assert_eq!(config.homeserver.as_deref(), Some("https://matrix.example.com"));
|
||||
assert_eq!(config.username.as_deref(), Some("@bot:example.com"));
|
||||
assert_eq!(
|
||||
config.effective_room_ids(),
|
||||
&["!abc:example.com", "!def:example.com"]
|
||||
|
||||
Reference in New Issue
Block a user