diff --git a/lib/philomena_web/award_json.ex b/lib/philomena_web/award_json.ex new file mode 100755 index 00000000..375de2a0 --- /dev/null +++ b/lib/philomena_web/award_json.ex @@ -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 diff --git a/lib/philomena_web/controllers/api/json/profile_controller.ex b/lib/philomena_web/controllers/api/json/profile_controller.ex new file mode 100755 index 00000000..1a201589 --- /dev/null +++ b/lib/philomena_web/controllers/api/json/profile_controller.ex @@ -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 diff --git a/lib/philomena_web/links_json.ex b/lib/philomena_web/links_json.ex new file mode 100755 index 00000000..eb9e31ba --- /dev/null +++ b/lib/philomena_web/links_json.ex @@ -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 diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index baa6d62f..73727e36 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -118,6 +118,7 @@ defmodule PhilomenaWeb.Router do resources "/tags", TagController, only: [:show] resources "/comments", CommentController, only: [:show] resources "/posts", PostController, only: [:show] + resources "/profiles", ProfileController, only: [:show] resources "/forums", ForumController, only: [:show, :index] do resources "/topics", Forum.TopicController, only: [:show, :index] do diff --git a/lib/philomena_web/user_json.ex b/lib/philomena_web/user_json.ex new file mode 100755 index 00000000..ad8a3d51 --- /dev/null +++ b/lib/philomena_web/user_json.ex @@ -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