WIP: track state between commands

This commit is contained in:
2026-01-31 12:37:38 -06:00
parent b697cc1b7b
commit 3b5a4a8cb2
5 changed files with 74 additions and 10 deletions
+22
View File
@@ -0,0 +1,22 @@
// Project-local debug tasks
//
// For more documentation on how to configure debug tasks,
// see: https://zed.dev/docs/debugger
[
{
"label": "Build & Debug magbot-rs",
"build": {
"command": "cargo",
"args": ["build"],
},
"program": "$ZED_WORKTREE_ROOT/target/debug/magbot-rs",
// sourceLanguages is required for CodeLLDB (not GDB) when using Rust
"sourceLanguages": ["rust"],
"request": "launch",
"adapter": "CodeLLDB",
"env": {
"HOMESERVER_URL": "https://matrix.org",
},
},
]
+2 -1
View File
@@ -6,7 +6,8 @@ use libmagbot::config::MagbotConfig;
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let config = MagbotConfig::from_env()?;
let file_str = libmagbot::config::read_file()?;
let config = MagbotConfig::from_str(&file_str)?;
let Ok(homeserver_fqdn) = env::var("HOMESERVER_URL") else {
eprintln!("Set the homeserver fqdn as the `HOMESERVER_URL` environment variable");
std::process::exit(1)
+2 -1
View File
@@ -90,6 +90,7 @@ pub async fn suggest(
mut ctx: Ctx<MagbotContext>,
) -> anyhow::Result<()> {
// save the title into memory with author
let user = event.sender.to_string();
match ctx.0.status {
MagbotStatus::Running => {
// grab the index of the first <space> character
@@ -109,7 +110,7 @@ pub async fn suggest(
room.send(reaction).await?;
}
MagbotStatus::Sleeping => {
let msg = RoomMessageEventContent::text_plain("{user} Magbot is not running");
let msg = RoomMessageEventContent::text_plain(format!("{user} Magbot is not running"));
room.send(msg).await?;
}
}
+46 -6
View File
@@ -26,15 +26,55 @@ pub struct MagbotConfig {
pub username: String,
pub password: String,
pub room_id: String,
pub status: MagbotStatus,
}
impl MagbotConfig {
pub fn from_env() -> anyhow::Result<Self> {
let mut file = File::open("config.yaml").unwrap_or(File::open("config.yaml")?);
let mut file_str = String::new();
file.read_to_string(&mut file_str)?;
pub fn from_str(input: &str) -> anyhow::Result<Self> {
let config: MagbotConfig = serde_yml::from_str(input)?;
let config: MagbotConfig = serde_yml::from_str(&file_str)?;
Ok(config)
}
}
impl Default for MagbotConfig {
fn default() -> Self {
Self {
username: String::new(),
password: String::new(),
room_id: String::new(),
}
}
}
pub fn read_file() -> anyhow::Result<String> {
let mut file = File::open("config.yml").unwrap_or(File::open("config.yaml")?);
let mut file_str = String::new();
file.read_to_string(&mut file_str)?;
Ok(file_str)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_config() {
let username = "magbot";
let password = "password";
let room_id = "!ajyOBbqoCIbBhEqNPU:matrix.org";
let _lookpack_sec = 86400;
let _lookpack_limit = 400;
let _dump_mode = true;
let _db_name = "";
let _vote_activity_hrs = 6672;
let str = r##"username: "magbot"
password: "password"
room_id: "!asdf:asdf.org"
"##;
let parsed = MagbotConfig::from_str(str).unwrap();
assert_eq!(username, parsed.username);
assert_eq!(password, parsed.password);
assert_eq!(room_id, parsed.room_id);
}
}
+2 -2
View File
@@ -111,7 +111,7 @@ pub async fn login_and_sync(
username: String,
password: String,
) -> anyhow::Result<()> {
let context = MagbotContext::default();
let mut context = MagbotContext::default();
// Note that when encryption is enabled, you should use a persistent store to be
// able to restore the session with a working encryption setup.
@@ -138,7 +138,7 @@ pub async fn login_and_sync(
// add our CommandBot to be notified of incoming messages, we do this after the
// initial sync to avoid responding to messages before the bot was running.
client.add_event_handler(on_room_message);
client.add_event_handler(|ctx| async move { on_room_message(ctx) });
// since we called `sync_once` before we entered our sync loop we must pass
// that sync token to `sync`