diff --git a/CHANGELOG.md b/CHANGELOG.md index 47e54ab..ecc6488 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,4 @@ -# First release +# v0.1.2 ## cli -- upload -- list -- search -- users -- categories - -## tokencracker -- cracks tokens +- optimized listing by not searching on the client but on the server diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 58fe93d..7ffdc8d 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cli" -version = "0.1.1" +version = "0.1.2" authors = ["LordMZTE "] edition = "2018" @@ -25,3 +25,5 @@ structopt = "0.3.21" term-table = "1.3.0" term_size = "0.3.2" tokio = { version = "0.2.23", features = ["macros", "fs", "process"] } +url = "2.2.0" + diff --git a/cli/src/commands/list.rs b/cli/src/commands/list.rs index 8aa8d32..3059ade 100644 --- a/cli/src/commands/list.rs +++ b/cli/src/commands/list.rs @@ -12,7 +12,11 @@ pub async fn run( // This needs to be done so both users, memes and categories will be requested // at once let (memes, ..) = tokio::try_join!( - api::memes(http), + api::memes( + http, + cat.as_ref().map(String::as_ref), + user.as_ref().map(String::as_ref) + ), async { if let Some(c) = cat.as_ref() { util::assert_category_exists(http, c).await @@ -29,12 +33,7 @@ pub async fn run( }, )?; - let memes = memes - .into_iter() - .filter(|&m| cat.as_ref().map(|q| &m.category == q).unwrap_or(true)) - .filter(|&m| user.as_ref().map(|q| &m.user == q).unwrap_or(true)); - - let mut memes = memes.collect::>(); + let mut memes = memes.iter().collect::>(); if let Some(s) = sorting { s.sort_with(&mut memes); diff --git a/cli/src/commands/search.rs b/cli/src/commands/search.rs index e1a4705..c4b83ac 100644 --- a/cli/src/commands/search.rs +++ b/cli/src/commands/search.rs @@ -10,9 +10,12 @@ pub async fn run( user: Option, cat: Option, ) -> Result<()> { - let memes = api::memes(http); let (memes, ..) = tokio::try_join!( - memes, + api::memes( + http, + cat.as_ref().map(String::as_ref), + user.as_ref().map(String::as_ref), + ), async { if let Some(u) = user.as_ref() { util::assert_user_exists(http, u).await @@ -33,11 +36,6 @@ pub async fn run( info!("Starting search with query '{}'", query); - let memes = memes - .iter() - .filter(|m| user.as_ref().map(|u| u == &m.user).unwrap_or(true)) - .filter(|m| cat.as_ref().map(|u| u == &m.category).unwrap_or(true)); - for meme in memes { let file_name = meme.file_name()?; if let Some(score) = fuzzy_matcher::clangd::fuzzy_match(file_name, &query) { diff --git a/cli/src/util.rs b/cli/src/util.rs index ec4af35..105923b 100644 --- a/cli/src/util.rs +++ b/cli/src/util.rs @@ -30,6 +30,7 @@ pub mod api { use log::info; use once_cell::sync::OnceCell; use reqwest::Client; + use url::Url; // cached api responses static CATS: OnceCell> = OnceCell::new(); @@ -48,13 +49,28 @@ pub mod api { })) } - pub async fn memes(http: &Client) -> Result<&Vec> { + pub async fn memes<'a>( + http: &Client, + cat_filter: Option<&str>, + usr_filter: Option<&str>, + ) -> Result<&'a Vec> { Ok(init_once_cell!(MEMES, { + let mut url = Url::options().parse("https://data.tilera.xyz/api/jensmemes/memes")?; + let mut pairs = url.query_pairs_mut(); + + if let Some(cat) = cat_filter { + pairs.append_pair("category", cat); + } + + if let Some(usr) = usr_filter { + pairs.append_pair("user", usr); + } + + // drop required in order to move the URL into the request + drop(pairs); + info!("Requesting memes from server"); - let res = http - .get("https://data.tilera.xyz/api/jensmemes/memes") - .send() - .await?; + let res = http.get(url).send().await?; let memes = serde_json::from_slice::(&res.bytes().await?)?; memes.memes