Skip to content

Commit

Permalink
Linkify chapters in Zulip new episode messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Dec 10, 2024
1 parent 2182a22 commit 3b0af88
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
34 changes: 27 additions & 7 deletions lib/changelog/zulip/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ defmodule Changelog.Zulip.Messages do

def new_episode(episode) do
[
"#{episode.summary} 🔗 #{EpisodeView.share_url(episode)}",
chapters(episode.audio_chapters)
summary(episode),
chapters(episode)
]
|> ListKit.compact_join("\n\n")
end

def chapters(nil), do: nil
def chapters([]), do: nil
def summary(episode) do
"#{episode.summary} 🔗 #{episode_url(episode)}"
end

def chapters(%{audio_chapters: nil}), do: nil
def chapters(%{audio_chapters: []}), do: nil

def chapters(chapters, padding \\ 2) do
def chapters(episode = %{audio_chapters: chapters}, padding \\ 2) do
numbers =
chapters
|> Enum.with_index(1)
Expand Down Expand Up @@ -60,8 +64,16 @@ defmodule Changelog.Zulip.Messages do
|> Enum.map(fn {chapter, index} ->
[
pad(Enum.at(numbers, index), numbers_length),
pad(Enum.at(starts, index), starts_length),
linkify(pad(Enum.at(titles, index), titles_length), chapter.title, chapter.link_url),
linkify(
pad(Enum.at(starts, index), starts_length),
Enum.at(starts, index),
episode_url(episode, chapter.starts_at)
),
linkify(
pad(Enum.at(titles, index), titles_length),
chapter.title,
chapter.link_url
),
pad(Enum.at(runs, index), runs_length)
]
|> Enum.join("|")
Expand All @@ -73,6 +85,14 @@ defmodule Changelog.Zulip.Messages do
|> Enum.join("\n")
end

defp episode_url(episode, starts_at \\ nil) do
if starts_at do
EpisodeView.share_url(episode) <> "#t=#{round(starts_at)}"
else
EpisodeView.share_url(episode)
end
end

defp linkify(string, _title, nil), do: string

defp linkify(string, title, link) do
Expand Down
35 changes: 24 additions & 11 deletions test/changelog/zulip/messages_test.exs
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
defmodule Changelog.Zulip.MessagesTest do
use ExUnit.Case

import Mock
import Changelog.Factory

alias Changelog.Zulip.Messages

describe "chapters/1" do
setup_with_mocks([
{ChangelogWeb.EpisodeView, [], [share_url: fn _ -> "https://cpu.fm/1" end]}
]) do
:ok
end

test "is nil when there are no chapters" do
assert Messages.chapters([]) == nil
episode = build(:episode, audio_chapters: [])
assert Messages.chapters(episode) == nil
end

test "is a markdown table when one chapter exists" do
ch1 = build(:episode_chapter, title: "Chapter 1", starts_at: 0, ends_at: 0)
episode = build(:episode, audio_chapters: [ch1])

table = """
| Ch | Start | Title | Runs |
| -- | ----- | --------- | ----- |
| 01 | 00:00 | Chapter 1 | 00:00 |
| 01 | [00:00](https://cpu.fm/1#t=0) | Chapter 1 | 00:00 |
"""

assert Messages.chapters([ch1]) == String.trim_trailing(table, "\n")
assert Messages.chapters(episode) == String.trim_trailing(table, "\n")
end

test "is a markdown table when multiple chapter exists" do
Expand All @@ -28,14 +37,16 @@ defmodule Changelog.Zulip.MessagesTest do

ch2 = build(:episode_chapter, title: "Go shorty", starts_at: 37, ends_at: 247)

episode = build(:episode, audio_chapters: [ch1, ch2])

table = """
| Ch | Start | Title | Runs |
| -- | ----- | ----------------------------- | ----- |
| 01 | 00:00 | Yo that's a rather long title | 00:36 |
| 02 | 00:37 | Go shorty | 03:30 |
| 01 | [00:00](https://cpu.fm/1#t=0) | Yo that's a rather long title | 00:36 |
| 02 | [00:37](https://cpu.fm/1#t=37) | Go shorty | 03:30 |
"""

assert Messages.chapters([ch1, ch2]) == String.trim_trailing(table, "\n")
assert Messages.chapters(episode) == String.trim_trailing(table, "\n")
end

test "it includes markdown links when chapter has one" do
Expand All @@ -44,19 +55,21 @@ defmodule Changelog.Zulip.MessagesTest do
title: "Link me up! This is the longest",
link_url: "https://link.zelda",
starts_at: 0,
ends_at: 36
ends_at: 136
)

ch2 = build(:episode_chapter, title: "No link here...", starts_at: 37, ends_at: 247)
ch2 = build(:episode_chapter, title: "No link here...", starts_at: 137, ends_at: 247)

episode = build(:episode, audio_chapters: [ch1, ch2])

table = """
| Ch | Start | Title | Runs |
| -- | ----- | ------------------------------- | ----- |
| 01 | 00:00 | [Link me up! This is the longest](https://link.zelda) | 00:36 |
| 02 | 00:37 | No link here... | 03:30 |
| 01 | [00:00](https://cpu.fm/1#t=0) | [Link me up! This is the longest](https://link.zelda) | 02:16 |
| 02 | [02:17](https://cpu.fm/1#t=137) | No link here... | 01:50 |
"""

assert Messages.chapters([ch1, ch2]) == String.trim_trailing(table, "\n")
assert Messages.chapters(episode) == String.trim_trailing(table, "\n")
end
end
end

0 comments on commit 3b0af88

Please sign in to comment.