Skip to content

Commit

Permalink
Simplify topic and person view rendering
Browse files Browse the repository at this point in the history
- Fetch episodes directly
- Remove unnecessary partials
- Remove related calls to Routes helpers
  • Loading branch information
jerodsanto committed Jan 10, 2024
1 parent f48861d commit 29b8678
Show file tree
Hide file tree
Showing 22 changed files with 48 additions and 63 deletions.
12 changes: 1 addition & 11 deletions lib/changelog_web/controllers/person_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule ChangelogWeb.PersonController do
Captcha,
Episode,
MapKit,
NewsItem,
Newsletters,
Person,
Podcast,
Expand Down Expand Up @@ -83,20 +82,11 @@ defmodule ChangelogWeb.PersonController do
|> Episode.published()
|> Episode.newest_first()
|> Episode.exclude_transcript()
|> Episode.preload_all()
|> Repo.paginate(Map.put(params, :page_size, 20))

items =
page.entries
|> NewsItem.with_episodes()
|> NewsItem.published()
|> NewsItem.newest_first()
|> NewsItem.preload_all()
|> Repo.all()
|> Enum.map(&NewsItem.load_object/1)

conn
|> assign(:person, person)
|> assign(:items, items)
|> assign(:page, page)
|> render(:show)
end
Expand Down
19 changes: 7 additions & 12 deletions lib/changelog_web/controllers/topic_controller.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ChangelogWeb.TopicController do
use ChangelogWeb, :controller

alias Changelog.{NewsItem, Topic}
alias Changelog.{Episode, Topic}

def index(conn, params) do
page =
Expand All @@ -18,21 +18,16 @@ defmodule ChangelogWeb.TopicController do
topic = Repo.get_by!(Topic, slug: slug)

page =
NewsItem
|> NewsItem.with_topic(topic)
|> NewsItem.audio()
|> NewsItem.published()
|> NewsItem.newest_first()
|> NewsItem.preload_all()
topic
|> assoc(:episodes)
|> Episode.published()
|> Episode.newest_first()
|> Episode.exclude_transcript()
|> Episode.preload_all()
|> Repo.paginate(params)

items =
page.entries
|> Enum.map(&NewsItem.load_object/1)

conn
|> assign(:topic, topic)
|> assign(:items, items)
|> assign(:page, page)
|> render(:show)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<li>🔥 <%= news_item_promotion_advice(@item) %></li>
</ol>

<p>Don't want these notifications? <em><%= link("Disable 'em", to: PersonView.opt_out_url(Endpoint, @person, "setting", "email_on_authored_news")) %></em></p>
<p>Don't want these notifications? <em><%= link("Disable 'em", to: PersonView.opt_out_url(@person, "setting", "email_on_authored_news")) %></em></p>

<p>💚, Logbot</p>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ Here are 3 ways you can help your submission make the grade:
Don't want these notifications? Disable 'em[2]

[1]: <%= Routes.news_item_url(Endpoint, :show, NewsItem.slug(@item)) %>
[2]: <%= PersonView.opt_out_url(Endpoint, @person, "setting", "email_on_authored_news") %>
[2]: <%= PersonView.opt_out_url(@person, "setting", "email_on_authored_news") %>

💚, Logbot
2 changes: 1 addition & 1 deletion lib/changelog_web/templates/email/comment_mention.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<%= link("Reply here", to: comment_url(@item, @comment), itemprop: "url") %>
</span>

<p>Seen enough? <%= link("Mute this discussion", to: PersonView.opt_out_url(Endpoint, @person, "news", @item.id)) %> or <%= link("disable mention notifications", to: PersonView.opt_out_url(Endpoint, @person, "setting", "email_on_comment_mentions")) %> all together.</p>
<p>Seen enough? <%= link("Mute this discussion", to: PersonView.opt_out_url(@person, "news", @item.id)) %> or <%= link("disable mention notifications", to: PersonView.opt_out_url(@person, "setting", "email_on_comment_mentions")) %> all together.</p>

<p>💚, Logbot</p>

Expand Down
4 changes: 2 additions & 2 deletions lib/changelog_web/templates/email/comment_mention.text.eex
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Reply here: <%= comment_url(@item, @comment) %>

Seen enough? Mute this discussion:

<%= PersonView.opt_out_url(Endpoint, @person, "news", @item.id) %>
<%= PersonView.opt_out_url(@person, "news", @item.id) %>

Or disable mention notifications all together:

<%= PersonView.opt_out_url(Endpoint, @person, "setting", "email_on_comment_mentions") %>
<%= PersonView.opt_out_url(@person, "setting", "email_on_comment_mentions") %>

