diff --git a/lib/philomena_web/controllers/api/json/image_controller.ex b/lib/philomena_web/controllers/api/json/image_controller.ex index f07d773e..ac056cf0 100644 --- a/lib/philomena_web/controllers/api/json/image_controller.ex +++ b/lib/philomena_web/controllers/api/json/image_controller.ex @@ -3,10 +3,24 @@ defmodule PhilomenaWeb.Api.Json.ImageController do alias PhilomenaWeb.ImageJson alias Philomena.Images.Image + alias Philomena.Repo + import Ecto.Query - plug :load_and_authorize_resource, model: Image, only: [:show], preload: [:tags, :user, :intensity] + def show(conn, %{"id" => id}) do + image = + Image + |> where(id: ^id) + |> preload([:tags, :user, :intensity]) + |> Repo.one() - def show(conn, _params) do - json(conn, %{image: ImageJson.as_json(conn, conn.assigns.image)}) + case image do + nil -> + conn + |> put_status(:not_found) + |> text("") + + _ -> + json(conn, %{image: ImageJson.as_json(conn, image)}) + end end end diff --git a/lib/philomena_web/image_json.ex b/lib/philomena_web/image_json.ex index 82971fee..eb8d6d44 100644 --- a/lib/philomena_web/image_json.ex +++ b/lib/philomena_web/image_json.ex @@ -1,7 +1,29 @@ defmodule PhilomenaWeb.ImageJson do alias PhilomenaWeb.ImageView - def as_json(conn, image) do + def as_json(_conn, %{hidden_from_users: true, duplicate_id: duplicate_id} = image) when not is_nil(duplicate_id) do + %{ + id: image.id, + created_at: image.created_at, + updated_at: image.updated_at, + first_seen_at: image.first_seen_at, + duplicate_of: image.duplicate_id, + deletion_reason: nil, + hidden_from_users: true + } + end + def as_json(_conn, %{hidden_from_users: true} = image) do + %{ + id: image.id, + created_at: image.created_at, + updated_at: image.updated_at, + first_seen_at: image.first_seen_at, + deletion_reason: image.deletion_reason, + duplicate_of: nil, + hidden_from_users: true + } + end + def as_json(conn, %{hidden_from_users: false} = image) do %{ id: image.id, created_at: image.created_at, @@ -33,7 +55,10 @@ defmodule PhilomenaWeb.ImageJson do representations: ImageView.thumb_urls(image, false), spoilered: ImageView.filter_or_spoiler_hits?(conn, image), thumbnails_generated: image.thumbnails_generated, - processed: image.processed + processed: image.processed, + deletion_reason: nil, + duplicate_of: nil, + hidden_from_users: false } end