diff --git a/lib/bdfr_browser/http/plug.ex b/lib/bdfr_browser/http/plug.ex index a59c020..5b9eb1c 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -123,6 +123,29 @@ defmodule BdfrBrowser.HTTP.Plug do |> send_resp(200, content) end + get "/s/:subreddits/most_comments" do + subreddit_names = String.split(subreddits, "+") + subreddit_records = Subreddit.multiple_names(subreddit_names) |> Repo.all() + {:ok, range_start} = Date.from_iso8601("1970-01-01") + range_end = Date.utc_today() + + tpl_args = [ + subreddit: subreddit_names, + date: "Most Comments", + posts: + subreddit_records + |> Post.during_range(range_start, range_end, :most_comments) + |> Post.with_comments(1) + |> Repo.all() + ] + + content = render_template("subreddit_posts", tpl_args) + + conn + |> put_resp_header("content-type", "text/html; charset=utf-8") + |> send_resp(200, content) + end + get "/chats" do tpl_args = [chats: Chat.listing() |> Repo.all()] content = render_template("chats", tpl_args) diff --git a/lib/bdfr_browser/post.ex b/lib/bdfr_browser/post.ex index 6962bd4..3263dc1 100644 --- a/lib/bdfr_browser/post.ex +++ b/lib/bdfr_browser/post.ex @@ -1,7 +1,7 @@ defmodule BdfrBrowser.Post do use Ecto.Schema - import Ecto.Query, only: [from: 2] + import Ecto.Query, only: [dynamic: 1, dynamic: 2, from: 2, having: 3] alias BdfrBrowser.{Comment, Subreddit} @@ -46,7 +46,9 @@ defmodule BdfrBrowser.Post do during_range(subreddit, Date.beginning_of_month(d), Date.end_of_month(d)) end - def during_range(subreddits, start_date, end_date) when is_list(subreddits) do + def during_range(subreddits, start_date, end_date, sort_field \\ :posted_at) + + def during_range(subreddits, start_date, end_date, sort_field) when is_list(subreddits) do subreddit_ids = for s <- subreddits, do: s.id from(p in __MODULE__, @@ -58,18 +60,19 @@ defmodule BdfrBrowser.Post do url: p.url, author: p.author, posted_at: p.posted_at, - num_comments: count(c.id), - subreddit: s.name + num_comments: selected_as(count(c.id), :num_comments), + subreddit: s.name, + date: fragment("to_char(?, 'YYYY-MM')", p.posted_at) }, where: p.subreddit_id in ^subreddit_ids and type(p.posted_at, :date) >= ^start_date and type(p.posted_at, :date) <= ^end_date, - order_by: [desc: p.posted_at], + order_by: ^dynamic_sort(sort_field), group_by: [p.id, s.name] ) end - def during_range(subreddit, start_date, end_date) do + def during_range(subreddit, start_date, end_date, sort_field) do from(p in __MODULE__, left_join: c in assoc(p, :comments), join: s in assoc(p, :subreddit), @@ -79,17 +82,22 @@ defmodule BdfrBrowser.Post do url: p.url, author: p.author, posted_at: p.posted_at, - num_comments: count(c.id), - subreddit: s.name + num_comments: selected_as(count(c.id), :num_comments), + subreddit: s.name, + date: fragment("to_char(?, 'YYYY-MM')", p.posted_at) }, where: p.subreddit_id == ^subreddit.id and type(p.posted_at, :date) >= ^start_date and type(p.posted_at, :date) <= ^end_date, - order_by: [desc: p.posted_at], + order_by: ^dynamic_sort(sort_field), group_by: [p.id, s.name] ) end + def with_comments(query, more_than \\ 0) do + having(query, [p, c, s], count(c.id) > ^more_than) + end + def get_full(id) do from(p in __MODULE__, where: p.id == ^id, @@ -169,4 +177,7 @@ defmodule BdfrBrowser.Post do group_by: [p.id, s.name] ) end + + defp dynamic_sort(:posted_at), do: [{:desc, dynamic([p], p.posted_at)}] + defp dynamic_sort(:most_comments), do: dynamic(fragment("num_comments DESC")) end diff --git a/priv/templates/http/post.eex b/priv/templates/http/post.eex index 92a275b..efd9706 100644 --- a/priv/templates/http/post.eex +++ b/priv/templates/http/post.eex @@ -12,7 +12,9 @@

<%= BdfrBrowser.RenderUtils.link_to_user(post.author) %> - - <%= DateTime.to_iso8601(post.posted_at) %> + <%= BdfrBrowser.RenderUtils.format_date(post.posted_at, :long) %> + - + <%= Float.round(post.upvote_ratio * 100, 1) %>% - Open reddit

diff --git a/priv/templates/http/subreddit.eex b/priv/templates/http/subreddit.eex index 4de6423..17e2194 100644 --- a/priv/templates/http/subreddit.eex +++ b/priv/templates/http/subreddit.eex @@ -12,7 +12,7 @@

- <%= if is_list(subreddit) do %> + <%= if is_list(subreddit) and length(subreddit) > 1 do %> Multi-Reddit <% else %> <%= subreddit %> @@ -41,6 +41,12 @@
+ <%= if is_list(subreddit) do %> + /most_comments" role="button">Most Comments + <% else %> + Most Comments + <% end %> + <%= for date <- dates do %> <%= if is_list(subreddit) do %> /<%= date %>" role="button"><%= date %> diff --git a/priv/templates/http/subreddit_posts.eex b/priv/templates/http/subreddit_posts.eex index c0a9255..a5393cd 100644 --- a/priv/templates/http/subreddit_posts.eex +++ b/priv/templates/http/subreddit_posts.eex @@ -13,7 +13,7 @@

- <%= if is_list(subreddit) do %> + <%= if is_list(subreddit) and length(subreddit) > 1 do %> Multi-Reddit <% else %> <%= subreddit %> @@ -25,7 +25,7 @@ <%= for post <- posts do %>
-
<%= post.title %>
+
<%= post.title %>
<%= BdfrBrowser.RenderUtils.post_type_icon(post.url) %>