💚, Logbot
2 changes: 1 addition & 1 deletion lib/changelog_web/templates/email/comment_reply.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<%= link("Reply here", to: comment_url(@item, @reply), itemprop: "url") %>
</span>

<p>Seen enough? <%= link("Mute this discussion", to: PersonView.opt_out_url(Endpoint, @person, "news", @item.id)) %> or <%= link("disable reply notifications", to: PersonView.opt_out_url(Endpoint, @person, "setting", "email_on_comment_replies")) %> all together.</p>
<p>Seen enough? <%= link("Mute this discussion", to: PersonView.opt_out_url(@person, "news", @item.id)) %> or <%= link("disable reply notifications", to: PersonView.opt_out_url(@person, "setting", "email_on_comment_replies")) %> all together.</p>

<p>💚, Logbot</p>

Expand Down
4 changes: 2 additions & 2 deletions lib/changelog_web/templates/email/comment_reply.text.eex
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Reply here: <%= comment_url(@item, @reply) %>

Seen enough? Mute this discussion:

<%= PersonView.opt_out_url(Endpoint, @person, "news", @item.id) %>
<%= PersonView.opt_out_url(@person, "news", @item.id) %>

Or disable reply notifications all together:

<%= PersonView.opt_out_url(Endpoint, @person, "setting", "email_on_comment_replies") %>
<%= PersonView.opt_out_url(@person, "setting", "email_on_comment_replies") %>

💚, Logbot
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<%= link("Reply here", to: comment_url(@item, @comment), itemprop: "url") %>
</span>

<p>Don't want these notifications? <em><%= link("Unsubscribe here", to: PersonView.opt_out_url(Endpoint, @person, "news", @item.id)) %></em></p>
<p>Don't want these notifications? <em><%= link("Unsubscribe here", to: PersonView.opt_out_url(@person, "news", @item.id)) %></em></p>

<p>💚, Logbot</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Reply here: <%= comment_url(@item, @comment) %>

Don't want these notifications? Unsubscribe here:

<%= PersonView.opt_out_url(Endpoint, @person, "news", @item.id) %>
<%= PersonView.opt_out_url(@person, "news", @item.id) %>

💚, Logbot

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<% end %>

<%= if @person do %>
<p>Don't want these notifications? <em><%= link("Disable 'em", to: PersonView.opt_out_url(Endpoint, @person, "podcast", @episode.podcast.slug)) %></em></p>
<p>Don't want these notifications? <em><%= link("Disable 'em", to: PersonView.opt_out_url(@person, "podcast", @episode.podcast.slug)) %></em></p>
<% end %>

<p>💚, Logbot</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Here's your new episode of <%= @episode.podcast.name %> with <%= SharedHelpers.c
Listen and discuss here: <%= EpisodeView.share_url(@episode) %>

<%= if @person do %>
Don't want these notifications? Disable 'em: <%= PersonView.opt_out_url(Endpoint, @person, "podcast", @episode.podcast.slug) %>
Don't want these notifications? Disable 'em: <%= PersonView.opt_out_url(@person, "podcast", @episode.podcast.slug) %>
<% end %>

💚, Logbot
Expand Down
2 changes: 1 addition & 1 deletion lib/changelog_web/templates/email/news_published.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<td class="footer" align="center">
<img class="spacer-20" src="<%= url(~p"/images/email/spacer_1.gif") %>">
<%= if @person do %>
<p><%= link("Unsubscribe", to: PersonView.opt_out_url(Endpoint, @person, "podcast", @episode.podcast.slug)) %></p>
<p><%= link("Unsubscribe", to: PersonView.opt_out_url(@person, "podcast", @episode.podcast.slug)) %></p>
<%= if String.length(@subscription.context) > 0 do %>
<p>(Context: <%= @subscription.context %>)</p>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion lib/changelog_web/templates/email/news_published.text.eex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= @episode.email_content |> SharedHelpers.md_to_text() %>

<%= if @person do %>
Unsubscribe: <%= PersonView.opt_out_url(Endpoint, @person, "podcast", @episode.podcast.slug) %>
Unsubscribe: <%= PersonView.opt_out_url(@person, "podcast", @episode.podcast.slug) %>
<% end %>

<%= if String.length(@subscription.context) > 0 do %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<li>🔥 <%= news_item_promotion_advice(@item) %></li>
</ol>

<p>Don't want these notifications? <em><%= link("Disable 'em", to: PersonView.opt_out_url(Endpoint, @person, "setting", "email_on_submitted_news")) %></em></p>
<p>Don't want these notifications? <em><%= link("Disable 'em", to: PersonView.opt_out_url(@person, "setting", "email_on_submitted_news")) %></em></p>

