From c93a0d9aa23352a6178f45d6662a4bfae8db1419 Mon Sep 17 00:00:00 2001 From: syeopite Date: Mon, 28 Jun 2021 22:15:29 -0700 Subject: [PATCH] Manually extract brand_redirect from 1b569bbc99207cae7c20aa285f42477ae361dd30 This commit manually extracts the brand_redirect function from the commit mentioned. However, the redirect to the `.../about` endpoint is removed due to the fact that it doesn't exist yet. This commit is also mainly just a bridge for the next few cherry picks from \#2215 --- src/invidious.cr | 4 ++++ src/invidious/routes/channels.cr | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/invidious.cr b/src/invidious.cr index fff9df53..0b2e7ea2 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -315,6 +315,10 @@ Invidious::Routing.get "/channel/:ucid/playlists", Invidious::Routes::Channels, Invidious::Routing.get "/channel/:ucid/community", Invidious::Routes::Channels, :community Invidious::Routing.get "/channel/:ucid/about", Invidious::Routes::Channels, :about +["", "/videos", "/playlists", "/community", "/about"].each do |path| + Invidious::Routing.get "/c/:user#{path}", Invidious::Routes::Channels, :brand_redirect +end + Invidious::Routing.get "/watch", Invidious::Routes::Watch, :handle Invidious::Routing.get "/watch/:id", Invidious::Routes::Watch, :redirect Invidious::Routing.get "/shorts/:id", Invidious::Routes::Watch, :redirect diff --git a/src/invidious/routes/channels.cr b/src/invidious/routes/channels.cr index 6a78c977..38404afc 100644 --- a/src/invidious/routes/channels.cr +++ b/src/invidious/routes/channels.cr @@ -101,6 +101,34 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute env.redirect "/channel/#{ucid}" end + def brand_redirect(env) + locale = LOCALES[env.get("preferences").as(Preferences).locale]? + + user = env.params.url["user"] + + response = YT_POOL.client &.get("/c/#{user}") + html = XML.parse_html(response.body) + + ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1] + if !ucid + env.response.status_code = 404 + return + end + + url = "/channel/#{ucid}" + + location = env.request.path.lchop?("/c/#{user}/") + if location + url += "/#{location}" + end + + if env.params.query.size > 0 + url += "?#{env.params.query}" + end + + env.redirect url + end + private def fetch_basic_information(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]?