Skip to content

Commit

Permalink
Update /live stuff to use Riverside URLs when applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Jan 16, 2024
1 parent 8518737 commit 9268acc
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 27 deletions.
8 changes: 8 additions & 0 deletions lib/changelog/kits/url_kit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ defmodule Changelog.UrlKit do
def sans_scheme(nil), do: nil
def sans_scheme(url), do: String.replace(url, Regexp.http(), "")

def sans_query(nil), do: nil
def sans_query(url) do
url
|> URI.parse()
|> Map.put(:query, nil)
|> URI.to_string()
end

def via_scribe(url) do
url |> URI.parse() |> Map.put(:host, "scribe.rip") |> URI.to_string()
end
Expand Down
4 changes: 4 additions & 0 deletions lib/changelog/schema/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ defmodule Changelog.Factory do
}
end

def live_podcast_factory do
%Changelog.Podcast{podcast_factory() | riverside_url: "https://riverside.fm/studio/livepod?t=123"}
end

def post_factory do
%Changelog.Post{
title: sequence(:name, &"Post #{&1}"),
Expand Down
2 changes: 1 addition & 1 deletion lib/changelog/slack/countdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Changelog.Slack.Countdown do
respond(
case Duration.to_hours(diff) do
h when h <= 0 ->
"#{live_message(podcast)}! Watch ~> #{LiveView.youtube_url(next_episode)} :tada:"
"#{live_message(podcast)}! Watch ~> #{LiveView.live_url(next_episode)} :tada:"

h when h < 2 ->
"There's just *#{formatted}* until #{podcast} (#{title}) :eyes:"
Expand Down
2 changes: 1 addition & 1 deletion lib/changelog_web/controllers/episode_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ defmodule ChangelogWeb.EpisodeController do
|> Episode.with_youtube_id()
|> Repo.get_by!(slug: slug)

redirect(conn, external: LiveView.youtube_url(episode))
redirect(conn, external: LiveView.live_url(episode))
end

def time(conn, %{"slug" => slug}, podcast) do
Expand Down
2 changes: 1 addition & 1 deletion lib/changelog_web/templates/live/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<%= if Enum.any?(@episodes) do %>
<div class="live_upcoming-grid">
<%= for episode <- @episodes do %>
<%= link to: youtube_link(episode), title: "View on YouTube", class: "live_upcoming-grid-item live_upcoming-grid-item--live" do %>
<%= link to: live_link(episode), title: "View Live", class: "live_upcoming-grid-item live_upcoming-grid-item--live" do %>
<div class="live_upcoming-grid-item-image">
<div class="image_border_hack">
<%= SharedHelpers.lazy_image(PodcastView.cover_url(episode.podcast, :medium), "#{episode.podcast.name} Artwork", width: 120, height: 120) %>
Expand Down
21 changes: 13 additions & 8 deletions lib/changelog_web/views/live_view.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ChangelogWeb.LiveView do
use ChangelogWeb, :public_view

alias Changelog.UrlKit
alias ChangelogWeb.{EpisodeView, PersonView, PodcastView, TimeView}

def render("ical.ics", %{episodes: episodes}) do
Expand All @@ -9,7 +10,7 @@ defmodule ChangelogWeb.LiveView do
%ICalendar.Event{
summary: "#{episode.podcast.name} Live",
description: episode_title_with_subtitle(episode),
url: youtube_url(episode),
url: live_url(episode),
dtstart: episode.recorded_at,
dtend: Timex.shift(episode.recorded_at, minutes: 90)
}
Expand Down Expand Up @@ -61,14 +62,18 @@ defmodule ChangelogWeb.LiveView do
end
end

def youtube_link(episode) do
if episode.youtube_id do
youtube_url(episode)
else
{:javascript, ~s{alert("The YouTube event for this episode hasn't been created yet.");}}
# For use in link(to: ) calls vs just the raw url (below)
def live_link(episode) do
case live_url(episode) do
nil -> {:javascript, ~s{alert("This live event is not yet configured.");}}
url -> url
end
end

def youtube_url(%{youtube_id: id}) when is_binary(id), do: "https://youtu.be/#{id}"
def youtube_url(_), do: "https://youtube.com/changelog"
def live_url(episode = %{youtube_id: id}) when not is_nil(id), do: youtube_url(episode)
def live_url(%{podcast: %{riverside_url: url}}) when not is_nil(url), do: UrlKit.sans_query(url)
def live_url(_else), do: nil

defp youtube_url(%{youtube_id: id}) when is_binary(id), do: "https://youtu.be/#{id}"
defp youtube_url(_), do: "https://youtube.com/changelog"
end
8 changes: 8 additions & 0 deletions test/changelog/kits/url_kit_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ defmodule Changelog.UrlKitTest do
end
end

describe "sans_query/1" do
test "it removes all query params" do
url = "https://news.ycombinator.com/item?id=18120667"
sans = UrlKit.sans_query(url)
assert sans == "https://news.ycombinator.com/item"
end
end

describe "via_scribe/1" do
test "it works on medium domain" do
url = "https://medium.com/@user/my-post-09a6af907a2"
Expand Down
23 changes: 15 additions & 8 deletions test/changelog/slack/countdown_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,55 @@ defmodule Changelog.Slack.CountdownTest do
use ExUnit.Case

import ChangelogWeb.TimeView, only: [hours_from_now: 1, hours_ago: 1]
import Changelog.Factory

alias Changelog.Slack.Countdown

describe "live" do
setup do
gotime = build(:live_podcast, name: "Go Time")
jsparty = build(:live_podcast, name: "JS Party")
[gotime: gotime, jsparty: jsparty]
end

test "when there's no upcoming recording" do
response = Countdown.live(nil)
assert response.text =~ "No live recordings scheduled"
assert response.data == nil
end

test "when the upcoming recording is far away" do
test "when the upcoming recording is far away", context do
far_away = hours_from_now(24 * 9)
response = Countdown.live(%{recorded_at: far_away, podcast: %{name: "Go Time"}, title: ""})
response = Countdown.live(%{recorded_at: far_away, podcast: context[:gotime], title: ""})
assert response.text =~ "There's still"
assert response.data == far_away
end

test "when the upcoming recording is < 24 hours away" do
test "when the upcoming recording is < 24 hours away", context do
less_than_24 = hours_from_now(14)

response =
Countdown.live(%{recorded_at: less_than_24, podcast: %{name: "JS Party"}, title: ""})
Countdown.live(%{recorded_at: less_than_24, podcast: context[:jsparty], title: ""})

assert response.text =~ "There's only"
assert response.data == less_than_24
end

test "when the upcoming recording is < 2 hours away" do
test "when the upcoming recording is < 2 hours away", context do
less_than_2 = hours_from_now(1)

response =
Countdown.live(%{recorded_at: less_than_2, podcast: %{name: "Backstage"}, title: ""})
Countdown.live(%{recorded_at: less_than_2, podcast: context[:jsparty], title: ""})

assert response.text =~ "There's just"
assert response.data == less_than_2
end

test "when the upcoming recording is in session and stream is live" do
test "when the upcoming recording is in session and stream is live", context do
less_than_1 = hours_ago(1)

response =
Countdown.live(%{id: 1, recorded_at: less_than_1, podcast: %{name: "Go Time"}, title: ""})
Countdown.live(%{id: 1, recorded_at: less_than_1, podcast: context[:gotime], title: ""})

assert response.text =~ "It's Go Time!"
assert response.data == less_than_1
Expand Down
16 changes: 8 additions & 8 deletions test/changelog_web/controllers/live_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule ChangelogWeb.LiveControllerTest do
episode1 = insert(:episode, recorded_live: true, recorded_at: hours_from_now(24))
episode2 = insert(:episode, recorded_live: true, recorded_at: hours_from_now(48))

conn = get(conn, Routes.live_path(conn, :index))
conn = get(conn, ~p"/live")

assert html_response(conn, 200) =~ episode1.title
assert html_response(conn, 200) =~ episode2.title
Expand All @@ -19,14 +19,14 @@ defmodule ChangelogWeb.LiveControllerTest do
episode1 = insert(:episode, recorded_live: true, recorded_at: hours_from_now(1))
episode2 = insert(:episode, recorded_live: true, recorded_at: hours_from_now(3))

conn = get(conn, Routes.live_path(conn, :index))
conn = get(conn, ~p"/live")

assert html_response(conn, 200) =~ episode1.title
assert html_response(conn, 200) =~ episode2.title
end

test "with no episodes coming soon or live now", %{conn: conn} do
conn = get(conn, Routes.live_path(conn, :index))
conn = get(conn, ~p"/live")

assert html_response(conn, 200) =~ LiveView.header_for_episode_list([])
end
Expand All @@ -35,7 +35,7 @@ defmodule ChangelogWeb.LiveControllerTest do
test "getting a live episode page", %{conn: conn} do
episode = insert(:episode, recorded_live: true, recorded_at: hours_from_now(1))

conn = get(conn, Routes.live_path(conn, :show, Episode.hashid(episode)))
conn = get(conn, ~p"/live/#{Episode.hashid(episode)}")

assert html_response(conn, 200) =~ episode.title
end
Expand All @@ -44,15 +44,15 @@ defmodule ChangelogWeb.LiveControllerTest do
episode = insert(:episode, recorded_live: false, recorded_at: hours_from_now(1))

assert_raise Ecto.NoResultsError, fn ->
get(conn, Routes.live_path(conn, :show, Episode.hashid(episode)))
get(conn, ~p"/live/#{Episode.hashid(episode)}")
end
end

test "getting ical sans podcast slug", %{conn: conn} do
e1 = insert(:episode, recorded_live: true, recorded_at: hours_from_now(1))
e2 = insert(:episode, recorded_live: true, recorded_at: hours_from_now(2))

conn = get(conn, Routes.live_path(conn, :ical))
conn = get(conn, ~p"/live/ical")

assert conn.resp_body =~ e1.title
assert conn.resp_body =~ e2.title
Expand All @@ -63,15 +63,15 @@ defmodule ChangelogWeb.LiveControllerTest do
e1 = insert(:episode, podcast: podcast, recorded_live: true, recorded_at: hours_from_now(1))
e2 = insert(:episode, recorded_live: true, recorded_at: hours_from_now(2))

conn = get(conn, Routes.live_path(conn, :ical, podcast.slug))
conn = get(conn, ~p"/live/ical/#{podcast.slug}")

assert conn.resp_body =~ e1.title
refute conn.resp_body =~ e2.title
end

describe "the live status" do
test "is false all the time", %{conn: conn} do
conn = get(conn, Routes.live_path(conn, :status))
conn = get(conn, ~p"/live/status")
response = json_response(conn, 200)
refute response["streaming"]
assert response["listeners"] == 0
Expand Down

0 comments on commit 9268acc

Please sign in to comment.