2019-12-04 01:50:23 +01:00
|
|
|
defmodule PhilomenaWeb.ImageJson do
|
|
|
|
alias PhilomenaWeb.ImageView
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
def as_json(_conn, %{hidden_from_users: true, duplicate_id: duplicate_id} = image)
|
|
|
|
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
|
|
|
|
2019-12-31 06:10:50 +01:00
|
|
|
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
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-31 06:10:50 +01:00
|
|
|
def as_json(conn, %{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,
|
|
|
|
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),
|
2019-12-24 22:14:42 +01:00
|
|
|
wilson_score: Philomena.Images.ElasticsearchIndex.wilson_score(image),
|
2019-12-30 15:18:38 +01:00
|
|
|
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: image.source_url,
|
|
|
|
view_url: ImageView.pretty_url(image, false, false),
|
|
|
|
representations: ImageView.thumb_urls(image, false),
|
|
|
|
spoilered: ImageView.filter_or_spoiler_hits?(conn, image),
|
|
|
|
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
|
2019-12-30 15:18:38 +01:00
|
|
|
|
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}
|
|
|
|
|
2019-12-30 15:18:38 +01:00
|
|
|
defp intensities(_), do: nil
|
2019-12-24 22:14:42 +01:00
|
|
|
end
|