Skip to content

Commit

Permalink
Adjust changelog.social posts for episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Jul 10, 2024
1 parent 4958643 commit 2017ca1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 51 deletions.
50 changes: 26 additions & 24 deletions lib/changelog/social/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,49 @@ defmodule Changelog.Social.Client do
end
end

def process_request_headers(headers) do
auth = Application.get_env(:changelog, :mastodon_api_token)

[{"Authorization", "Bearer #{auth}"} | headers]
end

# shortcut to get authorization code url to be opened in browser context
def authorize_url do
params = [
{"client_id", Application.get_env(:changelog, :mastodon_client_id)},
{"scope", "read+write+follow"},
{"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"},
{"response_type", "code"}
] |> Enum.map(fn({k, v}) -> "#{k}=#{v}" end) |> Enum.join("&")
params =
[
{"client_id", Application.get_env(:changelog, :mastodon_client_id)},
{"scope", "read+write+follow"},
{"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"},
{"response_type", "code"}
]
|> Enum.map(fn {k, v} -> "#{k}=#{v}" end)
|> Enum.join("&")

base_url() <> "/oauth/authorize?" <> params
end

# once we have the one-time authorize code, use here for multi-use token
def get_oauth_token(code) do
body = {:form, [
{"client_id", Application.get_env(:changelog, :mastodon_client_id)},
{"client_secret", Application.get_env(:changelog, :mastodon_client_secret)},
{"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"},
{"grant_type", "authorization_code"},
{"code", code},
{"scope", "read write follow"}
]}
body =
{:form,
[
{"client_id", Application.get_env(:changelog, :mastodon_client_id)},
{"client_secret", Application.get_env(:changelog, :mastodon_client_secret)},
{"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"},
{"grant_type", "authorization_code"},
{"code", code},
{"scope", "read write follow"}
]}

case HTTPoison.post(base_url() <> "/oauth/token", body) do
{:ok, %{status_code: 200, body: body}} -> {:ok, body |> Jason.decode!() |> Map.get("access_token")}
{:ok, %{body: body}} -> {:error, Jason.decode!(body) }
{:ok, %{status_code: 200, body: body}} ->
{:ok, body |> Jason.decode!() |> Map.get("access_token")}

{:ok, %{body: body}} ->
{:error, Jason.decode!(body)}
end
end

# https://docs.joinmastodon.org/methods/statuses/#create
def create_status(contents) do
def create_status(token, contents) do
params = [
{"status", contents}
]

post("/statuses", {:form, params})
post("/statuses", {:form, params}, [{"authorization", "Bearer #{token}"}])
end
end
61 changes: 36 additions & 25 deletions lib/changelog/social/social.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,55 @@ defmodule Changelog.Social do
use ChangelogWeb, :verified_routes

alias Changelog.Social.Client
alias Changelog.NewsItem
alias ChangelogWeb.NewsItemView
alias Changelog.Episode
alias ChangelogWeb.EpisodeView

# feed-only news items don't get posted
def post(%NewsItem{feed_only: true}), do: false
def post(episode = %Episode{}) do
episode = Episode.preload_all(episode)
podcast = episode.podcast

# episode news items don't get posted (for now)
def post(%NewsItem{type: :audio}), do: false
content = """
#{ann_emoj()} #{ann_text(podcast)}
# post news items also don't get posted (for now)
def post(item = %NewsItem{}) do
item = NewsItem.preload_all(item)
#{description(episode)}
content = """
#{headline(item)}
#{author(item)}
#{link(item)}
#{link_emoj()} #{link_url(episode)} #{tags(podcast)}
"""

Client.create_status(content)
token = token_for_podcast(podcast)

Client.create_status(token, content)
end

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

defp headline(item) do
item.headline
defp ann_text(podcast) do
case podcast.slug do
"podcast" -> "New Changelog interview!"
"shipit" -> "New episode of Ship It!"
_else -> "New episode of #{podcast.name}!"
end
end

defp author(%{author: nil}), do: nil
defp author(%{author: %{mastodon_handle: nil}}), do: nil
defp author(%{author: %{mastodon_handle: handle}}), do: "#{author_emoj()} by @#{handle}"
defp description(episode) do
episode |> EpisodeView.text_description(400)
end

defp link(item), do: "#{link_emoj()} #{link_url(item)}"
defp link_emoj, do: ~w(✨ 💫 🔗 👉 🎧) |> Enum.random()

defp author_emoj, do: ~w(✍️ 🖋 ✏️) |> Enum.random()
defp link_emoj, do: ~w(✨ 💫 🔗 👉) |> Enum.random()
defp link_url(episode), do: episode |> EpisodeView.share_url()

defp link_url(item) do
url(~p"/news/#{NewsItemView.hashid(item)}")
defp tags(podcast) do
case podcast.slug do
"news" -> "#news #podcast"
"gotime" -> "#golang #podcast"
_else -> "#podcast"
end
end

defp token_for_podcast(%{mastodon_token: nil}) do
Application.get_env(:changelog, :mastodon_api_token)
end

defp token_for_podcast(%{mastodon_token: token}), do: token
end
4 changes: 2 additions & 2 deletions lib/changelog_web/views/episode_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ defmodule ChangelogWeb.EpisodeView do
end
end

def text_description(episode) do
episode.summary |> SharedHelpers.md_to_text() |> SharedHelpers.truncate(320)
def text_description(episode, truncate_to \\ 320) do
episode.summary |> SharedHelpers.md_to_text() |> SharedHelpers.truncate(truncate_to)
end

def title_with_podcast_aside(episode) do
Expand Down

0 comments on commit 2017ca1

Please sign in to comment.