<p>💚, Logbot</p>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ Here are 3 ways you can help your submission make the grade:
Don't want these notifications? Disable 'em[2]

[1]: <%= Routes.news_item_url(Endpoint, :show, NewsItemView.hashid(@item)) %>
[2]: <%= PersonView.opt_out_url(Endpoint, @person, "setting", "email_on_submitted_news") %>
[2]: <%= PersonView.opt_out_url(@person, "setting", "email_on_submitted_news") %>

💚, Logbot
6 changes: 0 additions & 6 deletions lib/changelog_web/templates/person/_feed.html.eex

This file was deleted.

9 changes: 8 additions & 1 deletion lib/changelog_web/templates/person/show.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
<% end %>
<%= @person |> list_of_links(" · ") |> raw() %>
</div>

<div class="topic-header-meta"><%= SharedHelpers.pluralize(@page.total_entries, "episode", "episodes") %></div>
</div>
</header>

<a id="feed" class="anchor"></a>
<div class="feed">
<%= render("_feed.html", assigns) %>
<%= if Enum.any?(@page.entries) do %>
<%= render_many(@page, EpisodeView, "_item.html") %>
<%= render(SharedView, "_more_button.html", assigns) %>
<% else %>
<p class="feed-note">Nothing to see here. <%= link("Submit", to: ~p"/news/submit") %> some news or <%= link("request", to: ~p"/request/new") %> an episode.</p>
<% end %>
</div>
</div>
6 changes: 0 additions & 6 deletions lib/changelog_web/templates/topic/_feed.html.eex

This file was deleted.

11 changes: 8 additions & 3 deletions lib/changelog_web/templates/topic/show.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
</div>

<div class="topic-header-meta">
<%= SharedHelpers.pluralize(news_count(@topic), "episode", "episodes") %>
<%= SharedHelpers.pluralize(@page.total_entries, "episode", "episodes") %>
</div>
</div>

<%= link "All Topics", to: Routes.topic_path(@conn, :index), title: "View All Topics", class: "topic-header-back" %>
<%= link "All Topics", to: ~p"/topics", title: "View All Topics", class: "topic-header-back" %>
</header>

<a id="feed" class="anchor"></a>
<div class="feed">
<%= render("_feed.html", assigns) %>
<%= if Enum.any?(@page.entries) do %>
<%= render_many(@page, EpisodeView, "_item.html") %>
<%= render(SharedView, "_more_button.html", assigns) %>
<% else %>
<p class="feed-note">Nothing to see here. <%= link("Submit", to: ~p"/news/submit") %> some news or <%= link("request", to: ~p"/request/new") %> an episode.</p>
<% end %>
</div>
</div>
14 changes: 7 additions & 7 deletions lib/changelog_web/views/person_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ChangelogWeb.PersonView do
use ChangelogWeb, :public_view

alias Changelog.{Files, HtmlKit, NewsItem, Person, Podcast}
alias ChangelogWeb.{Endpoint, NewsItemView, SharedView, PodcastView}
alias ChangelogWeb.{EpisodeView, SharedView, PodcastView}

def avatar_url(person), do: avatar_url(person, :small)

Expand Down Expand Up @@ -113,24 +113,24 @@ defmodule ChangelogWeb.PersonView do
|> Enum.join(separator)
end

def opt_out_path(conn, person, type, id) do
def opt_out_path(person, type, id) do
{:ok, encoded} = Person.encoded_id(person)
Routes.home_path(conn, :opt_out, encoded, type, id)
~p"/~/nope/#{encoded}/#{type}/#{id}"
end

def opt_out_url(conn, person, type, id) do
def opt_out_url(person, type, id) do
{:ok, encoded} = Person.encoded_id(person)
Routes.home_url(conn, :opt_out, encoded, type, id)
url(~p"/~/nope/#{encoded}/#{type}/#{id}")
end

def profile_path(person = %{public_profile: true}) do
Routes.person_path(Endpoint, :show, person.handle)
~p"/person/#{person.handle}"
end

def profile_path(person), do: external_url(person)

def profile_url(person = %{public_profile: true}) do
Routes.person_url(Endpoint, :show, person.handle)
url(~p"/person/#{person.handle}")
end

def profile_url(person), do: external_url(person)
Expand Down
2 changes: 1 addition & 1 deletion lib/changelog_web/views/topic_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ChangelogWeb.TopicView do
use ChangelogWeb, :public_view

alias Changelog.Topic
alias ChangelogWeb.{NewsItemView}
alias ChangelogWeb.{EpisodeView}
alias Changelog.Files.Icon

def admin_edit_link(conn, %{admin: true}, topic) do
Expand Down

0 comments on commit 29b8678

Please sign in to comment.