NorkzYT e238624e8f feat(config.cr): introduce PagesEnabled struct for managing feature toggles for pages
refactor(routes): replace direct page_enabled checks with centralized logic in before_all.cr for cleaner endpoint management
chore(routes): remove redundant page_enabled checks from individual routes to streamline code and improve maintainability
2025-06-07 11:52:13 +00:00

119 lines
3.6 KiB
Crystal
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{% skip_file if flag?(:api_only) %}
module Invidious::Routes::Search
def self.opensearch(env)
locale = env.get("preferences").as(Preferences).locale
env.response.content_type = "application/opensearchdescription+xml"
XML.build(indent: " ", encoding: "UTF-8") do |xml|
xml.element("OpenSearchDescription", xmlns: "http://a9.com/-/spec/opensearch/1.1/") do
xml.element("ShortName") { xml.text "Invidious" }
xml.element("LongName") { xml.text "Invidious Search" }
xml.element("Description") { xml.text "Search for videos, channels, and playlists on Invidious" }
xml.element("InputEncoding") { xml.text "UTF-8" }
xml.element("Image", width: 48, height: 48, type: "image/x-icon") { xml.text "#{HOST_URL}/favicon.ico" }
xml.element("Url", type: "text/html", method: "get", template: "#{HOST_URL}/search?q={searchTerms}")
end
end
end
def self.results(env)
locale = env.get("preferences").as(Preferences).locale
query = env.params.query["search_query"]?
query ||= env.params.query["q"]?
page = env.params.query["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 "/search"
end
end
def self.search(env)
prefs = env.get("preferences").as(Preferences)
locale = prefs.locale
# otherwise, do a normal search
region = env.params.query["region"]? || prefs.region
query = Invidious::Search::Query.new(env.params.query, :regular, region)
# empty query → show homepage
if query.empty?
env.set "search", ""
return templated "search_homepage", navbar_search: false
end
# nonempty query → process it
user = env.get?("user")
if query.url?
return env.redirect UrlSanitizer.process(query.text).to_s
end
begin
items = user ? query.process(user.as(User)) : query.process
rescue ex : ChannelSearchException
return error_template 404, "Unable to find channel with id “#{HTML.escape(ex.channel)}”…"
rescue ex
return error_template 500, ex
end
redirect_url = Invidious::Frontend::Misc.redirect_url(env)
# Pagination
page_nav_html = Frontend::Pagination.nav_numeric(locale,
base_url: "/search?#{query.to_http_params}",
current_page: query.page,
show_next: (items.size >= 20)
)
# If it's a channel search, prefix the box; otherwise just show the text
if query.type == Invidious::Search::Query::Type::Channel
env.set "search", "channel:#{query.channel} #{query.text}"
else
env.set "search", query.text
end
templated "search"
end
def self.hashtag(env : HTTP::Server::Context)
locale = env.get("preferences").as(Preferences).locale
hashtag = env.params.url["hashtag"]?
if hashtag.nil? || hashtag.empty?
return error_template(400, "Invalid request")
end
page = env.params.query["page"]?
if page.nil?
page = 1
else
page = Math.max(1, page.to_i)
env.params.query.delete_all("page")
end
begin
items = Invidious::Hashtag.fetch(hashtag, page)
rescue ex
return error_template(500, ex)
end
# Pagination
hashtag_encoded = URI.encode_www_form(hashtag, space_to_plus: false)
page_nav_html = Frontend::Pagination.nav_numeric(locale,
base_url: "/hashtag/#{hashtag_encoded}",
current_page: page,
show_next: (items.size >= 60)
)
templated "hashtag"
end
end