philomena/lib/philomena_web/views/api/json/image_view.ex

107 lines
3.5 KiB
Elixir
Raw Normal View History

2020-03-30 01:39:36 +02:00
defmodule PhilomenaWeb.Api.Json.ImageView do
use PhilomenaWeb, :view
2019-12-04 01:50:23 +01:00
alias PhilomenaWeb.ImageView
2020-03-30 01:39:36 +02:00
def render("index.json", %{images: images, interactions: interactions, total: total} = assigns) do
%{
images: render_many(images, PhilomenaWeb.Api.Json.ImageView, "image.json", assigns),
interactions: interactions,
total: total
}
end
def render("show.json", %{image: image, interactions: interactions} = assigns) do
%{
image: render_one(image, PhilomenaWeb.Api.Json.ImageView, "image.json", assigns),
interactions: interactions
}
2020-03-30 01:39:36 +02:00
end
def render("image.json", %{
image: %{hidden_from_users: true, duplicate_id: duplicate_id} = image
})
2020-01-11 05:20:19 +01:00
when not is_nil(duplicate_id) do
2019-12-31 06:10:50 +01:00
%{
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
2020-01-11 05:20:19 +01:00
2020-03-30 01:39:36 +02:00
def render("image.json", %{image: %{hidden_from_users: true} = image}) do
2019-12-31 06:10:50 +01:00
%{
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
2020-01-11 05:20:19 +01:00
2020-03-30 01:39:36 +02:00
def render("image.json", %{conn: conn, image: %{hidden_from_users: false} = image}) do
2020-06-12 18:56:11 +02:00
result = render_one(image, PhilomenaWeb.Api.Json.ImageView, "image.json", %{image: image})
Map.put(result, :spoilered, ImageView.filter_or_spoiler_hits?(conn, image))
end
def render("image.json", %{image: %{hidden_from_users: false} = image}) do
2019-12-04 01:50:23 +01:00
%{
id: image.id,
created_at: image.created_at,
updated_at: image.updated_at,
first_seen_at: image.first_seen_at,
width: image.image_width,
height: image.image_height,
mime_type: image.image_mime_type,
2020-07-06 20:00:02 +02:00
size: image.image_size,
duration: image.image_duration,
animated: image.image_is_animated,
2019-12-04 01:50:23 +01:00
format: image.image_format,
aspect_ratio: image.image_aspect_ratio,
name: image.image_name,
sha512_hash: image.image_sha512_hash,
orig_sha512_hash: image.image_orig_sha512_hash,
tags: Enum.map(image.tags, & &1.name),
tag_ids: Enum.map(image.tags, & &1.id),
uploader: if(!!image.user and !image.anonymous, do: image.user.name),
uploader_id: if(!!image.user and !image.anonymous, do: image.user.id),
wilson_score: Philomena.Images.SearchIndex.wilson_score(image),
intensities: intensities(image),
2019-12-04 01:50:23 +01:00
score: image.score,
upvotes: image.upvotes_count,
downvotes: image.downvotes_count,
faves: image.faves_count,
comment_count: image.comments_count,
tag_count: length(image.tags),
description: image.description,
source_url:
if(Enum.count(image.sources) > 0, do: Enum.at(image.sources, 0).source, else: ""),
source_urls: Enum.map(image.sources, & &1.source),
2019-12-04 01:50:23 +01:00
view_url: ImageView.pretty_url(image, false, false),
representations: ImageView.thumb_urls(image, false),
thumbnails_generated: image.thumbnails_generated,
2019-12-31 06:10:50 +01:00
processed: image.processed,
deletion_reason: nil,
duplicate_of: nil,
hidden_from_users: false
2019-12-04 01:50:23 +01:00
}
end
def render("error.json", %{changeset: changeset}) do
%{
errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
}
end
2020-01-11 05:20:19 +01:00
defp intensities(%{intensity: %{nw: nw, ne: ne, sw: sw, se: se}}),
do: %{nw: nw, ne: ne, sw: sw, se: se}
defp intensities(_), do: nil
end