Making Matrix config fields optional to meet Twilio login needs

This commit is contained in:
Timmy
2026-03-24 21:05:35 +00:00
parent c0133fe733
commit a24e4c5c85
3 changed files with 48 additions and 16 deletions

View File

@@ -217,7 +217,7 @@ pub async fn run_bot(
) -> Result<(), String> { ) -> Result<(), String> {
let store_path = project_root.join(".storkit").join("matrix_store"); let store_path = project_root.join(".storkit").join("matrix_store");
let client = Client::builder() let client = Client::builder()
.homeserver_url(&config.homeserver) .homeserver_url(config.homeserver.as_deref().unwrap_or_default())
.sqlite_store(&store_path, None) .sqlite_store(&store_path, None)
.build() .build()
.await .await
@@ -232,7 +232,10 @@ pub async fn run_bot(
let mut login_builder = client let mut login_builder = client
.matrix_auth() .matrix_auth()
.login_username(&config.username, &config.password) .login_username(
config.username.as_deref().unwrap_or_default(),
config.password.as_deref().unwrap_or_default(),
)
.initial_device_display_name("Storkit Bot"); .initial_device_display_name("Storkit Bot");
if let Some(ref device_id) = saved_device_id { if let Some(ref device_id) = saved_device_id {
@@ -265,8 +268,10 @@ pub async fn run_bot(
{ {
use matrix_sdk::ruma::api::client::uiaa; use matrix_sdk::ruma::api::client::uiaa;
let password_auth = uiaa::AuthData::Password(uiaa::Password::new( let password_auth = uiaa::AuthData::Password(uiaa::Password::new(
uiaa::UserIdentifier::UserIdOrLocalpart(config.username.clone()), uiaa::UserIdentifier::UserIdOrLocalpart(
config.password.clone(), config.username.clone().unwrap_or_default(),
),
config.password.clone().unwrap_or_default(),
)); ));
if let Err(e) = client if let Err(e) = client
.encryption() .encryption()

View File

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

View File

@@ -84,7 +84,7 @@ pub fn spawn_bot(
crate::slog!( crate::slog!(
"[matrix-bot] Starting Matrix bot → homeserver={} rooms={:?}", "[matrix-bot] Starting Matrix bot → homeserver={} rooms={:?}",
config.homeserver, config.homeserver.as_deref().unwrap_or("(none)"),
config.effective_room_ids() config.effective_room_ids()
); );