Add #to_http_params method to Query (Fixes #3148)

This commit is contained in:
Samantaz Fox 2022-06-14 00:04:19 +02:00
parent 6c73614a47
commit 0e3820b634
No known key found for this signature in database
GPG key ID: F42821059186176E
4 changed files with 59 additions and 11 deletions

View file

@ -197,4 +197,46 @@ Spectator.describe Invidious::Search::Query do
) )
end end
end end
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
end
end end

View file

@ -59,6 +59,12 @@ module Invidious::Routes::Search
return error_template(500, ex) return error_template(500, ex)
end 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.text env.set "search", query.text
templated "search" templated "search"
end end

View file

@ -57,7 +57,7 @@ module Invidious::Search
# Get the page number (also common to all search types) # Get the page number (also common to all search types)
@page = params["page"]?.try &.to_i? || 1 @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? # NOTE: maybe raise in the future?
return if self.empty_raw_query? return if self.empty_raw_query?
@ -127,6 +127,16 @@ module Invidious::Search
return items return items
end end
# 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 !@channel.empty?
return params
end
# TODO: clean code # TODO: clean code
private def unnest_items(all_items) : Array(SearchItem) private def unnest_items(all_items) : Array(SearchItem)
items = [] of SearchItem items = [] of SearchItem

View file

@ -3,16 +3,6 @@
<link rel="stylesheet" href="/css/search.css?v=<%= ASSET_COMMIT %>"> <link rel="stylesheet" href="/css/search.css?v=<%= ASSET_COMMIT %>">
<% end %> <% 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 --> <!-- Search redirection and filtering UI -->
<%= Invidious::Frontend::SearchFilters.generate(query.filters, query.text, query.page, locale) %> <%= Invidious::Frontend::SearchFilters.generate(query.filters, query.text, query.page, locale) %>
<hr/> <hr/>