mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-02 15:48:00 +01:00
prevent double deletions
This commit is contained in:
parent
24994d2fa2
commit
cc0077d78f
4 changed files with 47 additions and 18 deletions
|
@ -214,6 +214,7 @@ defmodule Philomena.Images.Image do
|
||||||
def hide_changeset(image, attrs, user) do
|
def hide_changeset(image, attrs, user) do
|
||||||
image
|
image
|
||||||
|> cast(attrs, [:deletion_reason])
|
|> cast(attrs, [:deletion_reason])
|
||||||
|
|> validate_not_hidden()
|
||||||
|> put_change(:deleter_id, user.id)
|
|> put_change(:deleter_id, user.id)
|
||||||
|> put_change(:hidden_image_key, create_key())
|
|> put_change(:hidden_image_key, create_key())
|
||||||
|> put_change(:hidden_from_users, true)
|
|> put_change(:hidden_from_users, true)
|
||||||
|
@ -228,6 +229,7 @@ defmodule Philomena.Images.Image do
|
||||||
|
|
||||||
def merge_changeset(image, duplicate_of_image) do
|
def merge_changeset(image, duplicate_of_image) do
|
||||||
change(image)
|
change(image)
|
||||||
|
|> validate_not_hidden()
|
||||||
|> put_change(:duplicate_id, duplicate_of_image.id)
|
|> put_change(:duplicate_id, duplicate_of_image.id)
|
||||||
|> put_change(:hidden_image_key, create_key())
|
|> put_change(:hidden_image_key, create_key())
|
||||||
|> put_change(:hidden_from_users, true)
|
|> put_change(:hidden_from_users, true)
|
||||||
|
@ -235,6 +237,7 @@ defmodule Philomena.Images.Image do
|
||||||
|
|
||||||
def unhide_changeset(image) do
|
def unhide_changeset(image) do
|
||||||
change(image)
|
change(image)
|
||||||
|
|> validate_hidden()
|
||||||
|> put_change(:deleter_id, nil)
|
|> put_change(:deleter_id, nil)
|
||||||
|> put_change(:hidden_image_key, nil)
|
|> put_change(:hidden_image_key, nil)
|
||||||
|> put_change(:hidden_from_users, false)
|
|> put_change(:hidden_from_users, false)
|
||||||
|
@ -335,4 +338,18 @@ defmodule Philomena.Images.Image do
|
||||||
defp create_key do
|
defp create_key do
|
||||||
Base.encode16(:crypto.strong_rand_bytes(6), case: :lower)
|
Base.encode16(:crypto.strong_rand_bytes(6), case: :lower)
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -13,14 +13,19 @@ defmodule PhilomenaWeb.DuplicateReport.AcceptController do
|
||||||
preload: [:image, :duplicate_of_image]
|
preload: [:image, :duplicate_of_image]
|
||||||
|
|
||||||
def create(conn, _params) do
|
def create(conn, _params) do
|
||||||
{:ok, _report} =
|
report = conn.assigns.duplicate_report
|
||||||
DuplicateReports.accept_duplicate_report(
|
user = conn.assigns.current_user
|
||||||
conn.assigns.duplicate_report,
|
|
||||||
conn.assigns.current_user
|
|
||||||
)
|
|
||||||
|
|
||||||
|
case DuplicateReports.accept_duplicate_report(report, user) do
|
||||||
|
{:ok, _report} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Successfully accepted report.")
|
|> put_flash(:info, "Successfully accepted report.")
|
||||||
|> redirect(to: Routes.duplicate_report_path(conn, :index))
|
|> 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
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,14 +13,19 @@ defmodule PhilomenaWeb.DuplicateReport.AcceptReverseController do
|
||||||
preload: [:image, :duplicate_of_image]
|
preload: [:image, :duplicate_of_image]
|
||||||
|
|
||||||
def create(conn, _params) do
|
def create(conn, _params) do
|
||||||
{:ok, _report} =
|
report = conn.assigns.duplicate_report
|
||||||
DuplicateReports.accept_reverse_duplicate_report(
|
user = conn.assigns.current_user
|
||||||
conn.assigns.duplicate_report,
|
|
||||||
conn.assigns.current_user
|
|
||||||
)
|
|
||||||
|
|
||||||
|
case DuplicateReports.accept_reverse_duplicate_report(report, user) do
|
||||||
|
{:ok, _report} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Successfully accepted report in reverse.")
|
|> put_flash(:info, "Successfully accepted report in reverse.")
|
||||||
|> redirect(to: Routes.duplicate_report_path(conn, :index))
|
|> 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
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,8 +21,10 @@ defmodule PhilomenaWeb.Image.DeleteController do
|
||||||
|> put_flash(:info, "Image successfully hidden.")
|
|> put_flash(:info, "Image successfully hidden.")
|
||||||
|> redirect(to: Routes.image_path(conn, :show, image))
|
|> redirect(to: Routes.image_path(conn, :show, image))
|
||||||
|
|
||||||
{:error, :image, changeset, _changes} ->
|
_error ->
|
||||||
render(conn, "new.html", image: image, changeset: changeset)
|
conn
|
||||||
|
|> put_flash(:error, "Failed to hide image.")
|
||||||
|
|> redirect(to: Routes.image_path(conn, :show, image))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue