Skip to content

Commit

Permalink
Add the ability to email yourself custom feed links
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Sep 20, 2024
1 parent 639c478 commit 781b32c
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 6 deletions.
8 changes: 8 additions & 0 deletions lib/changelog/oban_workers/mail_deliverer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Changelog.ObanWorkers.MailDeliverer do
Episode,
EpisodeGuest,
EpisodeRequest,
Feed,
Mailer,
NewsItem,
NewsItemComment,
Expand Down Expand Up @@ -176,6 +177,13 @@ defmodule Changelog.ObanWorkers.MailDeliverer do
|> Mailer.deliver()
end

def feed_links(%{"feed" => id}) do
Feed
|> Repo.get(id)
|> Email.feed_links()
|> Mailer.deliver()
end

def subscriber_welcome(%{"person" => id, "newsletter" => slug}) do
newsletter = Newsletters.get_by_slug(slug)

Expand Down
1 change: 1 addition & 0 deletions lib/changelog/policies/feed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Changelog.Policies.Feed do
def create(actor), do: new(actor)

def edit(actor, feed), do: is_owner(actor, feed)
def email(actor, feed), do: edit(actor, feed)
def refresh(actor, feed), do: edit(actor, feed)
def update(actor, feed), do: edit(actor, feed)
def delete(actor, feed), do: edit(actor, feed)
Expand Down
12 changes: 12 additions & 0 deletions lib/changelog_web/controllers/admin/mailer_preview_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule ChangelogWeb.Admin.MailerPreviewController do
Episode,
EpisodeGuest,
EpisodeRequest,
Feed,
Mailer,
NewsItem,
NewsItemComment,
Expand Down Expand Up @@ -119,6 +120,17 @@ defmodule ChangelogWeb.Admin.MailerPreviewController do
Email.comment_subscription(subscription, comment)
end

def feed_links_email(person) do
feed =
person
|> assoc(:feeds)
|> Feed.limit(1)
|> Feed.preload_owner()
|> Repo.one()

Email.feed_links(feed)
end

# Podcast related emails
def episode_published_email(person) do
Email.episode_published(known_subscription(person), known_episode())
Expand Down
14 changes: 11 additions & 3 deletions lib/changelog_web/controllers/home/feed_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ defmodule ChangelogWeb.Home.FeedController do
use ChangelogWeb, :controller

alias Changelog.{Feed, Podcast}
alias Changelog.ObanWorkers.FeedUpdater
alias Changelog.ObanWorkers.{FeedUpdater, MailDeliverer}

plug :assign_podcasts
plug :assign_feed when action in [:edit, :update, :delete, :refresh]
plug :assign_feed when action in [:edit, :update, :delete, :email, :refresh]
plug Authorize, [Policies.Feed, :feed]
plug :preload_current_user_extras

Expand Down Expand Up @@ -75,11 +75,19 @@ defmodule ChangelogWeb.Home.FeedController do
|> redirect_next(params, ~p"/~/feeds")
end

def email(conn = %{assigns: %{feed: feed}}, params) do
MailDeliverer.queue("feed_links", %{"feed" => feed.id})

conn
|> put_flash(:success, "It should be in your inbox real soon 📥")
|> redirect_next(params, ~p"/~/feeds")
end

def refresh(conn = %{assigns: %{feed: feed}}, params) do
FeedUpdater.queue(feed)

conn
|> put_flash(:success, "Your feed is being rebuilt as we speak. 🥂")
|> put_flash(:success, "Your feed is being rebuilt as we speak 🥂")
|> redirect_next(params, ~p"/~/feeds")
end

Expand Down
14 changes: 13 additions & 1 deletion lib/changelog_web/email.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ChangelogWeb.Email do
import Swoosh.Email

alias Changelog.{EpisodeGuest, EpisodeRequest, NewsItem, Podcast}
alias Changelog.{EpisodeGuest, EpisodeRequest, Feed, NewsItem, Podcast}
alias ChangelogWeb.{EpisodeView, EmailView}

# Comment related emails
Expand Down Expand Up @@ -95,6 +95,18 @@ defmodule ChangelogWeb.Email do
|> render(:sign_in)
end

def feed_links(feed) do
feed = Feed.preload_owner(feed)

styled_email()
|> header("X-CMail-GroupName", "Feed Links")
|> to(feed.owner)
|> subject("Your custom feed links")
|> assign(:person, feed.owner)
|> assign(:feed, feed)
|> render(:feed_links)
end

# Podcast related emails
def episode_published(subscription, episode) do
{email, subject, template} = if Podcast.is_news(episode.podcast) do
Expand Down
1 change: 1 addition & 0 deletions lib/changelog_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ defmodule ChangelogWeb.Router do
post "/~/unsubscribe", HomeController, :unsubscribe

resources "/~/feeds", Home.FeedController
post "/~/feeds/:id/email", Home.FeedController, :email
post "/~/feeds/:id/refresh", Home.FeedController, :refresh

get "/in", AuthController, :new, as: :sign_in
Expand Down
20 changes: 20 additions & 0 deletions lib/changelog_web/templates/email/feed_links.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<tr>
<td class="content-block" itemprop="handler" itemscope itemtype="http://schema.org/HttpActionHandler">
<p><%= greeting(@person) %></p>

<p>Pick a link to add <em><%= @feed.name %></em> feed to your favorite podcast app!</p>

<ul>
<li><a href={feed_app_url(@feed, "castros://subscribe/")}>Castro</a></li>
<li><a href={feed_app_url(@feed, "overcast://x-callback-url/add?url=", false)}>Overcast</a></li>
<li><a href={feed_app_url(@feed, "downcast://")}>Downcast</a></li>
<li><a href={feed_app_url(@feed, "pktc://subscribe/")}>Pocket Casts</a></li>
<li><a href={feed_app_url(@feed, "podcastaddict://")}>Podcast Addict</a></li>
<li><a href={feed_app_url(@feed, "podcast://")}>Apple Podcasts</a></li>
</ul>

<p>Or copy/paste <%= link("this RSS feed", to: url(~p"/feed/#{@feed.slug}")) %> into any other app.</p>

<p>💚, Logbot</p>
</td>
</tr>
16 changes: 16 additions & 0 deletions lib/changelog_web/templates/email/feed_links.text.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= greeting(@person) %>

Pick a link to add '<%= @feed.name %>' feed to your favorite podcast app!

Castro: <%= feed_app_url(@feed, "castros://subscribe/") %>
Overcast: <%= feed_app_url(@feed, "overcast://x-callback-url/add?url=", false) %>
Downcast: <%= feed_app_url(@feed, "downcast://") %>
Pocket Casts: <%= feed_app_url(@feed, "pktc://subscribe/") %>
Apple Podcasts: <%= feed_app_url(@feed, "podcast://") %>
Podcast Addict: <%= feed_app_url(@feed, "podcastaddict://") %>


Direct RSS: <%= url(~p"/feed/#{@feed.slug}") %>


💚, Logbot
4 changes: 3 additions & 1 deletion lib/changelog_web/templates/home/feed/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
<% end %>
</td>
<td style={"width: #{widths.actions}%; text-align: right;"}>
<a href={~p"/feeds/#{feed.slug}"} data-copy={url(~p"/feeds/#{feed.slug}")}>Get URL</a>
<%= link("Email Me", to: ~p"/~/feeds/#{feed}/email", method: :post) %>
<br/>
<a href={~p"/feeds/#{feed.slug}"} data-copy={url(~p"/feeds/#{feed.slug}")}>Copy URL</a>
<br/>
<%= link("Refresh", to: ~p"/~/feeds/#{feed}/refresh", method: :post) %>
<br/>
Expand Down
12 changes: 11 additions & 1 deletion lib/changelog_web/views/email_view.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ChangelogWeb.EmailView do
use ChangelogWeb, :public_view

alias Changelog.{Faker, HtmlKit, NewsItem, Podcast}
alias Changelog.{Faker, HtmlKit, NewsItem, Podcast, UrlKit}

alias ChangelogWeb.{
AuthView,
Expand All @@ -13,6 +13,16 @@ defmodule ChangelogWeb.EmailView do
PodcastView
}

def feed_app_url(feed, app_prefix, strip_scheme \\ true) do
feed_url = url(~p"/feed/#{feed.slug}")

if strip_scheme do
app_prefix <> UrlKit.sans_scheme(feed_url)
else
app_prefix <> feed_url
end
end

def greeting(nil), do: "Hey there,"

def greeting(person) do
Expand Down
14 changes: 14 additions & 0 deletions test/changelog_web/controllers/home/feed_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule ChangelogWeb.HomeFeedControllerTest do
use ChangelogWeb.ConnCase
use Changelog.EmailCase

alias Changelog.Feed

Expand Down Expand Up @@ -69,6 +70,17 @@ defmodule ChangelogWeb.HomeFeedControllerTest do
assert html_response(conn, 200) =~ ~r/error/
end

@tag :as_inserted_member
test "sends feed URL email and redirects", %{conn: conn} do
feed = insert(:feed, owner: conn.assigns.current_user)

conn = post(conn, ~p"/~/feeds/#{feed}/email")

assert %{success: 1, failure: 0} = Oban.drain_queue(queue: :email)
assert_email_sent(ChangelogWeb.Email.feed_links(feed))
assert redirected_to(conn) == ~p"/~/feeds"
end

test "requires user auth on all actions", %{conn: conn} do
feed = insert(:feed)

Expand All @@ -78,6 +90,8 @@ defmodule ChangelogWeb.HomeFeedControllerTest do
get(conn, ~p"/~/feeds/new"),
post(conn, ~p"/~/feeds", feed: @valid_attrs),
get(conn, ~p"/~/feeds/#{feed}/edit"),
post(conn, ~p"/~/feeds/#{feed}/email"),
post(conn, ~p"/~/feeds/#{feed}/refresh"),
put(conn, ~p"/~/feeds/#{feed}", feed: @valid_attrs),
delete(conn, ~p"/~/feeds/#{feed}")
],
Expand Down

0 comments on commit 781b32c

Please sign in to comment.