prevent double deletions

This commit is contained in:
byte[] 2020-09-10 12:02:01 -04:00
parent 24994d2fa2
commit cc0077d78f
4 changed files with 47 additions and 18 deletions

View file

@ -214,6 +214,7 @@ defmodule Philomena.Images.Image do
def hide_changeset(image, attrs, user) do
image
|> cast(attrs, [:deletion_reason])
|> validate_not_hidden()
|> put_change(:deleter_id, user.id)
|> put_change(:hidden_image_key, create_key())
|> put_change(:hidden_from_users, true)
@ -228,6 +229,7 @@ defmodule Philomena.Images.Image do
def merge_changeset(image, duplicate_of_image) do
change(image)
|> validate_not_hidden()
|> put_change(:duplicate_id, duplicate_of_image.id)
|> put_change(:hidden_image_key, create_key())
|> put_change(:hidden_from_users, true)
@ -235,6 +237,7 @@ defmodule Philomena.Images.Image do
def unhide_changeset(image) do
change(image)
|> validate_hidden()
|> put_change(:deleter_id, nil)
|> put_change(:hidden_image_key, nil)
|> put_change(:hidden_from_users, false)
@ -335,4 +338,18 @@ defmodule Philomena.Images.Image do
defp create_key do
Base.encode16(:crypto.strong_rand_bytes(6), case: :lower)
end
defp validate_hidden(changeset) do
case get_field(changeset, :hidden_from_users) do
true -> changeset
false -> add_error(changeset, :hidden_from_users, "must be true")
end
end
defp validate_not_hidden(changeset) do
case get_field(changeset, :hidden_from_users) do
true -> add_error(changeset, :hidden_from_users, "must be false")
false -> changeset
end
end
end

View file

@ -13,14 +13,19 @@ defmodule PhilomenaWeb.DuplicateReport.AcceptController do
preload: [:image, :duplicate_of_image]
def create(conn, _params) do
{:ok, _report} =
DuplicateReports.accept_duplicate_report(
conn.assigns.duplicate_report,
conn.assigns.current_user
)
report = conn.assigns.duplicate_report
user = conn.assigns.current_user
conn
|> put_flash(:info, "Successfully accepted report.")
|> redirect(to: Routes.duplicate_report_path(conn, :index))
case DuplicateReports.accept_duplicate_report(report, user) do
{:ok, _report} ->
conn
|> put_flash(:info, "Successfully accepted report.")
|> redirect(to: Routes.duplicate_report_path(conn, :index))
_error ->
conn
|> put_flash(:error, "Failed to accept report! Maybe someone else already accepted it.")
|> redirect(to: Routes.duplicate_report_path(conn, :index))
end
end
end

View file

@ -13,14 +13,19 @@ defmodule PhilomenaWeb.DuplicateReport.AcceptReverseController do
preload: [:image, :duplicate_of_image]
def create(conn, _params) do
{:ok, _report} =
DuplicateReports.accept_reverse_duplicate_report(
conn.assigns.duplicate_report,
conn.assigns.current_user
)
report = conn.assigns.duplicate_report
user = conn.assigns.current_user
conn
|> put_flash(:info, "Successfully accepted report in reverse.")
|> redirect(to: Routes.duplicate_report_path(conn, :index))
case DuplicateReports.accept_reverse_duplicate_report(report, user) do
{:ok, _report} ->
conn
|> put_flash(:info, "Successfully accepted report in reverse.")
|> redirect(to: Routes.duplicate_report_path(conn, :index))
_error ->
conn
|> put_flash(:error, "Failed to accept report! Maybe someone else already accepted it.")
|> redirect(to: Routes.duplicate_report_path(conn, :index))
end
end
end

View file

@ -21,8 +21,10 @@ defmodule PhilomenaWeb.Image.DeleteController do
|> put_flash(:info, "Image successfully hidden.")
|> redirect(to: Routes.image_path(conn, :show, image))
{:error, :image, changeset, _changes} ->
render(conn, "new.html", image: image, changeset: changeset)
_error ->
conn
|> put_flash(:error, "Failed to hide image.")
|> redirect(to: Routes.image_path(conn, :show, image))
end
end