implement provider_id filter

This commit is contained in:
yggverse 2026-01-08 11:12:42 +02:00
parent aaad4fd49d
commit 5608e2e081
4 changed files with 36 additions and 17 deletions

View file

@ -18,6 +18,11 @@ pub struct Config {
#[arg(long, default_value_t = String::from("%d/%m/%Y %H:%M"))] #[arg(long, default_value_t = String::from("%d/%m/%Y %H:%M"))]
pub format_time: String, pub format_time: String,
/// Provider ID (`provider` table)
/// * None for the original content
#[arg(long, short)]
pub provider_id: Option<u64>,
/// Default listing limit /// Default listing limit
#[arg(long, default_value_t = 20)] #[arg(long, default_value_t = 20)]
pub list_limit: usize, pub list_limit: usize,

View file

@ -3,6 +3,7 @@ use rocket::serde::Serialize;
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
pub struct Global { pub struct Global {
pub list_limit: usize,
pub format_time: String, pub format_time: String,
pub list_limit: usize,
pub provider_id: Option<u64>,
} }

View file

@ -32,10 +32,12 @@ fn index(
time: String, time: String,
title: String, title: String,
} }
let total = db.contents_total().map_err(|e| { let total = db
error!("Could not get contents total: `{e}`"); .contents_total_by_provider_id(global.provider_id)
Status::InternalServerError .map_err(|e| {
})?; error!("Could not get contents total: `{e}`");
Status::InternalServerError
})?;
Ok(Template::render( Ok(Template::render(
"index", "index",
context! { context! {
@ -63,7 +65,7 @@ fn index(
back: page.map(|p| uri!(index(search, if p > 2 { Some(p - 1) } else { None }))), 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 } next: if page.unwrap_or(1) * global.list_limit >= total { None }
else { Some(uri!(index(search, Some(page.map_or(2, |p| p + 1))))) }, 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}`"); error!("Could not get contents: `{e}`");
Status::InternalServerError Status::InternalServerError
})? })?
@ -117,14 +119,18 @@ fn info(
} }
#[get("/rss")] #[get("/rss")]
fn rss(meta: &State<Meta>, db: &State<Mysql>) -> Result<RawXml<String>, Status> { fn rss(
global: &State<Global>,
meta: &State<Meta>,
db: &State<Mysql>,
) -> Result<RawXml<String>, Status> {
let mut f = Feed::new( let mut f = Feed::new(
&meta.title, &meta.title,
meta.description.as_deref(), meta.description.as_deref(),
1024, // @TODO 1024, // @TODO
); );
for c in db 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| { .map_err(|e| {
error!("Could not load channel item contents: `{e}`"); error!("Could not load channel item contents: `{e}`");
Status::InternalServerError Status::InternalServerError
@ -170,6 +176,7 @@ fn rocket() -> _ {
.manage(Global { .manage(Global {
format_time: config.format_time, format_time: config.format_time,
list_limit: config.list_limit, list_limit: config.list_limit,
provider_id: config.provider_id,
}) })
.manage(Meta { .manage(Meta {
description: config.description, description: config.description,

View file

@ -106,23 +106,29 @@ impl Mysql {
) )
} }
pub fn contents_total(&self) -> Result<usize, Error> { pub fn contents_total_by_provider_id(&self, provider_id: Option<u64>) -> Result<usize, Error> {
let total: Option<usize> = self let total: Option<usize> = self.pool.get_conn()?.exec_first(
.pool "SELECT COUNT(*) FROM `content` WHERE `provider_id` = ?",
.get_conn()? (provider_id,),
.query_first("SELECT COUNT(*) FROM `content`")?; )?;
Ok(total.unwrap_or(0)) Ok(total.unwrap_or(0))
} }
pub fn contents(&self, sort: Sort, limit: Option<usize>) -> Result<Vec<Content>, Error> { pub fn contents_by_provider_id(
self.pool.get_conn()?.query(format!( &self,
provider_id: Option<u64>,
sort: Sort,
limit: Option<usize>,
) -> Result<Vec<Content>, Error> {
self.pool.get_conn()?.exec(format!(
"SELECT `content_id`, "SELECT `content_id`,
`channel_item_id`, `channel_item_id`,
`provider_id`, `provider_id`,
`title`, `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) limit.unwrap_or(DEFAULT_LIMIT)
)) ),
(provider_id, ))
} }
pub fn contents_by_channel_item_id_provider_id( pub fn contents_by_channel_item_id_provider_id(