Invidious is an alternative front-end to YouTube
Routing: Handle current and future routes more nicely
Samantaz Fox49b9316b9f2e
diff --git a/src/invidious/routes/channels.cr b/src/invidious/routes/channels.crindex 9892ae2a..5500672f 100644--- a/src/invidious/routes/channels.cr+++ b/src/invidious/routes/channels.cr@@ -217,6 +217,11 @@ module Invidious::Routes::Channelsenv.redirect "/channel/#{ucid}"end+ private KNOWN_TABS = {+ "home", "videos", "shorts", "streams", "podcasts",+ "releases", "playlists", "community", "channels", "about",+ }+# Redirects brand url channels to a normal /channel/:ucid routedef self.brand_redirect(env)locale = env.get("preferences").as(Preferences).locale@@ -227,7 +232,10 @@ module Invidious::Routes::Channelsyt_url_params = URI::Params.encode(env.params.query.to_h.select(["a", "u", "user"]))# Retrieves URL params that only Invidious uses- invidious_url_params = URI::Params.encode(env.params.query.to_h.select!(["a", "u", "user"]))+ invidious_url_params = env.params.query.dup+ invidious_url_params.delete_all("a")+ invidious_url_params.delete_all("u")+ invidious_url_params.delete_all("user")beginresolved_url = YoutubeAPI.resolve_url("https://youtube.com#{env.request.path}#{yt_url_params.size > 0 ? "?#{yt_url_params}" : ""}")@@ -236,14 +244,17 @@ module Invidious::Routes::Channelsreturn error_template(404, translate(locale, "This channel does not exist."))end- selected_tab = env.request.path.split("/")[-1]- if {"home", "videos", "shorts", "streams", "playlists", "community", "channels", "about"}.includes? selected_tab+ selected_tab = env.params.url["tab"]?++ if KNOWN_TABS.includes? selected_taburl = "/channel/#{ucid}/#{selected_tab}"elseurl = "/channel/#{ucid}"end- env.redirect url+ url += "?#{invidious_url_params}" if !invidious_url_params.empty?++ return env.redirect urlend# Handles redirects for the /profile endpointdiff --git a/src/invidious/routing.cr b/src/invidious/routing.crindex 9c43171c..5ec7fae3 100644--- a/src/invidious/routing.cr+++ b/src/invidious/routing.cr@@ -124,22 +124,34 @@ module Invidious::Routingget "/channel/:ucid/community", Routes::Channels, :communityget "/channel/:ucid/channels", Routes::Channels, :channelsget "/channel/:ucid/about", Routes::Channels, :about+get "/channel/:ucid/live", Routes::Channels, :liveget "/user/:user/live", Routes::Channels, :liveget "/c/:user/live", Routes::Channels, :live- {"", "/videos", "/shorts", "/streams", "/playlists", "/community", "/about"}.each do |path|- # /c/LinusTechTips- get "/c/:user#{path}", Routes::Channels, :brand_redirect- # /user/linustechtips | Not always the same as /c/- get "/user/:user#{path}", Routes::Channels, :brand_redirect- # /@LinusTechTips | Handle- get "/@:user#{path}", Routes::Channels, :brand_redirect- # /attribution_link?a=anything&u=/channel/UCZYTClx2T1of7BRZ86-8fow- get "/attribution_link#{path}", Routes::Channels, :brand_redirect- # /profile?user=linustechtips- get "/profile/#{path}", Routes::Channels, :profile- end+ # Channel catch-all, to redirect future routes to the channel's home+ # NOTE: defined last in order to be processed after the other routes+ get "/channel/:ucid/*", Routes::Channels, :home++ # /c/LinusTechTips+ get "/c/:user", Routes::Channels, :brand_redirect+ get "/c/:user/:tab", Routes::Channels, :brand_redirect++ # /user/linustechtips (Not always the same as /c/)+ get "/user/:user", Routes::Channels, :brand_redirect+ get "/user/:user/:tab", Routes::Channels, :brand_redirect++ # /@LinusTechTips (Handle)+ get "/@:user", Routes::Channels, :brand_redirect+ get "/@:user/:tab", Routes::Channels, :brand_redirect++ # /attribution_link?a=anything&u=/channel/UCZYTClx2T1of7BRZ86-8fow+ get "/attribution_link", Routes::Channels, :brand_redirect+ get "/attribution_link/:tab", Routes::Channels, :brand_redirect++ # /profile?user=linustechtips+ get "/profile", Routes::Channels, :profile+ get "/profile/*", Routes::Channels, :profileenddef register_watch_routes