philomena/lib/philomena_web/views/user_attribution_view.ex

80 lines
2.3 KiB
Elixir
Raw Normal View History

2019-10-04 02:53:30 +02:00
defmodule PhilomenaWeb.UserAttributionView do
2019-11-11 17:56:34 +01:00
alias Philomena.Attribution
use Bitwise
2019-10-04 02:53:30 +02:00
use PhilomenaWeb, :view
2019-11-11 17:56:34 +01:00
def anonymous?(object) do
Attribution.anonymous?(object)
end
2019-11-11 17:56:34 +01:00
def anonymous_name(object) do
salt = anonymous_name_salt()
id = Attribution.object_identifier(object)
user_id = Attribution.best_user_identifier(object)
2019-11-27 02:45:57 +01:00
hash =
(:erlang.crc32(salt <> id <> user_id) &&& 0xffff)
|> Integer.to_string(16)
|> String.pad_leading(4, "0")
2019-11-27 02:45:57 +01:00
"Background Pony ##{hash}"
2019-11-11 17:56:34 +01:00
end
2019-11-11 18:25:51 +01:00
def anonymous_avatar(_object, class \\ "avatar--100px") do
2019-11-28 18:12:10 +01:00
class = Enum.join(["image-constrained", class], " ")
content_tag :div, [class: class] do
img_tag(Routes.static_path(PhilomenaWeb.Endpoint, "/images/no_avatar.svg"))
end
2019-11-11 18:25:51 +01:00
end
2019-11-11 18:35:11 +01:00
def user_avatar(object, class \\ "avatar--100px")
2019-11-11 18:28:43 +01:00
2019-11-11 18:35:11 +01:00
def user_avatar(%{user: nil} = object, class),
do: anonymous_avatar(object, class)
def user_avatar(%{user: %{avatar: nil}} = object, class),
do: anonymous_avatar(object, class)
def user_avatar(%{user: %{avatar: avatar}}, class) do
class = Enum.join(["image-constrained", class], " ")
content_tag :div, [class: class] do
img_tag(avatar_url_root() <> "/" <> avatar)
end
end
2019-11-11 18:25:51 +01:00
2019-11-28 03:31:53 +01:00
def user_labels(%{user: user}) do
[]
|> personal_title(user)
|> secondary_role(user)
|> staff_role(user)
end
defp personal_title(labels, %{personal_title: t}) when t not in [nil, ""],
do: [{"label--primary", t} | labels]
defp personal_title(labels, _user),
do: labels
defp secondary_role(labels, %{secondary_role: t}) when t not in [nil, ""],
do: [{"label--warning", t} | labels]
defp secondary_role(labels, _user),
do: labels
defp staff_role(labels, %{hide_default_role: false, role: "admin"}),
do: [{"label--danger", "Site Administrator"} | labels]
defp staff_role(labels, %{hide_default_role: false, role: "moderator"}),
do: [{"label--success", "Site Moderator"} | labels]
2019-11-28 18:12:10 +01:00
defp staff_role(labels, %{hide_default_role: false, role: "assistant"}),
2019-11-28 03:31:53 +01:00
do: [{"label--purple", "Site Assistant"} | labels]
defp staff_role(labels, _user),
do: labels
2019-11-11 18:25:51 +01:00
defp avatar_url_root do
Application.get_env(:philomena, :avatar_url_root)
end
2019-11-11 17:56:34 +01:00
defp anonymous_name_salt do
Application.get_env(:philomena, :anonymous_name_salt)
|> to_string()
end
2019-10-04 02:53:30 +02:00
end