2019-10-04 02:53:30 +02:00
|
|
|
defmodule PhilomenaWeb.UserAttributionView do
|
|
|
|
use PhilomenaWeb, :view
|
2019-12-25 19:32:24 +01:00
|
|
|
|
|
|
|
alias Philomena.Attribution
|
2019-11-11 17:56:34 +01:00
|
|
|
|
2019-11-25 03:16:22 +01:00
|
|
|
def anonymous?(object) do
|
|
|
|
Attribution.anonymous?(object)
|
|
|
|
end
|
|
|
|
|
2020-03-31 17:38:01 +02:00
|
|
|
def name(object) do
|
|
|
|
case is_nil(object.user) or anonymous?(object) do
|
|
|
|
true -> anonymous_name(object)
|
|
|
|
_false -> object.user.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_url(object) do
|
|
|
|
case is_nil(object.user) or anonymous?(object) do
|
|
|
|
true -> anonymous_avatar_url(anonymous_name(object))
|
|
|
|
_false -> user_avatar_url(object)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-10 17:57:01 +01:00
|
|
|
def anonymous_name(object, reveal_anon? \\ false) do
|
2019-11-11 17:56:34 +01:00
|
|
|
salt = anonymous_name_salt()
|
|
|
|
id = Attribution.object_identifier(object)
|
|
|
|
user_id = Attribution.best_user_identifier(object)
|
|
|
|
|
2020-05-10 12:20:21 +02:00
|
|
|
{:ok, <<key::size(16)>>} = :pbkdf2.pbkdf2(:sha256, id <> user_id, salt, 100, 2)
|
|
|
|
|
2019-11-27 02:45:57 +01:00
|
|
|
hash =
|
2020-05-10 12:20:21 +02:00
|
|
|
key
|
2019-11-27 02:45:57 +01:00
|
|
|
|> Integer.to_string(16)
|
2019-11-28 03:06:41 +01:00
|
|
|
|> String.pad_leading(4, "0")
|
2019-11-27 02:45:57 +01:00
|
|
|
|
2019-12-12 05:41:33 +01:00
|
|
|
case not is_nil(object.user) and reveal_anon? do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> "#{object.user.name} (##{hash}, hidden)"
|
2024-05-20 19:04:12 +02:00
|
|
|
false -> "#{gettext("Anonymous")} ##{hash}"
|
2019-12-10 17:57:01 +01:00
|
|
|
end
|
2019-11-11 17:56:34 +01:00
|
|
|
end
|
|
|
|
|
2024-05-20 19:04:12 +02:00
|
|
|
def anonymous_avatar(_name, class \\ "avatar--small") do
|
2019-11-28 18:12:10 +01:00
|
|
|
class = Enum.join(["image-constrained", class], " ")
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
content_tag :div, class: class do
|
2024-05-20 19:04:12 +02:00
|
|
|
raw("<img xlink:href=\"/images/no_avatar.svg\" src=\"/images/no_avatar.svg\">")
|
2019-11-28 18:12:10 +01:00
|
|
|
end
|
2019-11-11 18:25:51 +01:00
|
|
|
end
|
|
|
|
|
2024-05-01 00:10:05 +02:00
|
|
|
def user_avatar(object, class \\ "avatar--small")
|
2019-11-11 18:28:43 +01:00
|
|
|
|
2019-11-11 18:35:11 +01:00
|
|
|
def user_avatar(%{user: nil} = object, class),
|
2019-12-25 19:32:24 +01:00
|
|
|
do: anonymous_avatar(anonymous_name(object), class)
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-11 18:35:11 +01:00
|
|
|
def user_avatar(%{user: %{avatar: nil}} = object, class),
|
2019-12-25 19:32:24 +01:00
|
|
|
do: anonymous_avatar(object.user.name, class)
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2020-03-31 17:45:09 +02:00
|
|
|
def user_avatar(%{user: %{avatar: avatar}}, class) do
|
2019-11-28 18:19:27 +01:00
|
|
|
class = Enum.join(["image-constrained", class], " ")
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
content_tag :div, class: class do
|
2020-03-31 17:45:09 +02:00
|
|
|
img_tag(avatar_url_root() <> "/" <> avatar)
|
2019-11-28 18:19:27 +01:00
|
|
|
end
|
|
|
|
end
|
2019-11-11 18:25:51 +01:00
|
|
|
|
2020-03-31 17:38:01 +02:00
|
|
|
def user_avatar_url(%{user: %{avatar: nil}} = object) do
|
|
|
|
anonymous_avatar_url(object.user.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_avatar_url(%{user: %{avatar: avatar}}) do
|
|
|
|
avatar_url_root() <> "/" <> avatar
|
|
|
|
end
|
|
|
|
|
2024-05-20 19:04:12 +02:00
|
|
|
def anonymous_avatar_url(_), do: "/images/no_avatar.svg"
|
2020-03-31 17:38:01 +02:00
|
|
|
|
2024-05-20 19:04:12 +02:00
|
|
|
def user_icon(%{secondary_role: sr}) when sr in ["Site Developer", "Devops"],
|
|
|
|
do: "fa-screwdriver-wrench"
|
2020-03-31 17:38:01 +02:00
|
|
|
|
2024-05-07 19:33:56 +02:00
|
|
|
def user_icon(%{secondary_role: sr}) when sr in ["Public Relations"], do: "fa-bullhorn"
|
|
|
|
def user_icon(%{hide_default_role: true}), do: nil
|
|
|
|
def user_icon(%{role: role}) when role in ["admin", "moderator"], do: "fa-gavel"
|
|
|
|
def user_icon(%{role: "assistant"}), do: "fa-handshake-angle"
|
|
|
|
def user_icon(_), do: nil
|
|
|
|
|
2019-11-28 03:31:53 +01:00
|
|
|
def user_labels(%{user: user}) do
|
|
|
|
[]
|
|
|
|
|> personal_title(user)
|
|
|
|
|> secondary_role(user)
|
|
|
|
|> staff_role(user)
|
|
|
|
end
|
|
|
|
|
2019-12-08 03:30:47 +01:00
|
|
|
defp personal_title(labels, %{personal_title: t}) do
|
|
|
|
case blank?(t) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> labels
|
2019-12-08 03:30:47 +01:00
|
|
|
false -> [{"label--primary", t} | labels]
|
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-08 03:30:47 +01:00
|
|
|
defp personal_title(labels, _user), do: labels
|
2019-11-28 03:31:53 +01:00
|
|
|
|
2019-12-08 03:30:47 +01:00
|
|
|
defp secondary_role(labels, %{secondary_role: t}) do
|
|
|
|
case blank?(t) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> labels
|
2019-12-08 03:30:47 +01:00
|
|
|
false -> [{"label--warning", t} | labels]
|
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-08 03:30:47 +01:00
|
|
|
defp secondary_role(labels, _user), do: labels
|
2019-11-28 03:31:53 +01:00
|
|
|
|
2020-08-17 23:58:20 +02:00
|
|
|
defp staff_role(labels, %{hide_default_role: false, role: "admin", senior_staff: true}),
|
|
|
|
do: [{"label--danger", "Head Administrator"} | labels]
|
|
|
|
|
2019-11-28 03:31:53 +01:00
|
|
|
defp staff_role(labels, %{hide_default_role: false, role: "admin"}),
|
2021-01-20 22:47:06 +01:00
|
|
|
do: [{"label--danger", "Administrator"} | labels]
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2020-08-17 23:58:20 +02:00
|
|
|
defp staff_role(labels, %{hide_default_role: false, role: "moderator", senior_staff: true}),
|
|
|
|
do: [{"label--success", "Senior Moderator"} | labels]
|
|
|
|
|
2019-11-28 03:31:53 +01:00
|
|
|
defp staff_role(labels, %{hide_default_role: false, role: "moderator"}),
|
2021-01-20 22:47:06 +01:00
|
|
|
do: [{"label--success", "Moderator"} | labels]
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2020-08-17 23:58:20 +02:00
|
|
|
defp staff_role(labels, %{hide_default_role: false, role: "assistant", senior_staff: true}),
|
2024-04-30 09:25:23 +02:00
|
|
|
do: [{"label--special", "Senior Assistant"} | labels]
|
2020-08-17 23:58:20 +02:00
|
|
|
|
2019-11-28 18:12:10 +01:00
|
|
|
defp staff_role(labels, %{hide_default_role: false, role: "assistant"}),
|
2024-04-30 09:25:23 +02:00
|
|
|
do: [{"label--special", "Assistant"} | labels]
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-28 03:31:53 +01:00
|
|
|
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
|