From 72fec478920949b2bf68438736556cf8cac0b7af Mon Sep 17 00:00:00 2001 From: neon_arch Date: Fri, 22 Sep 2023 19:54:26 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20implement=20code=20to=20fet?= =?UTF-8?q?ch=20results=20based=20on=20parameters,=20cookies=20&=20config?= =?UTF-8?q?=20(#210)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/routes/search.rs | 63 ++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/src/server/routes/search.rs b/src/server/routes/search.rs index dc327d6..9abbb56 100644 --- a/src/server/routes/search.rs +++ b/src/server/routes/search.rs @@ -67,61 +67,48 @@ pub async fn search( None => 1, }; - let safe_search: u8 = match config.safe_search { - 3..=4 => config.safe_search, - _ => match ¶ms.safesearch { - Some(safesearch) => match safesearch { - 0..=2 => *safesearch, - _ => 1, - }, - None => config.safe_search, - }, - }; - let (_, results, _) = join!( results( format!( - "http://{}:{}/search?q={}&page={}&safesearch={}", + "http://{}:{}/search?q={}&page={}&safesearch=", config.binding_ip, config.port, query, page - 1, - safe_search ), &config, &cache, query, page - 1, req.clone(), - safe_search + ¶ms.safesearch ), results( format!( - "http://{}:{}/search?q={}&page={}&safesearch={}", - config.binding_ip, config.port, query, page, safe_search + "http://{}:{}/search?q={}&page={}&safesearch=", + config.binding_ip, config.port, query, page ), &config, &cache, query, page, req.clone(), - safe_search + ¶ms.safesearch ), results( format!( - "http://{}:{}/search?q={}&page={}&safesearch={}", + "http://{}:{}/search?q={}&page={}&safesearch=", config.binding_ip, config.port, query, page + 1, - safe_search ), &config, &cache, query, page + 1, req.clone(), - safe_search + ¶ms.safesearch ) ); @@ -156,7 +143,7 @@ async fn results( query: &str, page: u32, req: HttpRequest, - safe_search: u8, + safe_search: &Option, ) -> Result> { // fetch the cached results json. let cached_results = cache.cached_json(&url).await; @@ -165,7 +152,18 @@ async fn results( match cached_results { Ok(results) => Ok(results), Err(_) => { - if safe_search == 4 { + let mut safe_search_level: u8 = match config.safe_search { + 3..=4 => config.safe_search, + _ => match safe_search { + Some(safesearch) => match safesearch { + 0..=2 => *safesearch, + _ => config.safe_search, + }, + None => config.safe_search, + }, + }; + + if safe_search_level == 4 { let mut results: SearchResults = SearchResults::default(); let mut _flag: bool = is_match_from_filter_list(file_path(FileType::BlockList)?, query)?; @@ -176,6 +174,7 @@ async fn results( results.add_style(&config.style); results.set_page_query(query); cache.cache_results(&results, &url).await?; + results.set_safe_search_level(safe_search_level); return Ok(results); } } @@ -195,6 +194,17 @@ async fn results( .filter_map(|name| EngineHandler::new(name)) .collect(); + safe_search_level = match config.safe_search { + 3..=4 => config.safe_search, + _ => match safe_search { + Some(safesearch) => match safesearch { + 0..=2 => *safesearch, + _ => config.safe_search, + }, + None => cookie_value.safe_search_level, + }, + }; + aggregate( query, page, @@ -202,7 +212,7 @@ async fn results( config.debug, &engines, config.request_timeout, - safe_search, + safe_search_level, ) .await? } @@ -214,7 +224,7 @@ async fn results( config.debug, &config.upstream_search_engines, config.request_timeout, - safe_search, + safe_search_level, ) .await? } @@ -223,7 +233,10 @@ async fn results( results.set_filtered(); } results.add_style(&config.style); - cache.cache_results(&results, &url).await?; + cache + .cache_results(&results, &(format!("{url}{safe_search_level}"))) + .await?; + results.set_safe_search_level(safe_search_level); Ok(results) } }