From a2f5342a83f2b68a1a980eb5f1e92f3f74188ab9 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Sun, 13 Jun 2021 21:40:11 +0200 Subject: [PATCH 1/2] Multiple search fixes * Remove percent-encoding of the search query when calling youtube API, as it breaks UTF-8 * Empty search redirects to /search, not / * Show the fullscreen search "home page" (from #1977) at /search * Allow 'region=' parameter to be passed to /search * Other minor fixes --- src/invidious/helpers/youtube_api.cr | 2 +- src/invidious/routes/search.cr | 24 +++++++++++++++--------- src/invidious/views/search_homepage.ecr | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/invidious/helpers/youtube_api.cr b/src/invidious/helpers/youtube_api.cr index 544e635b..e27d4088 100644 --- a/src/invidious/helpers/youtube_api.cr +++ b/src/invidious/helpers/youtube_api.cr @@ -79,7 +79,7 @@ end def request_youtube_api_search(search_query : String, params : String, region = nil) # JSON Request data, required by the API data = { - "query" => URI.encode_www_form(search_query), + "query" => search_query, "context" => make_youtube_api_context(region), "params" => params, } diff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.cr index a993a17a..cb05435e 100644 --- a/src/invidious/routes/search.cr +++ b/src/invidious/routes/search.cr @@ -20,15 +20,17 @@ class Invidious::Routes::Search < Invidious::Routes::BaseRoute query = env.params.query["search_query"]? query ||= env.params.query["q"]? - query ||= "" - page = env.params.query["page"]?.try &.to_i? - page ||= 1 + page = env.params.query["page"]? - if query - env.redirect "/search?q=#{URI.encode_www_form(query)}&page=#{page}" + if query && !query.empty? + if page && !page.empty? + env.redirect "/search?q=" + URI.encode_www_form(query) + "&page=" + page + else + env.redirect "/search?q=" + URI.encode_www_form(query) + end else - env.redirect "/" + env.redirect "/search" end end @@ -38,9 +40,13 @@ class Invidious::Routes::Search < Invidious::Routes::BaseRoute query = env.params.query["search_query"]? query ||= env.params.query["q"]? - query ||= "" - return env.redirect "/" if query.empty? + if !query || query.empty? + # Display the full page search box implemented in #1977 + env.set "search", "" + templated "search_homepage", navbar_search: false + return + end page = env.params.query["page"]?.try &.to_i? page ||= 1 @@ -48,7 +54,7 @@ class Invidious::Routes::Search < Invidious::Routes::BaseRoute user = env.get? "user" begin - search_query, count, videos, operators = process_search_query(query, page, user, region: nil) + search_query, count, videos, operators = process_search_query(query, page, user, region: region) rescue ex return error_template(500, ex) end diff --git a/src/invidious/views/search_homepage.ecr b/src/invidious/views/search_homepage.ecr index 8927c3f1..7d2dab83 100644 --- a/src/invidious/views/search_homepage.ecr +++ b/src/invidious/views/search_homepage.ecr @@ -1,7 +1,7 @@ <% content_for "header" do %> "> - Invidious + Invidious - <%= translate(locale, "search") %> <% end %> From 3de92b337db9388152caf0e931cfbb07822198df Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 14 Jun 2021 17:10:02 +0200 Subject: [PATCH 2/2] Use if/else instead of return in search route --- src/invidious/routes/search.cr | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.cr index cb05435e..513904b8 100644 --- a/src/invidious/routes/search.cr +++ b/src/invidious/routes/search.cr @@ -45,27 +45,26 @@ class Invidious::Routes::Search < Invidious::Routes::BaseRoute # Display the full page search box implemented in #1977 env.set "search", "" templated "search_homepage", navbar_search: false - return - end + else + page = env.params.query["page"]?.try &.to_i? + page ||= 1 - page = env.params.query["page"]?.try &.to_i? - page ||= 1 + user = env.get? "user" - user = env.get? "user" + begin + search_query, count, videos, operators = process_search_query(query, page, user, region: region) + rescue ex + return error_template(500, ex) + end - begin - search_query, count, videos, operators = process_search_query(query, page, user, region: region) - rescue ex - return error_template(500, ex) - end + operator_hash = {} of String => String + operators.each do |operator| + key, value = operator.downcase.split(":") + operator_hash[key] = value + end - operator_hash = {} of String => String - operators.each do |operator| - key, value = operator.downcase.split(":") - operator_hash[key] = value + env.set "search", query + templated "search" end - - env.set "search", query - templated "search" end end