feat: User overview page

This commit is contained in:
Daniel Kempkens 2023-08-15 15:03:46 +02:00
parent 5d97a4f1d3
commit 5a9ecce1cf
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
7 changed files with 117 additions and 6 deletions

View file

@ -15,7 +15,7 @@ defmodule BdfrBrowser.Chat do
def listing do def listing do
from(c in __MODULE__, from(c in __MODULE__,
left_join: m in assoc(c, :messages), join: m in assoc(c, :messages),
select: %{id: c.id, accounts: c.accounts, num_messages: count(m.id), latest_message: max(m.posted_at)}, select: %{id: c.id, accounts: c.accounts, num_messages: count(m.id), latest_message: max(m.posted_at)},
order_by: [desc: max(m.posted_at)], order_by: [desc: max(m.posted_at)],
group_by: c.id group_by: c.id

View file

@ -1,6 +1,8 @@
defmodule BdfrBrowser.Comment do defmodule BdfrBrowser.Comment do
use Ecto.Schema use Ecto.Schema
import Ecto.Query, only: [from: 2]
alias BdfrBrowser.Post alias BdfrBrowser.Post
@primary_key {:id, :string, autogenerate: false} @primary_key {:id, :string, autogenerate: false}
@ -16,4 +18,23 @@ defmodule BdfrBrowser.Comment do
belongs_to :parent, __MODULE__ belongs_to :parent, __MODULE__
has_many :children, __MODULE__, foreign_key: :parent_id has_many :children, __MODULE__, foreign_key: :parent_id
end end
def by_author(author) do
from(c in __MODULE__,
join: p in assoc(c, :post),
join: s in assoc(p, :subreddit),
select: %{
body: c.body,
children: [],
posted_at: c.posted_at,
subreddit: s.name,
post_id: p.id,
post_title: p.title,
post_date: fragment("to_char(?, 'YYYY-MM')", p.posted_at)
},
where: c.author == ^author,
order_by: [desc: c.posted_at],
group_by: [c.id, p.id, s.name]
)
end
end end

View file

@ -1,7 +1,7 @@
defmodule BdfrBrowser.HTTP.Plug do defmodule BdfrBrowser.HTTP.Plug do
use Plug.Router use Plug.Router
alias BdfrBrowser.{Chat, Message, Repo, Post, Subreddit} alias BdfrBrowser.{Chat, Comment, Message, Repo, Post, Subreddit}
plug :match plug :match
plug :dispatch plug :dispatch
@ -88,6 +88,20 @@ defmodule BdfrBrowser.HTTP.Plug do
|> send_resp(200, content) |> send_resp(200, content)
end end
get "/user/:name" do
tpl_args = [
name: name,
posts: name |> Post.by_author() |> Repo.all(),
comments: name |> Comment.by_author() |> Repo.all()
]
content = render_template("user", tpl_args)
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_resp(200, content)
end
get "/static/*path" do get "/static/*path" do
file_path = Application.app_dir(:bdfr_browser, Path.join("priv/static", path)) file_path = Application.app_dir(:bdfr_browser, Path.join("priv/static", path))

View file

@ -37,7 +37,7 @@ defmodule BdfrBrowser.Post do
def during_range(subreddit, start_date, end_date) do def during_range(subreddit, start_date, end_date) do
from(p in __MODULE__, from(p in __MODULE__,
left_join: c in assoc(p, :comments), 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)}, select: %{id: p.id, title: p.title, author: p.author, posted_at: p.posted_at, num_comments: count(c.id)},
where: where:
p.subreddit_id == ^subreddit.id and type(p.posted_at, :date) >= ^start_date and p.subreddit_id == ^subreddit.id and type(p.posted_at, :date) >= ^start_date and
@ -46,4 +46,22 @@ defmodule BdfrBrowser.Post do
group_by: p.id group_by: p.id
) )
end end
def by_author(author) do
from(p in __MODULE__,
join: c in assoc(p, :comments),
join: s in assoc(p, :subreddit),
select: %{
id: p.id,
title: p.title,
posted_at: p.posted_at,
num_comments: count(c.id),
subreddit: s.name,
date: fragment("to_char(?, 'YYYY-MM')", p.posted_at)
},
where: p.author == ^author,
order_by: [desc: p.posted_at],
group_by: [p.id, s.name]
)
end
end end

View file

@ -9,7 +9,7 @@
<% end %> <% end %>
<footer class="blockquote-footer"> <footer class="blockquote-footer">
<%= comment.author %>, <a href="/user/<%= comment.author %>"><%= comment.author %></a>
<small><%= DateTime.to_iso8601(comment.posted_at) %></small> <small><%= DateTime.to_iso8601(comment.posted_at) %></small>
</footer> </footer>
</blockquote> </blockquote>

View file

@ -1,7 +1,7 @@
<h2><a href="<%= post.url %>"><%= post.title %></a></h2> <h2><%= post.title %></h2>
<p> <p>
<small><a href="https://reddit.com/user/<%= post.author %>"><%= post.author %></a></small> <small><a href="/user/<%= post.author %>"><%= post.author %></a></small>
- -
<small><%= DateTime.to_iso8601(post.posted_at) %></small> <small><%= DateTime.to_iso8601(post.posted_at) %></small>
- -

View file

@ -0,0 +1,58 @@
<h2><%= name %></h2>
<ul class="nav nav-pills" id="userTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="posts-tab" data-bs-toggle="tab" data-bs-target="#posts" type="button" role="tab" aria-controls="posts" aria-selected="true">
Posts (<%= length(posts) %>)
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="comments-tab" data-bs-toggle="tab" data-bs-target="#comments" type="button" role="tab" aria-controls="comments" aria-selected="false">
Comments (<%= length(comments) %>)
</button>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="posts" role="tabpanel" aria-labelledby="posts-tab" tabindex="0">
<div class="row text-center" style="margin-top: 5px;">
<div class="d-grid gap-2 col-12 mx-auto">
<%= for post <- posts do %>
<div class="card">
<div class="card-body">
<h5 class="card-title"><a href="/r/<%= post.subreddit %>/<%= post.date %>/<%= post.id %>"><%= post.title %></a></h5>
<h6 class="card-subtitle mb-2 text-body-secondary">
<a href="/r/<%= post.subreddit %>/<%= post.date %>/"><%= post.subreddit %></a> -
<%= post.num_comments %> comment(s) -
<%= Calendar.strftime(post.posted_at, "%Y-%m-%d") %>
</h6>
</div>
</div>
<% end %>
</div>
</div>
</div>
<div class="tab-pane" id="comments" role="tabpanel" aria-labelledby="comments-tab" tabindex="1">
<div class="row" style="margin-top: 5px;">
<div class="d-grid gap-2 col-12 mx-auto">
<%= for comment <- comments do %>
<div class="card" style="margin-bottom: 4px;">
<div class="card-body">
<blockquote class="blockquote mb-0" style="font-size: 1rem;">
<%= BdfrBrowser.RenderUtils.comment(comment.body) %>
<footer class="blockquote-footer">
<a href="/r/<%= comment.subreddit %>/<%= comment.post_date %>/<%= comment.post_id %>"><%= comment.post_title %></a>,
<a href="/r/<%= comment.subreddit %>/<%= comment.post_date %>/"><%= comment.subreddit %></a>,
<small><%= DateTime.to_iso8601(comment.posted_at) %></small>
</footer>
</blockquote>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>