copy tags on dupe merge

This commit is contained in:
byte[] 2019-12-17 12:13:05 -05:00
parent 10707cd92d
commit 3aa730a325
4 changed files with 43 additions and 4 deletions

View file

@ -91,7 +91,11 @@ defmodule Philomena.DuplicateReports do
{:ok, duplicate_report} = reject_duplicate_report(duplicate_report, user) {:ok, duplicate_report} = reject_duplicate_report(duplicate_report, user)
# Need a constraint for upsert, so have to do it the hard way # 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 = new_report =
if new_report do if new_report do

View file

@ -308,9 +308,11 @@ defmodule Philomena.Images do
|> internal_hide_image(image) |> internal_hide_image(image)
case result do case result do
{:ok, _changes} -> {:ok, changes} ->
tags = Tags.copy_tags(image, duplicate_of_image)
Interactions.migrate_interactions(image, duplicate_of_image) Interactions.migrate_interactions(image, duplicate_of_image)
result
{:ok, %{changes | tags: changes.tags ++ tags}}
_error -> _error ->
result result

View file

@ -257,6 +257,39 @@ defmodule Philomena.Tags do
|> Repo.update_all([]) |> Repo.update_all([])
end 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 """ @doc """
Returns an `%Ecto.Changeset{}` for tracking tag changes. Returns an `%Ecto.Changeset{}` for tracking tag changes.

View file

@ -16,7 +16,7 @@
p p
= render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @image, conn: @conn = 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 = if @source do
a href=Routes.duplicate_report_accept_reverse_path(@conn, :create, @report) data-method="post" a href=Routes.duplicate_report_accept_reverse_path(@conn, :create, @report) data-method="post"
button.button button.button