From ba6ecfcc1bfd1c05e085dcb0ee56800131251dc5 Mon Sep 17 00:00:00 2001 From: aglkm <39521015+aglkm@users.noreply.github.com> Date: Thu, 3 Jul 2025 13:57:31 +0300 Subject: [PATCH] improved query search --- src/main.rs | 41 +++++++++++++++++++++----------------- templates/base.html.tera | 2 +- templates/error.html.tera | 2 +- templates/search.html.tera | 4 ++-- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index c8eb548..b54af23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,51 +161,56 @@ async fn output(commit: &str) -> Template { // Handling search request. -// Using Option<&str> to match '/search' query without input params. +// Using Option<&str> to match '/search' query without query params. // https://github.com/rwf2/Rocket/issues/608 -#[get("/search?")] -pub async fn search(input: Option<&str>) -> Either { +#[get("/search?")] +pub async fn search(query: Option<&str>) -> Either { // Unwrap Option and forward to Search page if no parameters - let input = match input { + let query = match query { Some(value) => value, None => return Either::Left(Template::render("search", context! { route: "search", })), }; - // Trim and lowercase the input - let input = input.trim().to_lowercase(); + // Trim and lowercase the query + let query = query.trim().to_lowercase(); // Check for valid chars - if input.chars().all(|x| (x >= 'a' && x <= 'f') || (x >= '0' && x <= '9')) == true { + if query.chars().all(|x| (x >= 'a' && x <= 'f') || (x >= '0' && x <= '9')) == true { // Block number - if input.chars().all(char::is_numeric) == true { - return Either::Right(Redirect::to(uri!(block_details_by_height(input)))); + if query.chars().all(char::is_numeric) == true { + return Either::Right(Redirect::to(uri!(block_details_by_height(query)))); // Block hash - } else if input.len() == 64 { - return Either::Right(Redirect::to(uri!(block_header_by_hash(input)))); + } else if query.len() == 64 { + return Either::Right(Redirect::to(uri!(block_header_by_hash(query)))); // Kernel or Unspent Output - } else if input.len() == 66 { + } else if query.len() == 66 { // First search for Kernel. // If found, redirect to Kernel page, otherwise search for Unspent Output. - // As we can't distinguish between Kernel and Output, this will produce a redundant - // get_kernel call, but will allow for better UI (no need to ask user to input the type - // of the search request). + // As we can't distinguish between Kernel and Output, this will produce redundant + // get_kernel and get_output calls, but will allow for better UI (no need to ask user to + // input the type of the search request). let mut kernel = Kernel::new(); + let mut output = Output::new(); - let _ = requests::get_kernel(&input, &mut kernel).await; + let _ = requests::get_kernel(&query, &mut kernel).await; if kernel.excess.is_empty() == false { // Here we are redirecting to kernel page and call get_kernel again there. // Kernel page is a separate route and we want it to be accessed directly and // via search functionality. - return Either::Right(Redirect::to(uri!(kernel(input)))); + return Either::Right(Redirect::to(uri!(kernel(query)))); } else { // If Kernel not found, then search for Unspent Output - return Either::Right(Redirect::to(uri!(output(input)))); + let _ = requests::get_output(&query, &mut output).await; + + if output.commit.is_empty() == false { + return Either::Right(Redirect::to(uri!(output(query)))); + } } } } diff --git a/templates/base.html.tera b/templates/base.html.tera index edb41d0..93703a0 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -108,7 +108,7 @@