Merge pull request #42 from derpibooru/Damaged-API-user-profiles

This will add the  endpoint to the API.
This commit is contained in:
Luna D 2020-03-03 12:29:23 +01:00 committed by GitHub
commit bb4cbdb089
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 0 deletions

15
lib/philomena_web/award_json.ex Executable file
View file

@ -0,0 +1,15 @@
defmodule PhilomenaWeb.AwardsJson do
def as_json(_conn, award) do
%{
image_url: badge_url_root() <> "/" <> award.badge.image,
title: award.badge.title,
id: award.badge_id,
label: award.label,
awarded_on: award.awarded_on
}
end
defp badge_url_root do
Application.get_env(:philomena, :badge_url_root)
end
end

View file

@ -0,0 +1,26 @@
defmodule PhilomenaWeb.Api.Json.ProfileController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.UserJson
alias Philomena.Users.User
alias Philomena.Repo
import Ecto.Query
def show(conn, %{"id" => id}) do
profile =
User
|> where(id: ^id)
|> preload(public_links: :tag, awards: :badge)
|> Repo.one()
cond do
is_nil(profile) or profile.deleted_at ->
conn
|> put_status(:not_found)
|> text("")
true ->
json(conn, %{user: UserJson.as_json(conn, profile)})
end
end
end

20
lib/philomena_web/links_json.ex Executable file
View file

@ -0,0 +1,20 @@
defmodule PhilomenaWeb.LinksJson do
def as_json(_conn, %{public: false}), do: nil
def as_json(_conn, link) do
%{
user_id: link.user_id,
created_at: link.created_at,
state: link.aasm_state,
tag_id: tag_id(link.tag)
}
end
defp tag_id(nil) do
nil
end
defp tag_id(tag) do
tag.id
end
end

View file

@ -118,6 +118,7 @@ defmodule PhilomenaWeb.Router do
resources "/tags", TagController, only: [:show] resources "/tags", TagController, only: [:show]
resources "/comments", CommentController, only: [:show] resources "/comments", CommentController, only: [:show]
resources "/posts", PostController, only: [:show] resources "/posts", PostController, only: [:show]
resources "/profiles", ProfileController, only: [:show]
resources "/forums", ForumController, only: [:show, :index] do resources "/forums", ForumController, only: [:show, :index] do
resources "/topics", Forum.TopicController, only: [:show, :index] do resources "/topics", Forum.TopicController, only: [:show, :index] do

38
lib/philomena_web/user_json.ex Executable file
View file

@ -0,0 +1,38 @@
defmodule PhilomenaWeb.UserJson do
alias PhilomenaWeb.LinksJson
alias PhilomenaWeb.AwardsJson
def as_json(conn, user) do
%{
id: user.id,
name: user.name,
slug: user.slug,
role: role(user),
description: user.description,
avatar_url: avatar_url(user),
created_at: user.created_at,
comments_count: user.comments_posted_count,
uploads_count: user.uploads_count,
posts_count: user.forum_posts_count,
topics_count: user.topic_count,
links: Enum.map(user.public_links, &LinksJson.as_json(conn, &1)),
awards: Enum.map(user.awards, &AwardsJson.as_json(conn, &1))
}
end
defp role(%{hide_default_role: true}) do
"user"
end
defp role(user) do
user.role
end
defp avatar_url(%{avatar: nil}) do
nil
end
defp avatar_url(user) do
Application.get_env(:philomena, :avatar_url_root) <> "/" <> user.avatar
end
end