Initial commit

This commit is contained in:
Joseph Johansen 2024-06-14 19:12:22 +01:00
parent 1ae14cc224
commit 969e40ed40
7 changed files with 88 additions and 27 deletions

View File

@ -461,6 +461,7 @@
"next_steps_error_message": "After which you should try to: ",
"next_steps_error_message_refresh": "Refresh",
"next_steps_error_message_go_to_youtube": "Go to YouTube",
"next_steps_error_message_open_embed_as_video": "Open in new page",
"footer_donate_page": "Donate",
"footer_documentation": "Documentation",
"footer_source_code": "Source code",

View File

@ -0,0 +1,31 @@
require "../spec_helper"
require "kilt"
Spectator.describe "error_redirect_helper" do
it "shows next steps on embed page errors" do
current_url = "/embed/IeQT18gaB-c?si=YxBQzH-GBSTS4vBS"
test_env = test_env current_url
test_env.set "current_page", current_url
html = error_redirect_helper(test_env)
expect(html).to eq "<p style=\"margin-bottom: 4px;\">After which you should try to: </p>\n<ul>\n \n <li>\n <a href=\"/embed/IeQT18gaB-c?si=YxBQzH-GBSTS4vBS\">Refresh</a>\n </li>\n \n <li>\n <a href=\"/redirect?referer=/embed/IeQT18gaB-c?si=YxBQzH-GBSTS4vBS\">Switch Invidious Instance</a>\n </li>\n \n <li>\n <a href=\"https://youtube.com/embed/IeQT18gaB-c?si=YxBQzH-GBSTS4vBS\">Go to YouTube</a>\n </li>\n \n <li>\n <a href=\"/watch?v=IeQT18gaB-c?si=YxBQzH-GBSTS4vBS\">Open in new page</a>\n </li>\n \n</ul>\n"
end
it "shows next steps for watch pages" do
current_url = "/watch?v=IeQT18gaB-c?si=YxBQzH-GBSTS4vBS"
test_env = test_env current_url
test_env.set "current_page", current_url
html = error_redirect_helper(test_env)
expect(html).to eq "<p style=\"margin-bottom: 4px;\">After which you should try to: </p>\n<ul>\n \n <li>\n <a href=\"/watch?v=IeQT18gaB-c?si=YxBQzH-GBSTS4vBS\">Refresh</a>\n </li>\n \n <li>\n <a href=\"/redirect?referer=/watch?v=IeQT18gaB-c?si=YxBQzH-GBSTS4vBS\">Switch Invidious Instance</a>\n </li>\n \n <li>\n <a href=\"https://youtube.com/watch?v=IeQT18gaB-c?si=YxBQzH-GBSTS4vBS\">Go to YouTube</a>\n </li>\n \n</ul>\n"
end
it "returns an empty string for unknown pages" do
current_url = "/foo"
test_env = test_env current_url
test_env.set "current_page", current_url
html = error_redirect_helper(test_env)
expect(html).to eq ""
end
end

View File

@ -1,7 +1,5 @@
require "../spec_helper"
CONFIG = Config.from_yaml(File.open("config/config.example.yml"))
Spectator.describe "Helper" do
describe "#produce_channel_search_continuation" do
it "correctly produces token for searching a specific channel" do

View File

@ -10,9 +10,24 @@ require "../src/invidious/videos"
require "../src/invidious/playlists"
require "../src/invidious/search/ctoken"
require "../src/invidious/trending"
require "../src/invidious/config"
require "../src/invidious/user/preferences.cr"
require "spectator"
CONFIG = Config.from_yaml(File.open("config/config.example.yml"))
OUTPUT = CONFIG.output.upcase == "STDOUT" ? STDOUT : File.open(CONFIG.output, mode: "a")
LOGGER = Invidious::LogHandler.new(OUTPUT, CONFIG.log_level)
Spectator.configure do |config|
config.fail_blank
config.randomize
end
def test_env(current_url : String, request_method : String = "GET", response : IO = String::Builder.new)
con = ContextWithPreferences.new(
HTTP::Request.new(request_method, current_url),
HTTP::Server::Response.new(response),
)
con.preferences = Preferences.new(CONFIG.default_user_preferences.to_tuple)
con
end

View File

@ -80,7 +80,7 @@ class Config
property full_refresh : Bool = false
# Jobs config structure. See jobs.cr and jobs/base_job.cr
property jobs = Invidious::Jobs::JobsConfig.new
# property jobs = Invidious::Jobs::JobsConfig.new
# Used to tell Invidious it is behind a proxy, so links to resources should be https://
property https_only : Bool?

View File

@ -2,6 +2,10 @@
# Issue template
# -------------------
class ContextWithPreferences < HTTP::Server::Context
property preferences : Preferences?
end
macro error_template(*args)
error_template_helper(env, {{args.splat}})
end
@ -168,33 +172,37 @@ end
# Redirect
# -------------------
def error_redirect_helper(env : HTTP::Server::Context)
def error_redirect_helper(env : ContextWithPreferences)
request_path = env.request.path
locale = env.get("preferences").as(Preferences).locale
locale = env.preferences.try &.locale
if request_path.starts_with?("/search") || request_path.starts_with?("/watch") ||
request_path.starts_with?("/channel") || request_path.starts_with?("/playlist?list=PL")
next_steps_text = translate(locale, "next_steps_error_message")
refresh = translate(locale, "next_steps_error_message_refresh")
go_to_youtube = translate(locale, "next_steps_error_message_go_to_youtube")
switch_instance = translate(locale, "Switch Invidious Instance")
display_on_path = %w[
/search
/channel
/watch
/playlist?list=PL
/embed
]
return <<-END_HTML
<p style="margin-bottom: 4px;">#{next_steps_text}</p>
<ul>
<li>
<a href="#{env.request.resource}">#{refresh}</a>
</li>
<li>
<a href="/redirect?referer=#{env.get("current_page")}">#{switch_instance}</a>
</li>
<li>
<a href="https://youtube.com#{env.request.resource}">#{go_to_youtube}</a>
</li>
</ul>
END_HTML
else
return ""
return "" if display_on_path.none? { |path| request_path.starts_with? path }
next_steps_text = translate(locale, "next_steps_error_message")
refresh = translate(locale, "next_steps_error_message_refresh")
go_to_youtube = translate(locale, "next_steps_error_message_go_to_youtube")
switch_instance = translate(locale, "Switch Invidious Instance")
steps = {
refresh => env.request.resource,
switch_instance => "/redirect?referer=#{env.get("current_page")}",
go_to_youtube => "https://youtube.com#{env.request.resource}"
}
if request_path.starts_with?("/embed")
open_embed_as_video = translate(locale, "next_steps_error_message_open_embed_as_video")
non_embed_url = env.request.resource.sub("/embed/", "/watch?v=")
steps[open_embed_as_video] = non_embed_url
end
return rendered "components/error_redirect"
end

View File

@ -0,0 +1,8 @@
<p style="margin-bottom: 4px;"><%= next_steps_text %></p>
<ul>
<% steps.each do |label, href| %>
<li>
<a href="<%= href %>"><%= label %></a>
</li>
<% end %>
</ul>