From 29b86785154f6002ce9b743c8f88552002625e84 Mon Sep 17 00:00:00 2001 From: Jerod Santo Date: Wed, 10 Jan 2024 10:32:29 -0600 Subject: [PATCH] Simplify topic and person view rendering - Fetch episodes directly - Remove unnecessary partials - Remove related calls to Routes helpers --- .../controllers/person_controller.ex | 12 +----------- .../controllers/topic_controller.ex | 19 +++++++------------ .../email/authored_news_published.html.eex | 2 +- .../email/authored_news_published.text.eex | 2 +- .../templates/email/comment_mention.html.eex | 2 +- .../templates/email/comment_mention.text.eex | 4 ++-- .../templates/email/comment_reply.html.eex | 2 +- .../templates/email/comment_reply.text.eex | 4 ++-- .../email/comment_subscription.html.eex | 2 +- .../email/comment_subscription.text.eex | 2 +- .../email/episode_published.html.eex | 2 +- .../email/episode_published.text.eex | 2 +- .../templates/email/news_published.html.eex | 2 +- .../templates/email/news_published.text.eex | 2 +- .../email/submitted_news_published.html.eex | 2 +- .../email/submitted_news_published.text.eex | 2 +- .../templates/person/_feed.html.eex | 6 ------ .../templates/person/show.html.eex | 9 ++++++++- .../templates/topic/_feed.html.eex | 6 ------ .../templates/topic/show.html.eex | 11 ++++++++--- lib/changelog_web/views/person_view.ex | 14 +++++++------- lib/changelog_web/views/topic_view.ex | 2 +- 22 files changed, 48 insertions(+), 63 deletions(-) delete mode 100644 lib/changelog_web/templates/person/_feed.html.eex delete mode 100644 lib/changelog_web/templates/topic/_feed.html.eex diff --git a/lib/changelog_web/controllers/person_controller.ex b/lib/changelog_web/controllers/person_controller.ex index 64fec6896b..1d9d5097bc 100755 --- a/lib/changelog_web/controllers/person_controller.ex +++ b/lib/changelog_web/controllers/person_controller.ex @@ -6,7 +6,6 @@ defmodule ChangelogWeb.PersonController do Captcha, Episode, MapKit, - NewsItem, Newsletters, Person, Podcast, @@ -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 diff --git a/lib/changelog_web/controllers/topic_controller.ex b/lib/changelog_web/controllers/topic_controller.ex index b095d38697..89cff86bcd 100644 --- a/lib/changelog_web/controllers/topic_controller.ex +++ b/lib/changelog_web/controllers/topic_controller.ex @@ -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 = @@ -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 diff --git a/lib/changelog_web/templates/email/authored_news_published.html.eex b/lib/changelog_web/templates/email/authored_news_published.html.eex index 77be906072..98741f62ef 100644 --- a/lib/changelog_web/templates/email/authored_news_published.html.eex +++ b/lib/changelog_web/templates/email/authored_news_published.html.eex @@ -10,7 +10,7 @@
  • ๐Ÿ”ฅ <%= news_item_promotion_advice(@item) %>
  • -

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

    +

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

    ๐Ÿ’š, Logbot

    diff --git a/lib/changelog_web/templates/email/authored_news_published.text.eex b/lib/changelog_web/templates/email/authored_news_published.text.eex index a85f042e21..ae475eb906 100644 --- a/lib/changelog_web/templates/email/authored_news_published.text.eex +++ b/lib/changelog_web/templates/email/authored_news_published.text.eex @@ -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 diff --git a/lib/changelog_web/templates/email/comment_mention.html.eex b/lib/changelog_web/templates/email/comment_mention.html.eex index f642f44143..43de55ee99 100644 --- a/lib/changelog_web/templates/email/comment_mention.html.eex +++ b/lib/changelog_web/templates/email/comment_mention.html.eex @@ -10,7 +10,7 @@ <%= link("Reply here", to: comment_url(@item, @comment), itemprop: "url") %> -

    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.

    +

    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.

    ๐Ÿ’š, Logbot

    diff --git a/lib/changelog_web/templates/email/comment_mention.text.eex b/lib/changelog_web/templates/email/comment_mention.text.eex index b69c5ea719..62adb7e25c 100644 --- a/lib/changelog_web/templates/email/comment_mention.text.eex +++ b/lib/changelog_web/templates/email/comment_mention.text.eex @@ -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 diff --git a/lib/changelog_web/templates/email/comment_reply.html.eex b/lib/changelog_web/templates/email/comment_reply.html.eex index bb78113fb6..503a9032cf 100644 --- a/lib/changelog_web/templates/email/comment_reply.html.eex +++ b/lib/changelog_web/templates/email/comment_reply.html.eex @@ -10,7 +10,7 @@ <%= link("Reply here", to: comment_url(@item, @reply), itemprop: "url") %> -

    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.

    +

    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.

    ๐Ÿ’š, Logbot

    diff --git a/lib/changelog_web/templates/email/comment_reply.text.eex b/lib/changelog_web/templates/email/comment_reply.text.eex index 2fd070ebe7..0d33075091 100644 --- a/lib/changelog_web/templates/email/comment_reply.text.eex +++ b/lib/changelog_web/templates/email/comment_reply.text.eex @@ -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 diff --git a/lib/changelog_web/templates/email/comment_subscription.html.eex b/lib/changelog_web/templates/email/comment_subscription.html.eex index 5e10678539..bd28b2ddd4 100644 --- a/lib/changelog_web/templates/email/comment_subscription.html.eex +++ b/lib/changelog_web/templates/email/comment_subscription.html.eex @@ -10,7 +10,7 @@ <%= link("Reply here", to: comment_url(@item, @comment), itemprop: "url") %> -

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

    +

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

    ๐Ÿ’š, Logbot

    diff --git a/lib/changelog_web/templates/email/comment_subscription.text.eex b/lib/changelog_web/templates/email/comment_subscription.text.eex index a56fb0f7a8..e49b990e0d 100644 --- a/lib/changelog_web/templates/email/comment_subscription.text.eex +++ b/lib/changelog_web/templates/email/comment_subscription.text.eex @@ -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 diff --git a/lib/changelog_web/templates/email/episode_published.html.eex b/lib/changelog_web/templates/email/episode_published.html.eex index 749642db76..ca96bf029c 100644 --- a/lib/changelog_web/templates/email/episode_published.html.eex +++ b/lib/changelog_web/templates/email/episode_published.html.eex @@ -34,7 +34,7 @@ <% end %> <%= if @person do %> -

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

    +

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

    <% end %>

    ๐Ÿ’š, Logbot

    diff --git a/lib/changelog_web/templates/email/episode_published.text.eex b/lib/changelog_web/templates/email/episode_published.text.eex index 279aa1d8f3..b7221ea1c5 100644 --- a/lib/changelog_web/templates/email/episode_published.text.eex +++ b/lib/changelog_web/templates/email/episode_published.text.eex @@ -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 diff --git a/lib/changelog_web/templates/email/news_published.html.eex b/lib/changelog_web/templates/email/news_published.html.eex index 0dedc2c8d3..f779babeb7 100644 --- a/lib/changelog_web/templates/email/news_published.html.eex +++ b/lib/changelog_web/templates/email/news_published.html.eex @@ -86,7 +86,7 @@ "> <%= if @person do %> -

    <%= link("Unsubscribe", to: PersonView.opt_out_url(Endpoint, @person, "podcast", @episode.podcast.slug)) %>

    +

    <%= link("Unsubscribe", to: PersonView.opt_out_url(@person, "podcast", @episode.podcast.slug)) %>

    <%= if String.length(@subscription.context) > 0 do %>

    (Context: <%= @subscription.context %>)

    <% end %> diff --git a/lib/changelog_web/templates/email/news_published.text.eex b/lib/changelog_web/templates/email/news_published.text.eex index 8e5316a658..be0b7d6fad 100644 --- a/lib/changelog_web/templates/email/news_published.text.eex +++ b/lib/changelog_web/templates/email/news_published.text.eex @@ -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 %> diff --git a/lib/changelog_web/templates/email/submitted_news_published.html.eex b/lib/changelog_web/templates/email/submitted_news_published.html.eex index 4a4dc468ed..5dfe2eb3bf 100644 --- a/lib/changelog_web/templates/email/submitted_news_published.html.eex +++ b/lib/changelog_web/templates/email/submitted_news_published.html.eex @@ -10,7 +10,7 @@
  • ๐Ÿ”ฅ <%= news_item_promotion_advice(@item) %>
  • -

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

    +

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

    ๐Ÿ’š, Logbot

    diff --git a/lib/changelog_web/templates/email/submitted_news_published.text.eex b/lib/changelog_web/templates/email/submitted_news_published.text.eex index fe68ac4d9f..085b7fa515 100644 --- a/lib/changelog_web/templates/email/submitted_news_published.text.eex +++ b/lib/changelog_web/templates/email/submitted_news_published.text.eex @@ -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 diff --git a/lib/changelog_web/templates/person/_feed.html.eex b/lib/changelog_web/templates/person/_feed.html.eex deleted file mode 100644 index 5352913fb4..0000000000 --- a/lib/changelog_web/templates/person/_feed.html.eex +++ /dev/null @@ -1,6 +0,0 @@ -<%= if Enum.any?(@items) do %> - <%= render_many(@items, NewsItemView, "_summary.html", Map.merge(assigns, %{as: :item, style: "date"})) %> - <%= render(SharedView, "_more_button.html", assigns) %> -<% else %> -

    Nothing to see here. <%= link("Submit", to: Routes.news_item_path(@conn, :new)) %> some news or <%= link("request", to: Routes.episode_request_path(@conn, :new)) %> an episode.

    -<% end %> diff --git a/lib/changelog_web/templates/person/show.html.eex b/lib/changelog_web/templates/person/show.html.eex index 76bdcdaae4..b52d618726 100644 --- a/lib/changelog_web/templates/person/show.html.eex +++ b/lib/changelog_web/templates/person/show.html.eex @@ -15,11 +15,18 @@ <% end %> <%= @person |> list_of_links(" ยท ") |> raw() %> + +
    <%= SharedHelpers.pluralize(@page.total_entries, "episode", "episodes") %>
    - <%= render("_feed.html", assigns) %> + <%= if Enum.any?(@page.entries) do %> + <%= render_many(@page, EpisodeView, "_item.html") %> + <%= render(SharedView, "_more_button.html", assigns) %> + <% else %> +

    Nothing to see here. <%= link("Submit", to: ~p"/news/submit") %> some news or <%= link("request", to: ~p"/request/new") %> an episode.

    + <% end %>
    diff --git a/lib/changelog_web/templates/topic/_feed.html.eex b/lib/changelog_web/templates/topic/_feed.html.eex deleted file mode 100644 index 5352913fb4..0000000000 --- a/lib/changelog_web/templates/topic/_feed.html.eex +++ /dev/null @@ -1,6 +0,0 @@ -<%= if Enum.any?(@items) do %> - <%= render_many(@items, NewsItemView, "_summary.html", Map.merge(assigns, %{as: :item, style: "date"})) %> - <%= render(SharedView, "_more_button.html", assigns) %> -<% else %> -

    Nothing to see here. <%= link("Submit", to: Routes.news_item_path(@conn, :new)) %> some news or <%= link("request", to: Routes.episode_request_path(@conn, :new)) %> an episode.

    -<% end %> diff --git a/lib/changelog_web/templates/topic/show.html.eex b/lib/changelog_web/templates/topic/show.html.eex index 3c7f2f4323..d57f49b028 100644 --- a/lib/changelog_web/templates/topic/show.html.eex +++ b/lib/changelog_web/templates/topic/show.html.eex @@ -13,15 +13,20 @@
    - <%= SharedHelpers.pluralize(news_count(@topic), "episode", "episodes") %> + <%= SharedHelpers.pluralize(@page.total_entries, "episode", "episodes") %>
    - <%= 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" %>
    - <%= render("_feed.html", assigns) %> + <%= if Enum.any?(@page.entries) do %> + <%= render_many(@page, EpisodeView, "_item.html") %> + <%= render(SharedView, "_more_button.html", assigns) %> + <% else %> +

    Nothing to see here. <%= link("Submit", to: ~p"/news/submit") %> some news or <%= link("request", to: ~p"/request/new") %> an episode.

    + <% end %>
    diff --git a/lib/changelog_web/views/person_view.ex b/lib/changelog_web/views/person_view.ex index a3f54e9972..86ebed45e5 100644 --- a/lib/changelog_web/views/person_view.ex +++ b/lib/changelog_web/views/person_view.ex @@ -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) @@ -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) diff --git a/lib/changelog_web/views/topic_view.ex b/lib/changelog_web/views/topic_view.ex index 301c423929..9803ef6794 100644 --- a/lib/changelog_web/views/topic_view.ex +++ b/lib/changelog_web/views/topic_view.ex @@ -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