From 969e40ed4083723dac8213460d3e3ea12267f53d Mon Sep 17 00:00:00 2001 From: Joseph Johansen Date: Fri, 14 Jun 2024 19:12:22 +0100 Subject: [PATCH 1/4] 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 | 60 +++++++++++-------- .../views/components/error_redirect.ecr | 8 +++ 7 files changed, 90 insertions(+), 29 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 3987f796e..41bb5e35c 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 000000000..70d426600 --- /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 9fbb6d6fe..35387f093 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 b3060acfa..9a77d7ef7 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 09c2168b8..a13fe873f 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 21b789bc5..3e32489c1 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 - - 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") - - return <<-END_HTML -

#{next_steps_text}

- - END_HTML - else - return "" + locale = env.preferences.try &.locale + + display_on_path = %w[ + /search + /channel + /watch + /playlist?list=PL + /embed + ] + + 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 000000000..a648f5e73 --- /dev/null +++ b/src/invidious/views/components/error_redirect.ecr @@ -0,0 +1,8 @@ +

<%= next_steps_text %>

+ From a333bc6efe75b07dea68027a494fd72caa0c592c Mon Sep 17 00:00:00 2001 From: Joseph Johansen Date: Fri, 21 Jun 2024 17:57:02 +0100 Subject: [PATCH 2/4] Minor cleanup --- locales/en-US.json | 1 - spec/env_helper.cr | 34 +++++++++++++++++++++++++++++++++ spec/helpers/errors_spec.cr | 2 +- spec/spec_helper.cr | 9 --------- src/invidious/helpers/errors.cr | 15 ++------------- 5 files changed, 37 insertions(+), 24 deletions(-) create mode 100644 spec/env_helper.cr diff --git a/locales/en-US.json b/locales/en-US.json index 41bb5e35c..3987f796e 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -461,7 +461,6 @@ "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/env_helper.cr b/spec/env_helper.cr new file mode 100644 index 000000000..6cfb84ebe --- /dev/null +++ b/spec/env_helper.cr @@ -0,0 +1,34 @@ +require "./spec_helper" + +class ContextWithPreferences < HTTP::Server::Context + property preferences : Preferences? + + def get(key : String) + return preferences if key == "preferences" + + super + end + + def get?(key : String) + return preferences if key == "preferences" + + super + end + + def set(key : String, val : Preferences) + if key == "preferences" + self.preferences = val + else + super + end + end +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/spec/helpers/errors_spec.cr b/spec/helpers/errors_spec.cr index 70d426600..4deacbd10 100644 --- a/spec/helpers/errors_spec.cr +++ b/spec/helpers/errors_spec.cr @@ -1,4 +1,4 @@ -require "../spec_helper" +require "../env_helper" require "kilt" Spectator.describe "error_redirect_helper" do diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 9a77d7ef7..4b4f04efa 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -22,12 +22,3 @@ 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/helpers/errors.cr b/src/invidious/helpers/errors.cr index 3e32489c1..0651a9549 100644 --- a/src/invidious/helpers/errors.cr +++ b/src/invidious/helpers/errors.cr @@ -2,10 +2,6 @@ # Issue template # ------------------- -class ContextWithPreferences < HTTP::Server::Context - property preferences : Preferences? -end - macro error_template(*args) error_template_helper(env, {{args.splat}}) end @@ -172,10 +168,10 @@ end # Redirect # ------------------- -def error_redirect_helper(env : ContextWithPreferences) +def error_redirect_helper(env : HTTP::Server::Context) request_path = env.request.path - locale = env.preferences.try &.locale + locale = env.get("preferences").as(Preferences).locale display_on_path = %w[ /search @@ -197,12 +193,5 @@ def error_redirect_helper(env : ContextWithPreferences) 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 From 5b175f6b33435b35e0048428f2855eaa3fa09f66 Mon Sep 17 00:00:00 2001 From: Joseph Johansen Date: Fri, 21 Jun 2024 18:18:54 +0100 Subject: [PATCH 3/4] Fix specs and clean up --- spec/helpers/errors_spec.cr | 2 +- spec/spec_helper.cr | 2 ++ src/invidious/config.cr | 2 +- src/invidious/helpers/errors.cr | 38 +++++++++++++++------------------ 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/spec/helpers/errors_spec.cr b/spec/helpers/errors_spec.cr index 4deacbd10..a07e2f2ef 100644 --- a/spec/helpers/errors_spec.cr +++ b/spec/helpers/errors_spec.cr @@ -8,7 +8,7 @@ Spectator.describe "error_redirect_helper" do 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" + expect(html).to eq "

After which you should try to:

\n\n" end it "shows next steps for watch pages" do diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 4b4f04efa..16bf421ca 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -12,6 +12,8 @@ require "../src/invidious/search/ctoken" require "../src/invidious/trending" require "../src/invidious/config" require "../src/invidious/user/preferences.cr" +require "../src/invidious/jobs" +require "../src/invidious/jobs/*" require "spectator" CONFIG = Config.from_yaml(File.open("config/config.example.yml")) diff --git a/src/invidious/config.cr b/src/invidious/config.cr index a13fe873f..09c2168b8 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 0651a9549..514a425a8 100644 --- a/src/invidious/helpers/errors.cr +++ b/src/invidious/helpers/errors.cr @@ -173,25 +173,21 @@ def error_redirect_helper(env : HTTP::Server::Context) locale = env.get("preferences").as(Preferences).locale - display_on_path = %w[ - /search - /channel - /watch - /playlist?list=PL - /embed - ] - - 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}" - } - - return rendered "components/error_redirect" + if request_path.starts_with?("/search") || request_path.starts_with?("/watch") || + request_path.starts_with?("/channel") || request_path.starts_with?("/playlist?list=PL") || + request_path.starts_with?("/embed") + 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}" + } + + return rendered "components/error_redirect" + else + return "" + end end From f52ef639eda266ffcd4527a8053941d17cd5d3e6 Mon Sep 17 00:00:00 2001 From: Joseph Johansen Date: Fri, 21 Jun 2024 18:22:42 +0100 Subject: [PATCH 4/4] Fix compialtion for all tests --- spec/spec_helper.cr | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 16bf421ca..035196b81 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -17,8 +17,6 @@ require "../src/invidious/jobs/*" 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