Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show available actions on embed error page #4760

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions spec/env_helper.cr
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions spec/helpers/errors_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "../env_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</ul>\n"
Comment on lines +5 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a second I though that we had an URL handling issue, but it's just that you're not passing an encoded URL like the HTTP server handlers would do (see how in the "Switch Invidious Instance" URL nothing after referer= is URL encoded?).

It also made me realize that the si= parameter isn't removed from the "Watch on Youtube URL" (for the record, this is used to track who shared the video)

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
2 changes: 0 additions & 2 deletions spec/invidious/helpers_spec.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ 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 "../src/invidious/jobs"
require "../src/invidious/jobs/*"
require "spectator"

CONFIG = Config.from_yaml(File.open("config/config.example.yml"))

Spectator.configure do |config|
config.fail_blank
config.randomize
Expand Down
23 changes: 8 additions & 15 deletions src/invidious/helpers/errors.cr
Original file line number Diff line number Diff line change
Expand Up @@ -174,26 +174,19 @@ def error_redirect_helper(env : HTTP::Server::Context)
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")
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}"
}
Comment on lines +183 to +187
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that by doing that, you're creating a Hash whose index is the translated string. Using an NamedTuple is more efficient as there is no need to hash a potentially long UTF-8 string:

Suggested change
steps = {
refresh => env.request.resource,
switch_instance => "/redirect?referer=#{env.get("current_page")}",
go_to_youtube => "https://youtube.com#{env.request.resource}"
}
steps = {
{label: refresh, url: env.request.resource},
{label: switch_instance, url: "/redirect?referer=#{env.get("current_page")}"},
{label: go_to_youtube, url: "https://youtube.com#{env.request.resource}"}
}


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
return rendered "components/error_redirect"
else
return ""
end
Expand Down
8 changes: 8 additions & 0 deletions src/invidious/views/components/error_redirect.ecr
Original file line number Diff line number Diff line change
@@ -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>
Loading