diff --git a/lib/philomena/images/image.ex b/lib/philomena/images/image.ex index 25b2ff2c..805267ff 100644 --- a/lib/philomena/images/image.ex +++ b/lib/philomena/images/image.ex @@ -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 diff --git a/lib/philomena_web/controllers/duplicate_report/accept_controller.ex b/lib/philomena_web/controllers/duplicate_report/accept_controller.ex index 24dd76a2..ef789b37 100644 --- a/lib/philomena_web/controllers/duplicate_report/accept_controller.ex +++ b/lib/philomena_web/controllers/duplicate_report/accept_controller.ex @@ -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 diff --git a/lib/philomena_web/controllers/duplicate_report/accept_reverse_controller.ex b/lib/philomena_web/controllers/duplicate_report/accept_reverse_controller.ex index b2c8a940..34953ffc 100644 --- a/lib/philomena_web/controllers/duplicate_report/accept_reverse_controller.ex +++ b/lib/philomena_web/controllers/duplicate_report/accept_reverse_controller.ex @@ -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 diff --git a/lib/philomena_web/controllers/image/delete_controller.ex b/lib/philomena_web/controllers/image/delete_controller.ex index c62cf376..b3dde1bf 100644 --- a/lib/philomena_web/controllers/image/delete_controller.ex +++ b/lib/philomena_web/controllers/image/delete_controller.ex @@ -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