admin: allow changing tags on deleted images, fix dupe report accepting

This commit is contained in:
byte[] 2019-12-23 09:46:48 -05:00
parent f847c560b1
commit 44a59aa67b
3 changed files with 48 additions and 19 deletions

View file

@ -4,6 +4,7 @@ defmodule Philomena.DuplicateReports do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Ecto.Multi
alias Philomena.Repo alias Philomena.Repo
alias Philomena.DuplicateReports.DuplicateReport alias Philomena.DuplicateReports.DuplicateReport
@ -71,19 +72,34 @@ defmodule Philomena.DuplicateReports do
# TODO: can we get this in a single transaction? # TODO: can we get this in a single transaction?
def accept_duplicate_report(%DuplicateReport{} = duplicate_report, user) do def accept_duplicate_report(%DuplicateReport{} = duplicate_report, user) do
result = changeset =
duplicate_report duplicate_report
|> DuplicateReport.accept_changeset(user) |> DuplicateReport.accept_changeset(user)
|> Repo.update()
case result do Multi.new()
{:ok, duplicate_report} -> |> Multi.update(:duplicate_report, changeset)
|> Multi.run(:other_reports, fn repo, %{duplicate_report: duplicate_report} ->
{count, nil} =
DuplicateReport
|> where(
[dr],
(dr.image_id == ^duplicate_report.image_id and dr.duplicate_of_image_id == ^duplicate_report.duplicate_of_image_id) or
(dr.image_id == ^duplicate_report.duplicate_of_image_id and dr.duplicate_of_image_id == ^duplicate_report.image_id)
)
|> where([dr], dr.id != ^duplicate_report.id)
|> repo.update_all(set: [state: "rejected"])
{:ok, count}
end)
|> Repo.isolated_transaction(:serializable)
|> case do
{:ok, %{duplicate_report: duplicate_report}} ->
duplicate_report = Repo.preload(duplicate_report, [:image, :duplicate_of_image]) duplicate_report = Repo.preload(duplicate_report, [:image, :duplicate_of_image])
Images.merge_image(duplicate_report.image, duplicate_report.duplicate_of_image) Images.merge_image(duplicate_report.image, duplicate_report.duplicate_of_image)
_error -> error ->
result error
end end
end end

View file

@ -261,21 +261,29 @@ defmodule Philomena.Images do
{:ok, count} {:ok, count}
end) end)
|> Multi.run(:added_tag_count, fn repo, %{image: {_image, added_tags, _removed}} -> |> Multi.run(:added_tag_count, fn
tag_ids = added_tags |> Enum.map(& &1.id) _repo, %{image: {%{hidden_from_users: true}, _added, _removed}} ->
tags = Tag |> where([t], t.id in ^tag_ids) {:ok, 0}
{count, nil} = repo.update_all(tags, inc: [images_count: 1]) repo, %{image: {_image, added_tags, _removed}} ->
tag_ids = added_tags |> Enum.map(& &1.id)
tags = Tag |> where([t], t.id in ^tag_ids)
{:ok, count} {count, nil} = repo.update_all(tags, inc: [images_count: 1])
{:ok, count}
end) end)
|> Multi.run(:removed_tag_count, fn repo, %{image: {_image, _added, removed_tags}} -> |> Multi.run(:removed_tag_count, fn
tag_ids = removed_tags |> Enum.map(& &1.id) _repo, %{image: {%{hidden_from_users: true}, _added, _removed}} ->
tags = Tag |> where([t], t.id in ^tag_ids) {:ok, 0}
{count, nil} = repo.update_all(tags, inc: [images_count: -1]) repo, %{image: {_image, _added, removed_tags}} ->
tag_ids = removed_tags |> Enum.map(& &1.id)
tags = Tag |> where([t], t.id in ^tag_ids)
{:ok, count} {count, nil} = repo.update_all(tags, inc: [images_count: -1])
{:ok, count}
end) end)
|> Repo.isolated_transaction(:serializable) |> Repo.isolated_transaction(:serializable)
end end
@ -388,6 +396,12 @@ defmodule Philomena.Images do
def unhide_image(image), do: {:ok, image} def unhide_image(image), do: {:ok, image}
def batch_update(image_ids, added_tags, removed_tags, tag_change_attributes) do def batch_update(image_ids, added_tags, removed_tags, tag_change_attributes) do
image_ids =
Image
|> where([i], i.id in ^image_ids and i.hidden_from_users == false)
|> select([i], i.id)
|> Repo.all()
added_tags = Enum.map(added_tags, & &1.id) added_tags = Enum.map(added_tags, & &1.id)
removed_tags = Enum.map(removed_tags, & &1.id) removed_tags = Enum.map(removed_tags, & &1.id)

View file

@ -43,9 +43,8 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
def can?(%User{role: "moderator"}, :show, %Filter{}), do: true def can?(%User{role: "moderator"}, :show, %Filter{}), do: true
# Manage images # Manage images
def can?(%User{role: "moderator"}, :show, %Image{}), do: true def can?(%User{role: "moderator"}, _action, Image), do: true
def can?(%User{role: "moderator"}, :hide, %Image{}), do: true def can?(%User{role: "moderator"}, _action, %Image{}), do: true
def can?(%User{role: "moderator"}, :edit_description, %Image{}), do: true
# View comments # View comments
def can?(%User{role: "moderator"}, :show, %Comment{}), do: true def can?(%User{role: "moderator"}, :show, %Comment{}), do: true