Invidious is an alternative front-end to YouTube
Add #to_http_params method to Query (Fixes #3148)
Samantaz Fox0e3820b634cd
diff --git a/spec/invidious/search/query_spec.cr b/spec/invidious/search/query_spec.crindex 4853e9e9..063b69f1 100644--- a/spec/invidious/search/query_spec.cr+++ b/spec/invidious/search/query_spec.cr@@ -197,4 +197,46 @@ Spectator.describe Invidious::Search::Query do)endend++ describe "#to_http_params" do+ it "formats regular search" do+ query = described_class.new(+ HTTP::Params.parse("q=The+Simpsons+hiding+in+bush&duration=short"),+ Invidious::Search::Query::Type::Regular, nil+ )++ params = query.to_http_params++ expect(params).to have_key("duration")+ expect(params["duration"]?).to eq("short")++ expect(params).to have_key("q")+ expect(params["q"]?).to eq("The Simpsons hiding in bush")++ # Check if there aren't other parameters+ params.delete("duration")+ params.delete("q")+ expect(params).to be_empty+ end++ it "formats channel search" do+ query = described_class.new(+ HTTP::Params.parse("q=channel:UC2DjFE7Xf11URZqWBigcVOQ%20multimeter"),+ Invidious::Search::Query::Type::Regular, nil+ )++ params = query.to_http_params++ expect(params).to have_key("channel")+ expect(params["channel"]?).to eq("UC2DjFE7Xf11URZqWBigcVOQ")++ expect(params).to have_key("q")+ expect(params["q"]?).to eq("multimeter")++ # Check if there aren't other parameters+ params.delete("channel")+ params.delete("q")+ expect(params).to be_empty+ end+ endenddiff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.crindex 6f8bffea..2a9705cf 100644--- a/src/invidious/routes/search.cr+++ b/src/invidious/routes/search.cr@@ -59,6 +59,12 @@ module Invidious::Routes::Searchreturn error_template(500, ex)end+ params = query.to_http_params+ url_prev_page = "/search?#{params}&page=#{query.page - 1}"+ url_next_page = "/search?#{params}&page=#{query.page + 1}"++ redirect_url = Invidious::Frontend::Misc.redirect_url(env)+env.set "search", query.texttemplated "search"enddiff --git a/src/invidious/search/query.cr b/src/invidious/search/query.crindex 34b36b1d..24e79609 100644--- a/src/invidious/search/query.cr+++ b/src/invidious/search/query.cr@@ -57,7 +57,7 @@ module Invidious::Search# Get the page number (also common to all search types)@page = params["page"]?.try &.to_i? || 1- # Stop here is raw query in empty+ # Stop here if raw query is empty# NOTE: maybe raise in the future?return if self.empty_raw_query?@@ -127,6 +127,16 @@ module Invidious::Searchreturn itemsend+ # Return the HTTP::Params corresponding to this Query (invidious format)+ def to_http_params : HTTP::Params+ params = @filters.to_iv_params++ params["q"] = @query+ params["channel"] = @channel if [email protected]?++ return params+ end+# TODO: clean codeprivate def unnest_items(all_items) : Array(SearchItem)items = [] of SearchItemdiff --git a/src/invidious/views/search.ecr b/src/invidious/views/search.ecrindex 7110703e..254449a1 100644--- a/src/invidious/views/search.ecr+++ b/src/invidious/views/search.ecr@@ -3,16 +3,6 @@<link rel="stylesheet" href="/css/search.css?v=<%= ASSET_COMMIT %>"><% end %>-<%-- search_query_encoded = URI.encode_www_form(query.text, space_to_plus: true)- filter_params = query.filters.to_iv_params-- url_prev_page = "/search?q=#{search_query_encoded}&#{filter_params}&page=#{query.page - 1}"- url_next_page = "/search?q=#{search_query_encoded}&#{filter_params}&page=#{query.page + 1}"-- redirect_url = Invidious::Frontend::Misc.redirect_url(env)--%>-<!-- Search redirection and filtering UI --><%= Invidious::Frontend::SearchFilters.generate(query.filters, query.text, query.page, locale) %><hr/>