From a333bc6efe75b07dea68027a494fd72caa0c592c Mon Sep 17 00:00:00 2001 From: Joseph Johansen Date: Fri, 21 Jun 2024 17:57:02 +0100 Subject: [PATCH] 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 41bb5e35..3987f796 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 00000000..6cfb84eb --- /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 70d42660..4deacbd1 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 9a77d7ef..4b4f04ef 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 3e32489c..0651a954 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