mirror of
https://github.com/YGGverse/rssto.git
synced 2026-03-31 17:15:29 +00:00
implement provider_id filter
This commit is contained in:
parent
aaad4fd49d
commit
5608e2e081
4 changed files with 36 additions and 17 deletions
|
|
@ -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<u64>,
|
||||
|
||||
/// Default listing limit
|
||||
#[arg(long, default_value_t = 20)]
|
||||
pub list_limit: usize,
|
||||
|
|
|
|||
|
|
@ -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<u64>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<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(
|
||||
&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,
|
||||
|
|
|
|||
|
|
@ -106,23 +106,29 @@ impl Mysql {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn contents_total(&self) -> Result<usize, Error> {
|
||||
let total: Option<usize> = self
|
||||
.pool
|
||||
.get_conn()?
|
||||
.query_first("SELECT COUNT(*) FROM `content`")?;
|
||||
pub fn contents_total_by_provider_id(&self, provider_id: Option<u64>) -> Result<usize, Error> {
|
||||
let total: Option<usize> = 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<usize>) -> Result<Vec<Content>, Error> {
|
||||
self.pool.get_conn()?.query(format!(
|
||||
pub fn contents_by_provider_id(
|
||||
&self,
|
||||
provider_id: Option<u64>,
|
||||
sort: Sort,
|
||||
limit: Option<usize>,
|
||||
) -> Result<Vec<Content>, 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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue