diff --git a/lib/bdfr_browser/comment.ex b/lib/bdfr_browser/comment.ex index d1be3e3..74e2122 100644 --- a/lib/bdfr_browser/comment.ex +++ b/lib/bdfr_browser/comment.ex @@ -3,7 +3,7 @@ defmodule BdfrBrowser.Comment do import Ecto.Query, only: [from: 2] - alias BdfrBrowser.Post + alias BdfrBrowser.{Post, Repo} @primary_key {:id, :string, autogenerate: false} @foreign_key_type :string @@ -19,6 +19,10 @@ defmodule BdfrBrowser.Comment do has_many :children, __MODULE__, foreign_key: :parent_id end + def fetch_children!(comment) do + Repo.preload(comment, children: from(c in __MODULE__, order_by: [asc: c.posted_at])).children + end + def by_author(author) do from(c in __MODULE__, join: p in assoc(c, :post), diff --git a/lib/bdfr_browser/http/plug.ex b/lib/bdfr_browser/http/plug.ex index 3125aa1..d9d098e 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -47,7 +47,7 @@ defmodule BdfrBrowser.HTTP.Plug do end get "/r/:subreddit/:date/:id" do - post_record = Post |> Repo.get(id) |> Repo.preload(comments: :children) + post_record = id |> Post.get_full() |> Repo.one() tpl_args = [ subreddit: subreddit, diff --git a/lib/bdfr_browser/post.ex b/lib/bdfr_browser/post.ex index 5cd1a14..bb8d712 100644 --- a/lib/bdfr_browser/post.ex +++ b/lib/bdfr_browser/post.ex @@ -47,6 +47,20 @@ defmodule BdfrBrowser.Post do ) end + def get_full(id) do + from(p in __MODULE__, + where: p.id == ^id, + preload: [ + comments: + ^from(c in Comment, + where: is_nil(c.parent_id), + order_by: [asc: c.posted_at], + preload: [children: ^from(c1 in Comment, order_by: [asc: c1.posted_at])] + ) + ] + ) + end + def by_author(author) do from(p in __MODULE__, join: c in assoc(p, :comments), diff --git a/lib/bdfr_browser/render_utils.ex b/lib/bdfr_browser/render_utils.ex index e09de8e..8f125ee 100644 --- a/lib/bdfr_browser/render_utils.ex +++ b/lib/bdfr_browser/render_utils.ex @@ -1,4 +1,8 @@ defmodule BdfrBrowser.RenderUtils do + def selftext(txt) do + Earmark.as_html!(txt) + end + def comment(cmt) do Earmark.as_html!(cmt) end diff --git a/priv/templates/http/_comment.eex b/priv/templates/http/_comment.eex index 565b518..6df9d92 100644 --- a/priv/templates/http/_comment.eex +++ b/priv/templates/http/_comment.eex @@ -17,6 +17,6 @@ -<%= for reply <- BdfrBrowser.Repo.preload(comment, :children).children do %> +<%= for reply <- BdfrBrowser.Comment.fetch_children!(comment) do %> <%= EEx.eval_file(comment_template, comment: reply, level: level + 1, comment_template: comment_template) %> <% end %> diff --git a/priv/templates/http/post.eex b/priv/templates/http/post.eex index d582664..f2a939b 100644 --- a/priv/templates/http/post.eex +++ b/priv/templates/http/post.eex @@ -8,6 +8,15 @@ Open reddit

+<%= unless is_nil(post.selftext) do %> +
+
+ <%= BdfrBrowser.RenderUtils.selftext(post.selftext) %> +
+
+
+<% end %> + <%= unless Enum.empty?(media.images) do %>