mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-01 03:46:44 +01:00
Fixed user.avatar failure. Ran mix format.
This commit is contained in:
parent
0ae05b97b2
commit
7a164944b4
4 changed files with 99 additions and 98 deletions
|
@ -1,16 +1,15 @@
|
||||||
defmodule PhilomenaWeb.AwardsJson do
|
defmodule PhilomenaWeb.AwardsJson do
|
||||||
|
def as_json(_conn, award) do
|
||||||
def as_json(_conn, award) do
|
%{
|
||||||
%{
|
image_url: badge_url_root() <> "/" <> award.badge.image,
|
||||||
image_url: badge_url_root() <> "/" <> award.badge.image,
|
title: award.badge.title,
|
||||||
title: award.badge.title,
|
id: award.badge_id,
|
||||||
id: award.badge_id,
|
label: award.label,
|
||||||
label: award.label,
|
awarded_on: award.awarded_on
|
||||||
awarded_on: award.awarded_on
|
}
|
||||||
}
|
end
|
||||||
end
|
|
||||||
|
defp badge_url_root do
|
||||||
defp badge_url_root do
|
Application.get_env(:philomena, :badge_url_root)
|
||||||
Application.get_env(:philomena, :badge_url_root)
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
|
@ -1,26 +1,26 @@
|
||||||
defmodule PhilomenaWeb.Api.Json.ProfileController do
|
defmodule PhilomenaWeb.Api.Json.ProfileController do
|
||||||
use PhilomenaWeb, :controller
|
use PhilomenaWeb, :controller
|
||||||
|
|
||||||
alias PhilomenaWeb.UserJson
|
alias PhilomenaWeb.UserJson
|
||||||
alias Philomena.Users.User
|
alias Philomena.Users.User
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
def show(conn, %{"id" => id}) do
|
def show(conn, %{"id" => id}) do
|
||||||
profile =
|
profile =
|
||||||
User
|
User
|
||||||
|> where(id: ^id)
|
|> where(id: ^id)
|
||||||
|> preload([public_links: :tag, awards: :badge])
|
|> preload(public_links: :tag, awards: :badge)
|
||||||
|> Repo.one()
|
|> Repo.one()
|
||||||
|
|
||||||
cond do
|
cond do
|
||||||
is_nil(profile) or profile.deleted_at ->
|
is_nil(profile) or profile.deleted_at ->
|
||||||
conn
|
conn
|
||||||
|> put_status(:not_found)
|
|> put_status(:not_found)
|
||||||
|> text("")
|
|> text("")
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
json(conn, %{user: UserJson.as_json(conn, profile)})
|
json(conn, %{user: UserJson.as_json(conn, profile)})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,21 +1,20 @@
|
||||||
defmodule PhilomenaWeb.LinksJson do
|
defmodule PhilomenaWeb.LinksJson do
|
||||||
|
def as_json(_conn, %{public: false}), do: nil
|
||||||
def as_json(_conn, %{public: false}), do: nil
|
|
||||||
|
def as_json(_conn, link) do
|
||||||
def as_json(_conn, link) do
|
%{
|
||||||
%{
|
user_id: link.user_id,
|
||||||
user_id: link.user_id,
|
created_at: link.created_at,
|
||||||
created_at: link.created_at,
|
state: link.aasm_state,
|
||||||
state: link.aasm_state,
|
tag_id: tag_id(link.tag)
|
||||||
tag_id: tag_id(link.tag)
|
}
|
||||||
}
|
end
|
||||||
end
|
|
||||||
|
defp tag_id(nil) do
|
||||||
defp tag_id(nil) do
|
nil
|
||||||
nil
|
end
|
||||||
end
|
|
||||||
|
defp tag_id(tag) do
|
||||||
defp tag_id(tag) do
|
tag.id
|
||||||
tag.id
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
|
@ -1,35 +1,38 @@
|
||||||
defmodule PhilomenaWeb.UserJson do
|
defmodule PhilomenaWeb.UserJson do
|
||||||
|
alias PhilomenaWeb.LinksJson
|
||||||
alias PhilomenaWeb.LinksJson
|
alias PhilomenaWeb.AwardsJson
|
||||||
alias PhilomenaWeb.AwardsJson
|
|
||||||
|
def as_json(conn, user) do
|
||||||
def as_json(conn, user) do
|
%{
|
||||||
%{
|
id: user.id,
|
||||||
id: user.id,
|
name: user.name,
|
||||||
name: user.name,
|
slug: user.slug,
|
||||||
slug: user.slug,
|
role: role(user),
|
||||||
role: role(user),
|
description: user.description,
|
||||||
description: user.description,
|
avatar_url: avatar_url(user),
|
||||||
avatar_url: avatar_url_root() <> "/" <> user.avatar,
|
created_at: user.created_at,
|
||||||
created_at: user.created_at,
|
comments_count: user.comments_posted_count,
|
||||||
comments_count: user.comments_posted_count,
|
uploads_count: user.uploads_count,
|
||||||
uploads_count: user.uploads_count,
|
posts_count: user.forum_posts_count,
|
||||||
posts_count: user.forum_posts_count,
|
topics_count: user.topic_count,
|
||||||
topics_count: user.topic_count,
|
links: Enum.map(user.public_links, &LinksJson.as_json(conn, &1)),
|
||||||
links: Enum.map(user.public_links, &LinksJson.as_json(conn, &1)),
|
awards: Enum.map(user.awards, &AwardsJson.as_json(conn, &1))
|
||||||
awards: Enum.map(user.awards, &AwardsJson.as_json(conn, &1))
|
}
|
||||||
}
|
end
|
||||||
end
|
|
||||||
|
defp role(%{hide_default_role: true}) do
|
||||||
defp role(%{hide_default_role: true}) do
|
"user"
|
||||||
"user"
|
end
|
||||||
end
|
|
||||||
|
defp role(user) do
|
||||||
defp role(user) do
|
user.role
|
||||||
user.role
|
end
|
||||||
end
|
|
||||||
|
defp avatar_url(%{avatar: nil}) do
|
||||||
defp avatar_url_root do
|
nil
|
||||||
Application.get_env(:philomena, :avatar_url_root)
|
end
|
||||||
end
|
|
||||||
end
|
defp avatar_url(user) do
|
||||||
|
Application.get_env(:philomena, :avatar_url_root) <> "/" <> user.avatar
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue