mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 21:47:59 +01:00
copy tags on dupe merge
This commit is contained in:
parent
10707cd92d
commit
3aa730a325
4 changed files with 43 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue