From 36f467144cdc0687a762bbc8c9b3cbfebbc3541d Mon Sep 17 00:00:00 2001 From: Jerod Santo Date: Wed, 22 Jan 2025 10:15:09 -0600 Subject: [PATCH] Add YT episode redirects, e.g. changelog.fm/624/yt --- .../controllers/episode_controller.ex | 11 +++++++++- lib/changelog_web/plugs/vanity_domains.ex | 3 +-- lib/changelog_web/router.ex | 2 +- lib/changelog_web/views/episode_view.ex | 4 ++++ .../controllers/episode_controller_test.exs | 20 +++++++++++++++++++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/changelog_web/controllers/episode_controller.ex b/lib/changelog_web/controllers/episode_controller.ex index fc0e9ebac5..5ced4cb4de 100644 --- a/lib/changelog_web/controllers/episode_controller.ex +++ b/lib/changelog_web/controllers/episode_controller.ex @@ -2,7 +2,7 @@ defmodule ChangelogWeb.EpisodeController do use ChangelogWeb, :controller alias Changelog.{Episode, NewsItem, Podcast, Subscription} - alias ChangelogWeb.{Email, LiveView, TimeView, Xml} + alias ChangelogWeb.{Email, EpisodeView, LiveView, TimeView, Xml} plug :assign_podcast @@ -247,6 +247,15 @@ defmodule ChangelogWeb.EpisodeController do |> redirect(to: ~p"/#{podcast.slug}/#{episode.slug}") end + def yt(conn, %{"slug" => slug}, podcast) do + episode = + assoc(podcast, :episodes) + |> Episode.preload_podcast() + |> Repo.get_by!(slug: slug) + + redirect(conn, external: EpisodeView.yt_url(episode)) + end + defp assign_podcast(conn, _) do podcast = Repo.get_by!(Podcast, slug: conn.params["podcast"]) assign(conn, :podcast, podcast) diff --git a/lib/changelog_web/plugs/vanity_domains.ex b/lib/changelog_web/plugs/vanity_domains.ex index 1ab7974799..d0ac3ca1ca 100644 --- a/lib/changelog_web/plugs/vanity_domains.ex +++ b/lib/changelog_web/plugs/vanity_domains.ex @@ -36,8 +36,7 @@ defmodule ChangelogWeb.Plug.VanityDomains do end defp determine_destination(%{slug: "podcast"}, ["sotl"]) do - "https://changelog.typeform.com/to/yjHJiFlv" - # "https://changelog.fm/571" + changelog_destination(["topic", "sotl"]) end defp determine_destination(%{slug: "podcast"}, ["edu"]) do diff --git a/lib/changelog_web/router.ex b/lib/changelog_web/router.ex index 40b6ada064..481ef08df3 100644 --- a/lib/changelog_web/router.ex +++ b/lib/changelog_web/router.ex @@ -301,7 +301,7 @@ defmodule ChangelogWeb.Router do post "/:podcast/:slug/unsubscribe", EpisodeController, :unsubscribe, as: :episode for subpage <- - ~w(embed preview play share img discuss transcript live time chapters psc email)a do + ~w(embed preview play share img discuss transcript live time chapters psc email yt)a do get "/:podcast/:slug/#{subpage}", EpisodeController, subpage, as: :episode end diff --git a/lib/changelog_web/views/episode_view.ex b/lib/changelog_web/views/episode_view.ex index e826497f74..83cd561032 100644 --- a/lib/changelog_web/views/episode_view.ex +++ b/lib/changelog_web/views/episode_view.ex @@ -298,6 +298,10 @@ defmodule ChangelogWeb.EpisodeView do Github.Source.new("transcripts", episode).repo_url end + def yt_url(%{youtube_id: id}) when not is_nil(id), do: "https://youtu.be/#{id}" + + def yt_url(%{podcast: podcast}), do: podcast.youtube_url + defp chapters_json(chapters) do chapters |> Enum.with_index() diff --git a/test/changelog_web/controllers/episode_controller_test.exs b/test/changelog_web/controllers/episode_controller_test.exs index c2a5426698..a5a9742841 100644 --- a/test/changelog_web/controllers/episode_controller_test.exs +++ b/test/changelog_web/controllers/episode_controller_test.exs @@ -313,6 +313,26 @@ defmodule ChangelogWeb.EpisodeControllerTest do end end + describe "yt" do + test "redirects to podcast youtube URL when episodes does not have id", %{conn: conn} do + p = insert(:podcast, youtube_url: "https://www.youtube.com/changelog") + e = insert(:episode, podcast: p) + + conn = get(conn, ~p"/#{p.slug}/#{e.slug}/yt") + + assert_redirected_to(conn, "https://www.youtube.com/changelog") + end + + test "redirects to episode youtube URL when episodes has id", %{conn: conn} do + p = insert(:podcast, youtube_url: "https://www.youtube.com/changelog") + e = insert(:episode, podcast: p, youtube_id: "8675309") + + conn = get(conn, ~p"/#{p.slug}/#{e.slug}/yt") + + assert_redirected_to(conn, "https://youtu.be/8675309") + end + end + describe "time" do test "404 when episode is not recorded live", %{conn: conn} do p = insert(:podcast)