mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-19 14:17:59 +01:00
add avatars to post and comment json views
This commit is contained in:
parent
724b3a97b1
commit
c8b0d9ca3d
3 changed files with 43 additions and 23 deletions
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue