summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamantaz Fox <coding@samantaz.fr>2022-06-14 00:04:19 +0200
committerSamantaz Fox <coding@samantaz.fr>2022-07-04 22:21:27 +0200
commit0e3820b634cd94a647af099805d3957cd5c8998c (patch)
treef0361bafac7fea1ccf8d1ee4942380a26e8578e1
parent6c73614a47a5a88df45aa32fadb64e85595d2a18 (diff)
downloadinvidious-0e3820b634cd94a647af099805d3957cd5c8998c.tar.gz
invidious-0e3820b634cd94a647af099805d3957cd5c8998c.tar.bz2
invidious-0e3820b634cd94a647af099805d3957cd5c8998c.zip
Add #to_http_params method to Query (Fixes #3148)
-rw-r--r--spec/invidious/search/query_spec.cr42
-rw-r--r--src/invidious/routes/search.cr6
-rw-r--r--src/invidious/search/query.cr12
-rw-r--r--src/invidious/views/search.ecr10
4 files changed, 59 insertions, 11 deletions
diff --git a/spec/invidious/search/query_spec.cr b/spec/invidious/search/query_spec.cr
index 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
)
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
diff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.cr
index 6f8bffea..2a9705cf 100644
--- a/src/invidious/routes/search.cr
+++ b/src/invidious/routes/search.cr
@@ -59,6 +59,12 @@ module Invidious::Routes::Search
return 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.text
templated "search"
end
diff --git a/src/invidious/search/query.cr b/src/invidious/search/query.cr
index 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::Search
return items
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
private def unnest_items(all_items) : Array(SearchItem)
items = [] of SearchItem
diff --git a/src/invidious/views/search.ecr b/src/invidious/views/search.ecr
index 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/>