From 5608e2e0810dc9680ec51f0bd3a2bdf84647e9dd Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 8 Jan 2026 11:12:42 +0200 Subject: [PATCH] implement `provider_id` filter --- crates/http/src/config.rs | 5 +++++ crates/http/src/global.rs | 3 ++- crates/http/src/main.rs | 21 ++++++++++++++------- crates/mysql/src/lib.rs | 24 +++++++++++++++--------- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/crates/http/src/config.rs b/crates/http/src/config.rs index 5247a45..56c3f41 100644 --- a/crates/http/src/config.rs +++ b/crates/http/src/config.rs @@ -18,6 +18,11 @@ pub struct Config { #[arg(long, default_value_t = String::from("%d/%m/%Y %H:%M"))] pub format_time: String, + /// Provider ID (`provider` table) + /// * None for the original content + #[arg(long, short)] + pub provider_id: Option, + /// Default listing limit #[arg(long, default_value_t = 20)] pub list_limit: usize, diff --git a/crates/http/src/global.rs b/crates/http/src/global.rs index 933e67e..8e25ad0 100644 --- a/crates/http/src/global.rs +++ b/crates/http/src/global.rs @@ -3,6 +3,7 @@ use rocket::serde::Serialize; #[derive(Clone, Debug, Serialize)] #[serde(crate = "rocket::serde")] pub struct Global { - pub list_limit: usize, pub format_time: String, + pub list_limit: usize, + pub provider_id: Option, } diff --git a/crates/http/src/main.rs b/crates/http/src/main.rs index ff5be61..1e3302d 100644 --- a/crates/http/src/main.rs +++ b/crates/http/src/main.rs @@ -32,10 +32,12 @@ fn index( time: String, title: String, } - let total = db.contents_total().map_err(|e| { - error!("Could not get contents total: `{e}`"); - Status::InternalServerError - })?; + let total = db + .contents_total_by_provider_id(global.provider_id) + .map_err(|e| { + error!("Could not get contents total: `{e}`"); + Status::InternalServerError + })?; Ok(Template::render( "index", context! { @@ -63,7 +65,7 @@ fn index( back: page.map(|p| uri!(index(search, if p > 2 { Some(p - 1) } else { None }))), next: if page.unwrap_or(1) * global.list_limit >= total { None } else { Some(uri!(index(search, Some(page.map_or(2, |p| p + 1))))) }, - rows: db.contents(Sort::Desc, Some(global.list_limit)).map_err(|e| { + rows: db.contents_by_provider_id(global.provider_id, Sort::Desc, Some(global.list_limit)).map_err(|e| { error!("Could not get contents: `{e}`"); Status::InternalServerError })? @@ -117,14 +119,18 @@ fn info( } #[get("/rss")] -fn rss(meta: &State, db: &State) -> Result, Status> { +fn rss( + global: &State, + meta: &State, + db: &State, +) -> Result, Status> { let mut f = Feed::new( &meta.title, meta.description.as_deref(), 1024, // @TODO ); for c in db - .contents(Sort::Desc, Some(20)) // @TODO + .contents_by_provider_id(global.provider_id, Sort::Desc, Some(20)) // @TODO .map_err(|e| { error!("Could not load channel item contents: `{e}`"); Status::InternalServerError @@ -170,6 +176,7 @@ fn rocket() -> _ { .manage(Global { format_time: config.format_time, list_limit: config.list_limit, + provider_id: config.provider_id, }) .manage(Meta { description: config.description, diff --git a/crates/mysql/src/lib.rs b/crates/mysql/src/lib.rs index 21b6f9d..e601545 100644 --- a/crates/mysql/src/lib.rs +++ b/crates/mysql/src/lib.rs @@ -106,23 +106,29 @@ impl Mysql { ) } - pub fn contents_total(&self) -> Result { - let total: Option = self - .pool - .get_conn()? - .query_first("SELECT COUNT(*) FROM `content`")?; + pub fn contents_total_by_provider_id(&self, provider_id: Option) -> Result { + let total: Option = self.pool.get_conn()?.exec_first( + "SELECT COUNT(*) FROM `content` WHERE `provider_id` = ?", + (provider_id,), + )?; Ok(total.unwrap_or(0)) } - pub fn contents(&self, sort: Sort, limit: Option) -> Result, Error> { - self.pool.get_conn()?.query(format!( + pub fn contents_by_provider_id( + &self, + provider_id: Option, + sort: Sort, + limit: Option, + ) -> Result, Error> { + self.pool.get_conn()?.exec(format!( "SELECT `content_id`, `channel_item_id`, `provider_id`, `title`, - `description` FROM `content` ORDER BY `content_id` {sort} LIMIT {}", + `description` FROM `content` WHERE `provider_id` = ? ORDER BY `content_id` {sort} LIMIT {}", limit.unwrap_or(DEFAULT_LIMIT) - )) + ), + (provider_id, )) } pub fn contents_by_channel_item_id_provider_id(