diff --git a/lib/bdfr_browser/http/plug.ex b/lib/bdfr_browser/http/plug.ex index 5342721..b7eeaed 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -8,11 +8,18 @@ defmodule BdfrBrowser.HTTP.Plug do get "/" do config_file = Application.fetch_env!(:bdfr_browser, :config_file) - archived_subreddits = Enum.sort(config_file["archived_subreddits"]) - archived_users = Enum.sort(config_file["archived_users"]) - saved_searches = Enum.sort(config_file["saved_searches"]) + archived_subreddits = Enum.sort_by(config_file["archived_subreddits"], &String.downcase/1) + archived_users = Enum.sort_by(config_file["archived_users"], &String.downcase/1) + simulated_multireddits = Enum.sort_by(config_file["simulated_multireddits"], fn {t, _} -> String.downcase(t) end) + saved_searches = Enum.sort_by(config_file["saved_searches"], &String.downcase/1) + + tpl_args = [ + subreddits: archived_subreddits, + users: archived_users, + multireddits: simulated_multireddits, + searches: saved_searches + ] - tpl_args = [subreddits: archived_subreddits, users: archived_users, searches: saved_searches] content = render_template("index", tpl_args) conn @@ -21,7 +28,10 @@ defmodule BdfrBrowser.HTTP.Plug do end get "/all/subreddits" do - tpl_args = [subreddits: Subreddit.names() |> Repo.all()] + config_file = Application.fetch_env!(:bdfr_browser, :config_file) + archived_subreddits = MapSet.new(config_file["archived_subreddits"]) + + tpl_args = [subreddits: Subreddit.names() |> Repo.all(), archived_subreddits: archived_subreddits] content = render_template("all_subreddits", tpl_args) conn @@ -78,6 +88,39 @@ defmodule BdfrBrowser.HTTP.Plug do |> send_resp(200, content) end + get "/m/:subreddits" do + subreddit_names = String.split(subreddits, "+") + subreddit_records = Subreddit.multiple_names(subreddit_names) |> Repo.all() + + tpl_args = [ + subreddit: subreddit_names, + dates: subreddit_records |> Post.date_listing() |> Repo.all() + ] + + content = render_template("subreddit", tpl_args) + + conn + |> put_resp_header("content-type", "text/html; charset=utf-8") + |> send_resp(200, content) + end + + get "/m/:subreddits/:date" do + subreddit_names = String.split(subreddits, "+") + subreddit_records = Subreddit.multiple_names(subreddit_names) |> Repo.all() + + tpl_args = [ + subreddit: subreddit_names, + date: date, + posts: subreddit_records |> Post.during_month(date) |> 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 032ee64..1b13b28 100644 --- a/lib/bdfr_browser/post.ex +++ b/lib/bdfr_browser/post.ex @@ -21,6 +21,17 @@ defmodule BdfrBrowser.Post do has_many :comments, Comment end + def date_listing(subreddits) when is_list(subreddits) do + subreddit_ids = for s <- subreddits, do: s.id + + from(p in __MODULE__, + select: fragment("to_char(?, 'YYYY-MM')", p.posted_at), + where: p.subreddit_id in ^subreddit_ids, + distinct: true, + order_by: [desc: fragment("to_char(?, 'YYYY-MM')", p.posted_at)] + ) + end + def date_listing(subreddit) do from(p in __MODULE__, select: fragment("to_char(?, 'YYYY-MM')", p.posted_at), @@ -35,15 +46,45 @@ 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 + subreddit_ids = for s <- subreddits, do: s.id + + from(p in __MODULE__, + left_join: c in assoc(p, :comments), + join: s in assoc(p, :subreddit), + select: %{ + id: p.id, + title: p.title, + author: p.author, + posted_at: p.posted_at, + num_comments: count(c.id), + subreddit: s.name + }, + 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], + group_by: [p.id, s.name] + ) + end + def during_range(subreddit, start_date, end_date) do from(p in __MODULE__, left_join: c in assoc(p, :comments), - select: %{id: p.id, title: p.title, author: p.author, posted_at: p.posted_at, num_comments: count(c.id)}, + join: s in assoc(p, :subreddit), + select: %{ + id: p.id, + title: p.title, + author: p.author, + posted_at: p.posted_at, + num_comments: count(c.id), + subreddit: s.name + }, 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], - group_by: p.id + group_by: [p.id, s.name] ) end diff --git a/lib/bdfr_browser/subreddit.ex b/lib/bdfr_browser/subreddit.ex index 0d8d42a..eb9423e 100644 --- a/lib/bdfr_browser/subreddit.ex +++ b/lib/bdfr_browser/subreddit.ex @@ -6,9 +6,13 @@ defmodule BdfrBrowser.Subreddit do alias BdfrBrowser.Post schema "subreddits" do - field(:name, :string) + field :name, :string - has_many(:posts, Post) + has_many :posts, Post + end + + def multiple_names(names) do + from(s in __MODULE__, where: s.name in ^names) end def names do diff --git a/priv/templates/http/all_subreddits.eex b/priv/templates/http/all_subreddits.eex index 9c8daa7..89de0d0 100644 --- a/priv/templates/http/all_subreddits.eex +++ b/priv/templates/http/all_subreddits.eex @@ -10,7 +10,10 @@
<%= for subreddit <- subreddits do %> - <%= subreddit %> + btn-lg" + href="/r/<%= subreddit %>" role="button"> + <%= subreddit %> + <% end %>
diff --git a/priv/templates/http/index.eex b/priv/templates/http/index.eex index 03d642a..fda547e 100644 --- a/priv/templates/http/index.eex +++ b/priv/templates/http/index.eex @@ -45,6 +45,18 @@
+

Simulated Multireddits

+ +
+
+ <%= for {title, subreddits} <- multireddits do %> + " role="button"><%= title %> + <% end %> +
+
+ +
+

Saved Searches

diff --git a/priv/templates/http/subreddit.eex b/priv/templates/http/subreddit.eex index 862aa59..ba2d0ed 100644 --- a/priv/templates/http/subreddit.eex +++ b/priv/templates/http/subreddit.eex @@ -1,16 +1,32 @@ -

<%= subreddit %>

+

+ <%= if is_list(subreddit) do %> + <%= Enum.join(subreddit, ", ") %> + <% else %> + <%= subreddit %> + <% end %> +

<%= for date <- dates do %> - <%= date %> + <%= if is_list(subreddit) do %> + /<%= date %>" role="button"><%= date %> + <% else %> + <%= date %> + <% end %> <% end %>
diff --git a/priv/templates/http/subreddit_posts.eex b/priv/templates/http/subreddit_posts.eex index 61982aa..b858eb0 100644 --- a/priv/templates/http/subreddit_posts.eex +++ b/priv/templates/http/subreddit_posts.eex @@ -1,19 +1,31 @@ -

<%= subreddit %>

+

+ <%= if is_list(subreddit) do %> + <%= Enum.join(subreddit, ", ") %> + <% else %> + <%= subreddit %> + <% end %> +

<%= for post <- posts do %>
-
<%= post.title %>
+
<%= post.title %>
<%= post.num_comments %> comment(s) - <%= DateTime.to_iso8601(post.posted_at) %>
diff --git a/priv/templates/http/user.eex b/priv/templates/http/user.eex index b62f19d..7c88e46 100644 --- a/priv/templates/http/user.eex +++ b/priv/templates/http/user.eex @@ -1,3 +1,10 @@ + +

<%= name %>