philomena/lib/philomena_web/views/duplicate_report_view.ex

142 lines
5.2 KiB
Elixir
Raw Normal View History

2019-12-01 05:51:44 +01:00
defmodule PhilomenaWeb.DuplicateReportView do
use PhilomenaWeb, :view
alias PhilomenaWeb.ImageView
@formats_order ~W(video/webm image/svg+xml image/png image/gif image/jpeg other)
def comparison_url(conn, image),
do: ImageView.thumb_url(image, can?(conn, :show, image), :full)
def largest_dimensions(images) do
images
|> Enum.map(&{&1.image_width, &1.image_height})
|> Enum.max_by(fn {w, h} -> w * h end)
end
def background_class(%{state: "rejected"}), do: "background-danger"
def background_class(%{state: "accepted"}), do: "background-success"
def background_class(%{state: "claimed"}), do: "background-warning"
def background_class(_duplicate_report), do: nil
def file_types(%{image: image, duplicate_of_image: duplicate_of_image}) do
2019-12-09 00:15:17 +01:00
source_type = String.upcase(to_string(image.image_format))
target_type = String.upcase(to_string(duplicate_of_image.image_format))
2019-12-01 05:51:44 +01:00
"(#{source_type}, #{target_type})"
end
def forward_merge?(%{image_id: image_id, duplicate_of_image_id: duplicate_of_image_id}),
do: duplicate_of_image_id > image_id
def higher_res?(%{image: image, duplicate_of_image: duplicate_of_image}),
2020-01-11 05:20:19 +01:00
do:
duplicate_of_image.image_width > image.image_width or
duplicate_of_image.image_height > image.image_height
2019-12-01 05:51:44 +01:00
def same_res?(%{image: image, duplicate_of_image: duplicate_of_image}),
2020-01-11 05:20:19 +01:00
do:
duplicate_of_image.image_width == image.image_width and
duplicate_of_image.image_height == image.image_height
2019-12-01 05:51:44 +01:00
def same_format?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: duplicate_of_image.image_mime_type == image.image_mime_type
def better_format?(%{image: image, duplicate_of_image: duplicate_of_image}) do
2020-01-11 05:20:19 +01:00
source_index =
Enum.find_index(@formats_order, &(image.image_mime_type == &1)) ||
length(@formats_order) - 1
target_index =
Enum.find_index(@formats_order, &(duplicate_of_image.image_mime_type == &1)) ||
length(@formats_order) - 1
2019-12-01 05:51:44 +01:00
target_index < source_index
end
def same_aspect_ratio?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: abs(duplicate_of_image.image_aspect_ratio - image.image_aspect_ratio) <= 0.009
def neither_have_source?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: blank?(duplicate_of_image.source_url) and blank?(image.source_url)
def same_source?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: to_string(duplicate_of_image.source_url) == to_string(image.source_url)
2019-12-08 04:25:47 +01:00
def similar_source?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: uri_host(image.source_url) == uri_host(duplicate_of_image.source_url)
2019-12-01 05:51:44 +01:00
def source_on_target?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: present?(duplicate_of_image.source_url) and blank?(image.source_url)
def source_on_source?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: blank?(duplicate_of_image.source_url) && present?(image.source_url)
def same_artist_tags?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: MapSet.equal?(artist_tags(image), artist_tags(duplicate_of_image))
def more_artist_tags_on_target?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: proper_subset?(artist_tags(image), artist_tags(duplicate_of_image))
def more_artist_tags_on_source?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: proper_subset?(artist_tags(duplicate_of_image), artist_tags(image))
def same_rating_tags?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: MapSet.equal?(rating_tags(image), rating_tags(duplicate_of_image))
def target_is_edit?(%{duplicate_of_image: duplicate_of_image}),
do: edit?(duplicate_of_image)
def source_is_edit?(%{image: image}),
do: edit?(image)
def both_are_edits?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: edit?(image) and edit?(duplicate_of_image)
def target_is_alternate_version?(%{duplicate_of_image: duplicate_of_image}),
do: alternate_version?(duplicate_of_image)
def source_is_alternate_version?(%{image: image}),
do: alternate_version?(image)
def both_are_alternate_versions?(%{image: image, duplicate_of_image: duplicate_of_image}),
do: alternate_version?(image) and alternate_version?(duplicate_of_image)
def mergeable?(%{image: image, duplicate_of_image: duplicate_of_image} = report) do
2020-09-06 22:03:31 +02:00
same_rating_tags?(report) and not image.hidden_from_users and
not duplicate_of_image.hidden_from_users
end
2019-12-01 05:51:44 +01:00
defp artist_tags(%{tags: tags}) do
tags
2020-01-11 05:20:19 +01:00
|> Enum.filter(&(&1.namespace == "artist"))
2019-12-01 05:51:44 +01:00
|> Enum.map(& &1.name)
|> MapSet.new()
end
defp rating_tags(%{tags: tags}) do
tags
2020-01-11 05:20:19 +01:00
|> Enum.filter(&(&1.category == "rating"))
2019-12-01 05:51:44 +01:00
|> Enum.map(& &1.name)
|> MapSet.new()
end
defp edit?(%{tags: tags}) do
tags
2020-01-11 05:20:19 +01:00
|> Enum.filter(&(&1.name == "edit"))
2019-12-01 05:51:44 +01:00
|> Enum.any?()
end
defp alternate_version?(%{tags: tags}) do
tags
2020-01-11 05:20:19 +01:00
|> Enum.filter(&(&1.name == "alternate version"))
2019-12-01 05:51:44 +01:00
|> Enum.any?()
end
defp proper_subset?(set1, set2),
do: MapSet.subset?(set1, set2) and not MapSet.equal?(set1, set2)
2019-12-08 04:25:47 +01:00
defp uri_host(nil), do: nil
defp uri_host(str), do: URI.parse(str).host
2019-12-01 05:51:44 +01:00
end