From 969e40ed4083723dac8213460d3e3ea12267f53d Mon Sep 17 00:00:00 2001 From: Joseph Johansen Date: Fri, 14 Jun 2024 19:12:22 +0100 Subject: [PATCH] Initial commit --- locales/en-US.json | 1 + spec/helpers/errors_spec.cr | 31 ++++++++++ spec/invidious/helpers_spec.cr | 2 - spec/spec_helper.cr | 15 +++++ src/invidious/config.cr | 2 +- src/invidious/helpers/errors.cr | 56 +++++++++++-------- .../views/components/error_redirect.ecr | 8 +++ 7 files changed, 88 insertions(+), 27 deletions(-) create mode 100644 spec/helpers/errors_spec.cr create mode 100644 src/invidious/views/components/error_redirect.ecr diff --git a/locales/en-US.json b/locales/en-US.json index 3987f796..41bb5e35 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -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", diff --git a/spec/helpers/errors_spec.cr b/spec/helpers/errors_spec.cr new file mode 100644 index 00000000..70d42660 --- /dev/null +++ b/spec/helpers/errors_spec.cr @@ -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 "

After which you should try to:

\n\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 "

After which you should try to:

\n\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 diff --git a/spec/invidious/helpers_spec.cr b/spec/invidious/helpers_spec.cr index 9fbb6d6f..35387f09 100644 --- a/spec/invidious/helpers_spec.cr +++ b/spec/invidious/helpers_spec.cr @@ -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 diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index b3060acf..9a77d7ef 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -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 diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 09c2168b..a13fe873 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -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? diff --git a/src/invidious/helpers/errors.cr b/src/invidious/helpers/errors.cr index 21b789bc..3e32489c 100644 --- a/src/invidious/helpers/errors.cr +++ b/src/invidious/helpers/errors.cr @@ -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 -

#{next_steps_text}

- - 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 diff --git a/src/invidious/views/components/error_redirect.ecr b/src/invidious/views/components/error_redirect.ecr new file mode 100644 index 00000000..a648f5e7 --- /dev/null +++ b/src/invidious/views/components/error_redirect.ecr @@ -0,0 +1,8 @@ +

<%= next_steps_text %>

+