This repository has been archived on 2018-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
forum/lib/forum/util.ex

74 lines
1.4 KiB
Elixir
Raw Normal View History

defmodule Forum.Util do
2018-02-23 20:04:04 +00:00
@moduledoc """
General utilities for the Forum
"""
2018-02-23 20:04:04 +00:00
import Plug.Conn
2018-02-23 20:04:04 +00:00
@doc """
Converts Markdown to HTML
"""
def md(text) do
text
|> Earmark.as_html!()
2018-02-25 10:23:12 +00:00
|> String.replace("<img", "<img class=\"img-responsive\"")
|> Phoenix.HTML.raw()
end
2018-02-23 20:04:04 +00:00
@doc """
Gets the logged-in username for a conn
"""
2018-02-23 08:57:00 +00:00
def current_username(conn) do
get_session(conn, :current_user)
end
2018-02-23 20:04:04 +00:00
@doc """
Can current user sudo?
"""
2018-03-01 19:18:47 +00:00
def sudoer?(conn) do
2018-02-23 08:57:00 +00:00
get_session(conn, :sudoer)
2018-02-22 19:58:27 +00:00
end
2018-03-01 19:18:47 +00:00
@doc """
"""
def logged_in?(conn) do
get_session(conn, :loggedin)
end
2018-02-23 20:04:04 +00:00
@doc """
2018-02-24 07:07:28 +00:00
Returns true if the current user matches the user -- or the current user can sudo
2018-02-23 20:04:04 +00:00
"""
2018-02-22 19:58:27 +00:00
def can_edit?(conn, user) do
2018-03-01 19:18:47 +00:00
user.name == current_username(conn) or sudoer?(conn)
end
2018-02-23 20:04:04 +00:00
@doc """
2018-02-24 07:07:28 +00:00
Make a time pretty for displaying it
2018-02-23 20:04:04 +00:00
"""
def pp_time(time) do
time
|> Timex.local()
|> Timex.Format.DateTime.Formatter.format!("%a %b %e, %Y ~ %I:%M %P", :strftime)
|> String.downcase()
end
2018-02-22 19:58:27 +00:00
2018-02-23 20:04:04 +00:00
@doc """
2018-02-24 07:07:28 +00:00
Set custom options for kerosene
* lowercase text
* hide for one page
2018-02-23 20:04:04 +00:00
"""
def paginate(conn, kerosene) do
if kerosene.total_pages > 1 do
2018-02-23 20:04:04 +00:00
Kerosene.HTML.paginate(
conn,
kerosene,
next_label: ">>",
previous_label: "<<",
first_label: "first",
last_label: "last"
)
end
end
end