add deletion/duplicate handling to api

This commit is contained in:
byte[] 2019-12-31 00:10:50 -05:00
parent 3d629bf157
commit f5bd8e7614
2 changed files with 44 additions and 5 deletions

View file

@ -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

View file

@ -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