From 86a257410c09cd5a5e109524a591a43b65196aa1 Mon Sep 17 00:00:00 2001 From: Joe Date: Sat, 20 Dec 2025 18:08:46 -0800 Subject: [PATCH] parse the feed command, add users --- migrations/0001_users.down.sql | 1 + migrations/0001_users.up.sql | 1 + ...001_feeds.down.sql => 0002_feeds.down.sql} | 0 .../{0001_feeds.up.sql => 0002_feeds.up.sql} | 4 +- ...{0002_runs.down.sql => 0003_runs.down.sql} | 0 .../{0002_runs.up.sql => 0003_runs.up.sql} | 0 src/server.rs | 69 ++++++++++++++----- 7 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 migrations/0001_users.down.sql create mode 100644 migrations/0001_users.up.sql rename migrations/{0001_feeds.down.sql => 0002_feeds.down.sql} (100%) rename migrations/{0001_feeds.up.sql => 0002_feeds.up.sql} (83%) rename migrations/{0002_runs.down.sql => 0003_runs.down.sql} (100%) rename migrations/{0002_runs.up.sql => 0003_runs.up.sql} (100%) diff --git a/migrations/0001_users.down.sql b/migrations/0001_users.down.sql new file mode 100644 index 0000000..d2f607c --- /dev/null +++ b/migrations/0001_users.down.sql @@ -0,0 +1 @@ +-- Add down migration script here diff --git a/migrations/0001_users.up.sql b/migrations/0001_users.up.sql new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/migrations/0001_users.up.sql @@ -0,0 +1 @@ + diff --git a/migrations/0001_feeds.down.sql b/migrations/0002_feeds.down.sql similarity index 100% rename from migrations/0001_feeds.down.sql rename to migrations/0002_feeds.down.sql diff --git a/migrations/0001_feeds.up.sql b/migrations/0002_feeds.up.sql similarity index 83% rename from migrations/0001_feeds.up.sql rename to migrations/0002_feeds.up.sql index 67cc498..a48cd8d 100644 --- a/migrations/0001_feeds.up.sql +++ b/migrations/0002_feeds.up.sql @@ -1,7 +1,7 @@ CREATE TABLE IF NOT EXISTS feeds ( - id INTEGER PRIMARY KEY, + id INTEGER PRIMARY KEY, url TEXT UNIQUE NOT NULL, - added_by TEXT NOT NULL, + added_by INT NOT NULL, last_modified_by TEXT NOT NULL, active BOOLEAN NOT NULL DEFAULT FALSE, created_at DATETIME NOT NULL DEFAULT current_timestamp, diff --git a/migrations/0002_runs.down.sql b/migrations/0003_runs.down.sql similarity index 100% rename from migrations/0002_runs.down.sql rename to migrations/0003_runs.down.sql diff --git a/migrations/0002_runs.up.sql b/migrations/0003_runs.up.sql similarity index 100% rename from migrations/0002_runs.up.sql rename to migrations/0003_runs.up.sql diff --git a/src/server.rs b/src/server.rs index 98d807c..282b86c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -11,7 +11,13 @@ use serde::Deserialize; use serde_json::{Map, Value}; use sqlx::SqlitePool; use tokio_util::sync::CancellationToken; -use winnow::Parser; +use winnow::{ + Parser, + ascii::space0, + combinator::{alt, fail}, + error::StrContext, + token::take_while, +}; type Payload = Map; @@ -68,9 +74,18 @@ async fn handle_manage_feed( if state.email == bot_email && state.token == token { tracing::debug!("gonna do a thing with {message:?}"); + let ZulipMessage { + mut content, + ref sender_email, + sender_id, + sender_full_name, + .. + } = message; + + let command = parse_command(&mut content.as_str()); + Json(HashMap::from([("content", "nee-ope")])).into_response() } else { - tracing::debug!("bad emal/token"); StatusCode::IM_A_TEAPOT.into_response() } } @@ -120,18 +135,40 @@ struct ZulipMessage { _rest: Payload, } -fn parse_command(input: &mut &str) -> String { - // let Ok(s) = ("***@", winnow::token::take_until(1.., "**")) - // .map(|(_, s, _)| s) - // .parse_next(input) - // else { - // return "could not find Blogdor's name".to_string(); - // }; - // if s != "Blogdor Outgoing Bot" { - // return "could not confirm Blogdor's name".to_string(); - // } - - // s.into() - - todo!() +enum Action { + Add, + Remove, + Help, +} + +struct FeedCommand<'req> { + feed: &'req str, + action: Action, +} + +fn parse_command<'i>(input: &mut &'i str) -> winnow::Result> { + let _ = "**@blogdor's manager**".parse_next(input)?; + let action = ( + space0, + alt(( + "add", + "remove", + "help", + fail.context(StrContext::Expected( + "supported commands are `add`, `remove`, or `help`".into(), + )), + )), + ) + .map(|(_, a)| match a { + "add" => Action::Add, + "remove" => Action::Remove, + "help" => Action::Help, + _ => unreachable!(), + }) + .parse_next(input)?; + + let feed = (space0, take_while(0.., |c: char| !c.is_whitespace())) + .map(|(_, f)| f) + .parse_next(input)?; + Ok(FeedCommand { feed, action }) }