97 lines
2.5 KiB
Elixir
97 lines
2.5 KiB
Elixir
![]() |
defmodule Content.PostsView do
|
||
|
use Content, :view
|
||
|
use Phoenix.HTML
|
||
|
import Plug.Conn
|
||
|
alias Content.Comment
|
||
|
alias Content.Post
|
||
|
alias Phoenix.HTML
|
||
|
alias Phoenix.HTML.Tag
|
||
|
|
||
|
def paginated_posts_path(conn, category, page) do
|
||
|
case category do
|
||
|
nil ->
|
||
|
Routes.blog_page_path(conn, :index_posts, page)
|
||
|
_ ->
|
||
|
Routes.category_page_path(conn, :index, category, page)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def authenticated_for_post?(conn, post) do
|
||
|
post.post_password == nil || String.length(post.post_password) == 0 || get_session(conn, :post_password) == post.post_password
|
||
|
end
|
||
|
|
||
|
def gravatar_url_for_email(email) do
|
||
|
email
|
||
|
|> Kernel.||("noreply@example.com")
|
||
|
|> String.trim()
|
||
|
|> String.downcase()
|
||
|
|> (&(:crypto.hash(:md5, &1))).()
|
||
|
|> Base.encode16()
|
||
|
|> String.downcase()
|
||
|
|> (&("https://www.gravatar.com/avatar/#{&1}")).()
|
||
|
end
|
||
|
|
||
|
def comment_changeset_for_post(%Post{} = post) do
|
||
|
%Comment{
|
||
|
comment_post_ID: post.'ID'
|
||
|
}
|
||
|
|> Comment.changeset()
|
||
|
end
|
||
|
|
||
|
def comment_changeset_for_parent(%Comment{} = comment) do
|
||
|
%Comment{
|
||
|
comment_parent: comment.comment_ID,
|
||
|
comment_post_ID: comment.comment_post_ID
|
||
|
}
|
||
|
|> Comment.changeset()
|
||
|
end
|
||
|
|
||
|
def auto_paragraph_tags(string) do
|
||
|
string
|
||
|
|> Kernel.||("")
|
||
|
|> String.split(["\n\n", "\r\n\r\n"], trim: true)
|
||
|
|> Enum.map(fn text ->
|
||
|
[Tag.content_tag(:p, text |> HTML.raw(), []), ?\n]
|
||
|
end)
|
||
|
|> HTML.html_escape()
|
||
|
end
|
||
|
|
||
|
def post_class(post) do
|
||
|
sticky =
|
||
|
if post.sticky do
|
||
|
"sticky"
|
||
|
end
|
||
|
"post post-#{post.'ID'} #{sticky}"
|
||
|
end
|
||
|
|
||
|
def post_topmatter(conn, post) do
|
||
|
author =
|
||
|
post.author ||
|
||
|
%Auth.User{
|
||
|
email: "example@example.org",
|
||
|
display_name: "Anonymous",
|
||
|
homepage_url: "#"
|
||
|
}
|
||
|
assigns = %{post: post, author: author, conn: conn}
|
||
|
~E"""
|
||
|
<% _ = assigns # suppress unused assigns warning %>
|
||
|
<div class="Comment-topmatter">
|
||
|
|
||
|
<h4>
|
||
|
<%= link to: author.homepage_url, rel: "author", class: "p-author h-card" do %>
|
||
|
<%= author.display_name %>
|
||
|
<%= img_tag gravatar_url_for_email(author.email), alt: "Photo of #{author.display_name}", class: "Gravatar u-photo" %>
|
||
|
<% end %>
|
||
|
</h4>
|
||
|
<h5>
|
||
|
<%= link to: Routes.posts_path(conn, :show, post) do %>
|
||
|
<time class="dt-published" datetime="<%= post.post_date %>">
|
||
|
<%= post.post_date |> Timex.format!("%F", :strftime) %>
|
||
|
</time>
|
||
|
<% end %>
|
||
|
</h5>
|
||
|
</div>
|
||
|
"""
|
||
|
end
|
||
|
end
|