Skip to content

Commit

Permalink
Replace names with mastodon handles when episode hosts/guests have them
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Aug 23, 2024
1 parent 1080928 commit abba958
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
51 changes: 42 additions & 9 deletions lib/changelog/social/social.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ defmodule Changelog.Social do
use ChangelogWeb, :verified_routes

alias Changelog.Social.Client
alias Changelog.Episode
alias ChangelogWeb.EpisodeView
alias Changelog.{Episode, StringKit}
alias ChangelogWeb.{EpisodeView, PersonView}
alias ChangelogWeb.Helpers.SharedHelpers

def post(episode = %Episode{}) do
episode = Episode.preload_all(episode)
podcast = episode.podcast

ann = "#{ann_emoj()} #{ann_text(podcast)}"
desc = description(episode)
link = "#{link_emoj()} #{link_url(episode)} #{tags(podcast)}"

# max status length is 500. 490 for wiggle room
room_for_desc = 490 - String.length(ann) - String.length(link)

content = """
#{ann_emoj()} #{ann_text(podcast)}
#{ann}
#{description(episode)}
#{SharedHelpers.truncate(desc, room_for_desc)}
#{link_emoj()} #{link_url(episode)} #{tags(podcast)}
#{link}
"""

token = token_for_podcast(podcast)
Expand All @@ -25,20 +33,45 @@ defmodule Changelog.Social do
end
end

def description(episode) do
episode = episode |> Episode.preload_hosts() |> Episode.preload_guests()

episode.summary
|> SharedHelpers.md_to_text()
|> replace_host_references(episode.hosts)
|> replace_guest_references(episode.guests)
end

defp replace_host_references(text, hosts) do
hosts
|> Enum.filter(&(StringKit.present?(&1.mastodon_handle)))
|> Enum.reduce(text, fn host, acc ->
first_name = PersonView.first_name(host)
acc
|> String.replace(host.name, "@" <> host.mastodon_handle)
|> String.replace(first_name, "@" <> host.mastodon_handle)
end)
end

defp replace_guest_references(text, guests) do
guests
|> Enum.filter(&(StringKit.present?(&1.mastodon_handle)))
|> Enum.reduce(text, fn guest, acc ->
String.replace(acc, guest.name, "@" <> guest.mastodon_handle)
end)
end

defp ann_emoj, do: ~w(🙌 🎉 🔥 💥 🚢 🚀 ⚡️ ✨ 🤘) |> Enum.random()

defp ann_text(podcast) do
case podcast.slug do
"friends" -> "It's Changelog & Friends!"
"podcast" -> "New Changelog interview!"
"shipit" -> "New episode of Ship It!"
_else -> "New episode of #{podcast.name}!"
end
end

defp description(episode) do
episode |> EpisodeView.text_description(400)
end

defp link_emoj, do: ~w(✨ 💫 🔗 👉 🎧) |> Enum.random()

defp link_url(episode), do: episode |> EpisodeView.share_url()
Expand Down
49 changes: 49 additions & 0 deletions test/changelog/social/social_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule Changelog.Social.SocialTest do
use Changelog.SchemaCase

import Mock

alias Changelog.Social

describe "post/1" do
test "creates a status with applicable data" do
podcast = insert(:podcast, mastodon_token: "12345")
episode = insert(:published_episode, podcast: podcast)

with_mock(Social.Client, create_status: fn _, _ -> {:ok, %{body: %{"url" => "https://test.com"}}} end) do
Social.post(episode)

assert called(Social.Client.create_status("12345", :_))
assert Repo.get(Changelog.Episode, episode.id).socialize_url == "https://test.com"
end
end
end

describe "description/1" do
test "replaces nothing when no hosts or guests" do
episode = insert(:episode, summary: "Jerod interviews Uncle Buck & Buck Cherry about some cool stuff")

assert Social.description(episode) == episode.summary
end

test "replaces referenced names with mastodon handles" do
host1 = insert(:person, name: "Jerod Santo", mastodon_handle: "[email protected]")
host2 = insert(:person, name: "Adam Stac", mastodon_handle: nil)
host3 = insert(:person, name: "Gerhard Lazu", mastodon_handle: "[email protected]")
guest1 = insert(:person, name: "Uncle Buck", mastodon_handle: "")
guest2 = insert(:person, name: "Buck Cherry", mastodon_handle: "[email protected]")

episode = insert(:episode, summary: "Jerod, Gerhard Lazu & Adam Stac interviews Uncle Buck & Buck Cherry about some cool stuff")

insert(:episode_host, episode: episode, person: host1)
insert(:episode_host, episode: episode, person: host2)
insert(:episode_host, episode: episode, person: host3)
insert(:episode_guest, episode: episode, person: guest1)
insert(:episode_guest, episode: episode, person: guest2)

expected = "@[email protected], @[email protected] & Adam Stac interviews Uncle Buck & @[email protected] about some cool stuff"

assert Social.description(episode) == expected
end
end
end

0 comments on commit abba958

Please sign in to comment.