diff --git a/lib/philomena/duplicate_reports.ex b/lib/philomena/duplicate_reports.ex index 7aa65069..55b468ce 100644 --- a/lib/philomena/duplicate_reports.ex +++ b/lib/philomena/duplicate_reports.ex @@ -91,7 +91,11 @@ defmodule Philomena.DuplicateReports do {:ok, duplicate_report} = reject_duplicate_report(duplicate_report, user) # Need a constraint for upsert, so have to do it the hard way - new_report = Repo.get_by(DuplicateReport, duplicate_of_image_id: duplicate_report.image_id) + new_report = + DuplicateReport + |> where(duplicate_of_image_id: ^duplicate_report.image_id) + |> limit(1) + |> Repo.one() new_report = if new_report do diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index d5e48665..34551f6a 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -308,9 +308,11 @@ defmodule Philomena.Images do |> internal_hide_image(image) case result do - {:ok, _changes} -> + {:ok, changes} -> + tags = Tags.copy_tags(image, duplicate_of_image) Interactions.migrate_interactions(image, duplicate_of_image) - result + + {:ok, %{changes | tags: changes.tags ++ tags}} _error -> result diff --git a/lib/philomena/tags.ex b/lib/philomena/tags.ex index 2220f716..8feb17e2 100644 --- a/lib/philomena/tags.ex +++ b/lib/philomena/tags.ex @@ -257,6 +257,39 @@ defmodule Philomena.Tags do |> Repo.update_all([]) end + def copy_tags(source, target) do + # Ecto bug: + # ** (DBConnection.EncodeError) Postgrex expected a binary, got 5. + # + # what I would like to do: + # |> select([t], %{image_id: ^target.id, tag_id: t.tag_id}) + # + # what I have to do instead: + + taggings = + Tagging + |> where(image_id: ^source.id) + |> select([t], %{image_id: ^to_string(target.id), tag_id: t.tag_id}) + |> Repo.all() + |> Enum.map(&%{&1 | image_id: String.to_integer(&1.image_id)}) + + {:ok, tag_ids} = + Repo.transaction fn -> + {_count, taggings} = Repo.insert_all(Tagging, taggings, on_conflict: :nothing, returning: [:tag_id]) + tag_ids = Enum.map(taggings, & &1.tag_id) + + Tag + |> where([t], t.id in ^tag_ids) + |> Repo.update_all(inc: [images_count: 1]) + + tag_ids + end + + Tag + |> where([t], t.id in ^tag_ids) + |> Repo.all() + end + @doc """ Returns an `%Ecto.Changeset{}` for tracking tag changes. diff --git a/lib/philomena_web/templates/duplicate_report/_image_cell.html.slime b/lib/philomena_web/templates/duplicate_report/_image_cell.html.slime index f7364217..d507863c 100644 --- a/lib/philomena_web/templates/duplicate_report/_image_cell.html.slime +++ b/lib/philomena_web/templates/duplicate_report/_image_cell.html.slime @@ -16,7 +16,7 @@ p = render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @image, conn: @conn - = if can?(@conn, :edit, @report) do + = if can?(@conn, :edit, @report) and same_rating_tags?(@report) do = if @source do a href=Routes.duplicate_report_accept_reverse_path(@conn, :create, @report) data-method="post" button.button