diff --git a/lib/philomena_web/views/api/json/comment_view.ex b/lib/philomena_web/views/api/json/comment_view.ex index 15900c9d..5d80b42b 100644 --- a/lib/philomena_web/views/api/json/comment_view.ex +++ b/lib/philomena_web/views/api/json/comment_view.ex @@ -36,11 +36,8 @@ defmodule PhilomenaWeb.Api.Json.CommentView do id: comment.id, image_id: comment.image_id, user_id: if(not comment.anonymous, do: comment.user_id), - author: - if(comment.anonymous or is_nil(comment.user), - do: UserAttributionView.anonymous_name(comment), - else: comment.user.name - ), + author: UserAttributionView.name(comment), + avatar: UserAttributionView.avatar_url(comment), body: nil, created_at: comment.created_at, updated_at: comment.updated_at, @@ -54,11 +51,8 @@ defmodule PhilomenaWeb.Api.Json.CommentView do id: comment.id, image_id: comment.image_id, user_id: if(not comment.anonymous, do: comment.user_id), - author: - if(comment.anonymous or is_nil(comment.user), - do: UserAttributionView.anonymous_name(comment), - else: comment.user.name - ), + author: UserAttributionView.name(comment), + avatar: UserAttributionView.avatar_url(comment), body: comment.body, created_at: comment.created_at, updated_at: comment.updated_at, diff --git a/lib/philomena_web/views/api/json/forum/topic/post_view.ex b/lib/philomena_web/views/api/json/forum/topic/post_view.ex index 5ed2f7fe..ef69e3d6 100644 --- a/lib/philomena_web/views/api/json/forum/topic/post_view.ex +++ b/lib/philomena_web/views/api/json/forum/topic/post_view.ex @@ -30,11 +30,8 @@ defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostView do %{ id: post.id, user_id: if(not post.anonymous, do: post.user_id), - author: - if(post.anonymous or is_nil(post.user), - do: UserAttributionView.anonymous_name(post), - else: post.user.name - ), + author: UserAttributionView.name(post), + avatar: UserAttributionView.avatar_url(post), body: nil, created_at: post.created_at, updated_at: post.updated_at, @@ -47,11 +44,8 @@ defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostView do %{ id: post.id, user_id: if(not post.anonymous, do: post.user_id), - author: - if(post.anonymous or is_nil(post.user), - do: UserAttributionView.anonymous_name(post), - else: post.user.name - ), + author: UserAttributionView.name(post), + avatar: UserAttributionView.avatar_url(post), body: post.body, created_at: post.created_at, updated_at: post.updated_at, diff --git a/lib/philomena_web/views/user_attribution_view.ex b/lib/philomena_web/views/user_attribution_view.ex index e9f0bdb3..1b8d1373 100644 --- a/lib/philomena_web/views/user_attribution_view.ex +++ b/lib/philomena_web/views/user_attribution_view.ex @@ -3,11 +3,26 @@ defmodule PhilomenaWeb.UserAttributionView do use Bitwise alias Philomena.Attribution + alias PhilomenaWeb.AvatarGeneratorView def anonymous?(object) do Attribution.anonymous?(object) end + 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 + def anonymous_name(object, reveal_anon? \\ false) do salt = anonymous_name_salt() id = Attribution.object_identifier(object) @@ -28,7 +43,7 @@ defmodule PhilomenaWeb.UserAttributionView do class = Enum.join(["image-constrained", class], " ") content_tag :div, class: class do - PhilomenaWeb.AvatarGeneratorView.generated_avatar(name) + AvatarGeneratorView.generated_avatar(name) end end @@ -40,14 +55,31 @@ defmodule PhilomenaWeb.UserAttributionView do def user_avatar(%{user: %{avatar: nil}} = object, class), do: anonymous_avatar(object.user.name, class) - def user_avatar(%{user: %{avatar: avatar}}, class) do + def user_avatar(object, class) do class = Enum.join(["image-constrained", class], " ") content_tag :div, class: class do - img_tag(avatar_url_root() <> "/" <> avatar) + user_avatar_url(object) end end + 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 + + def anonymous_avatar_url(name) do + svg = + name + |> AvatarGeneratorView.generated_avatar() + |> Enum.map_join(&safe_to_string/1) + + "data:image/svg+xml;base64," <> Base.encode64(svg) + end + def user_labels(%{user: user}) do [] |> personal_title(user)