Skip to content

Commit

Permalink
Add YT episode redirects, e.g. changelog.fm/624/yt
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Jan 22, 2025
1 parent 3940e77 commit 36f4671
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/changelog_web/controllers/episode_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions lib/changelog_web/plugs/vanity_domains.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/changelog_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/changelog_web/views/episode_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions test/changelog_web/controllers/episode_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 36f4671

Please sign in to comment.