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_name(object) do
|
|
|
|
salt = anonymous_name_salt()
|
|
|
|
id = Attribution.object_identifier(object)
|
|
|
|
user_id = Attribution.best_user_identifier(object)
|
|
|
|
|
|
|
|
(:erlang.crc32(salt <> id <> user_id) &&& 0xffff)
|
|
|
|
|> Integer.to_string(16)
|
|
|
|
end
|
|
|
|
|
2019-11-11 18:25:51 +01:00
|
|
|
def anonymous_avatar(_object, class \\ "avatar--100px") do
|
|
|
|
img_tag(Routes.static_path(PhilomenaWeb.Endpoint, "/images/no_avatar.svg"), class: class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_avatar(object, class \\ "avatar--100px") do
|
|
|
|
if object.user.avatar do
|
|
|
|
img_tag(avatar_url_root() <> object.user.avatar, class: class)
|
|
|
|
else
|
|
|
|
anonymous_avatar(object, class)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|