mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-01 03:46:44 +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,
|
id: comment.id,
|
||||||
image_id: comment.image_id,
|
image_id: comment.image_id,
|
||||||
user_id: if(not comment.anonymous, do: comment.user_id),
|
user_id: if(not comment.anonymous, do: comment.user_id),
|
||||||
author:
|
author: UserAttributionView.name(comment),
|
||||||
if(comment.anonymous or is_nil(comment.user),
|
avatar: UserAttributionView.avatar_url(comment),
|
||||||
do: UserAttributionView.anonymous_name(comment),
|
|
||||||
else: comment.user.name
|
|
||||||
),
|
|
||||||
body: nil,
|
body: nil,
|
||||||
created_at: comment.created_at,
|
created_at: comment.created_at,
|
||||||
updated_at: comment.updated_at,
|
updated_at: comment.updated_at,
|
||||||
|
@ -54,11 +51,8 @@ defmodule PhilomenaWeb.Api.Json.CommentView do
|
||||||
id: comment.id,
|
id: comment.id,
|
||||||
image_id: comment.image_id,
|
image_id: comment.image_id,
|
||||||
user_id: if(not comment.anonymous, do: comment.user_id),
|
user_id: if(not comment.anonymous, do: comment.user_id),
|
||||||
author:
|
author: UserAttributionView.name(comment),
|
||||||
if(comment.anonymous or is_nil(comment.user),
|
avatar: UserAttributionView.avatar_url(comment),
|
||||||
do: UserAttributionView.anonymous_name(comment),
|
|
||||||
else: comment.user.name
|
|
||||||
),
|
|
||||||
body: comment.body,
|
body: comment.body,
|
||||||
created_at: comment.created_at,
|
created_at: comment.created_at,
|
||||||
updated_at: comment.updated_at,
|
updated_at: comment.updated_at,
|
||||||
|
|
|
@ -30,11 +30,8 @@ defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostView do
|
||||||
%{
|
%{
|
||||||
id: post.id,
|
id: post.id,
|
||||||
user_id: if(not post.anonymous, do: post.user_id),
|
user_id: if(not post.anonymous, do: post.user_id),
|
||||||
author:
|
author: UserAttributionView.name(post),
|
||||||
if(post.anonymous or is_nil(post.user),
|
avatar: UserAttributionView.avatar_url(post),
|
||||||
do: UserAttributionView.anonymous_name(post),
|
|
||||||
else: post.user.name
|
|
||||||
),
|
|
||||||
body: nil,
|
body: nil,
|
||||||
created_at: post.created_at,
|
created_at: post.created_at,
|
||||||
updated_at: post.updated_at,
|
updated_at: post.updated_at,
|
||||||
|
@ -47,11 +44,8 @@ defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostView do
|
||||||
%{
|
%{
|
||||||
id: post.id,
|
id: post.id,
|
||||||
user_id: if(not post.anonymous, do: post.user_id),
|
user_id: if(not post.anonymous, do: post.user_id),
|
||||||
author:
|
author: UserAttributionView.name(post),
|
||||||
if(post.anonymous or is_nil(post.user),
|
avatar: UserAttributionView.avatar_url(post),
|
||||||
do: UserAttributionView.anonymous_name(post),
|
|
||||||
else: post.user.name
|
|
||||||
),
|
|
||||||
body: post.body,
|
body: post.body,
|
||||||
created_at: post.created_at,
|
created_at: post.created_at,
|
||||||
updated_at: post.updated_at,
|
updated_at: post.updated_at,
|
||||||
|
|
|
@ -3,11 +3,26 @@ defmodule PhilomenaWeb.UserAttributionView do
|
||||||
use Bitwise
|
use Bitwise
|
||||||
|
|
||||||
alias Philomena.Attribution
|
alias Philomena.Attribution
|
||||||
|
alias PhilomenaWeb.AvatarGeneratorView
|
||||||
|
|
||||||
def anonymous?(object) do
|
def anonymous?(object) do
|
||||||
Attribution.anonymous?(object)
|
Attribution.anonymous?(object)
|
||||||
end
|
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
|
def anonymous_name(object, reveal_anon? \\ false) do
|
||||||
salt = anonymous_name_salt()
|
salt = anonymous_name_salt()
|
||||||
id = Attribution.object_identifier(object)
|
id = Attribution.object_identifier(object)
|
||||||
|
@ -28,7 +43,7 @@ defmodule PhilomenaWeb.UserAttributionView do
|
||||||
class = Enum.join(["image-constrained", class], " ")
|
class = Enum.join(["image-constrained", class], " ")
|
||||||
|
|
||||||
content_tag :div, class: class do
|
content_tag :div, class: class do
|
||||||
PhilomenaWeb.AvatarGeneratorView.generated_avatar(name)
|
AvatarGeneratorView.generated_avatar(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,14 +55,31 @@ defmodule PhilomenaWeb.UserAttributionView do
|
||||||
def user_avatar(%{user: %{avatar: nil}} = object, class),
|
def user_avatar(%{user: %{avatar: nil}} = object, class),
|
||||||
do: anonymous_avatar(object.user.name, 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], " ")
|
class = Enum.join(["image-constrained", class], " ")
|
||||||
|
|
||||||
content_tag :div, class: class do
|
content_tag :div, class: class do
|
||||||
img_tag(avatar_url_root() <> "/" <> avatar)
|
user_avatar_url(object)
|
||||||
end
|
end
|
||||||
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
|
def user_labels(%{user: user}) do
|
||||||
[]
|
[]
|
||||||
|> personal_title(user)
|
|> personal_title(user)
|
||||||
|
|
Loading…
Reference in a new issue